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_instance_s /* {{{ */
43 graph_ident_t *select;
45 graph_ident_t **files;
48 graph_instance_t *next;
49 }; /* }}} struct graph_instance_s */
51 struct graph_config_s /* {{{ */
53 graph_ident_t *select;
60 graph_instance_t *instances;
63 }; /* }}} struct graph_config_s */
65 struct def_callback_data_s
67 graph_instance_t *inst;
70 typedef struct def_callback_data_s def_callback_data_t;
75 static graph_config_t *graph_config_head = NULL;
76 static graph_config_t *graph_config_staging = NULL;
78 static time_t gl_last_update = 0;
83 /* FIXME: These "print_*" functions are used for debugging. They should be
84 * removed at some point. */
85 static int print_files (const graph_instance_t *inst) /* {{{ */
89 for (i = 0; i < inst->files_num; i++)
91 graph_ident_t *ident = inst->files[i];
94 file = ident_to_file (ident);
95 printf (" File \"%s\"\n", file);
100 } /* }}} int print_instances */
102 static int print_instances (const graph_config_t *cfg) /* {{{ */
104 graph_instance_t *inst;
106 for (inst = cfg->instances; inst != NULL; inst = inst->next)
110 str = ident_to_string (inst->select);
111 printf (" Instance \"%s\"\n", str);
118 } /* }}} int print_instances */
120 static int print_graphs (void) /* {{{ */
124 for (cfg = graph_config_head; cfg != NULL; cfg = cfg->next)
128 str = ident_to_string (cfg->select);
129 printf ("Graph \"%s\"\n", str);
132 print_instances (cfg);
136 } /* }}} int print_graphs */
139 /* "Safe" version of strcmp(3): Either or both pointers may be NULL. */
140 static int strcmp_s (const char *s1, const char *s2) /* {{{ */
142 if ((s1 == NULL) && (s2 == NULL))
148 assert ((s1 != NULL) && (s2 != NULL));
150 return (strcmp (s1, s2));
151 } /* }}} int strcmp_s */
154 static void instance_destroy (graph_instance_t *inst) /* {{{ */
156 graph_instance_t *next;
164 ident_destroy (inst->select);
166 for (i = 0; i < inst->files_num; i++)
167 ident_destroy (inst->files[i]);
172 instance_destroy (next);
173 } /* }}} void instance_destroy */
176 * Copy part of an identifier. If the "template" value is ANY_TOKEN, "value" is
177 * copied and returned. This function is used when creating graph_instance_t
178 * from graph_config_t.
180 static graph_instance_t *instance_create (graph_config_t *cfg, /* {{{ */
181 const graph_ident_t *file)
185 if ((cfg == NULL) || (file == NULL))
188 i = malloc (sizeof (*i));
191 memset (i, 0, sizeof (*i));
193 i->select = ident_copy_with_selector (cfg->select, file,
194 IDENT_FLAG_REPLACE_ANY);
195 if (i->select == NULL)
197 DEBUG ("instance_create: ident_copy_with_selector returned NULL.\n");
207 if (cfg->instances == NULL)
211 graph_instance_t *last;
213 last = cfg->instances;
214 while (last->next != NULL)
221 } /* }}} graph_instance_t *instance_create */
223 static int instance_add_file (graph_instance_t *inst, /* {{{ */
224 const graph_ident_t *file)
228 tmp = realloc (inst->files, sizeof (*inst->files) * (inst->files_num + 1));
233 inst->files[inst->files_num] = ident_clone (file);
234 if (inst->files[inst->files_num] == NULL)
240 } /* }}} int instance_add_file */
242 static graph_instance_t *graph_find_instance (graph_config_t *cfg, /* {{{ */
243 const graph_ident_t *file)
247 if ((cfg == NULL) || (file == NULL))
250 for (i = cfg->instances; i != NULL; i = i->next)
251 if (ident_matches (i->select, file))
255 } /* }}} graph_instance_t *graph_find_instance */
257 static int graph_add_file (graph_config_t *cfg, const graph_ident_t *file) /* {{{ */
259 graph_instance_t *inst;
261 inst = graph_find_instance (cfg, file);
264 inst = instance_create (cfg, file);
269 return (instance_add_file (inst, file));
270 } /* }}} int graph_add_file */
272 static int graph_append (graph_config_t **head, /* {{{ */
275 graph_config_t *last;
281 head = &graph_config_head;
290 while (last->next != NULL)
296 } /* }}} int graph_append */
298 static graph_config_t *graph_create (const graph_ident_t *selector) /* {{{ */
302 cfg = malloc (sizeof (*cfg));
305 memset (cfg, 0, sizeof (*cfg));
307 if (selector != NULL)
308 cfg->select = ident_clone (selector);
313 cfg->vertical_label = NULL;
315 cfg->instances = NULL;
319 } /* }}} int graph_create */
321 static void graph_destroy (graph_config_t *cfg) /* {{{ */
323 graph_config_t *next;
330 ident_destroy (cfg->select);
333 free (cfg->vertical_label);
335 def_destroy (cfg->defs);
336 instance_destroy (cfg->instances);
338 graph_destroy (next);
339 } /* }}} void graph_destroy */
341 static int register_file (const graph_ident_t *file) /* {{{ */
346 for (cfg = graph_config_head; cfg != NULL; cfg = cfg->next)
350 if (!ident_matches (cfg->select, file))
353 status = graph_add_file (cfg, file);
366 cfg = graph_create (file);
367 graph_append (/* head = */ NULL, cfg);
368 graph_add_file (cfg, file);
372 } /* }}} int register_file */
374 static int callback_type (const char *type, void *user_data) /* {{{ */
376 gl_ident_stage_t *gl;
377 graph_ident_t *ident;
380 if ((type == NULL) || (user_data == NULL))
384 if ((gl->type != NULL) || (gl->type_instance != NULL))
387 gl->type = strdup (type);
388 if (gl->type == NULL)
391 gl->type_instance = strchr (gl->type, '-');
392 if (gl->type_instance != NULL)
394 *gl->type_instance = 0;
399 gl->type_instance = gl->type + strlen (gl->type);
402 ident = ident_create (gl->host,
403 gl->plugin, gl->plugin_instance,
404 gl->type, gl->type_instance);
411 status = register_file (ident);
412 ident_destroy (ident);
417 gl->type_instance = NULL;
420 } /* }}} int callback_type */
422 static int callback_plugin (const char *plugin, void *user_data) /* {{{ */
424 gl_ident_stage_t *gl;
427 if ((plugin == NULL) || (user_data == NULL))
431 if ((gl->plugin != NULL) || (gl->plugin_instance != NULL))
434 gl->plugin = strdup (plugin);
435 if (gl->plugin == NULL)
438 gl->plugin_instance = strchr (gl->plugin, '-');
439 if (gl->plugin_instance != NULL)
441 *gl->plugin_instance = 0;
442 gl->plugin_instance++;
446 gl->plugin_instance = gl->plugin + strlen (gl->plugin);
449 status = foreach_type (gl->host, plugin, callback_type, gl);
453 gl->plugin_instance = NULL;
456 } /* }}} int callback_plugin */
458 static int callback_host (const char *host, void *user_data) /* {{{ */
460 gl_ident_stage_t *gl;
463 if ((host == NULL) || (user_data == NULL))
467 if (gl->host != NULL)
470 gl->host = strdup (host);
471 if (gl->host == NULL)
474 status = foreach_plugin (host, callback_plugin, gl);
480 } /* }}} int callback_host */
482 static const char *get_part_from_param (const char *prim_key, /* {{{ */
487 val = param (prim_key);
491 return (param (sec_key));
492 } /* }}} const char *get_part_from_param */
494 /* Create one DEF for each data source in the file. Called by
495 * "gl_inst_get_default_defs" for each file. */
496 static graph_def_t *gl_ident_get_default_defs (graph_config_t *cfg, /* {{{ */
497 graph_ident_t *ident, graph_def_t *def_head)
499 graph_def_t *defs = NULL;
506 if ((cfg == NULL) || (ident == NULL))
509 file = ident_to_file (ident);
512 DEBUG ("gl_ident_get_default_defs: ident_to_file returned NULL.\n");
516 DEBUG ("gl_ident_get_default_defs: file = %s;\n", file);
518 status = ds_list_from_rrd_file (file, &dses_num, &dses);
525 for (i = 0; i < dses_num; i++)
529 def = def_search (def_head, ident, dses[i]);
533 def = def_create (cfg, ident, dses[i]);
540 def_append (defs, def);
549 } /* }}} int gl_ident_get_default_defs */
551 /* Create one or more DEFs for each file in the graph instance. The number
552 * depends on the number of data sources in each of the files. Called from
553 * "gl_instance_get_rrdargs" if no DEFs are available from the configuration.
555 static graph_def_t *gl_inst_get_default_defs (graph_config_t *cfg, /* {{{ */
556 graph_instance_t *inst)
558 graph_def_t *defs = NULL;
561 if ((cfg == NULL) || (inst == NULL))
564 for (i = 0; i < inst->files_num; i++)
568 def = gl_ident_get_default_defs (cfg, inst->files[i], defs);
575 def_append (defs, def);
579 } /* }}} graph_def_t *gl_inst_get_default_defs */
581 /* Called with each DEF in turn. Calls "def_get_rrdargs" with every appropriate
582 * file / DEF pair. */
583 static int gl_instance_get_rrdargs_cb (graph_def_t *def, void *user_data) /* {{{ */
585 def_callback_data_t *data = user_data;
586 graph_instance_t *inst = data->inst;
587 str_array_t *args = data->args;
591 for (i = 0; i < inst->files_num; i++)
593 if (!def_matches (def, inst->files[i]))
596 def_get_rrdargs (def, inst->files[i], args);
600 } /* }}} int gl_instance_get_rrdargs_cb */
605 static int config_get_string (const oconfig_item_t *ci, char **ret_str)
609 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
612 tmp = strdup (ci->values[0].value.string);
620 } /* }}} int config_get_string */
625 int graph_config_add (const oconfig_item_t *ci)
629 char *plugin_instance = NULL;
631 char *type_instance = NULL;
632 graph_config_t *cfg = NULL;
635 cfg = graph_create (/* selector = */ NULL);
639 for (i = 0; i < ci->children_num; i++)
641 oconfig_item_t *child;
643 child = ci->children + i;
645 if (strcasecmp ("Host", child->key) == 0)
646 config_get_string (child, &host);
647 else if (strcasecmp ("Plugin", child->key) == 0)
648 config_get_string (child, &plugin);
649 else if (strcasecmp ("PluginInstance", child->key) == 0)
650 config_get_string (child, &plugin_instance);
651 else if (strcasecmp ("Type", child->key) == 0)
652 config_get_string (child, &type);
653 else if (strcasecmp ("TypeInstance", child->key) == 0)
654 config_get_string (child, &type_instance);
655 else if (strcasecmp ("Title", child->key) == 0)
656 config_get_string (child, &cfg->title);
657 else if (strcasecmp ("VerticalLabel", child->key) == 0)
658 config_get_string (child, &cfg->vertical_label);
662 cfg->select = ident_create (host, plugin, plugin_instance,
663 type, type_instance);
664 if (cfg->select == NULL)
670 graph_append (&graph_config_staging, cfg);
673 } /* }}} graph_config_add */
675 int graph_config_submit (void) /* {{{ */
679 tmp = graph_config_head;
680 graph_config_head = graph_config_staging;
681 graph_config_staging = NULL;
686 } /* }}} int graph_config_submit */
688 int gl_instance_get_params (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
689 char *buffer, size_t buffer_size)
691 if ((inst == NULL) || (buffer == NULL) || (buffer_size < 1))
696 #define COPY_FIELD(field) do { \
697 const char *cfg_f = ident_get_##field (cfg->select); \
698 const char *inst_f = ident_get_##field (inst->select); \
699 if (strcmp (cfg_f, inst_f) == 0) \
701 strlcat (buffer, #field, buffer_size); \
702 strlcat (buffer, "=", buffer_size); \
703 strlcat (buffer, cfg_f, buffer_size); \
707 strlcat (buffer, "graph_", buffer_size); \
708 strlcat (buffer, #field, buffer_size); \
709 strlcat (buffer, "=", buffer_size); \
710 strlcat (buffer, cfg_f, buffer_size); \
711 strlcat (buffer, ";", buffer_size); \
712 strlcat (buffer, "inst_", buffer_size); \
713 strlcat (buffer, #field, buffer_size); \
714 strlcat (buffer, "=", buffer_size); \
715 strlcat (buffer, inst_f, buffer_size); \
720 strlcat (buffer, ";", buffer_size);
722 strlcat (buffer, ";", buffer_size);
723 COPY_FIELD(plugin_instance);
724 strlcat (buffer, ";", buffer_size);
726 strlcat (buffer, ";", buffer_size);
727 COPY_FIELD(type_instance);
732 } /* }}} int gl_instance_get_params */
734 graph_instance_t *inst_get_selected (graph_config_t *cfg) /* {{{ */
736 const char *host = get_part_from_param ("inst_host", "host");
737 const char *plugin = get_part_from_param ("inst_plugin", "plugin");
738 const char *plugin_instance = get_part_from_param ("inst_plugin_instance", "plugin_instance");
739 const char *type = get_part_from_param ("inst_type", "type");
740 const char *type_instance = get_part_from_param ("inst_type_instance", "type_instance");
741 graph_ident_t *ident;
742 graph_instance_t *inst;
745 cfg = graph_get_selected ();
749 DEBUG ("inst_get_selected: cfg == NULL;\n");
754 || (plugin == NULL) || (plugin_instance == NULL)
755 || (type == NULL) || (type_instance == NULL))
757 DEBUG ("inst_get_selected: A parameter is NULL.\n");
761 ident = ident_create (host, plugin, plugin_instance, type, type_instance);
763 for (inst = cfg->instances; inst != NULL; inst = inst->next)
765 if (ident_compare (ident, inst->select) != 0)
768 ident_destroy (ident);
772 DEBUG ("inst_get_selected: No match found.\n");
773 ident_destroy (ident);
775 } /* }}} graph_instance_t *inst_get_selected */
777 int gl_graph_get_all (gl_cfg_callback callback, /* {{{ */
782 if (callback == NULL)
787 for (cfg = graph_config_head; cfg != NULL; cfg = cfg->next)
791 status = (*callback) (cfg, user_data);
797 } /* }}} int gl_graph_get_all */
799 graph_config_t *graph_get_selected (void) /* {{{ */
801 const char *host = get_part_from_param ("graph_host", "host");
802 const char *plugin = get_part_from_param ("graph_plugin", "plugin");
803 const char *plugin_instance = get_part_from_param ("graph_plugin_instance", "plugin_instance");
804 const char *type = get_part_from_param ("graph_type", "type");
805 const char *type_instance = get_part_from_param ("graph_type_instance", "type_instance");
806 graph_ident_t *ident;
810 || (plugin == NULL) || (plugin_instance == NULL)
811 || (type == NULL) || (type_instance == NULL))
814 ident = ident_create (host, plugin, plugin_instance, type, type_instance);
818 for (cfg = graph_config_head; cfg != NULL; cfg = cfg->next)
820 if (ident_compare (ident, cfg->select) != 0)
823 ident_destroy (ident);
827 ident_destroy (ident);
829 } /* }}} graph_config_t *graph_get_selected */
831 int gl_graph_instance_get_all (graph_config_t *cfg, /* {{{ */
832 gl_inst_callback callback, void *user_data)
834 graph_instance_t *inst;
836 if ((cfg == NULL) || (callback == NULL))
839 for (inst = cfg->instances; inst != NULL; inst = inst->next)
843 status = (*callback) (cfg, inst, user_data);
849 } /* }}} int gl_graph_instance_get_all */
851 int gl_graph_get_title (graph_config_t *cfg, /* {{{ */
852 char *buffer, size_t buffer_size)
854 if ((cfg == NULL) || (buffer == NULL) || (buffer_size < 1))
857 if (cfg->title == NULL)
858 cfg->title = ident_to_string (cfg->select);
860 if (cfg->title == NULL)
863 strncpy (buffer, cfg->title, buffer_size);
864 buffer[buffer_size - 1] = 0;
867 } /* }}} int gl_graph_get_title */
869 graph_ident_t *gl_graph_get_selector (graph_config_t *cfg) /* {{{ */
874 return (ident_clone (cfg->select));
875 } /* }}} graph_ident_t *gl_graph_get_selector */
877 int gl_instance_get_all (gl_inst_callback callback, /* {{{ */
884 for (cfg = graph_config_head; cfg != NULL; cfg = cfg->next)
886 graph_instance_t *inst;
888 for (inst = cfg->instances; inst != NULL; inst = inst->next)
892 status = (*callback) (cfg, inst, user_data);
899 } /* }}} int gl_instance_get_all */
901 int gl_instance_get_rrdargs (graph_config_t *cfg, /* {{{ */
902 graph_instance_t *inst,
905 def_callback_data_t data = { inst, args };
906 graph_def_t *default_defs;
909 if ((cfg == NULL) || (inst == NULL) || (args == NULL))
912 if (cfg->title != NULL)
914 array_append (args, "-t");
915 array_append (args, cfg->title);
918 if (cfg->vertical_label != NULL)
920 array_append (args, "-v");
921 array_append (args, cfg->vertical_label);
924 if (cfg->defs == NULL)
926 default_defs = gl_inst_get_default_defs (cfg, inst);
928 if (default_defs == NULL)
931 status = def_foreach (default_defs, gl_instance_get_rrdargs_cb, &data);
933 if (default_defs != NULL)
934 def_destroy (default_defs);
938 status = def_foreach (cfg->defs, gl_instance_get_rrdargs_cb, &data);
942 } /* }}} int gl_instance_get_rrdargs */
944 graph_ident_t *gl_instance_get_selector (graph_instance_t *inst) /* {{{ */
949 return (ident_clone (inst->select));
950 } /* }}} graph_ident_t *gl_instance_get_selector */
954 int gl_update (void) /* {{{ */
961 printf ("Content-Type: text/plain\n\n");
966 if ((gl_last_update + UPDATE_INTERVAL) >= now)
969 graph_read_config ();
971 memset (&gl, 0, sizeof (gl));
974 gl.plugin_instance = NULL;
976 gl.type_instance = NULL;
978 status = foreach_host (callback_host, &gl);
980 gl_last_update = now;
983 } /* }}} int gl_update */
985 /* vim: set sw=2 sts=2 et fdm=marker : */