diff --git a/src/graph_def.c b/src/graph_def.c
index b8e0ce4e5402e9f76cf67ca879b15ef568418230..d01db8ea8b4e06e581705a74668273e89491887f 100644 (file)
--- a/src/graph_def.c
+++ b/src/graph_def.c
+/**
+ * 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>
#include "graph_def.h"
#include "graph.h"
#include "graph_config.h"
+#include "graph_ident.h"
#include "common.h"
#include "oconfig.h"
return (def);
} /* }}} graph_def_t *def_config_get_obj */
+static int def_to_json_recursive (const graph_def_t *def, /* {{{ */
+ yajl_gen handler)
+{
+ char color[16];
+
+ if (def == NULL)
+ return (0);
+
+ if (def->color < 0x00ffffff)
+ snprintf (color, sizeof (color), "#%06"PRIx32, def->color);
+ else
+ strncpy (color, "random", sizeof (color));
+ color[sizeof (color) - 1] = 0;
+
+ yajl_gen_map_open (handler);
+
+#define yajl_gen_string_cast(h,p,l) \
+ yajl_gen_string (h, (unsigned char *) p, (unsigned int) l)
+
+ yajl_gen_string_cast (handler, "select", strlen ("select"));
+ ident_to_json (def->select, handler);
+ if (def->ds_name != NULL)
+ {
+ yajl_gen_string_cast (handler, "ds_name", strlen ("ds_name"));
+ yajl_gen_string_cast (handler, def->ds_name, strlen (def->ds_name));
+ }
+ if (def->legend != NULL)
+ {
+ yajl_gen_string_cast (handler, "legend", strlen ("legend"));
+ yajl_gen_string_cast (handler, def->legend, strlen (def->legend));
+ }
+ yajl_gen_string_cast (handler, "color", strlen ("color"));
+ yajl_gen_string_cast (handler, color, strlen (color));
+ yajl_gen_string_cast (handler, "stack", strlen ("stack"));
+ yajl_gen_bool (handler, def->stack);
+ yajl_gen_string_cast (handler, "area", strlen ("area"));
+ yajl_gen_bool (handler, def->area);
+ if (def->format != NULL)
+ {
+ yajl_gen_string_cast (handler, "format", strlen ("format"));
+ yajl_gen_string_cast (handler, def->format, strlen (def->format));
+ }
+
+ yajl_gen_map_close (handler);
+
+ return (def_to_json_recursive (def->next, handler));
+#undef yajl_gen_string_cast
+} /* }}} int def_to_json_recursive */
+
/*
* Public functions
*/
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);
}
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);
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);
}
return (graph_add_def (cfg, def));
} /* }}} int def_foreach */
int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
- str_array_t *args)
+ rrd_args_t *args)
{
char *file;
int index;
+ char draw_def[64];
+ char legend[256];
+ uint32_t color;
if ((def == NULL) || (ident == NULL) || (args == NULL))
return (EINVAL);
DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
- index = array_argc (args);
+ 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++;
/* CDEFs */
- array_append_format (args, "DEF:def_%04i_min=%s:%s:MIN",
+ array_append_format (args->data, "DEF:def_%04i_min=%s:%s:MIN",
index, file, def->ds_name);
- array_append_format (args, "DEF:def_%04i_avg=%s:%s:AVERAGE",
+ array_append_format (args->data, "DEF:def_%04i_avg=%s:%s:AVERAGE",
index, file, def->ds_name);
- array_append_format (args, "DEF:def_%04i_max=%s:%s:MAX",
+ array_append_format (args->data, "DEF:def_%04i_max=%s:%s:MAX",
index, file, def->ds_name);
/* VDEFs */
- array_append_format (args, "VDEF:vdef_%04i_min=def_%04i_min,MINIMUM",
+ array_append_format (args->data, "VDEF:vdef_%04i_min=def_%04i_min,MINIMUM",
index, index);
- array_append_format (args, "VDEF:vdef_%04i_avg=def_%04i_avg,AVERAGE",
+ array_append_format (args->data, "VDEF:vdef_%04i_avg=def_%04i_avg,AVERAGE",
index, index);
- array_append_format (args, "VDEF:vdef_%04i_max=def_%04i_max,MAXIMUM",
+ array_append_format (args->data, "VDEF:vdef_%04i_max=def_%04i_max,MAXIMUM",
index, index);
- array_append_format (args, "VDEF:vdef_%04i_lst=def_%04i_avg,LAST",
+ array_append_format (args->data, "VDEF:vdef_%04i_lst=def_%04i_avg,LAST",
index, index);
+ if (def->stack)
+ {
+ if (args->last_stack_cdef[0] != 0)
+ {
+ array_append_format (args->calc, "CDEF:cdef_%04i_stack=%s,def_%04i_avg,+",
+ index, args->last_stack_cdef, index);
+ snprintf (draw_def, sizeof (draw_def), "cdef_%04i_stack", index);
+ }
+ else
+ {
+ snprintf (draw_def, sizeof (draw_def), "def_%04i_avg", index);
+ }
+ }
+ else
+ {
+ snprintf (draw_def, sizeof (draw_def), "def_%04i_avg", index);
+ }
+
+ if (def->area)
+ array_prepend_format (args->areas, "AREA:%s#%06"PRIx32,
+ draw_def, fade_color (color));
+
/* Graph part */
- 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,
- def->stack ? ":STACK" : "");
- array_append_format (args, "GPRINT:vdef_%04i_min:%s min,",
- index, (def->format != NULL) ? def->format : "%lg");
- array_append_format (args, "GPRINT:vdef_%04i_avg:%s avg,",
- index, (def->format != NULL) ? def->format : "%lg");
- array_append_format (args, "GPRINT:vdef_%04i_max:%s max,",
- index, (def->format != NULL) ? def->format : "%lg");
- array_append_format (args, "GPRINT:vdef_%04i_lst:%s last\\l",
- index, (def->format != NULL) ? def->format : "%lg");
+ array_prepend_format (args->lines, "GPRINT:vdef_%04i_lst:%s last\\l",
+ index, (def->format != NULL) ? def->format : "%6.2lf");
+ array_prepend_format (args->lines, "GPRINT:vdef_%04i_max:%s max,",
+ index, (def->format != NULL) ? def->format : "%6.2lf");
+ array_prepend_format (args->lines, "GPRINT:vdef_%04i_avg:%s avg,",
+ index, (def->format != NULL) ? def->format : "%6.2lf");
+ 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, color, legend);
free (file);
+ memcpy (args->last_stack_cdef, draw_def, sizeof (args->last_stack_cdef));
+
return (0);
} /* }}} int def_get_rrdargs */
+int def_to_json (const graph_def_t *def, /* {{{ */
+ yajl_gen handler)
+{
+ if (handler == NULL)
+ return (EINVAL);
+
+ yajl_gen_array_open (handler);
+ def_to_json_recursive (def, handler);
+ yajl_gen_array_close (handler);
+
+ return (0);
+} /* }}} int def_to_json */
+
/* vim: set sw=2 sts=2 et fdm=marker : */