Merge branch 'search'
[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>
9
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_params.h"
18
19 #include <fcgiapp.h>
20 #include <fcgi_stdio.h>
21
22 /*
23  * Data types
24  */
25 struct graph_config_s /* {{{ */
26 {
27   graph_ident_t *select;
28
29   char *title;
30   char *vertical_label;
31   _Bool show_zero;
32
33   graph_def_t *defs;
34
35   graph_instance_t *instances;
36 }; /* }}} struct graph_config_s */
37
38 /*
39  * Private functions
40  */
41 static graph_instance_t *graph_find_instance (graph_config_t *cfg, /* {{{ */
42     const graph_ident_t *ident)
43 {
44   if ((cfg == NULL) || (ident == NULL))
45     return (NULL);
46
47   return (inst_find_matching (cfg->instances, ident));
48 } /* }}} graph_instance_t *graph_find_instance */
49
50 /*
51  * Config functions
52  */
53 static graph_ident_t *graph_config_get_selector (const oconfig_item_t *ci) /* {{{ */
54 {
55   char *host = NULL;
56   char *plugin = NULL;
57   char *plugin_instance = NULL;
58   char *type = NULL;
59   char *type_instance = NULL;
60   graph_ident_t *ret;
61   int i;
62
63   for (i = 0; i < ci->children_num; i++)
64   {
65     oconfig_item_t *child;
66
67     child = ci->children + i;
68
69     if (strcasecmp ("Host", child->key) == 0)
70       graph_config_get_string (child, &host);
71     else if (strcasecmp ("Plugin", child->key) == 0)
72       graph_config_get_string (child, &plugin);
73     else if (strcasecmp ("PluginInstance", child->key) == 0)
74       graph_config_get_string (child, &plugin_instance);
75     else if (strcasecmp ("Type", child->key) == 0)
76       graph_config_get_string (child, &type);
77     else if (strcasecmp ("TypeInstance", child->key) == 0)
78       graph_config_get_string (child, &type_instance);
79     /* else: ignore all other directives here. */
80   } /* for */
81
82   ret = ident_create (host, plugin, plugin_instance, type, type_instance);
83
84   free (host);
85   free (plugin);
86   free (plugin_instance);
87   free (type);
88   free (type_instance);
89
90   return (ret);
91 } /* }}} int graph_config_get_selector */
92
93 /*
94  * Global functions
95  */
96 graph_config_t *graph_create (const graph_ident_t *selector) /* {{{ */
97 {
98   graph_config_t *cfg;
99
100   cfg = malloc (sizeof (*cfg));
101   if (cfg == NULL)
102     return (NULL);
103   memset (cfg, 0, sizeof (*cfg));
104
105   if (selector != NULL)
106     cfg->select = ident_clone (selector);
107   else
108     cfg->select = NULL;
109
110   cfg->title = NULL;
111   cfg->vertical_label = NULL;
112   cfg->defs = NULL;
113   cfg->instances = NULL;
114
115   return (cfg);
116 } /* }}} int graph_create */
117
118 void graph_destroy (graph_config_t *cfg) /* {{{ */
119 {
120   if (cfg == NULL)
121     return;
122
123   ident_destroy (cfg->select);
124
125   free (cfg->title);
126   free (cfg->vertical_label);
127
128   def_destroy (cfg->defs);
129   inst_destroy (cfg->instances);
130 } /* }}} void graph_destroy */
131
132 int graph_config_add (const oconfig_item_t *ci) /* {{{ */
133 {
134   graph_ident_t *select;
135   graph_config_t *cfg = NULL;
136   int i;
137
138   select = graph_config_get_selector (ci);
139   if (select == NULL)
140     return (EINVAL);
141
142   cfg = graph_create (/* selector = */ NULL);
143   if (cfg == NULL)
144     return (ENOMEM);
145
146   cfg->select = select;
147
148   for (i = 0; i < ci->children_num; i++)
149   {
150     oconfig_item_t *child;
151
152     child = ci->children + i;
153
154     if (strcasecmp ("Title", child->key) == 0)
155       graph_config_get_string (child, &cfg->title);
156     else if (strcasecmp ("VerticalLabel", child->key) == 0)
157       graph_config_get_string (child, &cfg->vertical_label);
158     else if (strcasecmp ("ShowZero", child->key) == 0)
159       graph_config_get_bool (child, &cfg->show_zero);
160     else if (strcasecmp ("DEF", child->key) == 0)
161       def_config (cfg, child);
162   } /* for */
163
164   gl_add_graph (cfg);
165
166   return (0);
167 } /* }}} graph_config_add */
168
169 int graph_add_file (graph_config_t *cfg, const graph_ident_t *file) /* {{{ */
170 {
171   graph_instance_t *inst;
172
173   inst = graph_find_instance (cfg, file);
174   if (inst == NULL)
175   {
176     inst = inst_create (cfg, file);
177     if (inst == NULL)
178       return (ENOMEM);
179
180     if (cfg->instances == NULL)
181       cfg->instances = inst;
182     else
183       inst_append (cfg->instances, inst);
184   }
185
186   return (inst_add_file (inst, file));
187 } /* }}} int graph_add_file */
188
189 int graph_get_title (graph_config_t *cfg, /* {{{ */
190     char *buffer, size_t buffer_size)
191 {
192   if ((cfg == NULL) || (buffer == NULL) || (buffer_size < 1))
193     return (EINVAL);
194
195   if (cfg->title == NULL)
196     cfg->title = ident_to_string (cfg->select);
197
198   if (cfg->title == NULL)
199     return (ENOMEM);
200
201   strncpy (buffer, cfg->title, buffer_size);
202   buffer[buffer_size - 1] = 0;
203
204   return (0);
205 } /* }}} int graph_get_title */
206
207 graph_ident_t *graph_get_selector (graph_config_t *cfg) /* {{{ */
208 {
209   if (cfg == NULL)
210     return (NULL);
211
212   return (ident_clone (cfg->select));
213 } /* }}} graph_ident_t *graph_get_selector */
214
215 graph_instance_t *graph_get_instances (graph_config_t *cfg) /* {{{ */
216 {
217   if (cfg == NULL)
218     return (NULL);
219
220   return (cfg->instances);
221 } /* }}} graph_instance_t *graph_get_instances */
222
223 graph_def_t *graph_get_defs (graph_config_t *cfg) /* {{{ */
224 {
225   if (cfg == NULL)
226     return (NULL);
227
228   return (cfg->defs);
229 } /* }}} graph_def_t *graph_get_defs */
230
231 int graph_add_def (graph_config_t *cfg, graph_def_t *def) /* {{{ */
232 {
233   if ((cfg == NULL) || (def == NULL))
234     return (EINVAL);
235
236   if (cfg->defs == NULL)
237   {
238     cfg->defs = def;
239     return (0);
240   }
241
242   return (def_append (cfg->defs, def));
243 } /* }}} int graph_add_def */
244
245 _Bool graph_matches (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */
246 {
247   if ((cfg == NULL) || (ident == NULL))
248     return (0);
249
250   return (ident_matches (cfg->select, ident));
251 } /* }}} _Bool graph_matches */
252
253 struct graph_search_data_s
254 {
255   graph_config_t *cfg;
256   graph_inst_callback_t callback;
257   void *user_data;
258 };
259 typedef struct graph_search_data_s graph_search_data_t;
260
261 static int graph_search_submit (graph_instance_t *inst, /* {{{ */
262     void *user_data)
263 {
264   graph_search_data_t *data = user_data;
265
266   if ((inst == NULL) || (data == NULL))
267     return (EINVAL);
268
269   return ((*data->callback) (data->cfg, inst, data->user_data));
270 } /* }}} int graph_search_submit */
271
272 int graph_search (graph_config_t *cfg, const char *term, /* {{{ */
273     graph_inst_callback_t callback,
274     void *user_data)
275 {
276   graph_search_data_t data = { cfg, callback, user_data };
277   char buffer[1024];
278   int status;
279
280   status = graph_get_title (cfg, buffer, sizeof (buffer));
281   if (status != 0)
282   {
283     fprintf (stderr, "graph_search: graph_get_title failed\n");
284     return (status);
285   }
286
287   if (strstr (buffer, term) != NULL)
288   {
289     status = inst_foreach (cfg->instances, graph_search_submit, &data);
290     if (status != 0)
291       return (status);
292   }
293   else
294   {
295     status = inst_search (cfg, cfg->instances, term,
296         graph_search_submit, &data);
297     if (status != 0)
298       return (status);
299   }
300
301   return (0);
302 } /* }}} int graph_search */
303
304 int graph_compare (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */
305 {
306   if ((cfg == NULL) || (ident == NULL))
307     return (0);
308
309   return (ident_compare (cfg->select, ident));
310 } /* }}} int graph_compare */
311
312 int graph_clear_instances (graph_config_t *cfg) /* {{{ */
313 {
314   if (cfg == NULL)
315     return (EINVAL);
316
317   inst_destroy (cfg->instances);
318   cfg->instances = NULL;
319
320   return (0);
321 } /* }}} int graph_clear_instances */
322
323 int graph_get_rrdargs (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
324     str_array_t *args)
325 {
326   if ((cfg == NULL) || (inst == NULL) || (args == NULL))
327     return (EINVAL);
328
329   if (cfg->title != NULL)
330   {
331     array_append (args, "-t");
332     array_append (args, cfg->title);
333   }
334
335   if (cfg->vertical_label != NULL)
336   {
337     array_append (args, "-v");
338     array_append (args, cfg->vertical_label);
339   }
340
341   if (cfg->show_zero)
342   {
343     array_append (args, "-l");
344     array_append (args, "0");
345   }
346
347   return (0);
348 } /* }}} int graph_get_rrdargs */
349
350 /* vim: set sw=2 sts=2 et fdm=marker : */