src/graph_def.c: Use "ident_describe" to generate a legend entry …
[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 /*
152  * Public functions
153  */
154 graph_def_t *def_create (graph_config_t *cfg, graph_ident_t *ident, /* {{{ */
155     const char *ds_name)
156 {
157   graph_ident_t *selector;
158   graph_def_t *ret;
159
160   if ((cfg == NULL) || (ident == NULL) || (ds_name == NULL))
161     return (NULL);
162
163   selector = graph_get_selector (cfg);
164   if (selector == NULL)
165     return (NULL);
166
167   ret = malloc (sizeof (*ret));
168   if (ret == NULL)
169   {
170     ident_destroy (selector);
171     return (NULL);
172   }
173   memset (ret, 0, sizeof (*ret));
174   ret->legend = NULL;
175   ret->format = NULL;
176
177   ret->ds_name = strdup (ds_name);
178   if (ret->ds_name == NULL)
179   {
180     ident_destroy (selector);
181     free (ret);
182     return (NULL);
183   }
184
185   ret->color = get_random_color ();
186   ret->next = NULL;
187
188   ret->select = ident_copy_with_selector (selector, ident,
189       IDENT_FLAG_REPLACE_ALL);
190   if (ret->select == NULL)
191   {
192     ident_destroy (selector);
193     free (ret->ds_name);
194     free (ret);
195     return (NULL);
196   }
197
198   ident_destroy (selector);
199   return (ret);
200 } /* }}} graph_def_t *def_create */
201
202 void def_destroy (graph_def_t *def) /* {{{ */
203 {
204   graph_def_t *next;
205
206   if (def == NULL)
207     return;
208
209   next = def->next;
210
211   ident_destroy (def->select);
212
213   free (def->ds_name);
214   free (def->legend);
215   free (def->format);
216
217   free (def);
218
219   def_destroy (next);
220 } /* }}} void def_destroy */
221
222 int def_config (graph_config_t *cfg, const oconfig_item_t *ci) /* {{{ */
223 {
224   graph_def_t *def;
225   int i;
226
227   def = def_config_get_obj (cfg, ci);
228   if (def == NULL)
229     return (EINVAL);
230
231   for (i = 0; i < ci->children_num; i++)
232   {
233     oconfig_item_t *child;
234
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);
246   }
247
248   return (graph_add_def (cfg, def));
249 } /* }}} int def_config */
250
251 int def_append (graph_def_t *head, graph_def_t *def) /* {{{ */
252 {
253   graph_def_t *ptr;
254
255   if ((head == NULL) || (def == NULL))
256     return (EINVAL);
257
258   ptr = head;
259   while (ptr->next != NULL)
260     ptr = ptr->next;
261
262   ptr->next = def;
263
264   return (0);
265 } /* }}} int def_append */
266
267 graph_def_t *def_search (graph_def_t *head, graph_ident_t *ident, /* {{{ */
268     const char *ds_name)
269 {
270   graph_def_t *ptr;
271
272   if ((head == NULL) || (ident == NULL) || (ds_name == NULL))
273     return (NULL);
274
275   for (ptr = head; ptr != NULL; ptr = ptr->next)
276   {
277     if (!ident_matches (ptr->select, ident))
278       continue;
279
280     if (strcmp (ptr->ds_name, ds_name) == 0)
281       return (ptr);
282   }
283
284   return (NULL);
285 } /* }}} graph_def_t *def_search */
286
287 _Bool def_matches (graph_def_t *def, graph_ident_t *ident) /* {{{ */
288 {
289   return (ident_matches (def->select, ident));
290 } /* }}} _Bool def_matches */
291
292 int def_foreach (graph_def_t *def, def_callback_t callback, /* {{{ */
293     void *user_data)
294 {
295   graph_def_t *ptr;
296
297   if ((def == NULL) || (callback == NULL))
298     return (EINVAL);
299
300   for (ptr = def; ptr != NULL; ptr = ptr->next)
301   {
302     int status;
303
304     status = (*callback) (ptr, user_data);
305     if (status != 0)
306       return (status);
307   }
308
309   return (0);
310 } /* }}} int def_foreach */
311
312 int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
313     rrd_args_t *args)
314 {
315   char *file;
316   int index;
317   char draw_def[64];
318   char legend[256];
319
320   if ((def == NULL) || (ident == NULL) || (args == NULL))
321     return (EINVAL);
322
323   file = ident_to_file (ident);
324   if (file == NULL)
325   {
326     DEBUG ("gl_ident_get_rrdargs: ident_to_file returned NULL.\n");
327     return (-1);
328   }
329
330   DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
331
332   if (def->legend != NULL)
333   {
334     strncpy (legend, def->legend, sizeof (legend));
335     legend[sizeof (legend) - 1] = 0;
336   }
337   else
338   {
339     ident_describe (ident, def->select,
340         legend, sizeof (legend));
341
342     if ((legend[0] == 0) || (strcmp ("default", legend) == 0))
343     {
344       strncpy (legend, def->ds_name, sizeof (legend));
345       legend[sizeof (legend) - 1] = 0;
346     }
347   }
348
349   index = args->index;
350   args->index++;
351
352   /* CDEFs */
353   array_append_format (args->data, "DEF:def_%04i_min=%s:%s:MIN",
354       index, file, def->ds_name);
355   array_append_format (args->data, "DEF:def_%04i_avg=%s:%s:AVERAGE",
356       index, file, def->ds_name);
357   array_append_format (args->data, "DEF:def_%04i_max=%s:%s:MAX",
358       index, file, def->ds_name);
359   /* VDEFs */
360   array_append_format (args->data, "VDEF:vdef_%04i_min=def_%04i_min,MINIMUM",
361       index, index);
362   array_append_format (args->data, "VDEF:vdef_%04i_avg=def_%04i_avg,AVERAGE",
363       index, index);
364   array_append_format (args->data, "VDEF:vdef_%04i_max=def_%04i_max,MAXIMUM",
365       index, index);
366   array_append_format (args->data, "VDEF:vdef_%04i_lst=def_%04i_avg,LAST",
367       index, index);
368
369   if (def->stack)
370   {
371     if (args->last_stack_cdef[0] != 0)
372     {
373       array_append_format (args->calc, "CDEF:cdef_%04i_stack=%s,def_%04i_avg,+",
374           index, args->last_stack_cdef, index);
375       snprintf (draw_def, sizeof (draw_def), "cdef_%04i_stack", index);
376     }
377     else
378     {
379       snprintf (draw_def, sizeof (draw_def), "def_%04i_avg", index);
380     }
381   }
382   else
383   {
384     snprintf (draw_def, sizeof (draw_def), "def_%04i_avg", index);
385   }
386
387   if (def->area)
388     array_prepend_format (args->areas, "AREA:%s#%06"PRIx32,
389         draw_def, fade_color (def->color));
390
391   /* Graph part */
392   array_prepend_format (args->lines, "GPRINT:vdef_%04i_lst:%s last\\l",
393       index, (def->format != NULL) ? def->format : "%6.2lf");
394   array_prepend_format (args->lines, "GPRINT:vdef_%04i_max:%s max,",
395       index, (def->format != NULL) ? def->format : "%6.2lf");
396   array_prepend_format (args->lines, "GPRINT:vdef_%04i_avg:%s avg,",
397       index, (def->format != NULL) ? def->format : "%6.2lf");
398   array_prepend_format (args->lines, "GPRINT:vdef_%04i_min:%s min,",
399       index, (def->format != NULL) ? def->format : "%6.2lf");
400   array_prepend_format (args->lines, "LINE1:%s#%06"PRIx32":%s",
401       draw_def, def->color, legend);
402
403   free (file);
404
405   memcpy (args->last_stack_cdef, draw_def, sizeof (args->last_stack_cdef));
406
407   return (0);
408 } /* }}} int def_get_rrdargs */
409
410 /* vim: set sw=2 sts=2 et fdm=marker : */