Code

src/utils_cgi.[ch]: Basically a rewrite of the parameter handling functions.
[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>
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"
15 #include <fcgiapp.h>
16 #include <fcgi_stdio.h>
18 struct graph_instance_s /* {{{ */
19 {
20   graph_ident_t *select;
22   graph_ident_t **files;
23   size_t files_num;
24 }; /* }}} struct graph_instance_s */
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;
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;
48   if ((cfg == NULL) || (ident == NULL))
49     return (def_head);
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   }
58   status = ds_list_from_rrd_file (file, &dses_num, &dses);
59   if (status != 0)
60   {
61     free (file);
62     return (def_head);
63   }
65   for (i = 0; i < dses_num; i++)
66   {
67     graph_def_t *def;
69     def = def_search (def_head, ident, dses[i]);
70     if (def != NULL)
71       continue;
73     def = def_create (cfg, ident, dses[i]);
74     if (def == NULL)
75       continue;
77     if (defs == NULL)
78       defs = def;
79     else
80       def_append (defs, def);
82     free (dses[i]);
83   }
85   free (dses);
86   free (file);
88   return (defs);
89 } /* }}} int ident_get_default_defs */
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;
101   if ((cfg == NULL) || (inst == NULL))
102     return (NULL);
104   for (i = 0; i < inst->files_num; i++)
105   {
106     graph_def_t *def;
108     def = ident_get_default_defs (cfg, inst->files[i], defs);
109     if (def == NULL)
110       continue;
112     if (defs == NULL)
113       defs = def;
114     else
115       def_append (defs, def);
116   }
118   return (defs);
119 } /* }}} graph_def_t *inst_get_default_defs */
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) /* {{{ */
125   def_callback_data_t *data = user_data;
126   graph_instance_t *inst = data->inst;
127   str_array_t *args = data->args;
129   size_t i;
131   for (i = 0; i < inst->files_num; i++)
132   {
133     if (!def_matches (def, inst->files[i]))
134       continue;
136     def_get_rrdargs (def, inst->files[i], args);
137   }
139   return (0);
140 } /* }}} int gl_instance_get_rrdargs_cb */
142 static const char *get_part_from_param (const char *prim_key, /* {{{ */
143     const char *sec_key)
145   const char *val;
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 */
154 /*
155  * Public functions
156  */
157 graph_instance_t *inst_create (graph_config_t *cfg, /* {{{ */
158     const graph_ident_t *ident)
160   graph_instance_t *i;
161   graph_ident_t *selector;
163   if ((cfg == NULL) || (ident == NULL))
164     return (NULL);
166   i = malloc (sizeof (*i));
167   if (i == NULL)
168     return (NULL);
169   memset (i, 0, sizeof (*i));
171   selector = graph_get_selector (cfg);
172   if (selector == NULL)
173   {
174     fprintf (stderr, "inst_create: graph_get_selector failed\n");
175     free (i);
176     return (NULL);
177   }
179   i->select = ident_copy_with_selector (selector, ident,
180       IDENT_FLAG_REPLACE_ANY);
181   if (i->select == NULL)
182   {
183     fprintf (stderr, "inst_create: ident_copy_with_selector failed\n");
184     ident_destroy (selector);
185     free (i);
186     return (NULL);
187   }
189   ident_destroy (selector);
191   i->files = NULL;
192   i->files_num = 0;
194   return (i);
195 } /* }}} graph_instance_t *inst_create */
197 void inst_destroy (graph_instance_t *inst) /* {{{ */
199   size_t i;
201   if (inst == NULL)
202     return;
204   ident_destroy (inst->select);
206   for (i = 0; i < inst->files_num; i++)
207     ident_destroy (inst->files[i]);
208   free (inst->files);
210   free (inst);
211 } /* }}} void inst_destroy */
213 int inst_add_file (graph_instance_t *inst, /* {{{ */
214     const graph_ident_t *file)
216   graph_ident_t **tmp;
218   tmp = realloc (inst->files, sizeof (*inst->files) * (inst->files_num + 1));
219   if (tmp == NULL)
220     return (ENOMEM);
221   inst->files = tmp;
223   inst->files[inst->files_num] = ident_clone (file);
224   if (inst->files[inst->files_num] == NULL)
225     return (ENOMEM);
227   inst->files_num++;
229   return (0);
230 } /* }}} int inst_add_file */
232 graph_instance_t *inst_get_selected (graph_config_t *cfg) /* {{{ */
234   const char *host = get_part_from_param ("inst_host", "host");
235   const char *plugin = get_part_from_param ("inst_plugin", "plugin");
236   const char *plugin_instance = get_part_from_param ("inst_plugin_instance",
237       "plugin_instance");
238   const char *type = get_part_from_param ("inst_type", "type");
239   const char *type_instance = get_part_from_param ("inst_type_instance",
240       "type_instance");
242   graph_ident_t *ident;
243   graph_instance_t *inst;
245   if (cfg == NULL)
246     cfg = gl_graph_get_selected ();
248   if (cfg == NULL)
249   {
250     DEBUG ("inst_get_selected: cfg == NULL;\n");
251     return (NULL);
252   }
254   if ((host == NULL)
255       || (plugin == NULL) || (plugin_instance == NULL)
256       || (type == NULL) || (type_instance == NULL))
257   {
258     DEBUG ("inst_get_selected: A parameter is NULL.\n");
259     return (NULL);
260   }
262   ident = ident_create (host, plugin, plugin_instance, type, type_instance);
263   if (ident == NULL)
264   {
265     fprintf (stderr, "inst_get_selected: ident_create failed\n");
266     return (NULL);
267   }
269   inst = graph_inst_find_exact (cfg, ident);
270   ident_destroy (ident);
272   return (inst);
273 } /* }}} graph_instance_t *inst_get_selected */
275 int inst_get_rrdargs (graph_config_t *cfg, /* {{{ */
276     graph_instance_t *inst,
277     str_array_t *args)
279   def_callback_data_t data = { inst, args };
280   graph_def_t *defs;
281   int status;
283   if ((cfg == NULL) || (inst == NULL) || (args == NULL))
284     return (EINVAL);
286   status = graph_get_rrdargs (cfg, inst, args);
287   if (status != 0)
288     return (status);
290   defs = graph_get_defs (cfg);
291   if (defs == NULL)
292   {
293     defs = inst_get_default_defs (cfg, inst);
295     if (defs == NULL)
296       return (-1);
298     status = def_foreach (defs, gl_instance_get_rrdargs_cb, &data);
300     def_destroy (defs);
301   }
302   else
303   {
304     status = def_foreach (defs, gl_instance_get_rrdargs_cb, &data);
305   }
307   return (status);
308 } /* }}} int inst_get_rrdargs */
310 graph_ident_t *inst_get_selector (graph_instance_t *inst) /* {{{ */
312   if (inst == NULL)
313     return (NULL);
315   return (ident_clone (inst->select));
316 } /* }}} graph_ident_t *inst_get_selector */
318 int inst_get_params (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
319     char *buffer, size_t buffer_size)
321   graph_ident_t *cfg_select;
323   if ((cfg == NULL) || (inst == NULL)
324       || (buffer == NULL) || (buffer_size < 1))
325     return (EINVAL);
327   cfg_select = graph_get_selector (cfg);
328   if (cfg_select == NULL)
329   {
330     fprintf (stderr, "inst_get_params: graph_get_selector failed");
331     return (-1);
332   }
334   buffer[0] = 0;
336 #define COPY_FIELD(field) do {                                  \
337   const char *cfg_f  = ident_get_##field (cfg_select);          \
338   const char *inst_f = ident_get_##field (inst->select);        \
339   if (strcmp (cfg_f, inst_f) == 0)                              \
340   {                                                             \
341     strlcat (buffer, #field, buffer_size);                      \
342     strlcat (buffer, "=", buffer_size);                         \
343     strlcat (buffer, cfg_f, buffer_size);                       \
344   }                                                             \
345   else                                                          \
346   {                                                             \
347     strlcat (buffer, "graph_", buffer_size);                    \
348     strlcat (buffer, #field, buffer_size);                      \
349     strlcat (buffer, "=", buffer_size);                         \
350     strlcat (buffer, cfg_f, buffer_size);                       \
351     strlcat (buffer, ";", buffer_size);                         \
352     strlcat (buffer, "inst_", buffer_size);                     \
353     strlcat (buffer, #field, buffer_size);                      \
354     strlcat (buffer, "=", buffer_size);                         \
355     strlcat (buffer, inst_f, buffer_size);                      \
356   }                                                             \
357 } while (0)
359   COPY_FIELD(host);
360   strlcat (buffer, ";", buffer_size);
361   COPY_FIELD(plugin);
362   strlcat (buffer, ";", buffer_size);
363   COPY_FIELD(plugin_instance);
364   strlcat (buffer, ";", buffer_size);
365   COPY_FIELD(type);
366   strlcat (buffer, ";", buffer_size);
367   COPY_FIELD(type_instance);
369 #undef COPY_FIELD
371   ident_destroy (cfg_select);
373   return (0);
374 } /* }}} int inst_get_params */
376 int inst_compare_ident (graph_instance_t *inst, /* {{{ */
377     const graph_ident_t *ident)
379   if ((inst == NULL) || (ident == NULL))
380     return (0);
382   return (ident_compare (inst->select, ident));
383 } /* }}} int inst_compare_ident */
385 _Bool inst_matches_ident (graph_instance_t *inst, /* {{{ */
386     const graph_ident_t *ident)
388   if ((inst == NULL) || (ident == NULL))
389     return (0);
391   return (ident_matches (inst->select, ident));
392 } /* }}} _Bool inst_matches_ident */
394 _Bool inst_matches_string (graph_config_t *cfg, /* {{{ */
395     graph_instance_t *inst,
396     const char *term)
398   char buffer[1024];
399   int status;
401   if ((cfg == NULL) || (inst == NULL) || (term == NULL))
402     return (0);
404   status = inst_describe (cfg, inst, buffer, sizeof (buffer));
405   if (status != 0)
406   {
407     fprintf (stderr, "inst_matches_string: inst_describe failed\n");
408     return (status);
409   }
411   strtolower (buffer);
413   /* no match */
414   if (strstr (buffer, term) == NULL)
415     return (0);
417   return (1);
418 } /* }}} _Bool inst_matches_string */
420 _Bool inst_matches_field (graph_instance_t *inst, /* {{{ */
421     graph_ident_field_t field, const char *field_value)
423   const char *selector_field;
424   size_t i;
426   if ((inst == NULL) || (field_value == NULL))
427     return (0);
429   selector_field = ident_get_field (inst->select, field);
430   if (selector_field == NULL)
431     return (0);
433   assert (!IS_ANY (selector_field));
434   if (!IS_ALL (selector_field))
435   {
436     if (strcasecmp (selector_field, field_value) == 0)
437       return (1);
438     else
439       return (0);
440   }
442   /* The selector field is an ALL selector
443    * => we need to check the files to see if the instance matches. */
444   for (i = 0; i < inst->files_num; i++)
445   {
446     selector_field = ident_get_field (inst->files[i], field);
447     if (selector_field == NULL)
448       continue;
450     assert (!IS_ANY (selector_field));
451     assert (!IS_ALL (selector_field));
453     if (strcasecmp (selector_field, field_value) == 0)
454       return (1);
455   } /* for files */
457   return (0);
458 } /* }}} _Bool inst_matches_field */
460 int inst_describe (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
461     char *buffer, size_t buffer_size)
463   graph_ident_t *cfg_select;
465   if ((cfg == NULL) || (inst == NULL)
466       || (buffer == NULL) || (buffer_size < 2))
467     return (EINVAL);
469   cfg_select = graph_get_selector (cfg);
470   if (cfg_select == NULL)
471   {
472     fprintf (stderr, "inst_describe: graph_get_selector failed\n");
473     return (-1);
474   }
476   buffer[0] = 0;
478 #define CHECK_FIELD(field) do {                                              \
479   if (IS_ANY (ident_get_##field (cfg_select)))                               \
480   {                                                                          \
481     if (buffer[0] != 0)                                                      \
482       strlcat (buffer, "/", buffer_size);                                    \
483     strlcat (buffer, ident_get_##field (inst->select), buffer_size);         \
484   }                                                                          \
485 } while (0)
487   CHECK_FIELD (host);
488   CHECK_FIELD (plugin);
489   CHECK_FIELD (plugin_instance);
490   CHECK_FIELD (type);
491   CHECK_FIELD (type_instance);
493 #undef CHECK_FIELD
495   if (buffer[0] == 0)
496     strlcat (buffer, "default", buffer_size);
498   ident_destroy (cfg_select);
500   return (0);
501 } /* }}} int inst_describe */
503 time_t inst_get_mtime (graph_instance_t *inst) /* {{{ */
505   size_t i;
506   time_t mtime;
508   if (inst == NULL)
509     return (0);
511   mtime = 0;
512   for (i = 0; i < inst->files_num; i++)
513   {
514     time_t tmp;
516     tmp = ident_get_mtime (inst->files[i]);
517     if (mtime < tmp)
518       mtime = tmp;
519   }
521   return (mtime);
522 } /* }}} time_t inst_get_mtime */
524 /* vim: set sw=2 sts=2 et fdm=marker : */