graph_{data,def}_json actions: Don't destroy the graph_config_t object.
[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   snprintf (color, sizeof (color), "#%06"PRIx32, def->color);
160   color[sizeof (color) - 1] = 0;
161
162   yajl_gen_map_open (handler);
163
164 #define yajl_gen_string_cast(h,p,l) \
165   yajl_gen_string (h, (unsigned char *) p, (unsigned int) l)
166
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));
181
182   yajl_gen_map_close (handler);
183
184   return (def_to_json_recursive (def->next, handler));
185 #undef yajl_gen_string_cast
186 } /* }}} int def_to_json_recursive */
187
188 /*
189  * Public functions
190  */
191 graph_def_t *def_create (graph_config_t *cfg, graph_ident_t *ident, /* {{{ */
192     const char *ds_name)
193 {
194   graph_ident_t *selector;
195   graph_def_t *ret;
196
197   if ((cfg == NULL) || (ident == NULL) || (ds_name == NULL))
198   {
199     fprintf (stderr, "def_create: An argument is NULL\n");
200     return (NULL);
201   }
202
203   selector = graph_get_selector (cfg);
204   if (selector == NULL)
205   {
206     fprintf (stderr, "def_create: graph_get_selector failed\n");
207     return (NULL);
208   }
209
210   ret = malloc (sizeof (*ret));
211   if (ret == NULL)
212   {
213     fprintf (stderr, "def_create: malloc failed\n");
214     ident_destroy (selector);
215     return (NULL);
216   }
217   memset (ret, 0, sizeof (*ret));
218   ret->legend = NULL;
219   ret->format = NULL;
220
221   ret->ds_name = strdup (ds_name);
222   if (ret->ds_name == NULL)
223   {
224     fprintf (stderr, "def_create: Unable to copy DS name\n");
225     ident_destroy (selector);
226     free (ret);
227     return (NULL);
228   }
229
230   ret->color = UINT32_MAX;
231   ret->next = NULL;
232
233   ret->select = ident_copy_with_selector (selector, ident,
234       IDENT_FLAG_REPLACE_ALL);
235   if (ret->select == NULL)
236   {
237     fprintf (stderr, "def_create: ident_copy_with_selector failed\n");
238     ident_destroy (selector);
239     free (ret->ds_name);
240     free (ret);
241     return (NULL);
242   }
243
244   ident_destroy (selector);
245   return (ret);
246 } /* }}} graph_def_t *def_create */
247
248 void def_destroy (graph_def_t *def) /* {{{ */
249 {
250   graph_def_t *next;
251
252   if (def == NULL)
253     return;
254
255   next = def->next;
256
257   ident_destroy (def->select);
258
259   free (def->ds_name);
260   free (def->legend);
261   free (def->format);
262
263   free (def);
264
265   def_destroy (next);
266 } /* }}} void def_destroy */
267
268 int def_config (graph_config_t *cfg, const oconfig_item_t *ci) /* {{{ */
269 {
270   graph_def_t *def;
271   int i;
272
273   def = def_config_get_obj (cfg, ci);
274   if (def == NULL)
275     return (EINVAL);
276
277   for (i = 0; i < ci->children_num; i++)
278   {
279     oconfig_item_t *child;
280
281     child = ci->children + i;
282     if (strcasecmp ("Legend", child->key) == 0)
283       graph_config_get_string (child, &def->legend);
284     else if (strcasecmp ("Color", child->key) == 0)
285       def_config_color (child, &def->color);
286     else if (strcasecmp ("Stack", child->key) == 0)
287       graph_config_get_bool (child, &def->stack);
288     else if (strcasecmp ("Area", child->key) == 0)
289       graph_config_get_bool (child, &def->area);
290     else if (strcasecmp ("Format", child->key) == 0)
291       graph_config_get_string (child, &def->format);
292   }
293
294   return (graph_add_def (cfg, def));
295 } /* }}} int def_config */
296
297 int def_append (graph_def_t *head, graph_def_t *def) /* {{{ */
298 {
299   graph_def_t *ptr;
300
301   if ((head == NULL) || (def == NULL))
302     return (EINVAL);
303
304   ptr = head;
305   while (ptr->next != NULL)
306     ptr = ptr->next;
307
308   ptr->next = def;
309
310   return (0);
311 } /* }}} int def_append */
312
313 graph_def_t *def_search (graph_def_t *head, graph_ident_t *ident, /* {{{ */
314     const char *ds_name)
315 {
316   graph_def_t *ptr;
317
318   if ((head == NULL) || (ident == NULL) || (ds_name == NULL))
319     return (NULL);
320
321   for (ptr = head; ptr != NULL; ptr = ptr->next)
322   {
323     if (!ident_matches (ptr->select, ident))
324       continue;
325
326     if (strcmp (ptr->ds_name, ds_name) == 0)
327       return (ptr);
328   }
329
330   return (NULL);
331 } /* }}} graph_def_t *def_search */
332
333 _Bool def_matches (graph_def_t *def, graph_ident_t *ident) /* {{{ */
334 {
335   return (ident_matches (def->select, ident));
336 } /* }}} _Bool def_matches */
337
338 int def_foreach (graph_def_t *def, def_callback_t callback, /* {{{ */
339     void *user_data)
340 {
341   graph_def_t *ptr;
342
343   if ((def == NULL) || (callback == NULL))
344     return (EINVAL);
345
346   for (ptr = def; ptr != NULL; ptr = ptr->next)
347   {
348     int status;
349
350     status = (*callback) (ptr, user_data);
351     if (status != 0)
352       return (status);
353   }
354
355   return (0);
356 } /* }}} int def_foreach */
357
358 int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
359     rrd_args_t *args)
360 {
361   char *file;
362   int index;
363   char draw_def[64];
364   char legend[256];
365   uint32_t color;
366
367   if ((def == NULL) || (ident == NULL) || (args == NULL))
368     return (EINVAL);
369
370   file = ident_to_file (ident);
371   if (file == NULL)
372   {
373     DEBUG ("gl_ident_get_rrdargs: ident_to_file returned NULL.\n");
374     return (-1);
375   }
376
377   DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
378
379   if (def->legend != NULL)
380   {
381     strncpy (legend, def->legend, sizeof (legend));
382     legend[sizeof (legend) - 1] = 0;
383   }
384   else
385   {
386     ident_describe (ident, def->select,
387         legend, sizeof (legend));
388
389     if ((legend[0] == 0) || (strcmp ("default", legend) == 0))
390     {
391       strncpy (legend, def->ds_name, sizeof (legend));
392       legend[sizeof (legend) - 1] = 0;
393     }
394   }
395
396   color = def->color;
397   if (color > 0x00ffffff)
398     color = get_random_color ();
399
400   index = args->index;
401   args->index++;
402
403   /* CDEFs */
404   array_append_format (args->data, "DEF:def_%04i_min=%s:%s:MIN",
405       index, file, def->ds_name);
406   array_append_format (args->data, "DEF:def_%04i_avg=%s:%s:AVERAGE",
407       index, file, def->ds_name);
408   array_append_format (args->data, "DEF:def_%04i_max=%s:%s:MAX",
409       index, file, def->ds_name);
410   /* VDEFs */
411   array_append_format (args->data, "VDEF:vdef_%04i_min=def_%04i_min,MINIMUM",
412       index, index);
413   array_append_format (args->data, "VDEF:vdef_%04i_avg=def_%04i_avg,AVERAGE",
414       index, index);
415   array_append_format (args->data, "VDEF:vdef_%04i_max=def_%04i_max,MAXIMUM",
416       index, index);
417   array_append_format (args->data, "VDEF:vdef_%04i_lst=def_%04i_avg,LAST",
418       index, index);
419
420   if (def->stack)
421   {
422     if (args->last_stack_cdef[0] != 0)
423     {
424       array_append_format (args->calc, "CDEF:cdef_%04i_stack=%s,def_%04i_avg,+",
425           index, args->last_stack_cdef, index);
426       snprintf (draw_def, sizeof (draw_def), "cdef_%04i_stack", index);
427     }
428     else
429     {
430       snprintf (draw_def, sizeof (draw_def), "def_%04i_avg", index);
431     }
432   }
433   else
434   {
435     snprintf (draw_def, sizeof (draw_def), "def_%04i_avg", index);
436   }
437
438   if (def->area)
439     array_prepend_format (args->areas, "AREA:%s#%06"PRIx32,
440         draw_def, fade_color (color));
441
442   /* Graph part */
443   array_prepend_format (args->lines, "GPRINT:vdef_%04i_lst:%s last\\l",
444       index, (def->format != NULL) ? def->format : "%6.2lf");
445   array_prepend_format (args->lines, "GPRINT:vdef_%04i_max:%s max,",
446       index, (def->format != NULL) ? def->format : "%6.2lf");
447   array_prepend_format (args->lines, "GPRINT:vdef_%04i_avg:%s avg,",
448       index, (def->format != NULL) ? def->format : "%6.2lf");
449   array_prepend_format (args->lines, "GPRINT:vdef_%04i_min:%s min,",
450       index, (def->format != NULL) ? def->format : "%6.2lf");
451   array_prepend_format (args->lines, "LINE1:%s#%06"PRIx32":%s",
452       draw_def, color, legend);
453
454   free (file);
455
456   memcpy (args->last_stack_cdef, draw_def, sizeof (args->last_stack_cdef));
457
458   return (0);
459 } /* }}} int def_get_rrdargs */
460
461 int def_to_json (const graph_def_t *def, /* {{{ */
462     yajl_gen handler)
463 {
464   if (handler == NULL)
465     return (EINVAL);
466
467   yajl_gen_array_open (handler);
468   def_to_json_recursive (def, handler);
469   yajl_gen_array_close (handler);
470
471   return (0);
472 } /* }}} int def_to_json */
473
474 /* vim: set sw=2 sts=2 et fdm=marker : */