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))
163 selector = graph_get_selector (cfg);
164 if (selector == NULL)
167 ret = malloc (sizeof (*ret));
170 ident_destroy (selector);
173 memset (ret, 0, sizeof (*ret));
177 ret->ds_name = strdup (ds_name);
178 if (ret->ds_name == NULL)
180 ident_destroy (selector);
185 ret->color = get_random_color ();
188 ret->select = ident_copy_with_selector (selector, ident,
189 IDENT_FLAG_REPLACE_ALL);
190 if (ret->select == NULL)
192 ident_destroy (selector);
198 ident_destroy (selector);
200 } /* }}} graph_def_t *def_create */
202 void def_destroy (graph_def_t *def) /* {{{ */
211 ident_destroy (def->select);
220 } /* }}} void def_destroy */
222 int def_config (graph_config_t *cfg, const oconfig_item_t *ci) /* {{{ */
227 def = def_config_get_obj (cfg, ci);
231 for (i = 0; i < ci->children_num; i++)
233 oconfig_item_t *child;
235 child = ci->children + i;
236 if (strcasecmp ("Legend", child->key) == 0)
237 graph_config_get_string (child, &def->legend);
238 else if (strcasecmp ("Color", child->key) == 0)
239 def_config_color (child, &def->color);
240 else if (strcasecmp ("Stack", child->key) == 0)
241 graph_config_get_bool (child, &def->stack);
242 else if (strcasecmp ("Area", child->key) == 0)
243 graph_config_get_bool (child, &def->area);
244 else if (strcasecmp ("Format", child->key) == 0)
245 graph_config_get_string (child, &def->format);
248 return (graph_add_def (cfg, def));
249 } /* }}} int def_config */
251 int def_append (graph_def_t *head, graph_def_t *def) /* {{{ */
255 if ((head == NULL) || (def == NULL))
259 while (ptr->next != NULL)
265 } /* }}} int def_append */
267 graph_def_t *def_search (graph_def_t *head, graph_ident_t *ident, /* {{{ */
272 if ((head == NULL) || (ident == NULL) || (ds_name == NULL))
275 for (ptr = head; ptr != NULL; ptr = ptr->next)
277 if (!ident_matches (ptr->select, ident))
280 if (strcmp (ptr->ds_name, ds_name) == 0)
285 } /* }}} graph_def_t *def_search */
287 _Bool def_matches (graph_def_t *def, graph_ident_t *ident) /* {{{ */
289 return (ident_matches (def->select, ident));
290 } /* }}} _Bool def_matches */
292 int def_foreach (graph_def_t *def, def_callback_t callback, /* {{{ */
297 if ((def == NULL) || (callback == NULL))
300 for (ptr = def; ptr != NULL; ptr = ptr->next)
304 status = (*callback) (ptr, user_data);
310 } /* }}} int def_foreach */
312 int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
319 if ((def == NULL) || (ident == NULL) || (args == NULL))
322 file = ident_to_file (ident);
325 DEBUG ("gl_ident_get_rrdargs: ident_to_file returned NULL.\n");
329 DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
335 array_append_format (args->data, "DEF:def_%04i_min=%s:%s:MIN",
336 index, file, def->ds_name);
337 array_append_format (args->data, "DEF:def_%04i_avg=%s:%s:AVERAGE",
338 index, file, def->ds_name);
339 array_append_format (args->data, "DEF:def_%04i_max=%s:%s:MAX",
340 index, file, def->ds_name);
342 array_append_format (args->data, "VDEF:vdef_%04i_min=def_%04i_min,MINIMUM",
344 array_append_format (args->data, "VDEF:vdef_%04i_avg=def_%04i_avg,AVERAGE",
346 array_append_format (args->data, "VDEF:vdef_%04i_max=def_%04i_max,MAXIMUM",
348 array_append_format (args->data, "VDEF:vdef_%04i_lst=def_%04i_avg,LAST",
353 if (args->last_stack_cdef[0] != 0)
355 array_append_format (args->calc, "CDEF:cdef_%04i_stack=%s,def_%04i_avg,+",
356 index, args->last_stack_cdef, index);
357 snprintf (draw_def, sizeof (draw_def), "cdef_%04i_stack", index);
361 snprintf (draw_def, sizeof (draw_def), "def_%04i_avg", index);
366 snprintf (draw_def, sizeof (draw_def), "def_%04i_avg", index);
370 array_prepend_format (args->areas, "AREA:%s#%06"PRIx32,
371 draw_def, fade_color (def->color));
374 array_prepend_format (args->lines, "GPRINT:vdef_%04i_lst:%s last\\l",
375 index, (def->format != NULL) ? def->format : "%6.2lf");
376 array_prepend_format (args->lines, "GPRINT:vdef_%04i_max:%s max,",
377 index, (def->format != NULL) ? def->format : "%6.2lf");
378 array_prepend_format (args->lines, "GPRINT:vdef_%04i_avg:%s avg,",
379 index, (def->format != NULL) ? def->format : "%6.2lf");
380 array_prepend_format (args->lines, "GPRINT:vdef_%04i_min:%s min,",
381 index, (def->format != NULL) ? def->format : "%6.2lf");
382 array_prepend_format (args->lines, "LINE1:%s#%06"PRIx32":%s",
383 draw_def, def->color,
384 (def->legend != NULL) ? def->legend : def->ds_name);
388 memcpy (args->last_stack_cdef, draw_def, sizeof (args->last_stack_cdef));
391 } /* }}} int def_get_rrdargs */
393 /* vim: set sw=2 sts=2 et fdm=marker : */