11 #include "graph_ident.h"
12 #include "graph_instance.h"
13 #include "graph_list.h"
14 #include "graph_def.h"
15 #include "graph_config.h"
17 #include "filesystem.h"
18 #include "utils_cgi.h"
21 #include <fcgi_stdio.h>
26 struct graph_config_s /* {{{ */
28 graph_ident_t *select;
36 graph_instance_t **instances;
38 }; /* }}} struct graph_config_s */
47 static graph_ident_t *graph_config_get_selector (const oconfig_item_t *ci) /* {{{ */
51 char *plugin_instance = NULL;
53 char *type_instance = NULL;
57 for (i = 0; i < ci->children_num; i++)
59 oconfig_item_t *child;
61 child = ci->children + i;
63 if (strcasecmp ("Host", child->key) == 0)
64 graph_config_get_string (child, &host);
65 else if (strcasecmp ("Plugin", child->key) == 0)
66 graph_config_get_string (child, &plugin);
67 else if (strcasecmp ("PluginInstance", child->key) == 0)
68 graph_config_get_string (child, &plugin_instance);
69 else if (strcasecmp ("Type", child->key) == 0)
70 graph_config_get_string (child, &type);
71 else if (strcasecmp ("TypeInstance", child->key) == 0)
72 graph_config_get_string (child, &type_instance);
73 /* else: ignore all other directives here. */
76 ret = ident_create (host, plugin, plugin_instance, type, type_instance);
80 free (plugin_instance);
85 } /* }}} int graph_config_get_selector */
90 graph_config_t *graph_create (const graph_ident_t *selector) /* {{{ */
94 cfg = malloc (sizeof (*cfg));
97 memset (cfg, 0, sizeof (*cfg));
100 cfg->select = ident_clone (selector);
105 cfg->vertical_label = NULL;
107 cfg->instances = NULL;
110 } /* }}} int graph_create */
112 void graph_destroy (graph_config_t *cfg) /* {{{ */
119 ident_destroy (cfg->select);
122 free (cfg->vertical_label);
124 def_destroy (cfg->defs);
126 for (i = 0; i < cfg->instances_num; i++)
127 inst_destroy (cfg->instances[i]);
128 free (cfg->instances);
129 } /* }}} void graph_destroy */
131 int graph_config_add (const oconfig_item_t *ci) /* {{{ */
133 graph_ident_t *select;
134 graph_config_t *cfg = NULL;
137 select = graph_config_get_selector (ci);
141 cfg = graph_create (/* selector = */ NULL);
145 cfg->select = select;
147 for (i = 0; i < ci->children_num; i++)
149 oconfig_item_t *child;
151 child = ci->children + i;
153 if (strcasecmp ("Title", child->key) == 0)
154 graph_config_get_string (child, &cfg->title);
155 else if (strcasecmp ("VerticalLabel", child->key) == 0)
156 graph_config_get_string (child, &cfg->vertical_label);
157 else if (strcasecmp ("ShowZero", child->key) == 0)
158 graph_config_get_bool (child, &cfg->show_zero);
159 else if (strcasecmp ("DEF", child->key) == 0)
160 def_config (cfg, child);
166 } /* }}} graph_config_add */
168 int graph_add_file (graph_config_t *cfg, const graph_ident_t *file) /* {{{ */
170 graph_instance_t *inst;
172 inst = graph_inst_find_matching (cfg, file);
175 graph_instance_t **tmp;
177 tmp = realloc (cfg->instances,
178 sizeof (*cfg->instances) * (cfg->instances_num + 1));
181 cfg->instances = tmp;
183 inst = inst_create (cfg, file);
187 cfg->instances[cfg->instances_num] = inst;
188 cfg->instances_num++;
191 return (inst_add_file (inst, file));
192 } /* }}} int graph_add_file */
194 int graph_get_title (graph_config_t *cfg, /* {{{ */
195 char *buffer, size_t buffer_size)
197 if ((cfg == NULL) || (buffer == NULL) || (buffer_size < 1))
200 if (cfg->title == NULL)
201 cfg->title = ident_to_string (cfg->select);
203 if (cfg->title == NULL)
206 strncpy (buffer, cfg->title, buffer_size);
207 buffer[buffer_size - 1] = 0;
210 } /* }}} int graph_get_title */
212 int graph_get_params (graph_config_t *cfg, /* {{{ */
213 char *buffer, size_t buffer_size)
217 #define COPY_FIELD(field) do { \
218 const char *str = ident_get_##field (cfg->select); \
219 char uri_str[1024]; \
220 uri_escape_copy (uri_str, str, sizeof (uri_str)); \
221 strlcat (buffer, #field, buffer_size); \
222 strlcat (buffer, "=", buffer_size); \
223 strlcat (buffer, uri_str, buffer_size); \
227 strlcat (buffer, ";", buffer_size);
229 strlcat (buffer, ";", buffer_size);
230 COPY_FIELD(plugin_instance);
231 strlcat (buffer, ";", buffer_size);
233 strlcat (buffer, ";", buffer_size);
234 COPY_FIELD(type_instance);
239 } /* }}} int graph_get_params */
241 graph_ident_t *graph_get_selector (graph_config_t *cfg) /* {{{ */
246 return (ident_clone (cfg->select));
247 } /* }}} graph_ident_t *graph_get_selector */
249 graph_def_t *graph_get_defs (graph_config_t *cfg) /* {{{ */
255 } /* }}} graph_def_t *graph_get_defs */
257 int graph_add_def (graph_config_t *cfg, graph_def_t *def) /* {{{ */
259 if ((cfg == NULL) || (def == NULL))
262 if (cfg->defs == NULL)
268 return (def_append (cfg->defs, def));
269 } /* }}} int graph_add_def */
271 _Bool graph_matches_ident (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */
273 if ((cfg == NULL) || (ident == NULL))
276 return (ident_matches (cfg->select, ident));
277 } /* }}} _Bool graph_matches_ident */
279 _Bool graph_matches_field (graph_config_t *cfg, /* {{{ */
280 graph_ident_field_t field, const char *field_value)
282 const char *selector_value;
284 if ((cfg == NULL) || (field_value == NULL))
287 selector_value = ident_get_field (cfg->select, field);
288 if (selector_value == NULL)
291 if (IS_ALL (selector_value) || IS_ANY (selector_value))
293 else if (strcasecmp (selector_value, field_value) == 0)
297 } /* }}} _Bool graph_matches_field */
299 int graph_inst_foreach (graph_config_t *cfg, /* {{{ */
300 inst_callback_t cb, void *user_data)
305 for (i = 0; i < cfg->instances_num; i++)
307 status = (*cb) (cfg->instances[i], user_data);
313 } /* }}} int graph_inst_foreach */
315 graph_instance_t *graph_inst_find_exact (graph_config_t *cfg, /* {{{ */
316 graph_ident_t *ident)
320 if ((cfg == NULL) || (ident == NULL))
323 for (i = 0; i < cfg->instances_num; i++)
324 if (inst_compare_ident (cfg->instances[i], ident) == 0)
325 return (cfg->instances[i]);
328 } /* }}} graph_instance_t *graph_inst_find_exact */
330 graph_instance_t *graph_inst_find_matching (graph_config_t *cfg, /* {{{ */
331 const graph_ident_t *ident)
335 if ((cfg == NULL) || (ident == NULL))
338 for (i = 0; i < cfg->instances_num; i++)
339 if (inst_matches_ident (cfg->instances[i], ident))
340 return (cfg->instances[i]);
343 } /* }}} graph_instance_t *graph_inst_find_matching */
345 int graph_inst_search (graph_config_t *cfg, const char *term, /* {{{ */
346 graph_inst_callback_t cb,
353 status = graph_get_title (cfg, buffer, sizeof (buffer));
356 fprintf (stderr, "graph_inst_search: graph_get_title failed\n");
362 if (strstr (buffer, term) != NULL)
364 for (i = 0; i < cfg->instances_num; i++)
366 status = (*cb) (cfg, cfg->instances[i], user_data);
373 for (i = 0; i < cfg->instances_num; i++)
375 if (inst_matches_string (cfg, cfg->instances[i], term))
377 status = (*cb) (cfg, cfg->instances[i], user_data);
385 } /* }}} int graph_inst_search */
387 int graph_inst_search_field (graph_config_t *cfg, /* {{{ */
388 graph_ident_field_t field, const char *field_value,
389 graph_inst_callback_t callback, void *user_data)
392 const char *selector_field;
393 _Bool need_check_instances = 0;
395 if ((cfg == NULL) || (field_value == NULL) || (callback == NULL))
398 if (!graph_matches_field (cfg, field, field_value))
401 selector_field = ident_get_field (cfg->select, field);
402 if (selector_field == NULL)
405 if (IS_ALL (selector_field) || IS_ANY (selector_field))
406 need_check_instances = 1;
408 for (i = 0; i < cfg->instances_num; i++)
412 if (need_check_instances
413 && !inst_matches_field (cfg->instances[i], field, field_value))
416 status = (*callback) (cfg, cfg->instances[i], user_data);
422 } /* }}} int graph_inst_search_field */
424 int graph_compare (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */
426 if ((cfg == NULL) || (ident == NULL))
429 return (ident_compare (cfg->select, ident));
430 } /* }}} int graph_compare */
432 int graph_clear_instances (graph_config_t *cfg) /* {{{ */
439 for (i = 0; i < cfg->instances_num; i++)
440 inst_destroy (cfg->instances[i]);
441 free (cfg->instances);
442 cfg->instances = NULL;
443 cfg->instances_num = 0;
446 } /* }}} int graph_clear_instances */
448 int graph_get_rrdargs (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
451 if ((cfg == NULL) || (inst == NULL) || (args == NULL))
454 if (cfg->title != NULL)
456 array_append (args, "-t");
457 array_append (args, cfg->title);
460 if (cfg->vertical_label != NULL)
462 array_append (args, "-v");
463 array_append (args, cfg->vertical_label);
468 array_append (args, "-l");
469 array_append (args, "0");
473 } /* }}} int graph_get_rrdargs */
475 /* vim: set sw=2 sts=2 et fdm=marker : */