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 */
151 static int def_to_json_recursive (const graph_def_t *def, /* {{{ */
159 if (def->color < 0x00ffffff)
160 snprintf (color, sizeof (color), "#%06"PRIx32, def->color);
162 strncpy (color, "random", sizeof (color));
163 color[sizeof (color) - 1] = 0;
165 yajl_gen_map_open (handler);
167 #define yajl_gen_string_cast(h,p,l) \
168 yajl_gen_string (h, (unsigned char *) p, (unsigned int) l)
170 yajl_gen_string_cast (handler, "select", strlen ("select"));
171 ident_to_json (def->select, handler);
172 if (def->ds_name != NULL)
174 yajl_gen_string_cast (handler, "ds_name", strlen ("ds_name"));
175 yajl_gen_string_cast (handler, def->ds_name, strlen (def->ds_name));
177 if (def->legend != NULL)
179 yajl_gen_string_cast (handler, "legend", strlen ("legend"));
180 yajl_gen_string_cast (handler, def->legend, strlen (def->legend));
182 yajl_gen_string_cast (handler, "color", strlen ("color"));
183 yajl_gen_string_cast (handler, color, strlen (color));
184 yajl_gen_string_cast (handler, "stack", strlen ("stack"));
185 yajl_gen_bool (handler, def->stack);
186 yajl_gen_string_cast (handler, "area", strlen ("area"));
187 yajl_gen_bool (handler, def->area);
188 if (def->format != NULL)
190 yajl_gen_string_cast (handler, "format", strlen ("format"));
191 yajl_gen_string_cast (handler, def->format, strlen (def->format));
194 yajl_gen_map_close (handler);
196 return (def_to_json_recursive (def->next, handler));
197 #undef yajl_gen_string_cast
198 } /* }}} int def_to_json_recursive */
203 graph_def_t *def_create (graph_config_t *cfg, graph_ident_t *ident, /* {{{ */
206 graph_ident_t *selector;
209 if ((cfg == NULL) || (ident == NULL) || (ds_name == NULL))
211 fprintf (stderr, "def_create: An argument is NULL\n");
215 selector = graph_get_selector (cfg);
216 if (selector == NULL)
218 fprintf (stderr, "def_create: graph_get_selector failed\n");
222 ret = malloc (sizeof (*ret));
225 fprintf (stderr, "def_create: malloc failed\n");
226 ident_destroy (selector);
229 memset (ret, 0, sizeof (*ret));
233 ret->ds_name = strdup (ds_name);
234 if (ret->ds_name == NULL)
236 fprintf (stderr, "def_create: Unable to copy DS name\n");
237 ident_destroy (selector);
242 ret->color = UINT32_MAX;
245 ret->select = ident_copy_with_selector (selector, ident,
246 IDENT_FLAG_REPLACE_ALL);
247 if (ret->select == NULL)
249 fprintf (stderr, "def_create: ident_copy_with_selector failed\n");
250 ident_destroy (selector);
256 ident_destroy (selector);
258 } /* }}} graph_def_t *def_create */
260 void def_destroy (graph_def_t *def) /* {{{ */
269 ident_destroy (def->select);
278 } /* }}} void def_destroy */
280 int def_config (graph_config_t *cfg, const oconfig_item_t *ci) /* {{{ */
285 def = def_config_get_obj (cfg, ci);
289 for (i = 0; i < ci->children_num; i++)
291 oconfig_item_t *child;
293 child = ci->children + i;
294 if (strcasecmp ("Legend", child->key) == 0)
295 graph_config_get_string (child, &def->legend);
296 else if (strcasecmp ("Color", child->key) == 0)
297 def_config_color (child, &def->color);
298 else if (strcasecmp ("Stack", child->key) == 0)
299 graph_config_get_bool (child, &def->stack);
300 else if (strcasecmp ("Area", child->key) == 0)
301 graph_config_get_bool (child, &def->area);
302 else if (strcasecmp ("Format", child->key) == 0)
303 graph_config_get_string (child, &def->format);
306 return (graph_add_def (cfg, def));
307 } /* }}} int def_config */
309 int def_append (graph_def_t *head, graph_def_t *def) /* {{{ */
313 if ((head == NULL) || (def == NULL))
317 while (ptr->next != NULL)
323 } /* }}} int def_append */
325 graph_def_t *def_search (graph_def_t *head, graph_ident_t *ident, /* {{{ */
330 if ((head == NULL) || (ident == NULL) || (ds_name == NULL))
333 for (ptr = head; ptr != NULL; ptr = ptr->next)
335 if (!ident_matches (ptr->select, ident))
338 if (strcmp (ptr->ds_name, ds_name) == 0)
343 } /* }}} graph_def_t *def_search */
345 _Bool def_matches (graph_def_t *def, graph_ident_t *ident) /* {{{ */
347 return (ident_matches (def->select, ident));
348 } /* }}} _Bool def_matches */
350 int def_foreach (graph_def_t *def, def_callback_t callback, /* {{{ */
355 if ((def == NULL) || (callback == NULL))
358 for (ptr = def; ptr != NULL; ptr = ptr->next)
362 status = (*callback) (ptr, user_data);
368 } /* }}} int def_foreach */
370 int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
379 if ((def == NULL) || (ident == NULL) || (args == NULL))
382 file = ident_to_file (ident);
385 DEBUG ("gl_ident_get_rrdargs: ident_to_file returned NULL.\n");
389 DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
391 if (def->legend != NULL)
393 strncpy (legend, def->legend, sizeof (legend));
394 legend[sizeof (legend) - 1] = 0;
398 ident_describe (ident, def->select,
399 legend, sizeof (legend));
401 if ((legend[0] == 0) || (strcmp ("default", legend) == 0))
403 strncpy (legend, def->ds_name, sizeof (legend));
404 legend[sizeof (legend) - 1] = 0;
409 if (color > 0x00ffffff)
410 color = get_random_color ();
416 array_append_format (args->data, "DEF:def_%04i_min=%s:%s:MIN",
417 index, file, def->ds_name);
418 array_append_format (args->data, "DEF:def_%04i_avg=%s:%s:AVERAGE",
419 index, file, def->ds_name);
420 array_append_format (args->data, "DEF:def_%04i_max=%s:%s:MAX",
421 index, file, def->ds_name);
423 array_append_format (args->data, "VDEF:vdef_%04i_min=def_%04i_min,MINIMUM",
425 array_append_format (args->data, "VDEF:vdef_%04i_avg=def_%04i_avg,AVERAGE",
427 array_append_format (args->data, "VDEF:vdef_%04i_max=def_%04i_max,MAXIMUM",
429 array_append_format (args->data, "VDEF:vdef_%04i_lst=def_%04i_avg,LAST",
434 if (args->last_stack_cdef[0] != 0)
436 array_append_format (args->calc, "CDEF:cdef_%04i_stack=%s,def_%04i_avg,+",
437 index, args->last_stack_cdef, index);
438 snprintf (draw_def, sizeof (draw_def), "cdef_%04i_stack", index);
442 snprintf (draw_def, sizeof (draw_def), "def_%04i_avg", index);
447 snprintf (draw_def, sizeof (draw_def), "def_%04i_avg", index);
451 array_prepend_format (args->areas, "AREA:%s#%06"PRIx32,
452 draw_def, fade_color (color));
455 array_prepend_format (args->lines, "GPRINT:vdef_%04i_lst:%s last\\l",
456 index, (def->format != NULL) ? def->format : "%6.2lf");
457 array_prepend_format (args->lines, "GPRINT:vdef_%04i_max:%s max,",
458 index, (def->format != NULL) ? def->format : "%6.2lf");
459 array_prepend_format (args->lines, "GPRINT:vdef_%04i_avg:%s avg,",
460 index, (def->format != NULL) ? def->format : "%6.2lf");
461 array_prepend_format (args->lines, "GPRINT:vdef_%04i_min:%s min,",
462 index, (def->format != NULL) ? def->format : "%6.2lf");
463 array_prepend_format (args->lines, "LINE1:%s#%06"PRIx32":%s",
464 draw_def, color, legend);
468 memcpy (args->last_stack_cdef, draw_def, sizeof (args->last_stack_cdef));
471 } /* }}} int def_get_rrdargs */
473 int def_to_json (const graph_def_t *def, /* {{{ */
479 yajl_gen_array_open (handler);
480 def_to_json_recursive (def, handler);
481 yajl_gen_array_close (handler);
484 } /* }}} int def_to_json */
486 /* vim: set sw=2 sts=2 et fdm=marker : */