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