7ac8dac51920fa2b7f70f87a2155fade131d2684
1 /**
2 * collection4 - graph_def.c
3 * Copyright (C) 2010 Florian octo Forster
4 *
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.
9 *
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.
14 *
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
19 *
20 * Authors:
21 * Florian octo Forster <ff at octo.it>
22 **/
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
28 #include "graph_def.h"
29 #include "graph.h"
30 #include "graph_config.h"
31 #include "graph_ident.h"
32 #include "common.h"
33 #include "oconfig.h"
35 #include <fcgiapp.h>
36 #include <fcgi_stdio.h>
38 /*
39 * Data structures
40 */
41 struct graph_def_s
42 {
43 graph_ident_t *select;
45 char *ds_name;
46 char *legend;
47 uint32_t color;
48 _Bool stack;
49 _Bool area;
50 char *format;
52 graph_def_t *next;
53 };
55 /*
56 * Private functions
57 */
58 #define DEF_CONFIG_FIELD(field) \
59 static int def_config_##field (const oconfig_item_t *ci, graph_ident_t *ident) \
60 { \
61 char *tmp = NULL; \
62 int status = graph_config_get_string (ci, &tmp); \
63 if (status != 0) \
64 return (status); \
65 ident_set_##field (ident, tmp); \
66 free (tmp); \
67 return (0); \
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) /* {{{ */
79 {
80 char *tmp;
81 char *endptr;
82 uint32_t color;
84 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
85 return (EINVAL);
87 tmp = ci->values[0].value.string;
89 endptr = NULL;
90 errno = 0;
91 color = (uint32_t) strtoul (tmp, &endptr, /* base = */ 16);
92 if ((errno != 0) || (endptr == tmp) || (color > 0x00ffffff))
93 return (EINVAL);
95 *ret_color = color;
97 return (0);
98 } /* }}} int def_config_color */
100 static graph_def_t *def_config_get_obj (graph_config_t *cfg, /* {{{ */
101 const oconfig_item_t *ci)
102 {
103 graph_ident_t *ident;
104 char *ds_name = NULL;
105 graph_def_t *def;
106 int i;
108 ident = graph_get_selector (cfg);
109 if (ident == NULL)
110 {
111 fprintf (stderr, "def_config_get_obj: graph_get_selector failed");
112 return (NULL);
113 }
115 for (i = 0; i < ci->children_num; i++)
116 {
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);
133 #undef HANDLE_FIELD
134 }
136 def = def_create (cfg, ident, ds_name);
137 if (def == NULL)
138 {
139 fprintf (stderr, "def_config_get_obj: def_create failed\n");
140 ident_destroy (ident);
141 free (ds_name);
142 return (NULL);
143 }
145 ident_destroy (ident);
146 free (ds_name);
148 return (def);
149 } /* }}} graph_def_t *def_config_get_obj */
151 static int def_to_json_recursive (const graph_def_t *def, /* {{{ */
152 yajl_gen handler)
153 {
154 char color[16];
156 if (def == NULL)
157 return (0);
159 snprintf (color, sizeof (color), "%06"PRIx32, def->color);
160 color[sizeof (color) - 1] = 0;
162 yajl_gen_map_open (handler);
164 #define yajl_gen_string_cast(h,p,l) \
165 yajl_gen_string (h, (unsigned char *) p, (unsigned int) l)
167 yajl_gen_string_cast (handler, "select", strlen ("select"));
168 ident_to_json (def->select, handler);
169 yajl_gen_string_cast (handler, "ds_name", strlen ("ds_name"));
170 yajl_gen_string_cast (handler, def->ds_name, strlen (def->ds_name));
171 yajl_gen_string_cast (handler, "legend", strlen ("legend"));
172 yajl_gen_string_cast (handler, def->legend, strlen (def->legend));
173 yajl_gen_string_cast (handler, "color", strlen ("color"));
174 yajl_gen_string_cast (handler, color, strlen (color));
175 yajl_gen_string_cast (handler, "stack", strlen ("stack"));
176 yajl_gen_bool (handler, def->stack);
177 yajl_gen_string_cast (handler, "area", strlen ("area"));
178 yajl_gen_bool (handler, def->area);
179 yajl_gen_string_cast (handler, "format", strlen ("format"));
180 yajl_gen_string_cast (handler, def->format, strlen (def->format));
182 yajl_gen_map_close (handler);
184 return (def_to_json_recursive (def->next, handler));
185 } /* }}} int def_to_json_recursive */
187 /*
188 * Public functions
189 */
190 graph_def_t *def_create (graph_config_t *cfg, graph_ident_t *ident, /* {{{ */
191 const char *ds_name)
192 {
193 graph_ident_t *selector;
194 graph_def_t *ret;
196 if ((cfg == NULL) || (ident == NULL) || (ds_name == NULL))
197 {
198 fprintf (stderr, "def_create: An argument is NULL\n");
199 return (NULL);
200 }
202 selector = graph_get_selector (cfg);
203 if (selector == NULL)
204 {
205 fprintf (stderr, "def_create: graph_get_selector failed\n");
206 return (NULL);
207 }
209 ret = malloc (sizeof (*ret));
210 if (ret == NULL)
211 {
212 fprintf (stderr, "def_create: malloc failed\n");
213 ident_destroy (selector);
214 return (NULL);
215 }
216 memset (ret, 0, sizeof (*ret));
217 ret->legend = NULL;
218 ret->format = NULL;
220 ret->ds_name = strdup (ds_name);
221 if (ret->ds_name == NULL)
222 {
223 fprintf (stderr, "def_create: Unable to copy DS name\n");
224 ident_destroy (selector);
225 free (ret);
226 return (NULL);
227 }
229 ret->color = UINT32_MAX;
230 ret->next = NULL;
232 ret->select = ident_copy_with_selector (selector, ident,
233 IDENT_FLAG_REPLACE_ALL);
234 if (ret->select == NULL)
235 {
236 fprintf (stderr, "def_create: ident_copy_with_selector failed\n");
237 ident_destroy (selector);
238 free (ret->ds_name);
239 free (ret);
240 return (NULL);
241 }
243 ident_destroy (selector);
244 return (ret);
245 } /* }}} graph_def_t *def_create */
247 void def_destroy (graph_def_t *def) /* {{{ */
248 {
249 graph_def_t *next;
251 if (def == NULL)
252 return;
254 next = def->next;
256 ident_destroy (def->select);
258 free (def->ds_name);
259 free (def->legend);
260 free (def->format);
262 free (def);
264 def_destroy (next);
265 } /* }}} void def_destroy */
267 int def_config (graph_config_t *cfg, const oconfig_item_t *ci) /* {{{ */
268 {
269 graph_def_t *def;
270 int i;
272 def = def_config_get_obj (cfg, ci);
273 if (def == NULL)
274 return (EINVAL);
276 for (i = 0; i < ci->children_num; i++)
277 {
278 oconfig_item_t *child;
280 child = ci->children + i;
281 if (strcasecmp ("Legend", child->key) == 0)
282 graph_config_get_string (child, &def->legend);
283 else if (strcasecmp ("Color", child->key) == 0)
284 def_config_color (child, &def->color);
285 else if (strcasecmp ("Stack", child->key) == 0)
286 graph_config_get_bool (child, &def->stack);
287 else if (strcasecmp ("Area", child->key) == 0)
288 graph_config_get_bool (child, &def->area);
289 else if (strcasecmp ("Format", child->key) == 0)
290 graph_config_get_string (child, &def->format);
291 }
293 return (graph_add_def (cfg, def));
294 } /* }}} int def_config */
296 int def_append (graph_def_t *head, graph_def_t *def) /* {{{ */
297 {
298 graph_def_t *ptr;
300 if ((head == NULL) || (def == NULL))
301 return (EINVAL);
303 ptr = head;
304 while (ptr->next != NULL)
305 ptr = ptr->next;
307 ptr->next = def;
309 return (0);
310 } /* }}} int def_append */
312 graph_def_t *def_search (graph_def_t *head, graph_ident_t *ident, /* {{{ */
313 const char *ds_name)
314 {
315 graph_def_t *ptr;
317 if ((head == NULL) || (ident == NULL) || (ds_name == NULL))
318 return (NULL);
320 for (ptr = head; ptr != NULL; ptr = ptr->next)
321 {
322 if (!ident_matches (ptr->select, ident))
323 continue;
325 if (strcmp (ptr->ds_name, ds_name) == 0)
326 return (ptr);
327 }
329 return (NULL);
330 } /* }}} graph_def_t *def_search */
332 _Bool def_matches (graph_def_t *def, graph_ident_t *ident) /* {{{ */
333 {
334 return (ident_matches (def->select, ident));
335 } /* }}} _Bool def_matches */
337 int def_foreach (graph_def_t *def, def_callback_t callback, /* {{{ */
338 void *user_data)
339 {
340 graph_def_t *ptr;
342 if ((def == NULL) || (callback == NULL))
343 return (EINVAL);
345 for (ptr = def; ptr != NULL; ptr = ptr->next)
346 {
347 int status;
349 status = (*callback) (ptr, user_data);
350 if (status != 0)
351 return (status);
352 }
354 return (0);
355 } /* }}} int def_foreach */
357 int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
358 rrd_args_t *args)
359 {
360 char *file;
361 int index;
362 char draw_def[64];
363 char legend[256];
364 uint32_t color;
366 if ((def == NULL) || (ident == NULL) || (args == NULL))
367 return (EINVAL);
369 file = ident_to_file (ident);
370 if (file == NULL)
371 {
372 DEBUG ("gl_ident_get_rrdargs: ident_to_file returned NULL.\n");
373 return (-1);
374 }
376 DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
378 if (def->legend != NULL)
379 {
380 strncpy (legend, def->legend, sizeof (legend));
381 legend[sizeof (legend) - 1] = 0;
382 }
383 else
384 {
385 ident_describe (ident, def->select,
386 legend, sizeof (legend));
388 if ((legend[0] == 0) || (strcmp ("default", legend) == 0))
389 {
390 strncpy (legend, def->ds_name, sizeof (legend));
391 legend[sizeof (legend) - 1] = 0;
392 }
393 }
395 color = def->color;
396 if (color > 0x00ffffff)
397 color = get_random_color ();
399 index = args->index;
400 args->index++;
402 /* CDEFs */
403 array_append_format (args->data, "DEF:def_%04i_min=%s:%s:MIN",
404 index, file, def->ds_name);
405 array_append_format (args->data, "DEF:def_%04i_avg=%s:%s:AVERAGE",
406 index, file, def->ds_name);
407 array_append_format (args->data, "DEF:def_%04i_max=%s:%s:MAX",
408 index, file, def->ds_name);
409 /* VDEFs */
410 array_append_format (args->data, "VDEF:vdef_%04i_min=def_%04i_min,MINIMUM",
411 index, index);
412 array_append_format (args->data, "VDEF:vdef_%04i_avg=def_%04i_avg,AVERAGE",
413 index, index);
414 array_append_format (args->data, "VDEF:vdef_%04i_max=def_%04i_max,MAXIMUM",
415 index, index);
416 array_append_format (args->data, "VDEF:vdef_%04i_lst=def_%04i_avg,LAST",
417 index, index);
419 if (def->stack)
420 {
421 if (args->last_stack_cdef[0] != 0)
422 {
423 array_append_format (args->calc, "CDEF:cdef_%04i_stack=%s,def_%04i_avg,+",
424 index, args->last_stack_cdef, index);
425 snprintf (draw_def, sizeof (draw_def), "cdef_%04i_stack", index);
426 }
427 else
428 {
429 snprintf (draw_def, sizeof (draw_def), "def_%04i_avg", index);
430 }
431 }
432 else
433 {
434 snprintf (draw_def, sizeof (draw_def), "def_%04i_avg", index);
435 }
437 if (def->area)
438 array_prepend_format (args->areas, "AREA:%s#%06"PRIx32,
439 draw_def, fade_color (color));
441 /* Graph part */
442 array_prepend_format (args->lines, "GPRINT:vdef_%04i_lst:%s last\\l",
443 index, (def->format != NULL) ? def->format : "%6.2lf");
444 array_prepend_format (args->lines, "GPRINT:vdef_%04i_max:%s max,",
445 index, (def->format != NULL) ? def->format : "%6.2lf");
446 array_prepend_format (args->lines, "GPRINT:vdef_%04i_avg:%s avg,",
447 index, (def->format != NULL) ? def->format : "%6.2lf");
448 array_prepend_format (args->lines, "GPRINT:vdef_%04i_min:%s min,",
449 index, (def->format != NULL) ? def->format : "%6.2lf");
450 array_prepend_format (args->lines, "LINE1:%s#%06"PRIx32":%s",
451 draw_def, color, legend);
453 free (file);
455 memcpy (args->last_stack_cdef, draw_def, sizeof (args->last_stack_cdef));
457 return (0);
458 } /* }}} int def_get_rrdargs */
460 int def_to_json (const graph_def_t *def, /* {{{ */
461 yajl_gen handler)
462 {
463 if (handler == NULL)
464 return (EINVAL);
466 yajl_gen_array_open (handler);
467 def_to_json_recursive (def, handler);
468 yajl_gen_array_close (handler);
470 return (0);
471 } /* }}} int def_to_json */
473 /* vim: set sw=2 sts=2 et fdm=marker : */