Code

src/graph{,_instance}.[ch]: Implement graph instances as an array in graph_config_t.
[collection4.git] / src / graph.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <stdint.h>
4 #include <inttypes.h>
5 #include <string.h>
6 #include <time.h>
7 #include <errno.h>
8 #include <assert.h>
10 #include "graph.h"
11 #include "graph_list.h"
12 #include "graph_ident.h"
13 #include "graph_def.h"
14 #include "graph_config.h"
15 #include "common.h"
16 #include "filesystem.h"
17 #include "utils_cgi.h"
19 #include <fcgiapp.h>
20 #include <fcgi_stdio.h>
22 /*
23  * Data types
24  */
25 struct graph_config_s /* {{{ */
26 {
27   graph_ident_t *select;
29   char *title;
30   char *vertical_label;
31   _Bool show_zero;
33   graph_def_t *defs;
35   graph_instance_t **instances;
36   size_t instances_num;
37 }; /* }}} struct graph_config_s */
39 /*
40  * Private functions
41  */
43 /*
44  * Config functions
45  */
46 static graph_ident_t *graph_config_get_selector (const oconfig_item_t *ci) /* {{{ */
47 {
48   char *host = NULL;
49   char *plugin = NULL;
50   char *plugin_instance = NULL;
51   char *type = NULL;
52   char *type_instance = NULL;
53   graph_ident_t *ret;
54   int i;
56   for (i = 0; i < ci->children_num; i++)
57   {
58     oconfig_item_t *child;
60     child = ci->children + i;
62     if (strcasecmp ("Host", child->key) == 0)
63       graph_config_get_string (child, &host);
64     else if (strcasecmp ("Plugin", child->key) == 0)
65       graph_config_get_string (child, &plugin);
66     else if (strcasecmp ("PluginInstance", child->key) == 0)
67       graph_config_get_string (child, &plugin_instance);
68     else if (strcasecmp ("Type", child->key) == 0)
69       graph_config_get_string (child, &type);
70     else if (strcasecmp ("TypeInstance", child->key) == 0)
71       graph_config_get_string (child, &type_instance);
72     /* else: ignore all other directives here. */
73   } /* for */
75   ret = ident_create (host, plugin, plugin_instance, type, type_instance);
77   free (host);
78   free (plugin);
79   free (plugin_instance);
80   free (type);
81   free (type_instance);
83   return (ret);
84 } /* }}} int graph_config_get_selector */
86 /*
87  * Global functions
88  */
89 graph_config_t *graph_create (const graph_ident_t *selector) /* {{{ */
90 {
91   graph_config_t *cfg;
93   cfg = malloc (sizeof (*cfg));
94   if (cfg == NULL)
95     return (NULL);
96   memset (cfg, 0, sizeof (*cfg));
98   if (selector != NULL)
99     cfg->select = ident_clone (selector);
100   else
101     cfg->select = NULL;
103   cfg->title = NULL;
104   cfg->vertical_label = NULL;
105   cfg->defs = NULL;
106   cfg->instances = NULL;
108   return (cfg);
109 } /* }}} int graph_create */
111 void graph_destroy (graph_config_t *cfg) /* {{{ */
113   size_t i;
115   if (cfg == NULL)
116     return;
118   ident_destroy (cfg->select);
120   free (cfg->title);
121   free (cfg->vertical_label);
123   def_destroy (cfg->defs);
125   for (i = 0; i < cfg->instances_num; i++)
126     inst_destroy (cfg->instances[i]);
127   free (cfg->instances);
128 } /* }}} void graph_destroy */
130 int graph_config_add (const oconfig_item_t *ci) /* {{{ */
132   graph_ident_t *select;
133   graph_config_t *cfg = NULL;
134   int i;
136   select = graph_config_get_selector (ci);
137   if (select == NULL)
138     return (EINVAL);
140   cfg = graph_create (/* selector = */ NULL);
141   if (cfg == NULL)
142     return (ENOMEM);
144   cfg->select = select;
146   for (i = 0; i < ci->children_num; i++)
147   {
148     oconfig_item_t *child;
150     child = ci->children + i;
152     if (strcasecmp ("Title", child->key) == 0)
153       graph_config_get_string (child, &cfg->title);
154     else if (strcasecmp ("VerticalLabel", child->key) == 0)
155       graph_config_get_string (child, &cfg->vertical_label);
156     else if (strcasecmp ("ShowZero", child->key) == 0)
157       graph_config_get_bool (child, &cfg->show_zero);
158     else if (strcasecmp ("DEF", child->key) == 0)
159       def_config (cfg, child);
160   } /* for */
162   gl_add_graph (cfg);
164   return (0);
165 } /* }}} graph_config_add */
167 int graph_add_file (graph_config_t *cfg, const graph_ident_t *file) /* {{{ */
169   graph_instance_t *inst;
171   inst = graph_inst_find_matching (cfg, file);
172   if (inst == NULL)
173   {
174     graph_instance_t **tmp;
176     tmp = realloc (cfg->instances,
177         sizeof (*cfg->instances) * (cfg->instances_num + 1));
178     if (tmp == NULL)
179       return (ENOMEM);
180     cfg->instances = tmp;
182     inst = inst_create (cfg, file);
183     if (inst == NULL)
184       return (ENOMEM);
186     cfg->instances[cfg->instances_num] = inst;
187     cfg->instances_num++;
188   }
190   return (inst_add_file (inst, file));
191 } /* }}} int graph_add_file */
193 int graph_get_title (graph_config_t *cfg, /* {{{ */
194     char *buffer, size_t buffer_size)
196   if ((cfg == NULL) || (buffer == NULL) || (buffer_size < 1))
197     return (EINVAL);
199   if (cfg->title == NULL)
200     cfg->title = ident_to_string (cfg->select);
202   if (cfg->title == NULL)
203     return (ENOMEM);
205   strncpy (buffer, cfg->title, buffer_size);
206   buffer[buffer_size - 1] = 0;
208   return (0);
209 } /* }}} int graph_get_title */
211 int graph_get_params (graph_config_t *cfg, /* {{{ */
212     char *buffer, size_t buffer_size)
214   buffer[0] = 0;
216 #define COPY_FIELD(field) do {                                       \
217   const char *str = ident_get_##field (cfg->select);                 \
218   char uri_str[1024];                                                \
219   uri_escape (uri_str, str, sizeof (uri_str));                       \
220   strlcat (buffer, #field, buffer_size);                             \
221   strlcat (buffer, "=", buffer_size);                                \
222   strlcat (buffer, uri_str, buffer_size);                            \
223 } while (0)
225   COPY_FIELD(host);
226   strlcat (buffer, ";", buffer_size);
227   COPY_FIELD(plugin);
228   strlcat (buffer, ";", buffer_size);
229   COPY_FIELD(plugin_instance);
230   strlcat (buffer, ";", buffer_size);
231   COPY_FIELD(type);
232   strlcat (buffer, ";", buffer_size);
233   COPY_FIELD(type_instance);
235 #undef COPY_FIELD
237   return (0);
238 } /* }}} int graph_get_params */
240 graph_ident_t *graph_get_selector (graph_config_t *cfg) /* {{{ */
242   if (cfg == NULL)
243     return (NULL);
245   return (ident_clone (cfg->select));
246 } /* }}} graph_ident_t *graph_get_selector */
248 graph_def_t *graph_get_defs (graph_config_t *cfg) /* {{{ */
250   if (cfg == NULL)
251     return (NULL);
253   return (cfg->defs);
254 } /* }}} graph_def_t *graph_get_defs */
256 int graph_add_def (graph_config_t *cfg, graph_def_t *def) /* {{{ */
258   if ((cfg == NULL) || (def == NULL))
259     return (EINVAL);
261   if (cfg->defs == NULL)
262   {
263     cfg->defs = def;
264     return (0);
265   }
267   return (def_append (cfg->defs, def));
268 } /* }}} int graph_add_def */
270 _Bool graph_matches (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */
272   if ((cfg == NULL) || (ident == NULL))
273     return (0);
275   return (ident_matches (cfg->select, ident));
276 } /* }}} _Bool graph_matches */
278 int graph_inst_foreach (graph_config_t *cfg, /* {{{ */
279                 inst_callback_t cb, void *user_data)
281   size_t i;
282   int status;
284   for (i = 0; i < cfg->instances_num; i++)
285   {
286     status = (*cb) (cfg->instances[i], user_data);
287     if (status != 0)
288       return (status);
289   }
291   return (0);
292 } /* }}} int graph_inst_foreach */
294 graph_instance_t *graph_inst_find_exact (graph_config_t *cfg, /* {{{ */
295     graph_ident_t *ident)
297   size_t i;
299   if ((cfg == NULL) || (ident == NULL))
300     return (NULL);
302   for (i = 0; i < cfg->instances_num; i++)
303     if (inst_compare_ident (cfg->instances[i], ident) == 0)
304       return (cfg->instances[i]);
306   return (NULL);
307 } /* }}} graph_instance_t *graph_inst_find_exact */
309 graph_instance_t *graph_inst_find_matching (graph_config_t *cfg, /* {{{ */
310     const graph_ident_t *ident)
312   size_t i;
314   if ((cfg == NULL) || (ident == NULL))
315     return (NULL);
317   for (i = 0; i < cfg->instances_num; i++)
318     if (inst_matches_ident (cfg->instances[i], ident))
319       return (cfg->instances[i]);
321   return (NULL);
322 } /* }}} graph_instance_t *graph_inst_find_matching */
324 int graph_inst_search (graph_config_t *cfg, const char *term, /* {{{ */
325     graph_inst_callback_t cb,
326     void *user_data)
328   char buffer[1024];
329   int status;
330   size_t i;
332   status = graph_get_title (cfg, buffer, sizeof (buffer));
333   if (status != 0)
334   {
335     fprintf (stderr, "graph_inst_search: graph_get_title failed\n");
336     return (status);
337   }
339   strtolower (buffer);
341   if (strstr (buffer, term) != NULL)
342   {
343     for (i = 0; i < cfg->instances_num; i++)
344     {
345       status = (*cb) (cfg, cfg->instances[i], user_data);
346       if (status != 0)
347         return (status);
348     }
349   }
350   else
351   {
352     for (i = 0; i < cfg->instances_num; i++)
353     {
354       if (inst_matches_string (cfg, cfg->instances[i], term))
355       {
356         status = (*cb) (cfg, cfg->instances[i], user_data);
357         if (status != 0)
358           return (status);
359       }
360     }
361   }
363   return (0);
364 } /* }}} int graph_inst_search */
366 int graph_compare (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */
368   if ((cfg == NULL) || (ident == NULL))
369     return (0);
371   return (ident_compare (cfg->select, ident));
372 } /* }}} int graph_compare */
374 int graph_clear_instances (graph_config_t *cfg) /* {{{ */
376   size_t i;
378   if (cfg == NULL)
379     return (EINVAL);
381   for (i = 0; i < cfg->instances_num; i++)
382     inst_destroy (cfg->instances[i]);
383   free (cfg->instances);
384   cfg->instances = NULL;
385   cfg->instances_num = 0;
387   return (0);
388 } /* }}} int graph_clear_instances */
390 int graph_get_rrdargs (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
391     str_array_t *args)
393   if ((cfg == NULL) || (inst == NULL) || (args == NULL))
394     return (EINVAL);
396   if (cfg->title != NULL)
397   {
398     array_append (args, "-t");
399     array_append (args, cfg->title);
400   }
402   if (cfg->vertical_label != NULL)
403   {
404     array_append (args, "-v");
405     array_append (args, cfg->vertical_label);
406   }
408   if (cfg->show_zero)
409   {
410     array_append (args, "-l");
411     array_append (args, "0");
412   }
414   return (0);
415 } /* }}} int graph_get_rrdargs */
417 /* vim: set sw=2 sts=2 et fdm=marker : */