Implement "DEF" configuration.
[collection4.git] / graph_list.c
index 3edcb32..eff134f 100644 (file)
@@ -599,43 +599,33 @@ static int gl_instance_get_rrdargs_cb (graph_def_t *def, void *user_data) /* {{{
   return (0);
 } /* }}} int gl_instance_get_rrdargs_cb */
 
-/*
- * Config functions
- */
-static int config_get_string (const oconfig_item_t *ci, char **ret_str)
+static int gl_clear_instances (void) /* {{{ */
 {
-  char *tmp;
-
-  if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
-    return (EINVAL);
-
-  tmp = strdup (ci->values[0].value.string);
-  if (tmp == NULL)
-    return (ENOMEM);
+  graph_config_t *cfg;
 
-  free (*ret_str);
-  *ret_str = tmp;
+  for (cfg = graph_config_head; cfg != NULL; cfg = cfg->next)
+  {
+    instance_destroy (cfg->instances);
+    cfg->instances = NULL;
+  }
 
   return (0);
-} /* }}} int config_get_string */
+} /* }}} int gl_clear_instances */
+
 
 /*
- * Global functions
+ * Config functions
  */
-int graph_config_add (const oconfig_item_t *ci)
+static graph_ident_t *graph_config_get_selector (const oconfig_item_t *ci) /* {{{ */
 {
   char *host = NULL;
   char *plugin = NULL;
   char *plugin_instance = NULL;
   char *type = NULL;
   char *type_instance = NULL;
-  graph_config_t *cfg = NULL;
+  graph_ident_t *ret;
   int i;
 
-  cfg = graph_create (/* selector = */ NULL);
-  if (cfg == NULL)
-    return (ENOMEM);
-
   for (i = 0; i < ci->children_num; i++)
   {
     oconfig_item_t *child;
@@ -643,29 +633,62 @@ int graph_config_add (const oconfig_item_t *ci)
     child = ci->children + i;
 
     if (strcasecmp ("Host", child->key) == 0)
-      config_get_string (child, &host);
+      graph_config_get_string (child, &host);
     else if (strcasecmp ("Plugin", child->key) == 0)
-      config_get_string (child, &plugin);
+      graph_config_get_string (child, &plugin);
     else if (strcasecmp ("PluginInstance", child->key) == 0)
-      config_get_string (child, &plugin_instance);
+      graph_config_get_string (child, &plugin_instance);
     else if (strcasecmp ("Type", child->key) == 0)
-      config_get_string (child, &type);
+      graph_config_get_string (child, &type);
     else if (strcasecmp ("TypeInstance", child->key) == 0)
-      config_get_string (child, &type_instance);
-    else if (strcasecmp ("Title", child->key) == 0)
-      config_get_string (child, &cfg->title);
-    else if (strcasecmp ("VerticalLabel", child->key) == 0)
-      config_get_string (child, &cfg->vertical_label);
-    /* TODO: DEFs! */
+      graph_config_get_string (child, &type_instance);
+    /* else: ignore all other directives here. */
   } /* for */
 
-  cfg->select = ident_create (host, plugin, plugin_instance,
-      type, type_instance);
-  if (cfg->select == NULL)
-  {
-    graph_destroy (cfg);
+  ret = ident_create (host, plugin, plugin_instance, type, type_instance);
+
+  free (host);
+  free (plugin);
+  free (plugin_instance);
+  free (type);
+  free (type_instance);
+
+  return (ret);
+} /* }}} int graph_config_get_selector */
+
+/*
+ * Global functions
+ */
+
+int graph_config_add (const oconfig_item_t *ci) /* {{{ */
+{
+  graph_ident_t *select;
+  graph_config_t *cfg = NULL;
+  int i;
+
+  select = graph_config_get_selector (ci);
+  if (select == NULL)
     return (EINVAL);
-  }
+
+  cfg = graph_create (/* selector = */ NULL);
+  if (cfg == NULL)
+    return (ENOMEM);
+
+  cfg->select = select;
+
+  for (i = 0; i < ci->children_num; i++)
+  {
+    oconfig_item_t *child;
+
+    child = ci->children + i;
+
+    if (strcasecmp ("Title", child->key) == 0)
+      graph_config_get_string (child, &cfg->title);
+    else if (strcasecmp ("VerticalLabel", child->key) == 0)
+      graph_config_get_string (child, &cfg->vertical_label);
+    else if (strcasecmp ("DEF", child->key) == 0)
+      def_config (cfg, child);
+  } /* for */
 
   graph_append (&graph_config_staging, cfg);
 
@@ -874,6 +897,21 @@ graph_ident_t *gl_graph_get_selector (graph_config_t *cfg) /* {{{ */
   return (ident_clone (cfg->select));
 } /* }}} graph_ident_t *gl_graph_get_selector */
 
+int gl_graph_add_def (graph_config_t *cfg, graph_def_t *def) /* {{{ */
+{
+  if ((cfg == NULL) || (def == NULL))
+    return (EINVAL);
+
+  if (cfg->defs == NULL)
+  {
+    cfg->defs = def;
+    return (0);
+  }
+
+  return (def_append (cfg->defs, def));
+} /* }}} int gl_graph_add_def */
+
+
 int gl_instance_get_all (gl_inst_callback callback, /* {{{ */
     void *user_data)
 {
@@ -949,8 +987,6 @@ graph_ident_t *gl_instance_get_selector (graph_instance_t *inst) /* {{{ */
   return (ident_clone (inst->select));
 } /* }}} graph_ident_t *gl_instance_get_selector */
 
-/* DELETEME */
-
 int gl_update (void) /* {{{ */
 {
   time_t now;
@@ -975,6 +1011,7 @@ int gl_update (void) /* {{{ */
   gl.type = NULL;
   gl.type_instance = NULL;
 
+  gl_clear_instances ();
   status = foreach_host (callback_host, &gl);
 
   gl_last_update = now;