src/graph_def.c: Use "ident_describe" to generate a legend entry …
[collection4.git] / src / graph.c
1 /**
2  * collection4 - graph.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 <stdio.h>
26 #include <stdint.h>
27 #include <inttypes.h>
28 #include <string.h>
29 #include <time.h>
30 #include <errno.h>
31 #include <assert.h>
32
33 #include "graph.h"
34 #include "graph_ident.h"
35 #include "graph_instance.h"
36 #include "graph_list.h"
37 #include "graph_def.h"
38 #include "graph_config.h"
39 #include "common.h"
40 #include "filesystem.h"
41 #include "utils_cgi.h"
42
43 #include <fcgiapp.h>
44 #include <fcgi_stdio.h>
45
46 /*
47  * Data types
48  */
49 struct graph_config_s /* {{{ */
50 {
51   graph_ident_t *select;
52
53   char *title;
54   char *vertical_label;
55   _Bool show_zero;
56
57   graph_def_t *defs;
58
59   graph_instance_t **instances;
60   size_t instances_num;
61 }; /* }}} struct graph_config_s */
62
63 /*
64  * Private functions
65  */
66
67 /*
68  * Config functions
69  */
70 static graph_ident_t *graph_config_get_selector (const oconfig_item_t *ci) /* {{{ */
71 {
72   char *host = NULL;
73   char *plugin = NULL;
74   char *plugin_instance = NULL;
75   char *type = NULL;
76   char *type_instance = NULL;
77   graph_ident_t *ret;
78   int i;
79
80   for (i = 0; i < ci->children_num; i++)
81   {
82     oconfig_item_t *child;
83
84     child = ci->children + i;
85
86     if (strcasecmp ("Host", child->key) == 0)
87       graph_config_get_string (child, &host);
88     else if (strcasecmp ("Plugin", child->key) == 0)
89       graph_config_get_string (child, &plugin);
90     else if (strcasecmp ("PluginInstance", child->key) == 0)
91       graph_config_get_string (child, &plugin_instance);
92     else if (strcasecmp ("Type", child->key) == 0)
93       graph_config_get_string (child, &type);
94     else if (strcasecmp ("TypeInstance", child->key) == 0)
95       graph_config_get_string (child, &type_instance);
96     /* else: ignore all other directives here. */
97   } /* for */
98
99   ret = ident_create (host, plugin, plugin_instance, type, type_instance);
100
101   free (host);
102   free (plugin);
103   free (plugin_instance);
104   free (type);
105   free (type_instance);
106
107   return (ret);
108 } /* }}} int graph_config_get_selector */
109
110 /*
111  * Global functions
112  */
113 graph_config_t *graph_create (const graph_ident_t *selector) /* {{{ */
114 {
115   graph_config_t *cfg;
116
117   cfg = malloc (sizeof (*cfg));
118   if (cfg == NULL)
119     return (NULL);
120   memset (cfg, 0, sizeof (*cfg));
121
122   if (selector != NULL)
123     cfg->select = ident_clone (selector);
124   else
125     cfg->select = NULL;
126
127   cfg->title = NULL;
128   cfg->vertical_label = NULL;
129   cfg->defs = NULL;
130   cfg->instances = NULL;
131
132   return (cfg);
133 } /* }}} int graph_create */
134
135 void graph_destroy (graph_config_t *cfg) /* {{{ */
136 {
137   size_t i;
138
139   if (cfg == NULL)
140     return;
141
142   ident_destroy (cfg->select);
143
144   free (cfg->title);
145   free (cfg->vertical_label);
146
147   def_destroy (cfg->defs);
148
149   for (i = 0; i < cfg->instances_num; i++)
150     inst_destroy (cfg->instances[i]);
151   free (cfg->instances);
152 } /* }}} void graph_destroy */
153
154 int graph_config_add (const oconfig_item_t *ci) /* {{{ */
155 {
156   graph_ident_t *select;
157   graph_config_t *cfg = NULL;
158   int i;
159
160   select = graph_config_get_selector (ci);
161   if (select == NULL)
162     return (EINVAL);
163
164   cfg = graph_create (/* selector = */ NULL);
165   if (cfg == NULL)
166     return (ENOMEM);
167
168   cfg->select = select;
169
170   for (i = 0; i < ci->children_num; i++)
171   {
172     oconfig_item_t *child;
173
174     child = ci->children + i;
175
176     if (strcasecmp ("Title", child->key) == 0)
177       graph_config_get_string (child, &cfg->title);
178     else if (strcasecmp ("VerticalLabel", child->key) == 0)
179       graph_config_get_string (child, &cfg->vertical_label);
180     else if (strcasecmp ("ShowZero", child->key) == 0)
181       graph_config_get_bool (child, &cfg->show_zero);
182     else if (strcasecmp ("DEF", child->key) == 0)
183       def_config (cfg, child);
184   } /* for */
185
186   gl_add_graph (cfg);
187
188   return (0);
189 } /* }}} graph_config_add */
190
191 int graph_add_file (graph_config_t *cfg, const graph_ident_t *file) /* {{{ */
192 {
193   graph_instance_t *inst;
194
195   inst = graph_inst_find_matching (cfg, file);
196   if (inst == NULL)
197   {
198     graph_instance_t **tmp;
199
200     tmp = realloc (cfg->instances,
201         sizeof (*cfg->instances) * (cfg->instances_num + 1));
202     if (tmp == NULL)
203       return (ENOMEM);
204     cfg->instances = tmp;
205
206     inst = inst_create (cfg, file);
207     if (inst == NULL)
208       return (ENOMEM);
209
210     cfg->instances[cfg->instances_num] = inst;
211     cfg->instances_num++;
212   }
213
214   return (inst_add_file (inst, file));
215 } /* }}} int graph_add_file */
216
217 int graph_get_title (graph_config_t *cfg, /* {{{ */
218     char *buffer, size_t buffer_size)
219 {
220   if ((cfg == NULL) || (buffer == NULL) || (buffer_size < 1))
221     return (EINVAL);
222
223   if (cfg->title == NULL)
224     cfg->title = ident_to_string (cfg->select);
225
226   if (cfg->title == NULL)
227     return (ENOMEM);
228
229   strncpy (buffer, cfg->title, buffer_size);
230   buffer[buffer_size - 1] = 0;
231
232   return (0);
233 } /* }}} int graph_get_title */
234
235 int graph_get_params (graph_config_t *cfg, /* {{{ */
236     char *buffer, size_t buffer_size)
237 {
238   buffer[0] = 0;
239
240 #define COPY_FIELD(field) do {                                       \
241   const char *str = ident_get_##field (cfg->select);                 \
242   char uri_str[1024];                                                \
243   uri_escape_copy (uri_str, str, sizeof (uri_str));                  \
244   strlcat (buffer, #field, buffer_size);                             \
245   strlcat (buffer, "=", buffer_size);                                \
246   strlcat (buffer, uri_str, buffer_size);                            \
247 } while (0)
248
249   COPY_FIELD(host);
250   strlcat (buffer, ";", buffer_size);
251   COPY_FIELD(plugin);
252   strlcat (buffer, ";", buffer_size);
253   COPY_FIELD(plugin_instance);
254   strlcat (buffer, ";", buffer_size);
255   COPY_FIELD(type);
256   strlcat (buffer, ";", buffer_size);
257   COPY_FIELD(type_instance);
258
259 #undef COPY_FIELD
260
261   return (0);
262 } /* }}} int graph_get_params */
263
264 graph_ident_t *graph_get_selector (graph_config_t *cfg) /* {{{ */
265 {
266   if (cfg == NULL)
267     return (NULL);
268
269   return (ident_clone (cfg->select));
270 } /* }}} graph_ident_t *graph_get_selector */
271
272 graph_def_t *graph_get_defs (graph_config_t *cfg) /* {{{ */
273 {
274   if (cfg == NULL)
275     return (NULL);
276
277   return (cfg->defs);
278 } /* }}} graph_def_t *graph_get_defs */
279
280 int graph_add_def (graph_config_t *cfg, graph_def_t *def) /* {{{ */
281 {
282   graph_def_t *tmp;
283
284   if ((cfg == NULL) || (def == NULL))
285     return (EINVAL);
286
287   if (cfg->defs == NULL)
288   {
289     cfg->defs = def;
290     return (0);
291   }
292
293   /* Insert in reverse order. This makes the order in the config file and the
294    * order of the DEFs in the graph more natural. Really. */
295   tmp = cfg->defs;
296   cfg->defs = def;
297   return (def_append (cfg->defs, tmp));
298 } /* }}} int graph_add_def */
299
300 _Bool graph_ident_matches (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */
301 {
302 #if C4_DEBUG
303   if ((cfg == NULL) || (ident == NULL))
304     return (0);
305 #endif
306
307   return (ident_matches (cfg->select, ident));
308 } /* }}} _Bool graph_ident_matches */
309
310 _Bool graph_matches_ident (graph_config_t *cfg, /* {{{ */
311     const graph_ident_t *selector)
312 {
313 #if C4_DEBUG
314   if ((cfg == NULL) || (selector == NULL))
315     return (0);
316 #endif
317
318   return (ident_matches (selector, cfg->select));
319 } /* }}} _Bool graph_matches_ident */
320
321 _Bool graph_ident_intersect (graph_config_t *cfg, /* {{{ */
322     const graph_ident_t *selector)
323 {
324 #if C4_DEBUG
325   if ((cfg == NULL) || (selector == NULL))
326     return (0);
327 #endif
328
329   return (ident_intersect (cfg->select, selector));
330 } /* }}} _Bool graph_ident_intersect */
331
332 _Bool graph_matches_field (graph_config_t *cfg, /* {{{ */
333     graph_ident_field_t field, const char *field_value)
334 {
335   const char *selector_value;
336
337   if ((cfg == NULL) || (field_value == NULL))
338     return (0);
339
340   selector_value = ident_get_field (cfg->select, field);
341   if (selector_value == NULL)
342     return (0);
343
344   if (IS_ALL (selector_value) || IS_ANY (selector_value))
345     return (1);
346   else if (strcasecmp (selector_value, field_value) == 0)
347     return (1);
348
349   return (0);
350 } /* }}} _Bool graph_matches_field */
351
352 int graph_inst_foreach (graph_config_t *cfg, /* {{{ */
353                 inst_callback_t cb, void *user_data)
354 {
355   size_t i;
356   int status;
357
358   for (i = 0; i < cfg->instances_num; i++)
359   {
360     status = (*cb) (cfg->instances[i], user_data);
361     if (status != 0)
362       return (status);
363   }
364
365   return (0);
366 } /* }}} int graph_inst_foreach */
367
368 graph_instance_t *graph_inst_find_exact (graph_config_t *cfg, /* {{{ */
369     graph_ident_t *ident)
370 {
371   size_t i;
372
373   if ((cfg == NULL) || (ident == NULL))
374     return (NULL);
375
376   for (i = 0; i < cfg->instances_num; i++)
377     if (inst_compare_ident (cfg->instances[i], ident) == 0)
378       return (cfg->instances[i]);
379
380   return (NULL);
381 } /* }}} graph_instance_t *graph_inst_find_exact */
382
383 graph_instance_t *graph_inst_find_matching (graph_config_t *cfg, /* {{{ */
384     const graph_ident_t *ident)
385 {
386   size_t i;
387
388   if ((cfg == NULL) || (ident == NULL))
389     return (NULL);
390
391   for (i = 0; i < cfg->instances_num; i++)
392     if (inst_ident_matches (cfg->instances[i], ident))
393       return (cfg->instances[i]);
394
395   return (NULL);
396 } /* }}} graph_instance_t *graph_inst_find_matching */
397
398 int graph_inst_find_all_matching (graph_config_t *cfg, /* {{{ */
399     const graph_ident_t *ident,
400     graph_inst_callback_t callback, void *user_data)
401 {
402   size_t i;
403
404   if ((cfg == NULL) || (ident == NULL) || (callback == NULL))
405     return (EINVAL);
406
407   for (i = 0; i < cfg->instances_num; i++)
408   {
409     int status;
410
411     if (!inst_matches_ident (cfg->instances[i], ident))
412       continue;
413
414     status = (*callback) (cfg, cfg->instances[i], user_data);
415     if (status != 0)
416       return (status);
417   }
418
419   return (0);
420 } /* }}} int graph_inst_find_all_matching */
421
422 /* Lightweight variant of the "graph_search_inst" which is used if the
423  * search_info_t doesn't select any field values explicitely. */
424 static int graph_search_inst_noselector (graph_config_t *cfg, /* {{{ */
425     search_info_t *si, graph_inst_callback_t cb, void *user_data)
426 {
427   char title[1024];
428   int status;
429   size_t i;
430
431   /* parameters have already been checked in "graph_search_inst" */
432
433   status = graph_get_title (cfg, title, sizeof (title));
434   if (status != 0)
435   {
436     fprintf (stderr, "graph_search_inst_noselector: "
437         "graph_get_title failed\n");
438     return (status);
439   }
440   strtolower (title);
441
442   for (i = 0; i < cfg->instances_num; i++)
443   {
444     if (search_graph_inst_matches (si, cfg, cfg->instances[i], title))
445     {
446       status = (*cb) (cfg, cfg->instances[i], user_data);
447       if (status != 0)
448         return (status);
449     }
450   } /* for (cfg->instances_num) */
451
452   return (0);
453 } /* }}} int graph_search_inst_noselector */
454
455 /* When this function is called from graph_list, it will already have checked
456  * that the selector of the graph does not contradict the field selections contained in
457  * the search_info_t. We still have to check if the instance contradicts the
458  * search parameters, though, since the "ANY" wildcard is filled in now -
459  * possibly with contradicting values. */
460 int graph_search_inst (graph_config_t *cfg, search_info_t *si, /* {{{ */
461     graph_inst_callback_t cb,
462     void *user_data)
463 {
464   char title[1024];
465   int status;
466   size_t i;
467   graph_ident_t *search_selector;
468
469   if ((cfg == NULL) || (si == NULL) || (cb == NULL))
470     return (EINVAL);
471
472   if (!search_has_selector (si))
473     return (graph_search_inst_noselector (cfg, si, cb, user_data));
474
475   search_selector = search_to_ident (si);
476   if (search_selector == NULL)
477     return (ENOMEM);
478
479   status = graph_get_title (cfg, title, sizeof (title));
480   if (status != 0)
481   {
482     ident_destroy (search_selector);
483     fprintf (stderr, "graph_search_inst: graph_get_title failed\n");
484     return (status);
485   }
486   strtolower (title);
487
488   for (i = 0; i < cfg->instances_num; i++)
489   {
490     graph_ident_t *inst_selector;
491
492     inst_selector = inst_get_selector (cfg->instances[i]);
493     if (inst_selector == NULL)
494       continue;
495
496     /* If the two selectors contradict one another, there is no point in
497      * calling the (more costly) "search_graph_inst_matches" function. */
498     if (!ident_intersect (search_selector, inst_selector))
499     {
500       ident_destroy (inst_selector);
501       continue;
502     }
503
504     if (search_graph_inst_matches (si, cfg, cfg->instances[i], title))
505     {
506       status = (*cb) (cfg, cfg->instances[i], user_data);
507       if (status != 0)
508       {
509         ident_destroy (search_selector);
510         ident_destroy (inst_selector);
511         return (status);
512       }
513     }
514
515     ident_destroy (inst_selector);
516   } /* for (cfg->instances_num) */
517
518   ident_destroy (search_selector);
519   return (0);
520 } /* }}} int graph_search_inst */
521
522 int graph_search_inst_string (graph_config_t *cfg, const char *term, /* {{{ */
523     graph_inst_callback_t cb,
524     void *user_data)
525 {
526   char buffer[1024];
527   int status;
528   size_t i;
529
530   status = graph_get_title (cfg, buffer, sizeof (buffer));
531   if (status != 0)
532   {
533     fprintf (stderr, "graph_search_inst_string: graph_get_title failed\n");
534     return (status);
535   }
536
537   strtolower (buffer);
538
539   if (strstr (buffer, term) != NULL)
540   {
541     for (i = 0; i < cfg->instances_num; i++)
542     {
543       status = (*cb) (cfg, cfg->instances[i], user_data);
544       if (status != 0)
545         return (status);
546     }
547   }
548   else
549   {
550     for (i = 0; i < cfg->instances_num; i++)
551     {
552       if (inst_matches_string (cfg, cfg->instances[i], term))
553       {
554         status = (*cb) (cfg, cfg->instances[i], user_data);
555         if (status != 0)
556           return (status);
557       }
558     }
559   }
560
561   return (0);
562 } /* }}} int graph_search_inst_string */
563
564 int graph_inst_search_field (graph_config_t *cfg, /* {{{ */
565     graph_ident_field_t field, const char *field_value,
566     graph_inst_callback_t callback, void *user_data)
567 {
568   size_t i;
569   const char *selector_field;
570   _Bool need_check_instances = 0;
571
572   if ((cfg == NULL) || (field_value == NULL) || (callback == NULL))
573     return (EINVAL);
574
575   if (!graph_matches_field (cfg, field, field_value))
576     return (0);
577
578   selector_field = ident_get_field (cfg->select, field);
579   if (selector_field == NULL)
580     return (-1);
581
582   if (IS_ALL (selector_field) || IS_ANY (selector_field))
583     need_check_instances = 1;
584
585   for (i = 0; i < cfg->instances_num; i++)
586   {
587     int status;
588
589     if (need_check_instances
590         && !inst_matches_field (cfg->instances[i], field, field_value))
591       continue;
592
593     status = (*callback) (cfg, cfg->instances[i], user_data);
594     if (status != 0)
595       return (status);
596   }
597
598   return (0);
599 } /* }}} int graph_inst_search_field */
600
601 int graph_compare (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */
602 {
603   if ((cfg == NULL) || (ident == NULL))
604     return (0);
605
606   return (ident_compare (cfg->select, ident));
607 } /* }}} int graph_compare */
608
609 static int graph_sort_instances_cb (const void *v0, const void *v1) /* {{{ */
610 {
611   return (inst_compare (*(graph_instance_t * const *) v0,
612         *(graph_instance_t * const *) v1));
613 } /* }}} int graph_sort_instances_cb */
614
615 size_t graph_num_instances (graph_config_t *cfg) /* {{{ */
616 {
617   if (cfg == NULL)
618     return ((size_t) -1);
619
620   return (cfg->instances_num);
621 } /* }}} size_t graph_num_instances */
622
623 int graph_sort_instances (graph_config_t *cfg) /* {{{ */
624 {
625   if (cfg == NULL)
626     return (EINVAL);
627
628   if (cfg->instances_num < 2)
629     return (0);
630
631   qsort (cfg->instances, cfg->instances_num, sizeof (*cfg->instances),
632       graph_sort_instances_cb);
633
634   return (0);
635 } /* }}} int graph_sort_instances */
636
637 int graph_clear_instances (graph_config_t *cfg) /* {{{ */
638 {
639   size_t i;
640
641   if (cfg == NULL)
642     return (EINVAL);
643
644   for (i = 0; i < cfg->instances_num; i++)
645     inst_destroy (cfg->instances[i]);
646   free (cfg->instances);
647   cfg->instances = NULL;
648   cfg->instances_num = 0;
649
650   return (0);
651 } /* }}} int graph_clear_instances */
652
653 int graph_get_rrdargs (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
654     rrd_args_t *args)
655 {
656   if ((cfg == NULL) || (inst == NULL) || (args == NULL))
657     return (EINVAL);
658
659   if (cfg->title != NULL)
660   {
661     array_append (args->options, "-t");
662     array_append (args->options, cfg->title);
663   }
664
665   if (cfg->vertical_label != NULL)
666   {
667     array_append (args->options, "-v");
668     array_append (args->options, cfg->vertical_label);
669   }
670
671   if (cfg->show_zero)
672   {
673     array_append (args->options, "-l");
674     array_append (args->options, "0");
675   }
676
677   return (0);
678 } /* }}} int graph_get_rrdargs */
679
680 /* vim: set sw=2 sts=2 et fdm=marker : */