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 }; /* }}} struct graph_config_s */
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);
47 return (inst_find_matching (cfg->instances, ident));
48 } /* }}} graph_instance_t *graph_find_instance */
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;
63 for (i = 0; i < ci->children_num; i++)
64 {
65 oconfig_item_t *child;
67 child = ci->children + i;
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 */
82 ret = ident_create (host, plugin, plugin_instance, type, type_instance);
84 free (host);
85 free (plugin);
86 free (plugin_instance);
87 free (type);
88 free (type_instance);
90 return (ret);
91 } /* }}} int graph_config_get_selector */
93 /*
94 * Global functions
95 */
96 graph_config_t *graph_create (const graph_ident_t *selector) /* {{{ */
97 {
98 graph_config_t *cfg;
100 cfg = malloc (sizeof (*cfg));
101 if (cfg == NULL)
102 return (NULL);
103 memset (cfg, 0, sizeof (*cfg));
105 if (selector != NULL)
106 cfg->select = ident_clone (selector);
107 else
108 cfg->select = NULL;
110 cfg->title = NULL;
111 cfg->vertical_label = NULL;
112 cfg->defs = NULL;
113 cfg->instances = NULL;
115 return (cfg);
116 } /* }}} int graph_create */
118 void graph_destroy (graph_config_t *cfg) /* {{{ */
119 {
120 if (cfg == NULL)
121 return;
123 ident_destroy (cfg->select);
125 free (cfg->title);
126 free (cfg->vertical_label);
128 def_destroy (cfg->defs);
129 inst_destroy (cfg->instances);
130 } /* }}} void graph_destroy */
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;
138 select = graph_config_get_selector (ci);
139 if (select == NULL)
140 return (EINVAL);
142 cfg = graph_create (/* selector = */ NULL);
143 if (cfg == NULL)
144 return (ENOMEM);
146 cfg->select = select;
148 for (i = 0; i < ci->children_num; i++)
149 {
150 oconfig_item_t *child;
152 child = ci->children + i;
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 */
164 gl_add_graph (cfg);
166 return (0);
167 } /* }}} graph_config_add */
169 int graph_add_file (graph_config_t *cfg, const graph_ident_t *file) /* {{{ */
170 {
171 graph_instance_t *inst;
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);
180 if (cfg->instances == NULL)
181 cfg->instances = inst;
182 else
183 inst_append (cfg->instances, inst);
184 }
186 return (inst_add_file (inst, file));
187 } /* }}} int graph_add_file */
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);
195 if (cfg->title == NULL)
196 cfg->title = ident_to_string (cfg->select);
198 if (cfg->title == NULL)
199 return (ENOMEM);
201 strncpy (buffer, cfg->title, buffer_size);
202 buffer[buffer_size - 1] = 0;
204 return (0);
205 } /* }}} int graph_get_title */
207 graph_ident_t *graph_get_selector (graph_config_t *cfg) /* {{{ */
208 {
209 if (cfg == NULL)
210 return (NULL);
212 return (ident_clone (cfg->select));
213 } /* }}} graph_ident_t *graph_get_selector */
215 graph_instance_t *graph_get_instances (graph_config_t *cfg) /* {{{ */
216 {
217 if (cfg == NULL)
218 return (NULL);
220 return (cfg->instances);
221 } /* }}} graph_instance_t *graph_get_instances */
223 graph_def_t *graph_get_defs (graph_config_t *cfg) /* {{{ */
224 {
225 if (cfg == NULL)
226 return (NULL);
228 return (cfg->defs);
229 } /* }}} graph_def_t *graph_get_defs */
231 int graph_add_def (graph_config_t *cfg, graph_def_t *def) /* {{{ */
232 {
233 if ((cfg == NULL) || (def == NULL))
234 return (EINVAL);
236 if (cfg->defs == NULL)
237 {
238 cfg->defs = def;
239 return (0);
240 }
242 return (def_append (cfg->defs, def));
243 } /* }}} int graph_add_def */
245 _Bool graph_matches (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */
246 {
247 if ((cfg == NULL) || (ident == NULL))
248 return (0);
250 return (ident_matches (cfg->select, ident));
251 } /* }}} _Bool graph_matches */
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;
261 static int graph_search_submit (graph_instance_t *inst, /* {{{ */
262 void *user_data)
263 {
264 graph_search_data_t *data = user_data;
266 if ((inst == NULL) || (data == NULL))
267 return (EINVAL);
269 return ((*data->callback) (data->cfg, inst, data->user_data));
270 } /* }}} int graph_search_submit */
272 int graph_inst_foreach (graph_config_t *cfg, /* {{{ */
273 inst_callback_t cb, void *user_data)
274 {
275 return (inst_foreach (cfg->instances, cb, user_data));
276 } /* }}} int graph_inst_foreach */
278 int graph_search (graph_config_t *cfg, const char *term, /* {{{ */
279 graph_inst_callback_t callback,
280 void *user_data)
281 {
282 graph_search_data_t data = { cfg, callback, user_data };
283 char buffer[1024];
284 int status;
286 status = graph_get_title (cfg, buffer, sizeof (buffer));
287 if (status != 0)
288 {
289 fprintf (stderr, "graph_search: graph_get_title failed\n");
290 return (status);
291 }
293 strtolower (buffer);
295 if (strstr (buffer, term) != NULL)
296 {
297 status = inst_foreach (cfg->instances, graph_search_submit, &data);
298 if (status != 0)
299 return (status);
300 }
301 else
302 {
303 status = inst_search (cfg, cfg->instances, term,
304 graph_search_submit, &data);
305 if (status != 0)
306 return (status);
307 }
309 return (0);
310 } /* }}} int graph_search */
312 int graph_compare (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */
313 {
314 if ((cfg == NULL) || (ident == NULL))
315 return (0);
317 return (ident_compare (cfg->select, ident));
318 } /* }}} int graph_compare */
320 int graph_clear_instances (graph_config_t *cfg) /* {{{ */
321 {
322 if (cfg == NULL)
323 return (EINVAL);
325 inst_destroy (cfg->instances);
326 cfg->instances = NULL;
328 return (0);
329 } /* }}} int graph_clear_instances */
331 int graph_get_rrdargs (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
332 str_array_t *args)
333 {
334 if ((cfg == NULL) || (inst == NULL) || (args == NULL))
335 return (EINVAL);
337 if (cfg->title != NULL)
338 {
339 array_append (args, "-t");
340 array_append (args, cfg->title);
341 }
343 if (cfg->vertical_label != NULL)
344 {
345 array_append (args, "-v");
346 array_append (args, cfg->vertical_label);
347 }
349 if (cfg->show_zero)
350 {
351 array_append (args, "-l");
352 array_append (args, "0");
353 }
355 return (0);
356 } /* }}} int graph_get_rrdargs */
358 /* vim: set sw=2 sts=2 et fdm=marker : */