10 #include "graph_list.h"
11 #include "graph_ident.h"
12 #include "graph_def.h"
13 #include "graph_config.h"
15 #include "utils_params.h"
18 #include <fcgi_stdio.h>
23 #define UPDATE_INTERVAL 10
25 #define ANY_TOKEN "/any/"
26 #define ALL_TOKEN "/all/"
31 struct gl_ident_stage_s /* {{{ */
35 char *plugin_instance;
39 typedef struct gl_ident_stage_s gl_ident_stage_t;
41 struct graph_config_s /* {{{ */
43 graph_ident_t *select;
50 graph_instance_t *instances;
53 }; /* }}} struct graph_config_s */
58 static graph_config_t *graph_config_head = NULL;
59 static graph_config_t *graph_config_staging = NULL;
61 static time_t gl_last_update = 0;
67 /* "Safe" version of strcmp(3): Either or both pointers may be NULL. */
68 static int strcmp_s (const char *s1, const char *s2) /* {{{ */
70 if ((s1 == NULL) && (s2 == NULL))
76 assert ((s1 != NULL) && (s2 != NULL));
78 return (strcmp (s1, s2));
79 } /* }}} int strcmp_s */
83 * Copy part of an identifier. If the "template" value is ANY_TOKEN, "value" is
84 * copied and returned. This function is used when creating graph_instance_t
85 * from graph_config_t.
88 static graph_instance_t *graph_find_instance (graph_config_t *cfg, /* {{{ */
89 const graph_ident_t *ident)
91 if ((cfg == NULL) || (ident == NULL))
94 return (inst_find_matching (cfg->instances, ident));
95 } /* }}} graph_instance_t *graph_find_instance */
97 static int graph_add_file (graph_config_t *cfg, const graph_ident_t *file) /* {{{ */
99 graph_instance_t *inst;
101 inst = graph_find_instance (cfg, file);
104 inst = inst_create (cfg, file);
108 if (cfg->instances == NULL)
109 cfg->instances = inst;
111 inst_append (cfg->instances, inst);
114 return (inst_add_file (inst, file));
115 } /* }}} int graph_add_file */
117 static int graph_append (graph_config_t **head, /* {{{ */
120 graph_config_t *last;
126 head = &graph_config_head;
135 while (last->next != NULL)
141 } /* }}} int graph_append */
143 static graph_config_t *graph_create (const graph_ident_t *selector) /* {{{ */
147 cfg = malloc (sizeof (*cfg));
150 memset (cfg, 0, sizeof (*cfg));
152 if (selector != NULL)
153 cfg->select = ident_clone (selector);
158 cfg->vertical_label = NULL;
160 cfg->instances = NULL;
164 } /* }}} int graph_create */
166 static void graph_destroy (graph_config_t *cfg) /* {{{ */
168 graph_config_t *next;
175 ident_destroy (cfg->select);
178 free (cfg->vertical_label);
180 def_destroy (cfg->defs);
181 inst_destroy (cfg->instances);
183 graph_destroy (next);
184 } /* }}} void graph_destroy */
186 static int register_file (const graph_ident_t *file) /* {{{ */
191 for (cfg = graph_config_head; cfg != NULL; cfg = cfg->next)
195 if (!ident_matches (cfg->select, file))
198 status = graph_add_file (cfg, file);
211 cfg = graph_create (file);
212 graph_append (/* head = */ NULL, cfg);
213 graph_add_file (cfg, file);
217 } /* }}} int register_file */
219 static int callback_type (const char *type, void *user_data) /* {{{ */
221 gl_ident_stage_t *gl;
222 graph_ident_t *ident;
225 if ((type == NULL) || (user_data == NULL))
229 if ((gl->type != NULL) || (gl->type_instance != NULL))
232 gl->type = strdup (type);
233 if (gl->type == NULL)
236 gl->type_instance = strchr (gl->type, '-');
237 if (gl->type_instance != NULL)
239 *gl->type_instance = 0;
244 gl->type_instance = gl->type + strlen (gl->type);
247 ident = ident_create (gl->host,
248 gl->plugin, gl->plugin_instance,
249 gl->type, gl->type_instance);
256 status = register_file (ident);
257 ident_destroy (ident);
262 gl->type_instance = NULL;
265 } /* }}} int callback_type */
267 static int callback_plugin (const char *plugin, void *user_data) /* {{{ */
269 gl_ident_stage_t *gl;
272 if ((plugin == NULL) || (user_data == NULL))
276 if ((gl->plugin != NULL) || (gl->plugin_instance != NULL))
279 gl->plugin = strdup (plugin);
280 if (gl->plugin == NULL)
283 gl->plugin_instance = strchr (gl->plugin, '-');
284 if (gl->plugin_instance != NULL)
286 *gl->plugin_instance = 0;
287 gl->plugin_instance++;
291 gl->plugin_instance = gl->plugin + strlen (gl->plugin);
294 status = foreach_type (gl->host, plugin, callback_type, gl);
298 gl->plugin_instance = NULL;
301 } /* }}} int callback_plugin */
303 static int callback_host (const char *host, void *user_data) /* {{{ */
305 gl_ident_stage_t *gl;
308 if ((host == NULL) || (user_data == NULL))
312 if (gl->host != NULL)
315 gl->host = strdup (host);
316 if (gl->host == NULL)
319 status = foreach_plugin (host, callback_plugin, gl);
325 } /* }}} int callback_host */
327 static const char *get_part_from_param (const char *prim_key, /* {{{ */
332 val = param (prim_key);
336 return (param (sec_key));
337 } /* }}} const char *get_part_from_param */
339 static int gl_clear_instances (void) /* {{{ */
343 for (cfg = graph_config_head; cfg != NULL; cfg = cfg->next)
345 inst_destroy (cfg->instances);
346 cfg->instances = NULL;
350 } /* }}} int gl_clear_instances */
356 static graph_ident_t *graph_config_get_selector (const oconfig_item_t *ci) /* {{{ */
360 char *plugin_instance = NULL;
362 char *type_instance = NULL;
366 for (i = 0; i < ci->children_num; i++)
368 oconfig_item_t *child;
370 child = ci->children + i;
372 if (strcasecmp ("Host", child->key) == 0)
373 graph_config_get_string (child, &host);
374 else if (strcasecmp ("Plugin", child->key) == 0)
375 graph_config_get_string (child, &plugin);
376 else if (strcasecmp ("PluginInstance", child->key) == 0)
377 graph_config_get_string (child, &plugin_instance);
378 else if (strcasecmp ("Type", child->key) == 0)
379 graph_config_get_string (child, &type);
380 else if (strcasecmp ("TypeInstance", child->key) == 0)
381 graph_config_get_string (child, &type_instance);
382 /* else: ignore all other directives here. */
385 ret = ident_create (host, plugin, plugin_instance, type, type_instance);
389 free (plugin_instance);
391 free (type_instance);
394 } /* }}} int graph_config_get_selector */
399 int graph_config_add (const oconfig_item_t *ci) /* {{{ */
401 graph_ident_t *select;
402 graph_config_t *cfg = NULL;
405 select = graph_config_get_selector (ci);
409 cfg = graph_create (/* selector = */ NULL);
413 cfg->select = select;
415 for (i = 0; i < ci->children_num; i++)
417 oconfig_item_t *child;
419 child = ci->children + i;
421 if (strcasecmp ("Title", child->key) == 0)
422 graph_config_get_string (child, &cfg->title);
423 else if (strcasecmp ("VerticalLabel", child->key) == 0)
424 graph_config_get_string (child, &cfg->vertical_label);
425 else if (strcasecmp ("DEF", child->key) == 0)
426 def_config (cfg, child);
429 graph_append (&graph_config_staging, cfg);
432 } /* }}} graph_config_add */
434 int graph_config_submit (void) /* {{{ */
438 tmp = graph_config_head;
439 graph_config_head = graph_config_staging;
440 graph_config_staging = NULL;
445 } /* }}} int graph_config_submit */
447 int gl_graph_get_all (gl_cfg_callback callback, /* {{{ */
452 if (callback == NULL)
457 for (cfg = graph_config_head; cfg != NULL; cfg = cfg->next)
461 status = (*callback) (cfg, user_data);
467 } /* }}} int gl_graph_get_all */
469 graph_config_t *graph_get_selected (void) /* {{{ */
471 const char *host = get_part_from_param ("graph_host", "host");
472 const char *plugin = get_part_from_param ("graph_plugin", "plugin");
473 const char *plugin_instance = get_part_from_param ("graph_plugin_instance", "plugin_instance");
474 const char *type = get_part_from_param ("graph_type", "type");
475 const char *type_instance = get_part_from_param ("graph_type_instance", "type_instance");
476 graph_ident_t *ident;
480 || (plugin == NULL) || (plugin_instance == NULL)
481 || (type == NULL) || (type_instance == NULL))
484 ident = ident_create (host, plugin, plugin_instance, type, type_instance);
488 for (cfg = graph_config_head; cfg != NULL; cfg = cfg->next)
490 if (ident_compare (ident, cfg->select) != 0)
493 ident_destroy (ident);
497 ident_destroy (ident);
499 } /* }}} graph_config_t *graph_get_selected */
501 /* gl_instance_get_all, gl_graph_instance_get_all {{{ */
502 struct gl_inst_callback_data /* {{{ */
505 gl_inst_callback callback;
507 }; /* }}} struct gl_inst_callback_data */
509 static int gl_inst_callback_handler (graph_instance_t *inst, /* {{{ */
512 struct gl_inst_callback_data *data = user_data;
514 return ((*data->callback) (data->cfg, inst, data->user_data));
515 } /* }}} int gl_inst_callback_handler */
517 int gl_graph_instance_get_all (graph_config_t *cfg, /* {{{ */
518 gl_inst_callback callback, void *user_data)
520 struct gl_inst_callback_data data =
527 if ((cfg == NULL) || (callback == NULL))
530 return (inst_foreach (cfg->instances, gl_inst_callback_handler, &data));
531 } /* }}} int gl_graph_instance_get_all */
533 int gl_instance_get_all (gl_inst_callback callback, /* {{{ */
540 for (cfg = graph_config_head; cfg != NULL; cfg = cfg->next)
544 status = gl_graph_instance_get_all (cfg, callback, user_data);
550 } /* }}} int gl_instance_get_all */
551 /* }}} gl_instance_get_all, gl_graph_instance_get_all */
553 int gl_graph_get_title (graph_config_t *cfg, /* {{{ */
554 char *buffer, size_t buffer_size)
556 if ((cfg == NULL) || (buffer == NULL) || (buffer_size < 1))
559 if (cfg->title == NULL)
560 cfg->title = ident_to_string (cfg->select);
562 if (cfg->title == NULL)
565 strncpy (buffer, cfg->title, buffer_size);
566 buffer[buffer_size - 1] = 0;
569 } /* }}} int gl_graph_get_title */
571 graph_ident_t *gl_graph_get_selector (graph_config_t *cfg) /* {{{ */
576 return (ident_clone (cfg->select));
577 } /* }}} graph_ident_t *gl_graph_get_selector */
579 graph_instance_t *gl_graph_get_instances (graph_config_t *cfg) /* {{{ */
584 return (cfg->instances);
585 } /* }}} graph_instance_t *gl_graph_get_instances */
587 graph_def_t *gl_graph_get_defs (graph_config_t *cfg) /* {{{ */
593 } /* }}} graph_def_t *gl_graph_get_defs */
595 int gl_graph_add_def (graph_config_t *cfg, graph_def_t *def) /* {{{ */
597 if ((cfg == NULL) || (def == NULL))
600 if (cfg->defs == NULL)
606 return (def_append (cfg->defs, def));
607 } /* }}} int gl_graph_add_def */
609 int gl_update (void) /* {{{ */
616 printf ("Content-Type: text/plain\n\n");
621 if ((gl_last_update + UPDATE_INTERVAL) >= now)
624 graph_read_config ();
626 memset (&gl, 0, sizeof (gl));
629 gl.plugin_instance = NULL;
631 gl.type_instance = NULL;
633 gl_clear_instances ();
634 status = foreach_host (callback_host, &gl);
636 gl_last_update = now;
639 } /* }}} int gl_update */
641 /* vim: set sw=2 sts=2 et fdm=marker : */