11 #include "graph_list.h"
12 #include "graph_ident.h"
13 #include "graph_def.h"
14 #include "graph_config.h"
16 #include "filesystem.h"
17 #include "utils_cgi.h"
20 #include <fcgi_stdio.h>
25 struct graph_config_s /* {{{ */
27 graph_ident_t *select;
35 graph_instance_t **instances;
37 }; /* }}} struct graph_config_s */
46 static graph_ident_t *graph_config_get_selector (const oconfig_item_t *ci) /* {{{ */
50 char *plugin_instance = NULL;
52 char *type_instance = NULL;
56 for (i = 0; i < ci->children_num; i++)
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. */
75 ret = ident_create (host, plugin, plugin_instance, type, type_instance);
79 free (plugin_instance);
84 } /* }}} int graph_config_get_selector */
89 graph_config_t *graph_create (const graph_ident_t *selector) /* {{{ */
93 cfg = malloc (sizeof (*cfg));
96 memset (cfg, 0, sizeof (*cfg));
99 cfg->select = ident_clone (selector);
104 cfg->vertical_label = NULL;
106 cfg->instances = NULL;
109 } /* }}} int graph_create */
111 void graph_destroy (graph_config_t *cfg) /* {{{ */
118 ident_destroy (cfg->select);
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;
136 select = graph_config_get_selector (ci);
140 cfg = graph_create (/* selector = */ NULL);
144 cfg->select = select;
146 for (i = 0; i < ci->children_num; i++)
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);
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);
174 graph_instance_t **tmp;
176 tmp = realloc (cfg->instances,
177 sizeof (*cfg->instances) * (cfg->instances_num + 1));
180 cfg->instances = tmp;
182 inst = inst_create (cfg, file);
186 cfg->instances[cfg->instances_num] = inst;
187 cfg->instances_num++;
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))
199 if (cfg->title == NULL)
200 cfg->title = ident_to_string (cfg->select);
202 if (cfg->title == NULL)
205 strncpy (buffer, cfg->title, buffer_size);
206 buffer[buffer_size - 1] = 0;
209 } /* }}} int graph_get_title */
211 int graph_get_params (graph_config_t *cfg, /* {{{ */
212 char *buffer, size_t buffer_size)
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); \
226 strlcat (buffer, ";", buffer_size);
228 strlcat (buffer, ";", buffer_size);
229 COPY_FIELD(plugin_instance);
230 strlcat (buffer, ";", buffer_size);
232 strlcat (buffer, ";", buffer_size);
233 COPY_FIELD(type_instance);
238 } /* }}} int graph_get_params */
240 graph_ident_t *graph_get_selector (graph_config_t *cfg) /* {{{ */
245 return (ident_clone (cfg->select));
246 } /* }}} graph_ident_t *graph_get_selector */
248 graph_def_t *graph_get_defs (graph_config_t *cfg) /* {{{ */
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))
261 if (cfg->defs == NULL)
267 return (def_append (cfg->defs, def));
268 } /* }}} int graph_add_def */
270 _Bool graph_matches_ident (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */
272 if ((cfg == NULL) || (ident == NULL))
275 return (ident_matches (cfg->select, ident));
276 } /* }}} _Bool graph_matches_ident */
278 _Bool graph_matches_field (graph_config_t *cfg, /* {{{ */
279 graph_ident_field_t field, const char *field_value)
281 const char *selector_value;
283 if ((cfg == NULL) || (field_value == NULL))
286 selector_value = ident_get_field (cfg->select, field);
287 if (selector_value == NULL)
290 if (IS_ALL (selector_value) || IS_ANY (selector_value))
292 else if (strcasecmp (selector_value, field_value) == 0)
296 } /* }}} _Bool graph_matches_field */
298 int graph_inst_foreach (graph_config_t *cfg, /* {{{ */
299 inst_callback_t cb, void *user_data)
304 for (i = 0; i < cfg->instances_num; i++)
306 status = (*cb) (cfg->instances[i], user_data);
312 } /* }}} int graph_inst_foreach */
314 graph_instance_t *graph_inst_find_exact (graph_config_t *cfg, /* {{{ */
315 graph_ident_t *ident)
319 if ((cfg == NULL) || (ident == NULL))
322 for (i = 0; i < cfg->instances_num; i++)
323 if (inst_compare_ident (cfg->instances[i], ident) == 0)
324 return (cfg->instances[i]);
327 } /* }}} graph_instance_t *graph_inst_find_exact */
329 graph_instance_t *graph_inst_find_matching (graph_config_t *cfg, /* {{{ */
330 const graph_ident_t *ident)
334 if ((cfg == NULL) || (ident == NULL))
337 for (i = 0; i < cfg->instances_num; i++)
338 if (inst_matches_ident (cfg->instances[i], ident))
339 return (cfg->instances[i]);
342 } /* }}} graph_instance_t *graph_inst_find_matching */
344 int graph_inst_search (graph_config_t *cfg, const char *term, /* {{{ */
345 graph_inst_callback_t cb,
352 status = graph_get_title (cfg, buffer, sizeof (buffer));
355 fprintf (stderr, "graph_inst_search: graph_get_title failed\n");
361 if (strstr (buffer, term) != NULL)
363 for (i = 0; i < cfg->instances_num; i++)
365 status = (*cb) (cfg, cfg->instances[i], user_data);
372 for (i = 0; i < cfg->instances_num; i++)
374 if (inst_matches_string (cfg, cfg->instances[i], term))
376 status = (*cb) (cfg, cfg->instances[i], user_data);
384 } /* }}} int graph_inst_search */
386 int graph_inst_search_field (graph_config_t *cfg, /* {{{ */
387 graph_ident_field_t field, const char *field_value,
388 graph_inst_callback_t callback, void *user_data)
392 if ((cfg == NULL) || (field_value == NULL) || (callback == NULL))
395 if (!graph_matches_field (cfg, field, field_value))
398 for (i = 0; i < cfg->instances_num; i++)
400 if (inst_matches_field (cfg->instances[i], field, field_value))
404 status = (*callback) (cfg, cfg->instances[i], user_data);
411 } /* }}} int graph_inst_search_field */
413 int graph_compare (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */
415 if ((cfg == NULL) || (ident == NULL))
418 return (ident_compare (cfg->select, ident));
419 } /* }}} int graph_compare */
421 int graph_clear_instances (graph_config_t *cfg) /* {{{ */
428 for (i = 0; i < cfg->instances_num; i++)
429 inst_destroy (cfg->instances[i]);
430 free (cfg->instances);
431 cfg->instances = NULL;
432 cfg->instances_num = 0;
435 } /* }}} int graph_clear_instances */
437 int graph_get_rrdargs (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
440 if ((cfg == NULL) || (inst == NULL) || (args == NULL))
443 if (cfg->title != NULL)
445 array_append (args, "-t");
446 array_append (args, cfg->title);
449 if (cfg->vertical_label != NULL)
451 array_append (args, "-v");
452 array_append (args, cfg->vertical_label);
457 array_append (args, "-l");
458 array_append (args, "0");
462 } /* }}} int graph_get_rrdargs */
464 /* vim: set sw=2 sts=2 et fdm=marker : */