2 * collection4 - graph_def.c
3 * Copyright (C) 2010 Florian octo Forster
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA
21 * Florian octo Forster <ff at octo.it>
28 #include "graph_def.h"
30 #include "graph_config.h"
31 #include "graph_ident.h"
36 #include <fcgi_stdio.h>
43 graph_ident_t *select;
58 #define DEF_CONFIG_FIELD(field) \
59 static int def_config_##field (const oconfig_item_t *ci, graph_ident_t *ident) \
62 int status = graph_config_get_string (ci, &tmp); \
65 ident_set_##field (ident, tmp); \
68 } /* }}} int def_config_field */
70 DEF_CONFIG_FIELD (host);
71 DEF_CONFIG_FIELD (plugin);
72 DEF_CONFIG_FIELD (plugin_instance);
73 DEF_CONFIG_FIELD (type);
74 DEF_CONFIG_FIELD (type_instance);
76 #undef DEF_CONFIG_FIELD
78 static int def_config_color (const oconfig_item_t *ci, uint32_t *ret_color) /* {{{ */
84 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
87 tmp = ci->values[0].value.string;
91 color = (uint32_t) strtoul (tmp, &endptr, /* base = */ 16);
92 if ((errno != 0) || (endptr == tmp) || (color > 0x00ffffff))
98 } /* }}} int def_config_color */
100 static graph_def_t *def_config_get_obj (graph_config_t *cfg, /* {{{ */
101 const oconfig_item_t *ci)
103 graph_ident_t *ident;
104 char *ds_name = NULL;
108 ident = graph_get_selector (cfg);
111 fprintf (stderr, "def_config_get_obj: graph_get_selector failed");
115 for (i = 0; i < ci->children_num; i++)
117 oconfig_item_t *child;
119 #define HANDLE_FIELD(name,field) \
120 else if (strcasecmp (name, child->key) == 0) \
121 def_config_##field (child, ident)
123 child = ci->children + i;
124 if (strcasecmp ("DSName", child->key) == 0)
125 graph_config_get_string (child, &ds_name);
127 HANDLE_FIELD ("Host", host);
128 HANDLE_FIELD ("Plugin", plugin);
129 HANDLE_FIELD ("PluginInstance", plugin_instance);
130 HANDLE_FIELD ("Type", type);
131 HANDLE_FIELD ("TypeInstance", type_instance);
136 def = def_create (cfg, ident, ds_name);
139 fprintf (stderr, "def_config_get_obj: def_create failed\n");
140 ident_destroy (ident);
145 ident_destroy (ident);
149 } /* }}} graph_def_t *def_config_get_obj */
154 graph_def_t *def_create (graph_config_t *cfg, graph_ident_t *ident, /* {{{ */
157 graph_ident_t *selector;
160 if ((cfg == NULL) || (ident == NULL) || (ds_name == NULL))
162 fprintf (stderr, "def_create: An argument is NULL\n");
166 selector = graph_get_selector (cfg);
167 if (selector == NULL)
169 fprintf (stderr, "def_create: graph_get_selector failed\n");
173 ret = malloc (sizeof (*ret));
176 fprintf (stderr, "def_create: malloc failed\n");
177 ident_destroy (selector);
180 memset (ret, 0, sizeof (*ret));
184 ret->ds_name = strdup (ds_name);
185 if (ret->ds_name == NULL)
187 fprintf (stderr, "def_create: Unable to copy DS name\n");
188 ident_destroy (selector);
193 ret->color = UINT32_MAX;
196 ret->select = ident_copy_with_selector (selector, ident,
197 IDENT_FLAG_REPLACE_ANY);
198 if (ret->select == NULL)
200 fprintf (stderr, "def_create: ident_copy_with_selector failed\n");
201 ident_destroy (selector);
207 ident_destroy (selector);
209 } /* }}} graph_def_t *def_create */
211 void def_destroy (graph_def_t *def) /* {{{ */
220 ident_destroy (def->select);
229 } /* }}} void def_destroy */
231 int def_config (graph_config_t *cfg, const oconfig_item_t *ci) /* {{{ */
236 def = def_config_get_obj (cfg, ci);
240 for (i = 0; i < ci->children_num; i++)
242 oconfig_item_t *child;
244 child = ci->children + i;
245 if (strcasecmp ("Legend", child->key) == 0)
246 graph_config_get_string (child, &def->legend);
247 else if (strcasecmp ("Color", child->key) == 0)
248 def_config_color (child, &def->color);
249 else if (strcasecmp ("Stack", child->key) == 0)
250 graph_config_get_bool (child, &def->stack);
251 else if (strcasecmp ("Area", child->key) == 0)
252 graph_config_get_bool (child, &def->area);
253 else if (strcasecmp ("Format", child->key) == 0)
254 graph_config_get_string (child, &def->format);
257 return (graph_add_def (cfg, def));
258 } /* }}} int def_config */
260 int def_append (graph_def_t *head, graph_def_t *def) /* {{{ */
264 if ((head == NULL) || (def == NULL))
268 while (ptr->next != NULL)
274 } /* }}} int def_append */
276 graph_def_t *def_search (graph_def_t *head, graph_ident_t *ident, /* {{{ */
281 if ((head == NULL) || (ident == NULL) || (ds_name == NULL))
284 for (ptr = head; ptr != NULL; ptr = ptr->next)
286 if (!ident_matches (ptr->select, ident))
289 if (strcmp (ptr->ds_name, ds_name) == 0)
294 } /* }}} graph_def_t *def_search */
296 _Bool def_matches (graph_def_t *def, graph_ident_t *ident) /* {{{ */
298 return (ident_matches (def->select, ident));
299 } /* }}} _Bool def_matches */
301 int def_foreach (graph_def_t *def, def_callback_t callback, /* {{{ */
306 if ((def == NULL) || (callback == NULL))
309 for (ptr = def; ptr != NULL; ptr = ptr->next)
313 status = (*callback) (ptr, user_data);
319 } /* }}} int def_foreach */
321 int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
330 if ((def == NULL) || (ident == NULL) || (args == NULL))
333 file = ident_to_file (ident);
336 DEBUG ("gl_ident_get_rrdargs: ident_to_file returned NULL.\n");
340 DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
342 if (def->legend != NULL)
344 strncpy (legend, def->legend, sizeof (legend));
345 legend[sizeof (legend) - 1] = 0;
349 ident_describe (ident, def->select,
350 legend, sizeof (legend));
352 if ((legend[0] == 0) || (strcmp ("default", legend) == 0))
354 strncpy (legend, def->ds_name, sizeof (legend));
355 legend[sizeof (legend) - 1] = 0;
360 if (color > 0x00ffffff)
361 color = get_random_color ();
367 array_append_format (args->data, "DEF:def_%04i_min=%s:%s:MIN",
368 index, file, def->ds_name);
369 array_append_format (args->data, "DEF:def_%04i_avg=%s:%s:AVERAGE",
370 index, file, def->ds_name);
371 array_append_format (args->data, "DEF:def_%04i_max=%s:%s:MAX",
372 index, file, def->ds_name);
374 array_append_format (args->data, "VDEF:vdef_%04i_min=def_%04i_min,MINIMUM",
376 array_append_format (args->data, "VDEF:vdef_%04i_avg=def_%04i_avg,AVERAGE",
378 array_append_format (args->data, "VDEF:vdef_%04i_max=def_%04i_max,MAXIMUM",
380 array_append_format (args->data, "VDEF:vdef_%04i_lst=def_%04i_avg,LAST",
385 if (args->last_stack_cdef[0] != 0)
387 array_append_format (args->calc, "CDEF:cdef_%04i_stack=%s,def_%04i_avg,+",
388 index, args->last_stack_cdef, index);
389 snprintf (draw_def, sizeof (draw_def), "cdef_%04i_stack", index);
393 snprintf (draw_def, sizeof (draw_def), "def_%04i_avg", index);
398 snprintf (draw_def, sizeof (draw_def), "def_%04i_avg", index);
402 array_prepend_format (args->areas, "AREA:%s#%06"PRIx32,
403 draw_def, fade_color (color));
406 array_prepend_format (args->lines, "GPRINT:vdef_%04i_lst:%s last\\l",
407 index, (def->format != NULL) ? def->format : "%6.2lf");
408 array_prepend_format (args->lines, "GPRINT:vdef_%04i_max:%s max,",
409 index, (def->format != NULL) ? def->format : "%6.2lf");
410 array_prepend_format (args->lines, "GPRINT:vdef_%04i_avg:%s avg,",
411 index, (def->format != NULL) ? def->format : "%6.2lf");
412 array_prepend_format (args->lines, "GPRINT:vdef_%04i_min:%s min,",
413 index, (def->format != NULL) ? def->format : "%6.2lf");
414 array_prepend_format (args->lines, "LINE1:%s#%06"PRIx32":%s",
415 draw_def, color, legend);
419 memcpy (args->last_stack_cdef, draw_def, sizeof (args->last_stack_cdef));
422 } /* }}} int def_get_rrdargs */
424 /* vim: set sw=2 sts=2 et fdm=marker : */