graph_to_json: Export graph title.
[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_inst (graph_config_t *graph, graph_instance_t *inst) /* {{{ */
192 {
193   graph_instance_t **tmp;
194
195   if ((graph == NULL) || (inst == NULL))
196     return (EINVAL);
197
198   tmp = realloc (graph->instances,
199       sizeof (*graph->instances) * (graph->instances_num + 1));
200   if (tmp == NULL)
201     return (ENOMEM);
202   graph->instances = tmp;
203
204   graph->instances[graph->instances_num] = inst;
205   graph->instances_num++;
206
207   return (0);
208 } /* }}} int graph_add_inst */
209
210 int graph_add_file (graph_config_t *cfg, const graph_ident_t *file) /* {{{ */
211 {
212   graph_instance_t *inst;
213
214   inst = graph_inst_find_matching (cfg, file);
215   if (inst == NULL)
216   {
217     inst = inst_create (cfg, file);
218     if (inst == NULL)
219       return (ENOMEM);
220
221     graph_add_inst (cfg, inst);
222   }
223
224   return (inst_add_file (inst, file));
225 } /* }}} int graph_add_file */
226
227 int graph_get_title (graph_config_t *cfg, /* {{{ */
228     char *buffer, size_t buffer_size)
229 {
230   if ((cfg == NULL) || (buffer == NULL) || (buffer_size < 1))
231     return (EINVAL);
232
233   if (cfg->title == NULL)
234     cfg->title = ident_to_string (cfg->select);
235
236   if (cfg->title == NULL)
237     return (ENOMEM);
238
239   strncpy (buffer, cfg->title, buffer_size);
240   buffer[buffer_size - 1] = 0;
241
242   return (0);
243 } /* }}} int graph_get_title */
244
245 int graph_get_params (graph_config_t *cfg, /* {{{ */
246     char *buffer, size_t buffer_size)
247 {
248   buffer[0] = 0;
249
250 #define COPY_FIELD(field) do {                                       \
251   const char *str = ident_get_##field (cfg->select);                 \
252   char uri_str[1024];                                                \
253   uri_escape_copy (uri_str, str, sizeof (uri_str));                  \
254   strlcat (buffer, #field, buffer_size);                             \
255   strlcat (buffer, "=", buffer_size);                                \
256   strlcat (buffer, uri_str, buffer_size);                            \
257 } while (0)
258
259   COPY_FIELD(host);
260   strlcat (buffer, ";", buffer_size);
261   COPY_FIELD(plugin);
262   strlcat (buffer, ";", buffer_size);
263   COPY_FIELD(plugin_instance);
264   strlcat (buffer, ";", buffer_size);
265   COPY_FIELD(type);
266   strlcat (buffer, ";", buffer_size);
267   COPY_FIELD(type_instance);
268
269 #undef COPY_FIELD
270
271   return (0);
272 } /* }}} int graph_get_params */
273
274 graph_ident_t *graph_get_selector (graph_config_t *cfg) /* {{{ */
275 {
276   if (cfg == NULL)
277     return (NULL);
278
279   return (ident_clone (cfg->select));
280 } /* }}} graph_ident_t *graph_get_selector */
281
282 graph_def_t *graph_get_defs (graph_config_t *cfg) /* {{{ */
283 {
284   if (cfg == NULL)
285     return (NULL);
286
287   return (cfg->defs);
288 } /* }}} graph_def_t *graph_get_defs */
289
290 int graph_add_def (graph_config_t *cfg, graph_def_t *def) /* {{{ */
291 {
292   graph_def_t *tmp;
293
294   if ((cfg == NULL) || (def == NULL))
295     return (EINVAL);
296
297   if (cfg->defs == NULL)
298   {
299     cfg->defs = def;
300     return (0);
301   }
302
303   /* Insert in reverse order. This makes the order in the config file and the
304    * order of the DEFs in the graph more natural. Really. */
305   tmp = cfg->defs;
306   cfg->defs = def;
307   return (def_append (cfg->defs, tmp));
308 } /* }}} int graph_add_def */
309
310 _Bool graph_ident_matches (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */
311 {
312 #if C4_DEBUG
313   if ((cfg == NULL) || (ident == NULL))
314     return (0);
315 #endif
316
317   return (ident_matches (cfg->select, ident));
318 } /* }}} _Bool graph_ident_matches */
319
320 _Bool graph_matches_ident (graph_config_t *cfg, /* {{{ */
321     const graph_ident_t *selector)
322 {
323 #if C4_DEBUG
324   if ((cfg == NULL) || (selector == NULL))
325     return (0);
326 #endif
327
328   return (ident_matches (selector, cfg->select));
329 } /* }}} _Bool graph_matches_ident */
330
331 _Bool graph_ident_intersect (graph_config_t *cfg, /* {{{ */
332     const graph_ident_t *selector)
333 {
334 #if C4_DEBUG
335   if ((cfg == NULL) || (selector == NULL))
336     return (0);
337 #endif
338
339   return (ident_intersect (cfg->select, selector));
340 } /* }}} _Bool graph_ident_intersect */
341
342 _Bool graph_matches_field (graph_config_t *cfg, /* {{{ */
343     graph_ident_field_t field, const char *field_value)
344 {
345   const char *selector_value;
346
347   if ((cfg == NULL) || (field_value == NULL))
348     return (0);
349
350   selector_value = ident_get_field (cfg->select, field);
351   if (selector_value == NULL)
352     return (0);
353
354   if (IS_ALL (selector_value) || IS_ANY (selector_value))
355     return (1);
356   else if (strcasecmp (selector_value, field_value) == 0)
357     return (1);
358
359   return (0);
360 } /* }}} _Bool graph_matches_field */
361
362 int graph_inst_foreach (graph_config_t *cfg, /* {{{ */
363                 inst_callback_t cb, void *user_data)
364 {
365   size_t i;
366   int status;
367
368   for (i = 0; i < cfg->instances_num; i++)
369   {
370     status = (*cb) (cfg->instances[i], user_data);
371     if (status != 0)
372       return (status);
373   }
374
375   return (0);
376 } /* }}} int graph_inst_foreach */
377
378 graph_instance_t *graph_inst_find_exact (graph_config_t *cfg, /* {{{ */
379     graph_ident_t *ident)
380 {
381   size_t i;
382
383   if ((cfg == NULL) || (ident == NULL))
384     return (NULL);
385
386   for (i = 0; i < cfg->instances_num; i++)
387     if (inst_compare_ident (cfg->instances[i], ident) == 0)
388       return (cfg->instances[i]);
389
390   return (NULL);
391 } /* }}} graph_instance_t *graph_inst_find_exact */
392
393 graph_instance_t *graph_inst_find_matching (graph_config_t *cfg, /* {{{ */
394     const graph_ident_t *ident)
395 {
396   size_t i;
397
398   if ((cfg == NULL) || (ident == NULL))
399     return (NULL);
400
401   for (i = 0; i < cfg->instances_num; i++)
402     if (inst_ident_matches (cfg->instances[i], ident))
403       return (cfg->instances[i]);
404
405   return (NULL);
406 } /* }}} graph_instance_t *graph_inst_find_matching */
407
408 int graph_inst_find_all_matching (graph_config_t *cfg, /* {{{ */
409     const graph_ident_t *ident,
410     graph_inst_callback_t callback, void *user_data)
411 {
412   size_t i;
413
414   if ((cfg == NULL) || (ident == NULL) || (callback == NULL))
415     return (EINVAL);
416
417   for (i = 0; i < cfg->instances_num; i++)
418   {
419     int status;
420
421     if (!inst_matches_ident (cfg->instances[i], ident))
422       continue;
423
424     status = (*callback) (cfg, cfg->instances[i], user_data);
425     if (status != 0)
426       return (status);
427   }
428
429   return (0);
430 } /* }}} int graph_inst_find_all_matching */
431
432 /* Lightweight variant of the "graph_search_inst" which is used if the
433  * search_info_t doesn't select any field values explicitely. */
434 static int graph_search_inst_noselector (graph_config_t *cfg, /* {{{ */
435     search_info_t *si, graph_inst_callback_t cb, void *user_data)
436 {
437   char title[1024];
438   int status;
439   size_t i;
440
441   /* parameters have already been checked in "graph_search_inst" */
442
443   status = graph_get_title (cfg, title, sizeof (title));
444   if (status != 0)
445   {
446     fprintf (stderr, "graph_search_inst_noselector: "
447         "graph_get_title failed\n");
448     return (status);
449   }
450   strtolower (title);
451
452   for (i = 0; i < cfg->instances_num; i++)
453   {
454     if (search_graph_inst_matches (si, cfg, cfg->instances[i], title))
455     {
456       status = (*cb) (cfg, cfg->instances[i], user_data);
457       if (status != 0)
458         return (status);
459     }
460   } /* for (cfg->instances_num) */
461
462   return (0);
463 } /* }}} int graph_search_inst_noselector */
464
465 /* When this function is called from graph_list, it will already have checked
466  * that the selector of the graph does not contradict the field selections contained in
467  * the search_info_t. We still have to check if the instance contradicts the
468  * search parameters, though, since the "ANY" wildcard is filled in now -
469  * possibly with contradicting values. */
470 int graph_search_inst (graph_config_t *cfg, search_info_t *si, /* {{{ */
471     graph_inst_callback_t cb,
472     void *user_data)
473 {
474   char title[1024];
475   int status;
476   size_t i;
477   graph_ident_t *search_selector;
478
479   if ((cfg == NULL) || (si == NULL) || (cb == NULL))
480     return (EINVAL);
481
482   if (!search_has_selector (si))
483     return (graph_search_inst_noselector (cfg, si, cb, user_data));
484
485   search_selector = search_to_ident (si);
486   if (search_selector == NULL)
487     return (ENOMEM);
488
489   status = graph_get_title (cfg, title, sizeof (title));
490   if (status != 0)
491   {
492     ident_destroy (search_selector);
493     fprintf (stderr, "graph_search_inst: graph_get_title failed\n");
494     return (status);
495   }
496   strtolower (title);
497
498   for (i = 0; i < cfg->instances_num; i++)
499   {
500     graph_ident_t *inst_selector;
501
502     inst_selector = inst_get_selector (cfg->instances[i]);
503     if (inst_selector == NULL)
504       continue;
505
506     /* If the two selectors contradict one another, there is no point in
507      * calling the (more costly) "search_graph_inst_matches" function. */
508     if (!ident_intersect (search_selector, inst_selector))
509     {
510       ident_destroy (inst_selector);
511       continue;
512     }
513
514     if (search_graph_inst_matches (si, cfg, cfg->instances[i], title))
515     {
516       status = (*cb) (cfg, cfg->instances[i], user_data);
517       if (status != 0)
518       {
519         ident_destroy (search_selector);
520         ident_destroy (inst_selector);
521         return (status);
522       }
523     }
524
525     ident_destroy (inst_selector);
526   } /* for (cfg->instances_num) */
527
528   ident_destroy (search_selector);
529   return (0);
530 } /* }}} int graph_search_inst */
531
532 int graph_search_inst_string (graph_config_t *cfg, const char *term, /* {{{ */
533     graph_inst_callback_t cb,
534     void *user_data)
535 {
536   char buffer[1024];
537   int status;
538   size_t i;
539
540   status = graph_get_title (cfg, buffer, sizeof (buffer));
541   if (status != 0)
542   {
543     fprintf (stderr, "graph_search_inst_string: graph_get_title failed\n");
544     return (status);
545   }
546
547   strtolower (buffer);
548
549   if (strstr (buffer, term) != NULL)
550   {
551     for (i = 0; i < cfg->instances_num; i++)
552     {
553       status = (*cb) (cfg, cfg->instances[i], user_data);
554       if (status != 0)
555         return (status);
556     }
557   }
558   else
559   {
560     for (i = 0; i < cfg->instances_num; i++)
561     {
562       if (inst_matches_string (cfg, cfg->instances[i], term))
563       {
564         status = (*cb) (cfg, cfg->instances[i], user_data);
565         if (status != 0)
566           return (status);
567       }
568     }
569   }
570
571   return (0);
572 } /* }}} int graph_search_inst_string */
573
574 int graph_inst_search_field (graph_config_t *cfg, /* {{{ */
575     graph_ident_field_t field, const char *field_value,
576     graph_inst_callback_t callback, void *user_data)
577 {
578   size_t i;
579   const char *selector_field;
580   _Bool need_check_instances = 0;
581
582   if ((cfg == NULL) || (field_value == NULL) || (callback == NULL))
583     return (EINVAL);
584
585   if (!graph_matches_field (cfg, field, field_value))
586     return (0);
587
588   selector_field = ident_get_field (cfg->select, field);
589   if (selector_field == NULL)
590     return (-1);
591
592   if (IS_ALL (selector_field) || IS_ANY (selector_field))
593     need_check_instances = 1;
594
595   for (i = 0; i < cfg->instances_num; i++)
596   {
597     int status;
598
599     if (need_check_instances
600         && !inst_matches_field (cfg->instances[i], field, field_value))
601       continue;
602
603     status = (*callback) (cfg, cfg->instances[i], user_data);
604     if (status != 0)
605       return (status);
606   }
607
608   return (0);
609 } /* }}} int graph_inst_search_field */
610
611 int graph_compare (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */
612 {
613   if ((cfg == NULL) || (ident == NULL))
614     return (0);
615
616   return (ident_compare (cfg->select, ident));
617 } /* }}} int graph_compare */
618
619 size_t graph_num_instances (graph_config_t *cfg) /* {{{ */
620 {
621   if (cfg == NULL)
622     return ((size_t) -1);
623
624   return (cfg->instances_num);
625 } /* }}} size_t graph_num_instances */
626
627 int graph_to_json (const graph_config_t *cfg, /* {{{ */
628     yajl_gen handler)
629 {
630   size_t i;
631
632   if ((cfg == NULL) || (handler == NULL))
633     return (EINVAL);
634
635   yajl_gen_map_open (handler);
636
637   yajl_gen_string (handler,
638       (unsigned char *) "title",
639       (unsigned int) strlen ("title"));
640   yajl_gen_string (handler,
641       (unsigned char *) cfg->title,
642       (unsigned int) strlen (cfg->title));
643
644   yajl_gen_string (handler,
645       (unsigned char *) "select",
646       (unsigned int) strlen ("select"));
647   ident_to_json (cfg->select, handler);
648
649   yajl_gen_string (handler,
650       (unsigned char *) "instances",
651       (unsigned int) strlen ("instances"));
652   yajl_gen_array_open (handler);
653   for (i = 0; i < cfg->instances_num; i++)
654     inst_to_json (cfg->instances[i], handler);
655   yajl_gen_array_close (handler);
656
657   yajl_gen_map_close (handler);
658
659   return (0);
660 } /* }}} int graph_to_json */
661
662 int graph_def_to_json (graph_config_t *cfg, /* {{{ */
663     graph_instance_t *inst,
664     yajl_gen handler)
665 {
666 #define yajl_gen_string_cast(h,p,l) \
667   yajl_gen_string (h, (unsigned char *) p, (unsigned int) l)
668
669   if ((cfg == NULL) || (handler == NULL))
670     return (EINVAL);
671
672   yajl_gen_map_open (handler);
673
674   yajl_gen_string_cast (handler, "select", strlen ("select"));
675   ident_to_json (cfg->select, handler);
676   if (cfg->title != NULL)
677   {
678     yajl_gen_string_cast (handler, "title", strlen ("title"));
679     yajl_gen_string_cast (handler, cfg->title, strlen (cfg->title));
680   }
681   if (cfg->vertical_label != NULL)
682   {
683     yajl_gen_string_cast (handler, "vertical_label", strlen ("vertical_label"));
684     yajl_gen_string_cast (handler, cfg->vertical_label, strlen (cfg->vertical_label));
685   }
686   yajl_gen_string_cast (handler, "show_zero", strlen ("show_zero"));
687   yajl_gen_bool (handler, cfg->show_zero);
688
689   yajl_gen_string_cast (handler, "defs", strlen ("defs"));
690   if (cfg->defs == NULL)
691   {
692     graph_def_t *defs;
693
694     defs = inst_get_default_defs (cfg, inst);
695     def_to_json (defs, handler);
696     def_destroy (defs);
697   }
698   else
699   {
700     def_to_json (cfg->defs, handler);
701   }
702
703   yajl_gen_map_close (handler);
704
705   return (0);
706 #undef yajl_gen_string_cast
707 } /* }}} int graph_def_to_json */
708
709 static int graph_sort_instances_cb (const void *v0, const void *v1) /* {{{ */
710 {
711   return (inst_compare (*(graph_instance_t * const *) v0,
712         *(graph_instance_t * const *) v1));
713 } /* }}} int graph_sort_instances_cb */
714
715 int graph_sort_instances (graph_config_t *cfg) /* {{{ */
716 {
717   if (cfg == NULL)
718     return (EINVAL);
719
720   if (cfg->instances_num < 2)
721     return (0);
722
723   qsort (cfg->instances, cfg->instances_num, sizeof (*cfg->instances),
724       graph_sort_instances_cb);
725
726   return (0);
727 } /* }}} int graph_sort_instances */
728
729 int graph_clear_instances (graph_config_t *cfg) /* {{{ */
730 {
731   size_t i;
732
733   if (cfg == NULL)
734     return (EINVAL);
735
736   for (i = 0; i < cfg->instances_num; i++)
737     inst_destroy (cfg->instances[i]);
738   free (cfg->instances);
739   cfg->instances = NULL;
740   cfg->instances_num = 0;
741
742   return (0);
743 } /* }}} int graph_clear_instances */
744
745 int graph_get_rrdargs (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
746     rrd_args_t *args)
747 {
748   if ((cfg == NULL) || (inst == NULL) || (args == NULL))
749     return (EINVAL);
750
751   if (cfg->title != NULL)
752   {
753     array_append (args->options, "-t");
754     array_append (args->options, cfg->title);
755   }
756
757   if (cfg->vertical_label != NULL)
758   {
759     array_append (args->options, "-v");
760     array_append (args->options, cfg->vertical_label);
761   }
762
763   if (cfg->show_zero)
764   {
765     array_append (args->options, "-l");
766     array_append (args->options, "0");
767   }
768
769   return (0);
770 } /* }}} int graph_get_rrdargs */
771
772 /* vim: set sw=2 sts=2 et fdm=marker : */