share/collection.js: Keep visibility when redrawing graphs.
[collection4.git] / src / graph_def.c
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  **/
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27
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"
34
35 #include <fcgiapp.h>
36 #include <fcgi_stdio.h>
37
38 /*
39  * Data structures
40  */
41 struct graph_def_s
42 {
43   graph_ident_t *select;
44
45   char *ds_name;
46   char *legend;
47   uint32_t color;
48   _Bool stack;
49   _Bool area;
50   char *format;
51
52   graph_def_t *next;
53 };
54
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 */
69
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);
75
76 #undef DEF_CONFIG_FIELD
77
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;
83
84   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
85     return (EINVAL);
86
87   tmp = ci->values[0].value.string;
88
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);
94
95   *ret_color = color;
96
97   return (0);
98 } /* }}} int def_config_color */
99
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;
107
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   }
114
115   for (i = 0; i < ci->children_num; i++)
116   {
117     oconfig_item_t *child;
118
119 #define HANDLE_FIELD(name,field) \
120     else if (strcasecmp (name, child->key) == 0) \
121       def_config_##field (child, ident)
122
123     child = ci->children + i;
124     if (strcasecmp ("DSName", child->key) == 0)
125       graph_config_get_string (child, &ds_name);
126
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);
132
133 #undef HANDLE_FIELD
134   }
135
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   }
144
145   ident_destroy (ident);
146   free (ds_name);
147
148   return (def);
149 } /* }}} graph_def_t *def_config_get_obj */
150
151 static int def_to_json_recursive (const graph_def_t *def, /* {{{ */
152     yajl_gen handler)
153 {
154   char color[16];
155
156   if (def == NULL)
157     return (0);
158
159   if (def->color < 0x00ffffff)
160     snprintf (color, sizeof (color), "#%06"PRIx32, def->color);
161   else
162     strncpy (color, "random", sizeof (color));
163   color[sizeof (color) - 1] = 0;
164
165   yajl_gen_map_open (handler);
166
167 #define yajl_gen_string_cast(h,p,l) \
168   yajl_gen_string (h, (unsigned char *) p, (unsigned int) l)
169
170   yajl_gen_string_cast (handler, "select", strlen ("select"));
171   ident_to_json (def->select, handler);
172   if (def->ds_name != NULL)
173   {
174     yajl_gen_string_cast (handler, "ds_name", strlen ("ds_name"));
175     yajl_gen_string_cast (handler, def->ds_name, strlen (def->ds_name));
176   }
177   if (def->legend != NULL)
178   {
179     yajl_gen_string_cast (handler, "legend", strlen ("legend"));
180     yajl_gen_string_cast (handler, def->legend, strlen (def->legend));
181   }
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)
189   {
190     yajl_gen_string_cast (handler, "format", strlen ("format"));
191     yajl_gen_string_cast (handler, def->format, strlen (def->format));
192   }
193
194   yajl_gen_map_close (handler);
195
196   return (def_to_json_recursive (def->next, handler));
197 #undef yajl_gen_string_cast
198 } /* }}} int def_to_json_recursive */
199
200 /*
201  * Public functions
202  */
203 graph_def_t *def_create (graph_config_t *cfg, graph_ident_t *ident, /* {{{ */
204     const char *ds_name)
205 {
206   graph_ident_t *selector;
207   graph_def_t *ret;
208
209   if ((cfg == NULL) || (ident == NULL) || (ds_name == NULL))
210   {
211     fprintf (stderr, "def_create: An argument is NULL\n");
212     return (NULL);
213   }
214
215   selector = graph_get_selector (cfg);
216   if (selector == NULL)
217   {
218     fprintf (stderr, "def_create: graph_get_selector failed\n");
219     return (NULL);
220   }
221
222   ret = malloc (sizeof (*ret));
223   if (ret == NULL)
224   {
225     fprintf (stderr, "def_create: malloc failed\n");
226     ident_destroy (selector);
227     return (NULL);
228   }
229   memset (ret, 0, sizeof (*ret));
230   ret->legend = NULL;
231   ret->format = NULL;
232
233   ret->ds_name = strdup (ds_name);
234   if (ret->ds_name == NULL)
235   {
236     fprintf (stderr, "def_create: Unable to copy DS name\n");
237     ident_destroy (selector);
238     free (ret);
239     return (NULL);
240   }
241
242   ret->color = UINT32_MAX;
243   ret->next = NULL;
244
245   ret->select = ident_copy_with_selector (selector, ident,
246       IDENT_FLAG_REPLACE_ALL);
247   if (ret->select == NULL)
248   {
249     fprintf (stderr, "def_create: ident_copy_with_selector failed\n");
250     ident_destroy (selector);
251     free (ret->ds_name);
252     free (ret);
253     return (NULL);
254   }
255
256   ident_destroy (selector);
257   return (ret);
258 } /* }}} graph_def_t *def_create */
259
260 void def_destroy (graph_def_t *def) /* {{{ */
261 {
262   graph_def_t *next;
263
264   if (def == NULL)
265     return;
266
267   next = def->next;
268
269   ident_destroy (def->select);
270
271   free (def->ds_name);
272   free (def->legend);
273   free (def->format);
274
275   free (def);
276
277   def_destroy (next);
278 } /* }}} void def_destroy */
279
280 int def_config (graph_config_t *cfg, const oconfig_item_t *ci) /* {{{ */
281 {
282   graph_def_t *def;
283   int i;
284
285   def = def_config_get_obj (cfg, ci);
286   if (def == NULL)
287     return (EINVAL);
288
289   for (i = 0; i < ci->children_num; i++)
290   {
291     oconfig_item_t *child;
292
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);
304   }
305
306   return (graph_add_def (cfg, def));
307 } /* }}} int def_config */
308
309 int def_append (graph_def_t *head, graph_def_t *def) /* {{{ */
310 {
311   graph_def_t *ptr;
312
313   if ((head == NULL) || (def == NULL))
314     return (EINVAL);
315
316   ptr = head;
317   while (ptr->next != NULL)
318     ptr = ptr->next;
319
320   ptr->next = def;
321
322   return (0);
323 } /* }}} int def_append */
324
325 graph_def_t *def_search (graph_def_t *head, graph_ident_t *ident, /* {{{ */
326     const char *ds_name)
327 {
328   graph_def_t *ptr;
329
330   if ((head == NULL) || (ident == NULL) || (ds_name == NULL))
331     return (NULL);
332
333   for (ptr = head; ptr != NULL; ptr = ptr->next)
334   {
335     if (!ident_matches (ptr->select, ident))
336       continue;
337
338     if (strcmp (ptr->ds_name, ds_name) == 0)
339       return (ptr);
340   }
341
342   return (NULL);
343 } /* }}} graph_def_t *def_search */
344
345 _Bool def_matches (graph_def_t *def, graph_ident_t *ident) /* {{{ */
346 {
347   return (ident_matches (def->select, ident));
348 } /* }}} _Bool def_matches */
349
350 int def_foreach (graph_def_t *def, def_callback_t callback, /* {{{ */
351     void *user_data)
352 {
353   graph_def_t *ptr;
354
355   if ((def == NULL) || (callback == NULL))
356     return (EINVAL);
357
358   for (ptr = def; ptr != NULL; ptr = ptr->next)
359   {
360     int status;
361
362     status = (*callback) (ptr, user_data);
363     if (status != 0)
364       return (status);
365   }
366
367   return (0);
368 } /* }}} int def_foreach */
369
370 int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
371     rrd_args_t *args)
372 {
373   char *file;
374   int index;
375   char draw_def[64];
376   char legend[256];
377   uint32_t color;
378
379   if ((def == NULL) || (ident == NULL) || (args == NULL))
380     return (EINVAL);
381
382   file = ident_to_file (ident);
383   if (file == NULL)
384   {
385     DEBUG ("gl_ident_get_rrdargs: ident_to_file returned NULL.\n");
386     return (-1);
387   }
388
389   DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
390
391   if (def->legend != NULL)
392   {
393     strncpy (legend, def->legend, sizeof (legend));
394     legend[sizeof (legend) - 1] = 0;
395   }
396   else
397   {
398     ident_describe (ident, def->select,
399         legend, sizeof (legend));
400
401     if ((legend[0] == 0) || (strcmp ("default", legend) == 0))
402     {
403       strncpy (legend, def->ds_name, sizeof (legend));
404       legend[sizeof (legend) - 1] = 0;
405     }
406   }
407
408   color = def->color;
409   if (color > 0x00ffffff)
410     color = get_random_color ();
411
412   index = args->index;
413   args->index++;
414
415   /* CDEFs */
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);
422   /* VDEFs */
423   array_append_format (args->data, "VDEF:vdef_%04i_min=def_%04i_min,MINIMUM",
424       index, index);
425   array_append_format (args->data, "VDEF:vdef_%04i_avg=def_%04i_avg,AVERAGE",
426       index, index);
427   array_append_format (args->data, "VDEF:vdef_%04i_max=def_%04i_max,MAXIMUM",
428       index, index);
429   array_append_format (args->data, "VDEF:vdef_%04i_lst=def_%04i_avg,LAST",
430       index, index);
431
432   if (def->stack)
433   {
434     if (args->last_stack_cdef[0] != 0)
435     {
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);
439     }
440     else
441     {
442       snprintf (draw_def, sizeof (draw_def), "def_%04i_avg", index);
443     }
444   }
445   else
446   {
447     snprintf (draw_def, sizeof (draw_def), "def_%04i_avg", index);
448   }
449
450   if (def->area)
451     array_prepend_format (args->areas, "AREA:%s#%06"PRIx32,
452         draw_def, fade_color (color));
453
454   /* Graph part */
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);
465
466   free (file);
467
468   memcpy (args->last_stack_cdef, draw_def, sizeof (args->last_stack_cdef));
469
470   return (0);
471 } /* }}} int def_get_rrdargs */
472
473 int def_to_json (const graph_def_t *def, /* {{{ */
474     yajl_gen handler)
475 {
476   if (handler == NULL)
477     return (EINVAL);
478
479   yajl_gen_array_open (handler);
480   def_to_json_recursive (def, handler);
481   yajl_gen_array_close (handler);
482
483   return (0);
484 } /* }}} int def_to_json */
485
486 /* vim: set sw=2 sts=2 et fdm=marker : */