"instance_data_json" action: Implement the "resolution" parameter.
[collection4.git] / src / graph_instance.c
1 /**
2  * collection4 - graph_instance.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 #include <time.h>
28 #include <assert.h>
29
30 #include "graph_instance.h"
31 #include "graph.h"
32 #include "graph_def.h"
33 #include "graph_ident.h"
34 #include "graph_list.h"
35 #include "common.h"
36 #include "utils_cgi.h"
37
38 #include <fcgiapp.h>
39 #include <fcgi_stdio.h>
40
41 struct graph_instance_s /* {{{ */
42 {
43   graph_ident_t *select;
44
45   graph_ident_t **files;
46   size_t files_num;
47 }; /* }}} struct graph_instance_s */
48
49 struct def_callback_data_s
50 {
51   graph_instance_t *inst;
52   rrd_args_t *args;
53 };
54 typedef struct def_callback_data_s def_callback_data_t;
55
56 /*
57  * Private functions
58  */
59 struct ident_get_default_defs__data_s
60 {
61   char **dses;
62   size_t dses_num;
63 };
64 typedef struct ident_get_default_defs__data_s ident_get_default_defs__data_t;
65
66 static int ident_get_default_defs__callback (__attribute__((unused))
67     graph_ident_t *ident,
68     const char *ds_name, void *user_data)
69 {
70   ident_get_default_defs__data_t *data = user_data;
71   char **tmp;
72
73   tmp = realloc (data->dses, (data->dses_num + 1) * sizeof (data->dses));
74   if (tmp == NULL)
75     return (ENOMEM);
76   data->dses = tmp;
77
78   data->dses[data->dses_num] = strdup (ds_name);
79   if (data->dses[data->dses_num] == NULL)
80     return (ENOMEM);
81
82   data->dses_num++;
83   return (0);
84 } /* }}} int ident_get_default_defs__callback */
85
86 /* Create one DEF for each data source in the file. Called by
87  * "inst_get_default_defs" for each file. */
88 static graph_def_t *ident_get_default_defs (graph_config_t *cfg, /* {{{ */
89     graph_ident_t *ident, graph_def_t *def_head)
90 {
91   graph_def_t *defs = NULL;
92   char *file;
93   ident_get_default_defs__data_t ds_data;
94   int status;
95   size_t i;
96
97   ds_data.dses = NULL;
98   ds_data.dses_num = 0;
99
100   if ((cfg == NULL) || (ident == NULL))
101     return (def_head);
102
103   file = ident_to_file (ident);
104   if (file == NULL)
105   {
106     fprintf (stderr, "ident_get_default_defs: ident_to_file failed\n");
107     return (def_head);
108   }
109
110   status = data_provider_get_ident_ds_names (ident,
111       ident_get_default_defs__callback, &ds_data);
112   if (status != 0)
113   {
114     free (file);
115     return (def_head);
116   }
117
118   for (i = 0; i < ds_data.dses_num; i++)
119   {
120     graph_def_t *def;
121
122     def = def_search (def_head, ident, ds_data.dses[i]);
123     if (def != NULL)
124       continue;
125
126     def = def_create (cfg, ident, ds_data.dses[i]);
127     if (def == NULL)
128       continue;
129
130     if (defs == NULL)
131       defs = def;
132     else
133       def_append (defs, def);
134
135     free (ds_data.dses[i]);
136   }
137
138   free (ds_data.dses);
139   free (file);
140
141   return (defs);
142 } /* }}} int ident_get_default_defs */
143
144 /* Called with each DEF in turn. Calls "def_get_rrdargs" with every appropriate
145  * file / DEF pair. */
146 static int gl_instance_get_rrdargs_cb (graph_def_t *def, void *user_data) /* {{{ */
147 {
148   def_callback_data_t *data = user_data;
149   graph_instance_t *inst = data->inst;
150   rrd_args_t *args = data->args;
151
152   size_t i;
153   int status;
154
155   for (i = 0; i < inst->files_num; i++)
156   {
157     if (!def_matches (def, inst->files[i]))
158       continue;
159
160     status = def_get_rrdargs (def, inst->files[i], args);
161     if (status != 0)
162     {
163       fprintf (stderr, "gl_instance_get_rrdargs_cb: def_get_rrdargs failed with status %i\n",
164           status);
165       fflush (stderr);
166     }
167   }
168
169   return (0);
170 } /* }}} int gl_instance_get_rrdargs_cb */
171
172 static const char *get_part_from_param (const char *prim_key, /* {{{ */
173     const char *sec_key)
174 {
175   const char *val;
176
177   val = param (prim_key);
178   if (val != NULL)
179     return (val);
180   
181   return (param (sec_key));
182 } /* }}} const char *get_part_from_param */
183
184 static graph_ident_t *inst_get_selector_from_params (void) /* {{{ */
185 {
186   const char *host = get_part_from_param ("inst_host", "host");
187   const char *plugin = get_part_from_param ("inst_plugin", "plugin");
188   const char *plugin_instance = get_part_from_param ("inst_plugin_instance",
189       "plugin_instance");
190   const char *type = get_part_from_param ("inst_type", "type");
191   const char *type_instance = get_part_from_param ("inst_type_instance",
192       "type_instance");
193
194   graph_ident_t *ident;
195
196   if ((host == NULL)
197       || (plugin == NULL) || (plugin_instance == NULL)
198       || (type == NULL) || (type_instance == NULL))
199   {
200     fprintf (stderr, "inst_get_selected: A parameter is NULL\n");
201     return (NULL);
202   }
203
204   ident = ident_create (host, plugin, plugin_instance, type, type_instance);
205   if (ident == NULL)
206   {
207     fprintf (stderr, "inst_get_selected: ident_create failed\n");
208     return (NULL);
209   }
210
211   return (ident);
212 } /* }}} graph_ident_t *inst_get_selector_from_params */
213
214 /*
215  * Public functions
216  */
217 graph_instance_t *inst_create (graph_config_t *cfg, /* {{{ */
218     const graph_ident_t *ident)
219 {
220   graph_instance_t *i;
221   graph_ident_t *selector;
222
223   if ((cfg == NULL) || (ident == NULL))
224     return (NULL);
225
226   i = malloc (sizeof (*i));
227   if (i == NULL)
228     return (NULL);
229   memset (i, 0, sizeof (*i));
230
231   selector = graph_get_selector (cfg);
232   if (selector == NULL)
233   {
234     fprintf (stderr, "inst_create: graph_get_selector failed\n");
235     free (i);
236     return (NULL);
237   }
238
239   i->select = ident_copy_with_selector (selector, ident,
240       IDENT_FLAG_REPLACE_ANY);
241   if (i->select == NULL)
242   {
243     fprintf (stderr, "inst_create: ident_copy_with_selector failed\n");
244     ident_destroy (selector);
245     free (i);
246     return (NULL);
247   }
248
249   ident_destroy (selector);
250
251   i->files = NULL;
252   i->files_num = 0;
253
254   return (i);
255 } /* }}} graph_instance_t *inst_create */
256
257 void inst_destroy (graph_instance_t *inst) /* {{{ */
258 {
259   size_t i;
260
261   if (inst == NULL)
262     return;
263
264   ident_destroy (inst->select);
265
266   for (i = 0; i < inst->files_num; i++)
267     ident_destroy (inst->files[i]);
268   free (inst->files);
269
270   free (inst);
271 } /* }}} void inst_destroy */
272
273 int inst_add_file (graph_instance_t *inst, /* {{{ */
274     const graph_ident_t *file)
275 {
276   graph_ident_t **tmp;
277
278   tmp = realloc (inst->files, sizeof (*inst->files) * (inst->files_num + 1));
279   if (tmp == NULL)
280     return (ENOMEM);
281   inst->files = tmp;
282
283   inst->files[inst->files_num] = ident_clone (file);
284   if (inst->files[inst->files_num] == NULL)
285     return (ENOMEM);
286
287   inst->files_num++;
288
289   return (0);
290 } /* }}} int inst_add_file */
291
292 graph_instance_t *inst_get_selected (graph_config_t *cfg) /* {{{ */
293 {
294   graph_ident_t *ident;
295   graph_instance_t *inst;
296
297   if (cfg == NULL)
298     cfg = gl_graph_get_selected ();
299
300   if (cfg == NULL)
301   {
302     DEBUG ("inst_get_selected: cfg == NULL;\n");
303     return (NULL);
304   }
305
306   ident = inst_get_selector_from_params ();
307   if (ident == NULL)
308   {
309     fprintf (stderr, "inst_get_selected: ident_create failed\n");
310     return (NULL);
311   }
312
313   inst = graph_inst_find_exact (cfg, ident);
314
315   ident_destroy (ident);
316   return (inst);
317 } /* }}} graph_instance_t *inst_get_selected */
318
319 int inst_get_all_selected (graph_config_t *cfg, /* {{{ */
320     graph_inst_callback_t callback, void *user_data)
321 {
322   graph_ident_t *ident;
323   int status;
324
325   if ((cfg == NULL) || (callback == NULL))
326     return (EINVAL);
327
328   ident = inst_get_selector_from_params ();
329   if (ident == NULL)
330   {
331     fprintf (stderr, "inst_get_all_selected: "
332         "inst_get_selector_from_params failed\n");
333     return (EINVAL);
334   }
335
336   status = graph_inst_find_all_matching (cfg, ident, callback, user_data);
337
338   ident_destroy (ident);
339   return (status);
340 } /* }}} int inst_get_all_selected */
341
342 int inst_get_rrdargs (graph_config_t *cfg, /* {{{ */
343     graph_instance_t *inst,
344     rrd_args_t *args)
345 {
346   def_callback_data_t data = { inst, args };
347   graph_def_t *defs;
348   int status;
349
350   if ((cfg == NULL) || (inst == NULL) || (args == NULL))
351     return (EINVAL);
352
353   status = graph_get_rrdargs (cfg, inst, args);
354   if (status != 0)
355     return (status);
356
357   defs = graph_get_defs (cfg);
358   if (defs == NULL)
359   {
360     defs = inst_get_default_defs (cfg, inst);
361
362     if (defs == NULL)
363       return (-1);
364
365     status = def_foreach (defs, gl_instance_get_rrdargs_cb, &data);
366
367     def_destroy (defs);
368   }
369   else
370   {
371     status = def_foreach (defs, gl_instance_get_rrdargs_cb, &data);
372   }
373
374   return (status);
375 } /* }}} int inst_get_rrdargs */
376
377 /* Create one or more DEFs for each file in the graph instance. The number
378  * depends on the number of data sources in each of the files. Called from
379  * "inst_get_rrdargs" if no DEFs are available from the configuration.
380  * */
381 graph_def_t *inst_get_default_defs (graph_config_t *cfg, /* {{{ */
382     graph_instance_t *inst)
383 {
384   graph_def_t *defs = NULL;
385   size_t i;
386
387   if ((cfg == NULL) || (inst == NULL))
388     return (NULL);
389
390   for (i = 0; i < inst->files_num; i++)
391   {
392     graph_def_t *def;
393
394     def = ident_get_default_defs (cfg, inst->files[i], defs);
395     if (def == NULL)
396       continue;
397
398     if (defs == NULL)
399       defs = def;
400     else
401       def_append (defs, def);
402   }
403
404   return (defs);
405 } /* }}} graph_def_t *inst_get_default_defs */
406
407 graph_ident_t *inst_get_selector (graph_instance_t *inst) /* {{{ */
408 {
409   if (inst == NULL)
410     return (NULL);
411
412   return (ident_clone (inst->select));
413 } /* }}} graph_ident_t *inst_get_selector */
414
415 int inst_get_params (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
416     char *buffer, size_t buffer_size)
417 {
418   graph_ident_t *cfg_select;
419
420   if ((cfg == NULL) || (inst == NULL)
421       || (buffer == NULL) || (buffer_size < 1))
422     return (EINVAL);
423
424   cfg_select = graph_get_selector (cfg);
425   if (cfg_select == NULL)
426   {
427     fprintf (stderr, "inst_get_params: graph_get_selector failed");
428     return (-1);
429   }
430
431   buffer[0] = 0;
432
433 #define COPY_ESCAPE(str) do {                                   \
434   char tmp[1024];                                               \
435   uri_escape_copy (tmp, (str), sizeof (tmp));                   \
436   strlcat (buffer, tmp, buffer_size);                           \
437 } while (0)                                                     \
438
439 #define COPY_FIELD(field) do {                                  \
440   const char *cfg_f  = ident_get_##field (cfg_select);          \
441   const char *inst_f = ident_get_##field (inst->select);        \
442   if (strcmp (cfg_f, inst_f) == 0)                              \
443   {                                                             \
444     strlcat (buffer, #field, buffer_size);                      \
445     strlcat (buffer, "=", buffer_size);                         \
446     COPY_ESCAPE (cfg_f);                                        \
447   }                                                             \
448   else                                                          \
449   {                                                             \
450     strlcat (buffer, "graph_", buffer_size);                    \
451     strlcat (buffer, #field, buffer_size);                      \
452     strlcat (buffer, "=", buffer_size);                         \
453     COPY_ESCAPE (cfg_f);                                        \
454     strlcat (buffer, ";", buffer_size);                         \
455     strlcat (buffer, "inst_", buffer_size);                     \
456     strlcat (buffer, #field, buffer_size);                      \
457     strlcat (buffer, "=", buffer_size);                         \
458     COPY_ESCAPE (inst_f);                                       \
459   }                                                             \
460 } while (0)
461
462   COPY_FIELD(host);
463   strlcat (buffer, ";", buffer_size);
464   COPY_FIELD(plugin);
465   strlcat (buffer, ";", buffer_size);
466   COPY_FIELD(plugin_instance);
467   strlcat (buffer, ";", buffer_size);
468   COPY_FIELD(type);
469   strlcat (buffer, ";", buffer_size);
470   COPY_FIELD(type_instance);
471
472 #undef COPY_FIELD
473 #undef COPY_ESCAPE
474
475   ident_destroy (cfg_select);
476
477   return (0);
478 } /* }}} int inst_get_params */
479
480 int inst_compare (const graph_instance_t *i0, /* {{{ */
481     const graph_instance_t *i1)
482 {
483   return (ident_compare (i0->select, i1->select));
484 } /* }}} int inst_compare */
485
486 int inst_compare_ident (graph_instance_t *inst, /* {{{ */
487     const graph_ident_t *ident)
488 {
489   if ((inst == NULL) || (ident == NULL))
490     return (0);
491
492   return (ident_compare (inst->select, ident));
493 } /* }}} int inst_compare_ident */
494
495 _Bool inst_ident_matches (graph_instance_t *inst, /* {{{ */
496     const graph_ident_t *ident)
497 {
498 #if C4_DEBUG
499   if ((inst == NULL) || (ident == NULL))
500     return (0);
501 #endif
502
503   return (ident_matches (inst->select, ident));
504 } /* }}} _Bool inst_ident_matches */
505
506 _Bool inst_matches_ident (graph_instance_t *inst, /* {{{ */
507     const graph_ident_t *ident)
508 {
509   if ((inst == NULL) || (ident == NULL))
510     return (0);
511
512   return (ident_matches (ident, inst->select));
513 } /* }}} _Bool inst_matches_ident */
514
515 _Bool inst_matches_string (graph_config_t *cfg, /* {{{ */
516     graph_instance_t *inst,
517     const char *term)
518 {
519   char buffer[1024];
520   int status;
521
522   if ((cfg == NULL) || (inst == NULL) || (term == NULL))
523     return (0);
524
525   status = inst_describe (cfg, inst, buffer, sizeof (buffer));
526   if (status != 0)
527   {
528     fprintf (stderr, "inst_matches_string: inst_describe failed\n");
529     return (status);
530   }
531
532   strtolower (buffer);
533
534   /* no match */
535   if (strstr (buffer, term) == NULL)
536     return (0);
537
538   return (1);
539 } /* }}} _Bool inst_matches_string */
540
541 _Bool inst_matches_field (graph_instance_t *inst, /* {{{ */
542     graph_ident_field_t field, const char *field_value)
543 {
544   const char *selector_field;
545   size_t i;
546
547   if ((inst == NULL) || (field_value == NULL))
548     return (0);
549
550   selector_field = ident_get_field (inst->select, field);
551   if (selector_field == NULL)
552     return (0);
553
554   assert (!IS_ANY (selector_field));
555   if (!IS_ALL (selector_field))
556   {
557     if (strcasecmp (selector_field, field_value) == 0)
558       return (1);
559     else
560       return (0);
561   }
562
563   /* The selector field is an ALL selector
564    * => we need to check the files to see if the instance matches. */
565   for (i = 0; i < inst->files_num; i++)
566   {
567     selector_field = ident_get_field (inst->files[i], field);
568     if (selector_field == NULL)
569       continue;
570
571     assert (!IS_ANY (selector_field));
572     assert (!IS_ALL (selector_field));
573
574     if (strcasecmp (selector_field, field_value) == 0)
575       return (1);
576   } /* for files */
577
578   return (0);
579 } /* }}} _Bool inst_matches_field */
580
581 int inst_to_json (const graph_instance_t *inst, /* {{{ */
582     yajl_gen handler)
583 {
584   size_t i;
585
586   if ((inst == NULL) || (handler == NULL))
587     return (EINVAL);
588
589   /* TODO: error handling */
590   yajl_gen_map_open (handler);
591   yajl_gen_string (handler,
592       (unsigned char *) "select",
593       (unsigned int) strlen ("select"));
594   ident_to_json (inst->select, handler);
595   yajl_gen_string (handler,
596       (unsigned char *) "files",
597       (unsigned int) strlen ("files"));
598   yajl_gen_array_open (handler);
599   for (i = 0; i < inst->files_num; i++)
600     ident_to_json (inst->files[i], handler);
601   yajl_gen_array_close (handler);
602   yajl_gen_map_close (handler);
603
604   return (0);
605 } /* }}} int inst_to_json */
606
607 int inst_data_to_json (const graph_instance_t *inst, /* {{{ */
608     dp_time_t begin, dp_time_t end, dp_time_t res,
609     yajl_gen handler)
610 {
611   size_t i;
612
613   yajl_gen_array_open (handler);
614   for (i = 0; i < inst->files_num; i++)
615     ident_data_to_json (inst->files[i], begin, end, res, handler);
616   yajl_gen_array_close (handler);
617
618   return (0);
619 } /* }}} int inst_data_to_json */
620
621 int inst_describe (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
622     char *buffer, size_t buffer_size)
623 {
624   graph_ident_t *cfg_select;
625   int status;
626
627   if ((cfg == NULL) || (inst == NULL)
628       || (buffer == NULL) || (buffer_size < 2))
629     return (EINVAL);
630
631   cfg_select = graph_get_selector (cfg);
632   if (cfg_select == NULL)
633   {
634     fprintf (stderr, "inst_describe: graph_get_selector failed\n");
635     return (-1);
636   }
637
638   status = ident_describe (inst->select, cfg_select,
639       buffer, buffer_size);
640
641   ident_destroy (cfg_select);
642
643   return (status);
644 } /* }}} int inst_describe */
645
646 time_t inst_get_mtime (graph_instance_t *inst) /* {{{ */
647 {
648   size_t i;
649   time_t mtime;
650
651   if (inst == NULL)
652     return (0);
653
654   mtime = 0;
655   for (i = 0; i < inst->files_num; i++)
656   {
657     time_t tmp;
658
659     tmp = ident_get_mtime (inst->files[i]);
660     if (mtime < tmp)
661       mtime = tmp;
662   }
663
664   return (mtime);
665 } /* }}} time_t inst_get_mtime */
666
667 /* vim: set sw=2 sts=2 et fdm=marker : */