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