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 (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 */
278 int graph_inst_foreach (graph_config_t *cfg, /* {{{ */
279 inst_callback_t cb, void *user_data)
284 for (i = 0; i < cfg->instances_num; i++)
286 status = (*cb) (cfg->instances[i], user_data);
292 } /* }}} int graph_inst_foreach */
294 graph_instance_t *graph_inst_find_exact (graph_config_t *cfg, /* {{{ */
295 graph_ident_t *ident)
299 if ((cfg == NULL) || (ident == 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]);
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)
314 if ((cfg == NULL) || (ident == NULL))
317 for (i = 0; i < cfg->instances_num; i++)
318 if (inst_matches_ident (cfg->instances[i], ident))
319 return (cfg->instances[i]);
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,
332 status = graph_get_title (cfg, buffer, sizeof (buffer));
335 fprintf (stderr, "graph_inst_search: graph_get_title failed\n");
341 if (strstr (buffer, term) != NULL)
343 for (i = 0; i < cfg->instances_num; i++)
345 status = (*cb) (cfg, cfg->instances[i], user_data);
352 for (i = 0; i < cfg->instances_num; i++)
354 if (inst_matches_string (cfg, cfg->instances[i], term))
356 status = (*cb) (cfg, cfg->instances[i], user_data);
364 } /* }}} int graph_inst_search */
366 int graph_compare (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */
368 if ((cfg == NULL) || (ident == NULL))
371 return (ident_compare (cfg->select, ident));
372 } /* }}} int graph_compare */
374 int graph_clear_instances (graph_config_t *cfg) /* {{{ */
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;
388 } /* }}} int graph_clear_instances */
390 int graph_get_rrdargs (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
393 if ((cfg == NULL) || (inst == NULL) || (args == NULL))
396 if (cfg->title != NULL)
398 array_append (args, "-t");
399 array_append (args, cfg->title);
402 if (cfg->vertical_label != NULL)
404 array_append (args, "-v");
405 array_append (args, cfg->vertical_label);
410 array_append (args, "-l");
411 array_append (args, "0");
415 } /* }}} int graph_get_rrdargs */
417 /* vim: set sw=2 sts=2 et fdm=marker : */