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