graph_def.c: Implement the "Stack" and "Area" options.
[collection4.git] / graph_list.c
index e203df4..eff134f 100644 (file)
@@ -10,6 +10,7 @@
 #include "graph_list.h"
 #include "graph_ident.h"
 #include "graph_def.h"
+#include "graph_config.h"
 #include "common.h"
 #include "utils_params.h"
 
@@ -72,6 +73,7 @@ typedef struct def_callback_data_s def_callback_data_t;
  * Global variables
  */
 static graph_config_t *graph_config_head = NULL;
+static graph_config_t *graph_config_staging = NULL;
 
 static time_t gl_last_update = 0;
 
@@ -267,20 +269,24 @@ static int graph_add_file (graph_config_t *cfg, const graph_ident_t *file) /* {{
   return (instance_add_file (inst, file));
 } /* }}} int graph_add_file */
 
-static int graph_append (graph_config_t *cfg) /* {{{ */
+static int graph_append (graph_config_t **head, /* {{{ */
+    graph_config_t *cfg)
 {
   graph_config_t *last;
 
   if (cfg == NULL)
     return (EINVAL);
 
-  if (graph_config_head == NULL)
+  if (head == NULL)
+    head = &graph_config_head;
+
+  if (*head == NULL)
   {
-    graph_config_head = cfg;
+    *head = cfg;
     return (0);
   }
 
-  last = graph_config_head;
+  last = *head;
   while (last->next != NULL)
     last = last->next;
 
@@ -298,7 +304,10 @@ static graph_config_t *graph_create (const graph_ident_t *selector) /* {{{ */
     return (NULL);
   memset (cfg, 0, sizeof (*cfg));
 
-  cfg->select = ident_clone (selector);
+  if (selector != NULL)
+    cfg->select = ident_clone (selector);
+  else
+    cfg->select = NULL;
 
   cfg->title = NULL;
   cfg->vertical_label = NULL;
@@ -355,70 +364,13 @@ static int register_file (const graph_ident_t *file) /* {{{ */
   if (num_graphs == 0)
   {
     cfg = graph_create (file);
-    graph_append (cfg);
+    graph_append (/* head = */ NULL, cfg);
     graph_add_file (cfg, file);
   }
 
   return (0);
 } /* }}} int register_file */
 
-static int FIXME_graph_create_from_file (const char *host, /* {{{ */
-    const char *plugin, const char *plugin_instance,
-    const char *type, const char *type_instance,
-    const char *title)
-{
-  graph_config_t *cfg;
-
-  cfg = malloc (sizeof (*cfg));
-  if (cfg == NULL)
-    return (ENOMEM);
-  memset (cfg, 0, sizeof (*cfg));
-
-  cfg->select = ident_create (host, plugin, plugin_instance, type, type_instance);
-
-  cfg->title = NULL;
-  if (title != NULL)
-    cfg->title = strdup (title);
-  cfg->vertical_label = NULL;
-  cfg->instances = NULL;
-  cfg->next = NULL;
-
-  graph_append (cfg);
-
-  return (0);
-} /* }}} int FIXME_graph_create_from_file */
-
-/* FIXME: Actually read the config file here. */
-static int read_graph_config (void) /* {{{ */
-{
-  if (graph_config_head != NULL)
-    return (0);
-
-  FIXME_graph_create_from_file (ANY_TOKEN, "cpu", ANY_TOKEN, "cpu", ALL_TOKEN,
-      "CPU {instance} usage");
-  FIXME_graph_create_from_file (ANY_TOKEN, "memory", "", "memory", ALL_TOKEN,
-      "Memory usage");
-  FIXME_graph_create_from_file (ANY_TOKEN, "swap", "", "swap", ALL_TOKEN,
-      "Swap");
-  FIXME_graph_create_from_file (ANY_TOKEN, ANY_TOKEN, ANY_TOKEN, "ps_state", ALL_TOKEN,
-      "Processes");
-  FIXME_graph_create_from_file (ANY_TOKEN, "cpu", ALL_TOKEN, "cpu", "idle",
-      "CPU idle overview");
-
-  return (0);
-} /* }}} int read_graph_config */
-
-static void gl_clear (void) /* {{{ */
-{
-  graph_config_t *cfg;
-
-  cfg = graph_config_head;
-  graph_config_head = NULL;
-  graph_destroy (cfg);
-
-  gl_last_update = 0;
-} /* }}} void gl_clear */
-
 static int callback_type (const char *type, void *user_data) /* {{{ */
 {
   gl_ident_stage_t *gl;
@@ -647,9 +599,115 @@ static int gl_instance_get_rrdargs_cb (graph_def_t *def, void *user_data) /* {{{
   return (0);
 } /* }}} int gl_instance_get_rrdargs_cb */
 
+static int gl_clear_instances (void) /* {{{ */
+{
+  graph_config_t *cfg;
+
+  for (cfg = graph_config_head; cfg != NULL; cfg = cfg->next)
+  {
+    instance_destroy (cfg->instances);
+    cfg->instances = NULL;
+  }
+
+  return (0);
+} /* }}} int gl_clear_instances */
+
+
+/*
+ * Config functions
+ */
+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_ident_t *ret;
+  int i;
+
+  for (i = 0; i < ci->children_num; i++)
+  {
+    oconfig_item_t *child;
+
+    child = ci->children + i;
+
+    if (strcasecmp ("Host", child->key) == 0)
+      graph_config_get_string (child, &host);
+    else if (strcasecmp ("Plugin", child->key) == 0)
+      graph_config_get_string (child, &plugin);
+    else if (strcasecmp ("PluginInstance", child->key) == 0)
+      graph_config_get_string (child, &plugin_instance);
+    else if (strcasecmp ("Type", child->key) == 0)
+      graph_config_get_string (child, &type);
+    else if (strcasecmp ("TypeInstance", child->key) == 0)
+      graph_config_get_string (child, &type_instance);
+    /* else: ignore all other directives here. */
+  } /* for */
+
+  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);
+
+  return (0);
+} /* }}} graph_config_add */
+
+int graph_config_submit (void) /* {{{ */
+{
+  graph_config_t *tmp;
+
+  tmp = graph_config_head;
+  graph_config_head = graph_config_staging;
+  graph_config_staging = NULL;
+
+  graph_destroy (tmp);
+
+  return (0);
+} /* }}} int graph_config_submit */
+
 int gl_instance_get_params (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
     char *buffer, size_t buffer_size)
 {
@@ -816,24 +874,18 @@ int gl_graph_instance_get_all (graph_config_t *cfg, /* {{{ */
 int gl_graph_get_title (graph_config_t *cfg, /* {{{ */
     char *buffer, size_t buffer_size)
 {
-  char *str;
-
   if ((cfg == NULL) || (buffer == NULL) || (buffer_size < 1))
     return (EINVAL);
 
-  if (cfg->title != NULL)
-    str = cfg->title;
-  else
-    str = ident_to_string (cfg->select);
+  if (cfg->title == NULL)
+    cfg->title = ident_to_string (cfg->select);
 
-  if (str == NULL)
+  if (cfg->title == NULL)
     return (ENOMEM);
 
-  strncpy (buffer, str, buffer_size);
+  strncpy (buffer, cfg->title, buffer_size);
   buffer[buffer_size - 1] = 0;
 
-  free (str);
-
   return (0);
 } /* }}} int gl_graph_get_title */
 
@@ -845,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)
 {
@@ -886,6 +953,12 @@ int gl_instance_get_rrdargs (graph_config_t *cfg, /* {{{ */
     array_append (args, cfg->title);
   }
 
+  if (cfg->vertical_label != NULL)
+  {
+    array_append (args, "-v");
+    array_append (args, cfg->vertical_label);
+  }
+
   if (cfg->defs == NULL)
   {
     default_defs = gl_inst_get_default_defs (cfg, inst);
@@ -914,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;
@@ -931,9 +1002,7 @@ int gl_update (void) /* {{{ */
   if ((gl_last_update + UPDATE_INTERVAL) >= now)
     return (0);
 
-  gl_clear ();
-
-  read_graph_config ();
+  graph_read_config ();
 
   memset (&gl, 0, sizeof (gl));
   gl.host = NULL;
@@ -942,9 +1011,10 @@ int gl_update (void) /* {{{ */
   gl.type = NULL;
   gl.type_instance = NULL;
 
+  gl_clear_instances ();
   status = foreach_host (callback_host, &gl);
 
-  /* print_graphs (); */
+  gl_last_update = now;
 
   return (status);
 } /* }}} int gl_update */