graph.c: Implement the "ShowZero" config option.
[collection4.git] / graph_def.c
index 24e66d4..4ea0966 100644 (file)
@@ -3,6 +3,7 @@
 #include <errno.h>
 
 #include "graph_def.h"
+#include "graph.h"
 #include "graph_config.h"
 #include "common.h"
 #include "oconfig.h"
@@ -20,6 +21,9 @@ struct graph_def_s
   char *ds_name;
   char *legend;
   uint32_t color;
+  _Bool stack;
+  _Bool area;
+  char *format;
 
   graph_def_t *next;
 };
@@ -69,6 +73,57 @@ static int def_config_color (const oconfig_item_t *ci, uint32_t *ret_color) /* {
   return (0);
 } /* }}} int def_config_color */
 
+static graph_def_t *def_config_get_obj (graph_config_t *cfg, /* {{{ */
+    const oconfig_item_t *ci)
+{
+  graph_ident_t *ident;
+  char *ds_name = NULL;
+  graph_def_t *def;
+  int i;
+
+  ident = graph_get_selector (cfg);
+  if (ident == NULL)
+  {
+    fprintf (stderr, "def_config_get_obj: graph_get_selector failed");
+    return (NULL);
+  }
+
+  for (i = 0; i < ci->children_num; i++)
+  {
+    oconfig_item_t *child;
+
+#define HANDLE_FIELD(name,field) \
+    else if (strcasecmp (name, child->key) == 0) \
+      def_config_##field (child, ident)
+
+    child = ci->children + i;
+    if (strcasecmp ("DSName", child->key) == 0)
+      graph_config_get_string (child, &ds_name);
+
+    HANDLE_FIELD ("Host", host);
+    HANDLE_FIELD ("Plugin", plugin);
+    HANDLE_FIELD ("PluginInstance", plugin_instance);
+    HANDLE_FIELD ("Type", type);
+    HANDLE_FIELD ("TypeInstance", type_instance);
+
+#undef HANDLE_FIELD
+  }
+
+  def = def_create (cfg, ident, ds_name);
+  if (def == NULL)
+  {
+    fprintf (stderr, "def_config_get_obj: def_create failed\n");
+    ident_destroy (ident);
+    free (ds_name);
+    return (NULL);
+  }
+
+  ident_destroy (ident);
+  free (ds_name);
+
+  return (def);
+} /* }}} graph_def_t *def_config_get_obj */
+
 /*
  * Public functions
  */
@@ -81,7 +136,7 @@ graph_def_t *def_create (graph_config_t *cfg, graph_ident_t *ident, /* {{{ */
   if ((cfg == NULL) || (ident == NULL) || (ds_name == NULL))
     return (NULL);
 
-  selector = gl_graph_get_selector (cfg);
+  selector = graph_get_selector (cfg);
   if (selector == NULL)
     return (NULL);
 
@@ -93,6 +148,7 @@ graph_def_t *def_create (graph_config_t *cfg, graph_ident_t *ident, /* {{{ */
   }
   memset (ret, 0, sizeof (*ret));
   ret->legend = NULL;
+  ret->format = NULL;
 
   ret->ds_name = strdup (ds_name);
   if (ret->ds_name == NULL)
@@ -131,6 +187,8 @@ void def_destroy (graph_def_t *def) /* {{{ */
   ident_destroy (def->select);
 
   free (def->ds_name);
+  free (def->legend);
+  free (def->format);
 
   free (def);
 
@@ -139,59 +197,34 @@ void def_destroy (graph_def_t *def) /* {{{ */
 
 int def_config (graph_config_t *cfg, const oconfig_item_t *ci) /* {{{ */
 {
-  graph_ident_t *ident;
-  char *ds_name = NULL;
-  char *legend = NULL;
-  uint32_t color = 0x01000000;
   graph_def_t *def;
   int i;
 
-  ident = gl_graph_get_selector (cfg);
-  if (ident == NULL)
-    return (ENOMEM);
+  def = def_config_get_obj (cfg, ci);
+  if (def == NULL)
+    return (EINVAL);
 
   for (i = 0; i < ci->children_num; i++)
   {
     oconfig_item_t *child;
 
-#define HANDLE_FIELD(name,field) \
-    else if (strcasecmp (name, child->key) == 0) \
-      def_config_##field (child, ident)
-
     child = ci->children + i;
-    if (strcasecmp ("DSName", child->key) == 0)
-      graph_config_get_string (child, &ds_name);
-    else if (strcasecmp ("Legend", child->key) == 0)
-      graph_config_get_string (child, &legend);
+    if (strcasecmp ("Legend", child->key) == 0)
+      graph_config_get_string (child, &def->legend);
     else if (strcasecmp ("Color", child->key) == 0)
-      def_config_color (child, &color);
-
-    HANDLE_FIELD ("Host", host);
-    HANDLE_FIELD ("Plugin", plugin);
-    HANDLE_FIELD ("PluginInstance", plugin_instance);
-    HANDLE_FIELD ("Type", type);
-    HANDLE_FIELD ("TypeInstance", type_instance);
-
-#undef HANDLE_FIELD
-  }
-
-  def = def_create (cfg, ident, ds_name);
-  if (def == NULL)
-  {
-    fprintf (stderr, "def_config: def_create failed (ds_name = %s)\n",
-        (ds_name != NULL) ? ds_name : "(null)");
-    ident_destroy (ident);
-    return (EINVAL);
+      def_config_color (child, &def->color);
+    else if (strcasecmp ("Stack", child->key) == 0)
+      graph_config_get_bool (child, &def->stack);
+    else if (strcasecmp ("Area", child->key) == 0)
+      graph_config_get_bool (child, &def->area);
+    else if (strcasecmp ("Format", child->key) == 0)
+      graph_config_get_string (child, &def->format);
+    else
+      fprintf (stderr, "def_config: Ignoring unknown config option \"%s\"",
+          child->key);
   }
 
-  def->legend = legend;
-  if (color < 0x01000000)
-    def->color = color;
-
-  ident_destroy (ident);
-  free (ds_name);
-
-  return (gl_graph_add_def (cfg, def));
+  return (graph_add_def (cfg, def));
 } /* }}} int def_config */
 
 int def_append (graph_def_t *head, graph_def_t *def) /* {{{ */
@@ -293,13 +326,19 @@ int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
       index, index);
 
   /* Graph part */
-  array_append_format (args, "LINE1:def_%04i_avg#%06"PRIx32":%s",
+  array_append_format (args, "%s:def_%04i_avg#%06"PRIx32":%s%s",
+      def->area ? "AREA" : "LINE1",
       index, def->color,
-      (def->legend != NULL) ? def->legend : def->ds_name);
-  array_append_format (args, "GPRINT:vdef_%04i_min:%%lg min,", index);
-  array_append_format (args, "GPRINT:vdef_%04i_avg:%%lg avg,", index);
-  array_append_format (args, "GPRINT:vdef_%04i_max:%%lg max,", index);
-  array_append_format (args, "GPRINT:vdef_%04i_lst:%%lg last\\l", index);
+      (def->legend != NULL) ? def->legend : def->ds_name,
+      def->stack ? ":STACK" : "");
+  array_append_format (args, "GPRINT:vdef_%04i_min:%s min,",
+      index, (def->format != NULL) ? def->format : "%6.2lf");
+  array_append_format (args, "GPRINT:vdef_%04i_avg:%s avg,",
+      index, (def->format != NULL) ? def->format : "%6.2lf");
+  array_append_format (args, "GPRINT:vdef_%04i_max:%s max,",
+      index, (def->format != NULL) ? def->format : "%6.2lf");
+  array_append_format (args, "GPRINT:vdef_%04i_lst:%s last\\l",
+      index, (def->format != NULL) ? def->format : "%6.2lf");
 
   free (file);