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;
36 }; /* }}} struct graph_config_s */
41 static graph_instance_t *graph_find_instance (graph_config_t *cfg, /* {{{ */
42 const graph_ident_t *ident)
44 if ((cfg == NULL) || (ident == NULL))
47 return (inst_find_matching (cfg->instances, ident));
48 } /* }}} graph_instance_t *graph_find_instance */
53 static graph_ident_t *graph_config_get_selector (const oconfig_item_t *ci) /* {{{ */
57 char *plugin_instance = NULL;
59 char *type_instance = NULL;
63 for (i = 0; i < ci->children_num; i++)
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. */
82 ret = ident_create (host, plugin, plugin_instance, type, type_instance);
86 free (plugin_instance);
91 } /* }}} int graph_config_get_selector */
96 graph_config_t *graph_create (const graph_ident_t *selector) /* {{{ */
100 cfg = malloc (sizeof (*cfg));
103 memset (cfg, 0, sizeof (*cfg));
105 if (selector != NULL)
106 cfg->select = ident_clone (selector);
111 cfg->vertical_label = NULL;
113 cfg->instances = NULL;
116 } /* }}} int graph_create */
118 void graph_destroy (graph_config_t *cfg) /* {{{ */
123 ident_destroy (cfg->select);
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) /* {{{ */
134 graph_ident_t *select;
135 graph_config_t *cfg = NULL;
138 select = graph_config_get_selector (ci);
142 cfg = graph_create (/* selector = */ NULL);
146 cfg->select = select;
148 for (i = 0; i < ci->children_num; i++)
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);
167 } /* }}} graph_config_add */
169 int graph_add_file (graph_config_t *cfg, const graph_ident_t *file) /* {{{ */
171 graph_instance_t *inst;
173 inst = graph_find_instance (cfg, file);
176 inst = inst_create (cfg, file);
180 if (cfg->instances == NULL)
181 cfg->instances = inst;
183 inst_append (cfg->instances, inst);
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)
192 if ((cfg == NULL) || (buffer == NULL) || (buffer_size < 1))
195 if (cfg->title == NULL)
196 cfg->title = ident_to_string (cfg->select);
198 if (cfg->title == NULL)
201 strncpy (buffer, cfg->title, buffer_size);
202 buffer[buffer_size - 1] = 0;
205 } /* }}} int graph_get_title */
207 graph_ident_t *graph_get_selector (graph_config_t *cfg) /* {{{ */
212 return (ident_clone (cfg->select));
213 } /* }}} graph_ident_t *graph_get_selector */
215 graph_instance_t *graph_get_instances (graph_config_t *cfg) /* {{{ */
220 return (cfg->instances);
221 } /* }}} graph_instance_t *graph_get_instances */
223 graph_def_t *graph_get_defs (graph_config_t *cfg) /* {{{ */
229 } /* }}} graph_def_t *graph_get_defs */
231 int graph_add_def (graph_config_t *cfg, graph_def_t *def) /* {{{ */
233 if ((cfg == NULL) || (def == NULL))
236 if (cfg->defs == NULL)
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) /* {{{ */
247 if ((cfg == NULL) || (ident == NULL))
250 return (ident_matches (cfg->select, ident));
251 } /* }}} _Bool graph_matches */
253 struct graph_search_data_s
256 graph_inst_callback_t callback;
259 typedef struct graph_search_data_s graph_search_data_t;
261 static int graph_search_submit (graph_instance_t *inst, /* {{{ */
264 graph_search_data_t *data = user_data;
266 if ((inst == NULL) || (data == NULL))
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)
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,
282 graph_search_data_t data = { cfg, callback, user_data };
286 status = graph_get_title (cfg, buffer, sizeof (buffer));
289 fprintf (stderr, "graph_search: graph_get_title failed\n");
295 if (strstr (buffer, term) != NULL)
297 status = inst_foreach (cfg->instances, graph_search_submit, &data);
303 status = inst_search (cfg, cfg->instances, term,
304 graph_search_submit, &data);
310 } /* }}} int graph_search */
312 int graph_compare (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */
314 if ((cfg == NULL) || (ident == NULL))
317 return (ident_compare (cfg->select, ident));
318 } /* }}} int graph_compare */
320 int graph_clear_instances (graph_config_t *cfg) /* {{{ */
325 inst_destroy (cfg->instances);
326 cfg->instances = NULL;
329 } /* }}} int graph_clear_instances */
331 int graph_get_rrdargs (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
334 if ((cfg == NULL) || (inst == NULL) || (args == NULL))
337 if (cfg->title != NULL)
339 array_append (args, "-t");
340 array_append (args, cfg->title);
343 if (cfg->vertical_label != NULL)
345 array_append (args, "-v");
346 array_append (args, cfg->vertical_label);
351 array_append (args, "-l");
352 array_append (args, "0");
356 } /* }}} int graph_get_rrdargs */
358 /* vim: set sw=2 sts=2 et fdm=marker : */