103be23860ba2b913633a9c11a4018989e670b26
[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   status = graph_get_rrdargs (cfg, inst, args);
292   if (status != 0)
293     return (status);
294
295   defs = graph_get_defs (cfg);
296   if (defs == NULL)
297   {
298     defs = inst_get_default_defs (cfg, inst);
299
300     if (defs == NULL)
301       return (-1);
302
303     status = def_foreach (defs, gl_instance_get_rrdargs_cb, &data);
304
305     def_destroy (defs);
306   }
307   else
308   {
309     status = def_foreach (defs, gl_instance_get_rrdargs_cb, &data);
310   }
311
312   return (status);
313 } /* }}} int inst_get_rrdargs */
314
315 graph_ident_t *inst_get_selector (graph_instance_t *inst) /* {{{ */
316 {
317   if (inst == NULL)
318     return (NULL);
319
320   return (ident_clone (inst->select));
321 } /* }}} graph_ident_t *inst_get_selector */
322
323 int inst_get_params (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
324     char *buffer, size_t buffer_size)
325 {
326   graph_ident_t *cfg_select;
327
328   if ((cfg == NULL) || (inst == NULL)
329       || (buffer == NULL) || (buffer_size < 1))
330     return (EINVAL);
331
332   cfg_select = graph_get_selector (cfg);
333   if (cfg_select == NULL)
334   {
335     fprintf (stderr, "inst_get_params: graph_get_selector failed");
336     return (-1);
337   }
338
339   buffer[0] = 0;
340
341 #define COPY_FIELD(field) do {                                  \
342   const char *cfg_f  = ident_get_##field (cfg_select);          \
343   const char *inst_f = ident_get_##field (inst->select);        \
344   if (strcmp (cfg_f, inst_f) == 0)                              \
345   {                                                             \
346     strlcat (buffer, #field, buffer_size);                      \
347     strlcat (buffer, "=", buffer_size);                         \
348     strlcat (buffer, cfg_f, buffer_size);                       \
349   }                                                             \
350   else                                                          \
351   {                                                             \
352     strlcat (buffer, "graph_", buffer_size);                    \
353     strlcat (buffer, #field, buffer_size);                      \
354     strlcat (buffer, "=", buffer_size);                         \
355     strlcat (buffer, cfg_f, buffer_size);                       \
356     strlcat (buffer, ";", buffer_size);                         \
357     strlcat (buffer, "inst_", buffer_size);                     \
358     strlcat (buffer, #field, buffer_size);                      \
359     strlcat (buffer, "=", buffer_size);                         \
360     strlcat (buffer, inst_f, buffer_size);                      \
361   }                                                             \
362 } while (0)
363
364   COPY_FIELD(host);
365   strlcat (buffer, ";", buffer_size);
366   COPY_FIELD(plugin);
367   strlcat (buffer, ";", buffer_size);
368   COPY_FIELD(plugin_instance);
369   strlcat (buffer, ";", buffer_size);
370   COPY_FIELD(type);
371   strlcat (buffer, ";", buffer_size);
372   COPY_FIELD(type_instance);
373
374 #undef COPY_FIELD
375
376   ident_destroy (cfg_select);
377
378   return (0);
379 } /* }}} int inst_get_params */
380
381 int inst_append (graph_instance_t *head, graph_instance_t *inst) /* {{{ */
382 {
383   graph_instance_t *ptr;
384
385   if ((head == NULL) || (inst == NULL))
386     return (EINVAL);
387
388   ptr = head;
389   while (ptr->next != NULL)
390     ptr = ptr->next;
391
392   ptr->next = inst;
393
394   return (0);
395 } /* }}} int inst_append */
396
397 int inst_foreach (graph_instance_t *inst, /* {{{ */
398                 inst_callback_t cb, void *user_data)
399 {
400   graph_instance_t *ptr;
401
402   if ((inst == NULL) || (cb == NULL))
403     return (EINVAL);
404
405   for (ptr = inst; ptr != NULL; ptr = ptr->next)
406   {
407     int status;
408
409     status = (*cb) (ptr, user_data);
410     if (status != 0)
411       return (status);
412   }
413
414   return (0);
415 } /* }}} int inst_foreach */
416
417 graph_instance_t *inst_find_matching (graph_instance_t *inst, /* {{{ */
418     const graph_ident_t *ident)
419 {
420   graph_instance_t *ptr;
421
422   if ((inst == NULL) || (ident == NULL))
423     return (NULL);
424
425   for (ptr = inst; ptr != NULL; ptr = ptr->next)
426     if (ident_matches (ptr->select, ident))
427       return (ptr);
428
429   return (NULL);
430 } /* }}} graph_instance_t *inst_find_matching */
431
432 int inst_describe (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
433     char *buffer, size_t buffer_size)
434 {
435   graph_ident_t *cfg_select;
436
437   if ((cfg == NULL) || (inst == NULL)
438       || (buffer == NULL) || (buffer_size < 2))
439     return (EINVAL);
440
441   cfg_select = graph_get_selector (cfg);
442   if (cfg_select == NULL)
443   {
444     fprintf (stderr, "inst_describe: graph_get_selector failed\n");
445     return (-1);
446   }
447
448   buffer[0] = 0;
449
450 #define CHECK_FIELD(field) do {                                              \
451   if (IS_ANY (ident_get_##field (cfg_select)))                               \
452   {                                                                          \
453     if (buffer[0] != 0)                                                      \
454       strlcat (buffer, "/", buffer_size);                                    \
455     strlcat (buffer, ident_get_##field (inst->select), buffer_size);         \
456   }                                                                          \
457 } while (0)
458
459   CHECK_FIELD (host);
460   CHECK_FIELD (plugin);
461   CHECK_FIELD (plugin_instance);
462   CHECK_FIELD (type);
463   CHECK_FIELD (type_instance);
464
465 #undef CHECK_FIELD
466
467   if (buffer[0] == 0)
468     strlcat (buffer, "default", buffer_size);
469
470   ident_destroy (cfg_select);
471
472   return (0);
473 } /* }}} int inst_describe */
474
475 /* vim: set sw=2 sts=2 et fdm=marker : */