src/utils_cgi.c: Fix format error.
[collection4.git] / src / graph_def.c
index 4bd5f1e..b358905 100644 (file)
@@ -1,3 +1,26 @@
+/**
+ * collection4 - graph_def.c
+ * Copyright (C) 2010  Florian octo Forster
+ * 
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   Florian octo Forster <ff at octo.it>
+ **/
+
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
@@ -135,15 +158,22 @@ graph_def_t *def_create (graph_config_t *cfg, graph_ident_t *ident, /* {{{ */
   graph_def_t *ret;
 
   if ((cfg == NULL) || (ident == NULL) || (ds_name == NULL))
+  {
+    fprintf (stderr, "def_create: An argument is NULL\n");
     return (NULL);
+  }
 
   selector = graph_get_selector (cfg);
   if (selector == NULL)
+  {
+    fprintf (stderr, "def_create: graph_get_selector failed\n");
     return (NULL);
+  }
 
   ret = malloc (sizeof (*ret));
   if (ret == NULL)
   {
+    fprintf (stderr, "def_create: malloc failed\n");
     ident_destroy (selector);
     return (NULL);
   }
@@ -154,18 +184,20 @@ graph_def_t *def_create (graph_config_t *cfg, graph_ident_t *ident, /* {{{ */
   ret->ds_name = strdup (ds_name);
   if (ret->ds_name == NULL)
   {
+    fprintf (stderr, "def_create: Unable to copy DS name\n");
     ident_destroy (selector);
     free (ret);
     return (NULL);
   }
 
-  ret->color = get_random_color ();
+  ret->color = UINT32_MAX;
   ret->next = NULL;
 
   ret->select = ident_copy_with_selector (selector, ident,
       IDENT_FLAG_REPLACE_ALL);
   if (ret->select == NULL)
   {
+    fprintf (stderr, "def_create: ident_copy_with_selector failed\n");
     ident_destroy (selector);
     free (ret->ds_name);
     free (ret);
@@ -292,6 +324,8 @@ int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
   char *file;
   int index;
   char draw_def[64];
+  char legend[256];
+  uint32_t color;
 
   if ((def == NULL) || (ident == NULL) || (args == NULL))
     return (EINVAL);
@@ -305,6 +339,27 @@ int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
 
   DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
 
+  if (def->legend != NULL)
+  {
+    strncpy (legend, def->legend, sizeof (legend));
+    legend[sizeof (legend) - 1] = 0;
+  }
+  else
+  {
+    ident_describe (ident, def->select,
+        legend, sizeof (legend));
+
+    if ((legend[0] == 0) || (strcmp ("default", legend) == 0))
+    {
+      strncpy (legend, def->ds_name, sizeof (legend));
+      legend[sizeof (legend) - 1] = 0;
+    }
+  }
+
+  color = def->color;
+  if (color > 0x00ffffff)
+    color = get_random_color ();
+
   index = args->index;
   args->index++;
 
@@ -345,7 +400,7 @@ int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
 
   if (def->area)
     array_prepend_format (args->areas, "AREA:%s#%06"PRIx32,
-        draw_def, fade_color (def->color));
+        draw_def, fade_color (color));
 
   /* Graph part */
   array_prepend_format (args->lines, "GPRINT:vdef_%04i_lst:%s last\\l",
@@ -357,8 +412,7 @@ int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
   array_prepend_format (args->lines, "GPRINT:vdef_%04i_min:%s min,",
       index, (def->format != NULL) ? def->format : "%6.2lf");
   array_prepend_format (args->lines, "LINE1:%s#%06"PRIx32":%s",
-      draw_def, def->color,
-      (def->legend != NULL) ? def->legend : def->ds_name);
+      draw_def, color, legend);
 
   free (file);