--- /dev/null
+AUTOMAKE_OPTIONS = foreign no-dependencies
+
+if COMPILER_IS_GCC
+AM_CFLAGS = -Wall -Wextra
+endif
+AM_YFLAGS = -d
+
+BUILT_SOURCES = parser.h
+
+bin_PROGRAMS = collection.fcgi
+
+collection_fcgi_SOURCES = main.c \
+ oconfig.c oconfig.h aux_types.h scanner.l parser.y \
+ action_graph.c action_graph.h \
+ action_list_graphs.c action_list_graphs.h \
+ common.c common.h \
+ filesystem.c filesystem.h \
++ graph_types.h \
+ graph.c graph.h \
+ graph_config.c graph_config.h \
+ graph_def.c graph_def.h \
+ graph_ident.c graph_ident.h \
+ graph_instance.c graph_instance.h \
+ graph_list.c graph_list.h \
+ utils_array.c utils_array.h \
+ utils_params.c utils_params.h
--- /dev/null
- __attribute__((unused)) void *user_data)
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+
+#include "action_list_graphs.h"
+#include "graph.h"
++#include "graph_ident.h"
+#include "graph_list.h"
+#include "utils_params.h"
+
+#include <fcgiapp.h>
+#include <fcgi_stdio.h>
+
+static int print_graph_inst_json (__attribute__((unused)) graph_config_t *cfg, /* {{{ */
+ graph_instance_t *inst,
+ void *user_data)
+{
+ _Bool *first;
+ graph_ident_t *ident;
+ char *json;
+
+ first = user_data;
+
+ ident = inst_get_selector (inst);
+ if (ident == NULL)
+ return (-1);
+
+ json = ident_to_json (ident);
+ if (json == NULL)
+ {
+ ident_destroy (ident);
+ return (ENOMEM);
+ }
+
+ if (*first)
+ printf ("%s", json);
+ else
+ printf (",\n%s", json);
+
+ *first = 0;
+
+ ident_destroy (ident);
+ return (0);
+} /* }}} int print_graph_inst_json */
+
+static int print_graph_json (graph_config_t *cfg, /* {{{ */
+ void *user_data)
+{
+ return (gl_graph_instance_get_all (cfg, print_graph_inst_json, user_data));
+} /* }}} int print_graph_json */
+
+static int list_graphs_json (void) /* {{{ */
+{
+ _Bool first = 1;
+
+ printf ("Content-Type: application/json\n\n");
+
+ printf ("[\n");
+ gl_graph_get_all (print_graph_json, /* user_data = */ &first);
+ printf ("\n]");
+
+ return (0);
+} /* }}} int list_graphs_json */
+
++struct callback_data_s
++{
++ graph_config_t *cfg;
++};
++typedef struct callback_data_s callback_data_t;
++
+static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */
+ graph_instance_t *inst,
- static int print_graph_html (graph_config_t *cfg, /* {{{ */
- __attribute__((unused)) void *user_data)
- {
- char buffer[1024];
-
- memset (buffer, 0, sizeof (buffer));
- graph_get_title (cfg, buffer, sizeof (buffer));
-
- printf (" <li>%s\n <ul>\n", buffer);
- gl_graph_instance_get_all (cfg, print_graph_inst_html, /* user_data = */ NULL);
- printf (" </ul></li>\n");
-
- return (0);
- } /* }}} int print_graph_html */
-
- static int list_graphs_html (void) /* {{{ */
++ void *user_data)
+{
++ callback_data_t *data = user_data;
+ char params[1024];
+ char desc[1024];
+
++ if (data->cfg != cfg)
++ {
++ if (data->cfg != NULL)
++ printf (" </ul></li>\n");
++
++ memset (desc, 0, sizeof (desc));
++ graph_get_title (cfg, desc, sizeof (desc));
++
++ printf (" <li>%s\n <ul>\n", desc);
++
++ data->cfg = cfg;
++ }
++
+ memset (params, 0, sizeof (params));
+ inst_get_params (cfg, inst, params, sizeof (params));
+
+ memset (desc, 0, sizeof (desc));
+ inst_describe (cfg, inst, desc, sizeof (desc));
+
+ printf (" <li><a href=\"%s?action=graph;%s\">%s</a></li>\n",
+ script_name (), params, desc);
+
+ return (0);
+} /* }}} int print_graph_inst_html */
+
- gl_graph_get_all (print_graph_html, /* user_data = */ NULL);
++static int list_graphs_html (const char *term) /* {{{ */
+{
++ callback_data_t data = { NULL };
+ printf ("Content-Type: text/html\n\n");
+
+ printf ("<ul>\n");
- return (list_graphs_html ());
++ if (term == NULL)
++ gl_instance_get_all (print_graph_inst_html, /* user_data = */ &data);
++ else
++ gl_search (term, print_graph_inst_html, /* user_data = */ &data);
++
++ if (data.cfg != NULL)
++ printf (" </ul></li>\n");
++
+ printf ("</ul>\n");
+
+ return (0);
+} /* }}} int list_graphs_html */
+
+int action_list_graphs (void) /* {{{ */
+{
+ const char *format;
+
+ gl_update ();
+
+ format = param ("format");
+ if (format == NULL)
+ format = "html";
+
+ if (strcmp ("json", format) == 0)
+ return (list_graphs_json ());
+ else
++ return (list_graphs_html (param ("search")));
+} /* }}} int action_list_graphs */
+
+/* vim: set sw=2 sts=2 et fdm=marker : */
--- /dev/null
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <inttypes.h>
+#include <string.h>
+#include <time.h>
+#include <errno.h>
+#include <assert.h>
+
+#include "graph.h"
+#include "graph_list.h"
+#include "graph_ident.h"
+#include "graph_def.h"
+#include "graph_config.h"
+#include "common.h"
+#include "filesystem.h"
+#include "utils_params.h"
+
+#include <fcgiapp.h>
+#include <fcgi_stdio.h>
+
+/*
+ * Data types
+ */
+struct graph_config_s /* {{{ */
+{
+ graph_ident_t *select;
+
+ char *title;
+ char *vertical_label;
++ _Bool show_zero;
+
+ graph_def_t *defs;
+
+ graph_instance_t *instances;
+}; /* }}} struct graph_config_s */
+
+/*
+ * Private functions
+ */
+static graph_instance_t *graph_find_instance (graph_config_t *cfg, /* {{{ */
+ const graph_ident_t *ident)
+{
+ if ((cfg == NULL) || (ident == NULL))
+ return (NULL);
+
+ return (inst_find_matching (cfg->instances, ident));
+} /* }}} graph_instance_t *graph_find_instance */
+
+/*
+ * Config functions
+ */
+static graph_ident_t *graph_config_get_selector (const oconfig_item_t *ci) /* {{{ */
+{
+ char *host = NULL;
+ char *plugin = NULL;
+ char *plugin_instance = NULL;
+ char *type = NULL;
+ char *type_instance = NULL;
+ graph_ident_t *ret;
+ int i;
+
+ for (i = 0; i < ci->children_num; i++)
+ {
+ oconfig_item_t *child;
+
+ child = ci->children + i;
+
+ if (strcasecmp ("Host", child->key) == 0)
+ graph_config_get_string (child, &host);
+ else if (strcasecmp ("Plugin", child->key) == 0)
+ graph_config_get_string (child, &plugin);
+ else if (strcasecmp ("PluginInstance", child->key) == 0)
+ graph_config_get_string (child, &plugin_instance);
+ else if (strcasecmp ("Type", child->key) == 0)
+ graph_config_get_string (child, &type);
+ else if (strcasecmp ("TypeInstance", child->key) == 0)
+ graph_config_get_string (child, &type_instance);
+ /* else: ignore all other directives here. */
+ } /* for */
+
+ ret = ident_create (host, plugin, plugin_instance, type, type_instance);
+
+ free (host);
+ free (plugin);
+ free (plugin_instance);
+ free (type);
+ free (type_instance);
+
+ return (ret);
+} /* }}} int graph_config_get_selector */
+
+/*
+ * Global functions
+ */
+graph_config_t *graph_create (const graph_ident_t *selector) /* {{{ */
+{
+ graph_config_t *cfg;
+
+ cfg = malloc (sizeof (*cfg));
+ if (cfg == NULL)
+ return (NULL);
+ memset (cfg, 0, sizeof (*cfg));
+
+ if (selector != NULL)
+ cfg->select = ident_clone (selector);
+ else
+ cfg->select = NULL;
+
+ cfg->title = NULL;
+ cfg->vertical_label = NULL;
+ cfg->defs = NULL;
+ cfg->instances = NULL;
+
+ return (cfg);
+} /* }}} int graph_create */
+
+void graph_destroy (graph_config_t *cfg) /* {{{ */
+{
+ if (cfg == NULL)
+ return;
+
+ ident_destroy (cfg->select);
+
+ free (cfg->title);
+ free (cfg->vertical_label);
+
+ def_destroy (cfg->defs);
+ inst_destroy (cfg->instances);
+} /* }}} void graph_destroy */
+
+int graph_config_add (const oconfig_item_t *ci) /* {{{ */
+{
+ graph_ident_t *select;
+ graph_config_t *cfg = NULL;
+ int i;
+
+ select = graph_config_get_selector (ci);
+ if (select == NULL)
+ return (EINVAL);
+
+ cfg = graph_create (/* selector = */ NULL);
+ if (cfg == NULL)
+ return (ENOMEM);
+
+ cfg->select = select;
+
+ for (i = 0; i < ci->children_num; i++)
+ {
+ oconfig_item_t *child;
+
+ child = ci->children + i;
+
+ if (strcasecmp ("Title", child->key) == 0)
+ graph_config_get_string (child, &cfg->title);
+ else if (strcasecmp ("VerticalLabel", child->key) == 0)
+ graph_config_get_string (child, &cfg->vertical_label);
++ else if (strcasecmp ("ShowZero", child->key) == 0)
++ graph_config_get_bool (child, &cfg->show_zero);
+ else if (strcasecmp ("DEF", child->key) == 0)
+ def_config (cfg, child);
+ } /* for */
+
+ gl_add_graph (cfg);
+
+ return (0);
+} /* }}} graph_config_add */
+
+int graph_add_file (graph_config_t *cfg, const graph_ident_t *file) /* {{{ */
+{
+ graph_instance_t *inst;
+
+ inst = graph_find_instance (cfg, file);
+ if (inst == NULL)
+ {
+ inst = inst_create (cfg, file);
+ if (inst == NULL)
+ return (ENOMEM);
+
+ if (cfg->instances == NULL)
+ cfg->instances = inst;
+ else
+ inst_append (cfg->instances, inst);
+ }
+
+ return (inst_add_file (inst, file));
+} /* }}} int graph_add_file */
+
+int graph_get_title (graph_config_t *cfg, /* {{{ */
+ char *buffer, size_t buffer_size)
+{
+ if ((cfg == NULL) || (buffer == NULL) || (buffer_size < 1))
+ return (EINVAL);
+
+ if (cfg->title == NULL)
+ cfg->title = ident_to_string (cfg->select);
+
+ if (cfg->title == NULL)
+ return (ENOMEM);
+
+ strncpy (buffer, cfg->title, buffer_size);
+ buffer[buffer_size - 1] = 0;
+
+ return (0);
+} /* }}} int graph_get_title */
+
+graph_ident_t *graph_get_selector (graph_config_t *cfg) /* {{{ */
+{
+ if (cfg == NULL)
+ return (NULL);
+
+ return (ident_clone (cfg->select));
+} /* }}} graph_ident_t *graph_get_selector */
+
+graph_instance_t *graph_get_instances (graph_config_t *cfg) /* {{{ */
+{
+ if (cfg == NULL)
+ return (NULL);
+
+ return (cfg->instances);
+} /* }}} graph_instance_t *graph_get_instances */
+
+graph_def_t *graph_get_defs (graph_config_t *cfg) /* {{{ */
+{
+ if (cfg == NULL)
+ return (NULL);
+
+ return (cfg->defs);
+} /* }}} graph_def_t *graph_get_defs */
+
+int graph_add_def (graph_config_t *cfg, graph_def_t *def) /* {{{ */
+{
+ if ((cfg == NULL) || (def == NULL))
+ return (EINVAL);
+
+ if (cfg->defs == NULL)
+ {
+ cfg->defs = def;
+ return (0);
+ }
+
+ return (def_append (cfg->defs, def));
+} /* }}} int graph_add_def */
+
+_Bool graph_matches (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */
+{
+ if ((cfg == NULL) || (ident == NULL))
+ return (0);
+
+ return (ident_matches (cfg->select, ident));
+} /* }}} _Bool graph_matches */
+
++struct graph_search_data_s
++{
++ graph_config_t *cfg;
++ graph_inst_callback_t callback;
++ void *user_data;
++};
++typedef struct graph_search_data_s graph_search_data_t;
++
++static int graph_search_submit (graph_instance_t *inst, /* {{{ */
++ void *user_data)
++{
++ graph_search_data_t *data = user_data;
++
++ if ((inst == NULL) || (data == NULL))
++ return (EINVAL);
++
++ return ((*data->callback) (data->cfg, inst, data->user_data));
++} /* }}} int graph_search_submit */
++
++int graph_search (graph_config_t *cfg, const char *term, /* {{{ */
++ graph_inst_callback_t callback,
++ void *user_data)
++{
++ graph_search_data_t data = { cfg, callback, user_data };
++ char buffer[1024];
++ int status;
++
++ status = graph_get_title (cfg, buffer, sizeof (buffer));
++ if (status != 0)
++ {
++ fprintf (stderr, "graph_search: graph_get_title failed\n");
++ return (status);
++ }
++
++ if (strstr (buffer, term) != NULL)
++ {
++ status = inst_foreach (cfg->instances, graph_search_submit, &data);
++ if (status != 0)
++ return (status);
++ }
++ else
++ {
++ status = inst_search (cfg, cfg->instances, term,
++ graph_search_submit, &data);
++ if (status != 0)
++ return (status);
++ }
++
++ return (0);
++} /* }}} int graph_search */
++
+int graph_compare (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */
+{
+ if ((cfg == NULL) || (ident == NULL))
+ return (0);
+
+ return (ident_compare (cfg->select, ident));
+} /* }}} int graph_compare */
+
+int graph_clear_instances (graph_config_t *cfg) /* {{{ */
+{
+ if (cfg == NULL)
+ return (EINVAL);
+
+ inst_destroy (cfg->instances);
+ cfg->instances = NULL;
+
+ return (0);
+} /* }}} int graph_clear_instances */
+
+int graph_get_rrdargs (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
+ str_array_t *args)
+{
+ if ((cfg == NULL) || (inst == NULL) || (args == NULL))
+ return (EINVAL);
+
+ if (cfg->title != NULL)
+ {
+ array_append (args, "-t");
+ array_append (args, cfg->title);
+ }
+
+ if (cfg->vertical_label != NULL)
+ {
+ array_append (args, "-v");
+ array_append (args, cfg->vertical_label);
+ }
+
++ if (cfg->show_zero)
++ {
++ array_append (args, "-l");
++ array_append (args, "0");
++ }
++
+ return (0);
+} /* }}} int graph_get_rrdargs */
+
+/* vim: set sw=2 sts=2 et fdm=marker : */
--- /dev/null
- /*
- * Data types
- */
- struct graph_config_s;
- typedef struct graph_config_s graph_config_t;
-
- #include "graph_def.h"
- #include "graph_ident.h"
- #include "graph_instance.h"
+#ifndef GRAPH_H
+#define GRAPH_H 1
+
++#include "graph_types.h"
+#include "oconfig.h"
+#include "utils_array.h"
+
+/*
+ * Functions
+ */
+graph_config_t *graph_create (const graph_ident_t *selector);
+
+void graph_destroy (graph_config_t *graph);
+
+int graph_config_add (const oconfig_item_t *ci);
+
+int graph_add_file (graph_config_t *cfg, const graph_ident_t *file);
+
+int graph_get_title (graph_config_t *cfg,
+ char *buffer, size_t buffer_size);
+
+graph_ident_t *graph_get_selector (graph_config_t *cfg);
+
+graph_instance_t *graph_get_instances (graph_config_t *cfg);
+
+graph_def_t *graph_get_defs (graph_config_t *cfg);
+
+int graph_add_def (graph_config_t *cfg, graph_def_t *def);
+
+_Bool graph_matches (graph_config_t *cfg, const graph_ident_t *ident);
+
++int graph_search (graph_config_t *cfg, const char *term,
++ graph_inst_callback_t callback, void *user_data);
++
+int graph_compare (graph_config_t *cfg, const graph_ident_t *ident);
+
+int graph_clear_instances (graph_config_t *cfg);
+
+int graph_get_rrdargs (graph_config_t *cfg, graph_instance_t *inst,
+ str_array_t *args);
+
+#endif /* GRAPH_H */
+/* vim: set sw=2 sts=2 et fdm=marker : */
--- /dev/null
- index, (def->format != NULL) ? def->format : "%lg");
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#include "graph_def.h"
+#include "graph.h"
+#include "graph_config.h"
++#include "graph_ident.h"
+#include "common.h"
+#include "oconfig.h"
+
+#include <fcgiapp.h>
+#include <fcgi_stdio.h>
+
+/*
+ * Data structures
+ */
+struct graph_def_s
+{
+ graph_ident_t *select;
+
+ char *ds_name;
+ char *legend;
+ uint32_t color;
+ _Bool stack;
+ _Bool area;
+ char *format;
+
+ graph_def_t *next;
+};
+
+/*
+ * Private functions
+ */
+#define DEF_CONFIG_FIELD(field) \
+static int def_config_##field (const oconfig_item_t *ci, graph_ident_t *ident) \
+{ \
+ char *tmp = NULL; \
+ int status = graph_config_get_string (ci, &tmp); \
+ if (status != 0) \
+ return (status); \
+ ident_set_##field (ident, tmp); \
+ free (tmp); \
+ return (0); \
+} /* }}} int def_config_field */
+
+DEF_CONFIG_FIELD (host);
+DEF_CONFIG_FIELD (plugin);
+DEF_CONFIG_FIELD (plugin_instance);
+DEF_CONFIG_FIELD (type);
+DEF_CONFIG_FIELD (type_instance);
+
+#undef DEF_CONFIG_FIELD
+
+static int def_config_color (const oconfig_item_t *ci, uint32_t *ret_color) /* {{{ */
+{
+ char *tmp;
+ char *endptr;
+ uint32_t color;
+
+ if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
+ return (EINVAL);
+
+ tmp = ci->values[0].value.string;
+
+ endptr = NULL;
+ errno = 0;
+ color = (uint32_t) strtoul (tmp, &endptr, /* base = */ 16);
+ if ((errno != 0) || (endptr == tmp) || (color > 0x00ffffff))
+ return (EINVAL);
+
+ *ret_color = color;
+
+ return (0);
+} /* }}} int def_config_color */
+
+static graph_def_t *def_config_get_obj (graph_config_t *cfg, /* {{{ */
+ const oconfig_item_t *ci)
+{
+ graph_ident_t *ident;
+ char *ds_name = NULL;
+ graph_def_t *def;
+ int i;
+
+ ident = graph_get_selector (cfg);
+ if (ident == NULL)
+ {
+ fprintf (stderr, "def_config_get_obj: graph_get_selector failed");
+ return (NULL);
+ }
+
+ for (i = 0; i < ci->children_num; i++)
+ {
+ oconfig_item_t *child;
+
+#define HANDLE_FIELD(name,field) \
+ else if (strcasecmp (name, child->key) == 0) \
+ def_config_##field (child, ident)
+
+ child = ci->children + i;
+ if (strcasecmp ("DSName", child->key) == 0)
+ graph_config_get_string (child, &ds_name);
+
+ HANDLE_FIELD ("Host", host);
+ HANDLE_FIELD ("Plugin", plugin);
+ HANDLE_FIELD ("PluginInstance", plugin_instance);
+ HANDLE_FIELD ("Type", type);
+ HANDLE_FIELD ("TypeInstance", type_instance);
+
+#undef HANDLE_FIELD
+ }
+
+ def = def_create (cfg, ident, ds_name);
+ if (def == NULL)
+ {
+ fprintf (stderr, "def_config_get_obj: def_create failed\n");
+ ident_destroy (ident);
+ free (ds_name);
+ return (NULL);
+ }
+
+ ident_destroy (ident);
+ free (ds_name);
+
+ return (def);
+} /* }}} graph_def_t *def_config_get_obj */
+
+/*
+ * Public functions
+ */
+graph_def_t *def_create (graph_config_t *cfg, graph_ident_t *ident, /* {{{ */
+ const char *ds_name)
+{
+ graph_ident_t *selector;
+ graph_def_t *ret;
+
+ if ((cfg == NULL) || (ident == NULL) || (ds_name == NULL))
+ return (NULL);
+
+ selector = graph_get_selector (cfg);
+ if (selector == NULL)
+ return (NULL);
+
+ ret = malloc (sizeof (*ret));
+ if (ret == NULL)
+ {
+ ident_destroy (selector);
+ return (NULL);
+ }
+ memset (ret, 0, sizeof (*ret));
+ ret->legend = NULL;
+ ret->format = NULL;
+
+ ret->ds_name = strdup (ds_name);
+ if (ret->ds_name == NULL)
+ {
+ ident_destroy (selector);
+ free (ret);
+ return (NULL);
+ }
+
+ ret->color = get_random_color ();
+ ret->next = NULL;
+
+ ret->select = ident_copy_with_selector (selector, ident,
+ IDENT_FLAG_REPLACE_ALL);
+ if (ret->select == NULL)
+ {
+ ident_destroy (selector);
+ free (ret->ds_name);
+ free (ret);
+ return (NULL);
+ }
+
+ ident_destroy (selector);
+ return (ret);
+} /* }}} graph_def_t *def_create */
+
+void def_destroy (graph_def_t *def) /* {{{ */
+{
+ graph_def_t *next;
+
+ if (def == NULL)
+ return;
+
+ next = def->next;
+
+ ident_destroy (def->select);
+
+ free (def->ds_name);
+ free (def->legend);
+ free (def->format);
+
+ free (def);
+
+ def_destroy (next);
+} /* }}} void def_destroy */
+
+int def_config (graph_config_t *cfg, const oconfig_item_t *ci) /* {{{ */
+{
+ graph_def_t *def;
+ int i;
+
+ def = def_config_get_obj (cfg, ci);
+ if (def == NULL)
+ return (EINVAL);
+
+ for (i = 0; i < ci->children_num; i++)
+ {
+ oconfig_item_t *child;
+
+ child = ci->children + i;
+ if (strcasecmp ("Legend", child->key) == 0)
+ graph_config_get_string (child, &def->legend);
+ else if (strcasecmp ("Color", child->key) == 0)
+ def_config_color (child, &def->color);
+ else if (strcasecmp ("Stack", child->key) == 0)
+ graph_config_get_bool (child, &def->stack);
+ else if (strcasecmp ("Area", child->key) == 0)
+ graph_config_get_bool (child, &def->area);
+ else if (strcasecmp ("Format", child->key) == 0)
+ graph_config_get_string (child, &def->format);
+ }
+
+ return (graph_add_def (cfg, def));
+} /* }}} int def_config */
+
+int def_append (graph_def_t *head, graph_def_t *def) /* {{{ */
+{
+ graph_def_t *ptr;
+
+ if ((head == NULL) || (def == NULL))
+ return (EINVAL);
+
+ ptr = head;
+ while (ptr->next != NULL)
+ ptr = ptr->next;
+
+ ptr->next = def;
+
+ return (0);
+} /* }}} int def_append */
+
+graph_def_t *def_search (graph_def_t *head, graph_ident_t *ident, /* {{{ */
+ const char *ds_name)
+{
+ graph_def_t *ptr;
+
+ if ((head == NULL) || (ident == NULL) || (ds_name == NULL))
+ return (NULL);
+
+ for (ptr = head; ptr != NULL; ptr = ptr->next)
+ {
+ if (!ident_matches (ptr->select, ident))
+ continue;
+
+ if (strcmp (ptr->ds_name, ds_name) == 0)
+ return (ptr);
+ }
+
+ return (NULL);
+} /* }}} graph_def_t *def_search */
+
+_Bool def_matches (graph_def_t *def, graph_ident_t *ident) /* {{{ */
+{
+ return (ident_matches (def->select, ident));
+} /* }}} _Bool def_matches */
+
+int def_foreach (graph_def_t *def, def_callback_t callback, /* {{{ */
+ void *user_data)
+{
+ graph_def_t *ptr;
+
+ if ((def == NULL) || (callback == NULL))
+ return (EINVAL);
+
+ for (ptr = def; ptr != NULL; ptr = ptr->next)
+ {
+ int status;
+
+ status = (*callback) (ptr, user_data);
+ if (status != 0)
+ return (status);
+ }
+
+ return (0);
+} /* }}} int def_foreach */
+
+int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
+ str_array_t *args)
+{
+ char *file;
+ int index;
+
+ if ((def == NULL) || (ident == NULL) || (args == NULL))
+ return (EINVAL);
+
+ file = ident_to_file (ident);
+ if (file == NULL)
+ {
+ DEBUG ("gl_ident_get_rrdargs: ident_to_file returned NULL.\n");
+ return (-1);
+ }
+
+ DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
+
+ index = array_argc (args);
+
+ /* CDEFs */
+ array_append_format (args, "DEF:def_%04i_min=%s:%s:MIN",
+ index, file, def->ds_name);
+ array_append_format (args, "DEF:def_%04i_avg=%s:%s:AVERAGE",
+ index, file, def->ds_name);
+ array_append_format (args, "DEF:def_%04i_max=%s:%s:MAX",
+ index, file, def->ds_name);
+ /* VDEFs */
+ array_append_format (args, "VDEF:vdef_%04i_min=def_%04i_min,MINIMUM",
+ index, index);
+ array_append_format (args, "VDEF:vdef_%04i_avg=def_%04i_avg,AVERAGE",
+ index, index);
+ array_append_format (args, "VDEF:vdef_%04i_max=def_%04i_max,MAXIMUM",
+ index, index);
+ array_append_format (args, "VDEF:vdef_%04i_lst=def_%04i_avg,LAST",
+ index, index);
+
+ /* Graph part */
+ array_append_format (args, "%s:def_%04i_avg#%06"PRIx32":%s%s",
+ def->area ? "AREA" : "LINE1",
+ index, def->color,
+ (def->legend != NULL) ? def->legend : def->ds_name,
+ def->stack ? ":STACK" : "");
+ array_append_format (args, "GPRINT:vdef_%04i_min:%s min,",
- index, (def->format != NULL) ? def->format : "%lg");
++ index, (def->format != NULL) ? def->format : "%6.2lf");
+ array_append_format (args, "GPRINT:vdef_%04i_avg:%s avg,",
- index, (def->format != NULL) ? def->format : "%lg");
++ index, (def->format != NULL) ? def->format : "%6.2lf");
+ array_append_format (args, "GPRINT:vdef_%04i_max:%s max,",
- index, (def->format != NULL) ? def->format : "%lg");
++ index, (def->format != NULL) ? def->format : "%6.2lf");
+ array_append_format (args, "GPRINT:vdef_%04i_lst:%s last\\l",
++ index, (def->format != NULL) ? def->format : "%6.2lf");
+
+ free (file);
+
+ return (0);
+} /* }}} int def_get_rrdargs */
+
+/* vim: set sw=2 sts=2 et fdm=marker : */
--- /dev/null
- struct graph_def_s;
- typedef struct graph_def_s graph_def_t;
-
- typedef int (*def_callback_t) (graph_def_t *def,
- void *user_data);
-
- #include "graph.h"
- #include "graph_ident.h"
+#ifndef GRAPH_DEF_H
+#define GRAPH_DEF_H 1
+
++#include "graph_types.h"
+#include "utils_array.h"
+#include "oconfig.h"
+
+graph_def_t *def_create (graph_config_t *cfg, graph_ident_t *ident,
+ const char *ds_name);
+
+void def_destroy (graph_def_t *def);
+
+int def_config (graph_config_t *cfg, const oconfig_item_t *ci);
+
+int def_append (graph_def_t *head, graph_def_t *def);
+
+graph_def_t *def_search (graph_def_t *head, graph_ident_t *ident,
+ const char *ds_name);
+
+_Bool def_matches (graph_def_t *def, graph_ident_t *ident);
+
+int def_foreach (graph_def_t *def, def_callback_t callback, void *user_data);
+
+int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident,
+ str_array_t *args);
+
+/* vim: set sw=2 sts=2 et fdm=marker : */
+#endif
--- /dev/null
- struct graph_ident_s;
- typedef struct graph_ident_s graph_ident_t;
-
+#ifndef GRAPH_IDENT_H
+#define GRAPH_IDENT_H 1
+
+#include <time.h>
++#include "graph_types.h"
+
+#define ANY_TOKEN "/any/"
+#define ALL_TOKEN "/all/"
+
+#define IS_ANY(str) (((str) != NULL) && (strcasecmp (ANY_TOKEN, (str)) == 0))
+#define IS_ALL(str) (((str) != NULL) && (strcasecmp (ALL_TOKEN, (str)) == 0))
+
- const graph_ident_t *ident);
+graph_ident_t *ident_create (const char *host,
+ const char *plugin, const char *plugin_instance,
+ const char *type, const char *type_instance);
+graph_ident_t *ident_clone (const graph_ident_t *ident);
+
+#define IDENT_FLAG_REPLACE_ALL 0x01
+#define IDENT_FLAG_REPLACE_ANY 0x02
+graph_ident_t *ident_copy_with_selector (const graph_ident_t *selector,
+ const graph_ident_t *ident, unsigned int flags);
+
+void ident_destroy (graph_ident_t *ident);
+
+const char *ident_get_host (graph_ident_t *ident);
+const char *ident_get_plugin (graph_ident_t *ident);
+const char *ident_get_plugin_instance (graph_ident_t *ident);
+const char *ident_get_type (graph_ident_t *ident);
+const char *ident_get_type_instance (graph_ident_t *ident);
+
+int ident_set_host (graph_ident_t *ident, const char *host);
+int ident_set_plugin (graph_ident_t *ident, const char *plugin);
+int ident_set_plugin_instance (graph_ident_t *ident,
+ const char *plugin_instance);
+int ident_set_type (graph_ident_t *ident, const char *type);
+int ident_set_type_instance (graph_ident_t *ident,
+ const char *type_instance);
+
+int ident_compare (const graph_ident_t *i0,
+ const graph_ident_t *i1);
+
+_Bool ident_matches (const graph_ident_t *selector,
++ const graph_ident_t *ident);
+
+char *ident_to_string (const graph_ident_t *ident);
+char *ident_to_file (const graph_ident_t *ident);
+char *ident_to_json (const graph_ident_t *ident);
+
+time_t ident_get_mtime (const graph_ident_t *ident);
+
+/* vim: set sw=2 sts=2 et fdm=marker : */
+#endif /* GRAPH_IDENT_H */
--- /dev/null
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <time.h>
+
+#include "graph_instance.h"
++#include "graph_def.h"
+#include "graph_ident.h"
+#include "graph_list.h"
+#include "common.h"
+#include "utils_params.h"
+
+#include <fcgiapp.h>
+#include <fcgi_stdio.h>
+
+struct graph_instance_s /* {{{ */
+{
+ graph_ident_t *select;
+
+ graph_ident_t **files;
+ size_t files_num;
+
+ graph_instance_t *next;
+}; /* }}} struct graph_instance_s */
+
+struct def_callback_data_s
+{
+ graph_instance_t *inst;
+ str_array_t *args;
+};
+typedef struct def_callback_data_s def_callback_data_t;
+
+/*
+ * Private functions
+ */
+/* Create one DEF for each data source in the file. Called by
+ * "inst_get_default_defs" for each file. */
+static graph_def_t *ident_get_default_defs (graph_config_t *cfg, /* {{{ */
+ graph_ident_t *ident, graph_def_t *def_head)
+{
+ graph_def_t *defs = NULL;
+ char *file;
+ char **dses = NULL;
+ size_t dses_num = 0;
+ int status;
+ size_t i;
+
+ if ((cfg == NULL) || (ident == NULL))
+ return (def_head);
+
+ file = ident_to_file (ident);
+ if (file == NULL)
+ {
+ fprintf (stderr, "ident_get_default_defs: ident_to_file failed\n");
+ return (def_head);
+ }
+
+ status = ds_list_from_rrd_file (file, &dses_num, &dses);
+ if (status != 0)
+ {
+ free (file);
+ return (def_head);
+ }
+
+ for (i = 0; i < dses_num; i++)
+ {
+ graph_def_t *def;
+
+ def = def_search (def_head, ident, dses[i]);
+ if (def != NULL)
+ continue;
+
+ def = def_create (cfg, ident, dses[i]);
+ if (def == NULL)
+ continue;
+
+ if (defs == NULL)
+ defs = def;
+ else
+ def_append (defs, def);
+
+ free (dses[i]);
+ }
+
+ free (dses);
+ free (file);
+
+ return (defs);
+} /* }}} int ident_get_default_defs */
+
+/* Create one or more DEFs for each file in the graph instance. The number
+ * depends on the number of data sources in each of the files. Called from
+ * "inst_get_rrdargs" if no DEFs are available from the configuration.
+ * */
+static graph_def_t *inst_get_default_defs (graph_config_t *cfg, /* {{{ */
+ graph_instance_t *inst)
+{
+ graph_def_t *defs = NULL;
+ size_t i;
+
+ if ((cfg == NULL) || (inst == NULL))
+ return (NULL);
+
+ for (i = 0; i < inst->files_num; i++)
+ {
+ graph_def_t *def;
+
+ def = ident_get_default_defs (cfg, inst->files[i], defs);
+ if (def == NULL)
+ continue;
+
+ if (defs == NULL)
+ defs = def;
+ else
+ def_append (defs, def);
+ }
+
+ return (defs);
+} /* }}} graph_def_t *inst_get_default_defs */
+
+/* Called with each DEF in turn. Calls "def_get_rrdargs" with every appropriate
+ * file / DEF pair. */
+static int gl_instance_get_rrdargs_cb (graph_def_t *def, void *user_data) /* {{{ */
+{
+ def_callback_data_t *data = user_data;
+ graph_instance_t *inst = data->inst;
+ str_array_t *args = data->args;
+
+ size_t i;
+
+ for (i = 0; i < inst->files_num; i++)
+ {
+ if (!def_matches (def, inst->files[i]))
+ continue;
+
+ def_get_rrdargs (def, inst->files[i], args);
+ }
+
+ return (0);
+} /* }}} int gl_instance_get_rrdargs_cb */
+
+static const char *get_part_from_param (const char *prim_key, /* {{{ */
+ const char *sec_key)
+{
+ const char *val;
+
+ val = param (prim_key);
+ if (val != NULL)
+ return (val);
+
+ return (param (sec_key));
+} /* }}} const char *get_part_from_param */
+
+/*
+ * Public functions
+ */
+graph_instance_t *inst_create (graph_config_t *cfg, /* {{{ */
+ const graph_ident_t *ident)
+{
+ graph_instance_t *i;
+ graph_ident_t *selector;
+
+ if ((cfg == NULL) || (ident == NULL))
+ return (NULL);
+
+ i = malloc (sizeof (*i));
+ if (i == NULL)
+ return (NULL);
+ memset (i, 0, sizeof (*i));
+
+ selector = graph_get_selector (cfg);
+ if (selector == NULL)
+ {
+ fprintf (stderr, "inst_create: graph_get_selector failed\n");
+ free (i);
+ return (NULL);
+ }
+
+ i->select = ident_copy_with_selector (selector, ident,
+ IDENT_FLAG_REPLACE_ANY);
+ if (i->select == NULL)
+ {
+ fprintf (stderr, "inst_create: ident_copy_with_selector failed\n");
+ ident_destroy (selector);
+ free (i);
+ return (NULL);
+ }
+
+ ident_destroy (selector);
+
+ i->files = NULL;
+ i->files_num = 0;
+
+ i->next = NULL;
+
+ return (i);
+} /* }}} graph_instance_t *inst_create */
+
+void inst_destroy (graph_instance_t *inst) /* {{{ */
+{
+ graph_instance_t *next;
+ size_t i;
+
+ if (inst == NULL)
+ return;
+
+ next = inst->next;
+
+ ident_destroy (inst->select);
+
+ for (i = 0; i < inst->files_num; i++)
+ ident_destroy (inst->files[i]);
+ free (inst->files);
+
+ free (inst);
+
+ inst_destroy (next);
+} /* }}} void inst_destroy */
+
+int inst_add_file (graph_instance_t *inst, /* {{{ */
+ const graph_ident_t *file)
+{
+ graph_ident_t **tmp;
+
+ tmp = realloc (inst->files, sizeof (*inst->files) * (inst->files_num + 1));
+ if (tmp == NULL)
+ return (ENOMEM);
+ inst->files = tmp;
+
+ inst->files[inst->files_num] = ident_clone (file);
+ if (inst->files[inst->files_num] == NULL)
+ return (ENOMEM);
+
+ inst->files_num++;
+
+ return (0);
+} /* }}} int inst_add_file */
+
+graph_instance_t *inst_get_selected (graph_config_t *cfg) /* {{{ */
+{
+ const char *host = get_part_from_param ("inst_host", "host");
+ const char *plugin = get_part_from_param ("inst_plugin", "plugin");
+ const char *plugin_instance = get_part_from_param ("inst_plugin_instance", "plugin_instance");
+ const char *type = get_part_from_param ("inst_type", "type");
+ const char *type_instance = get_part_from_param ("inst_type_instance", "type_instance");
+ graph_ident_t *ident;
+ graph_instance_t *inst;
+
+ if (cfg == NULL)
+ cfg = gl_graph_get_selected ();
+
+ if (cfg == NULL)
+ {
+ DEBUG ("inst_get_selected: cfg == NULL;\n");
+ return (NULL);
+ }
+
+ if ((host == NULL)
+ || (plugin == NULL) || (plugin_instance == NULL)
+ || (type == NULL) || (type_instance == NULL))
+ {
+ DEBUG ("inst_get_selected: A parameter is NULL.\n");
+ return (NULL);
+ }
+
+ ident = ident_create (host, plugin, plugin_instance, type, type_instance);
+
+ for (inst = graph_get_instances (cfg); inst != NULL; inst = inst->next)
+ {
+ if (ident_compare (ident, inst->select) != 0)
+ continue;
+
+ ident_destroy (ident);
+ return (inst);
+ }
+
+ DEBUG ("inst_get_selected: No match found.\n");
+ ident_destroy (ident);
+ return (NULL);
+} /* }}} graph_instance_t *inst_get_selected */
+
+int inst_get_rrdargs (graph_config_t *cfg, /* {{{ */
+ graph_instance_t *inst,
+ str_array_t *args)
+{
+ def_callback_data_t data = { inst, args };
+ graph_def_t *defs;
+ int status;
+
+ if ((cfg == NULL) || (inst == NULL) || (args == NULL))
+ return (EINVAL);
+
+ status = graph_get_rrdargs (cfg, inst, args);
+ if (status != 0)
+ return (status);
+
+ defs = graph_get_defs (cfg);
+ if (defs == NULL)
+ {
+ defs = inst_get_default_defs (cfg, inst);
+
+ if (defs == NULL)
+ return (-1);
+
+ status = def_foreach (defs, gl_instance_get_rrdargs_cb, &data);
+
+ def_destroy (defs);
+ }
+ else
+ {
+ status = def_foreach (defs, gl_instance_get_rrdargs_cb, &data);
+ }
+
+ return (status);
+} /* }}} int inst_get_rrdargs */
+
+graph_ident_t *inst_get_selector (graph_instance_t *inst) /* {{{ */
+{
+ if (inst == NULL)
+ return (NULL);
+
+ return (ident_clone (inst->select));
+} /* }}} graph_ident_t *inst_get_selector */
+
+int inst_get_params (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
+ char *buffer, size_t buffer_size)
+{
+ graph_ident_t *cfg_select;
+
+ if ((cfg == NULL) || (inst == NULL)
+ || (buffer == NULL) || (buffer_size < 1))
+ return (EINVAL);
+
+ cfg_select = graph_get_selector (cfg);
+ if (cfg_select == NULL)
+ {
+ fprintf (stderr, "inst_get_params: graph_get_selector failed");
+ return (-1);
+ }
+
+ buffer[0] = 0;
+
+#define COPY_FIELD(field) do { \
+ const char *cfg_f = ident_get_##field (cfg_select); \
+ const char *inst_f = ident_get_##field (inst->select); \
+ if (strcmp (cfg_f, inst_f) == 0) \
+ { \
+ strlcat (buffer, #field, buffer_size); \
+ strlcat (buffer, "=", buffer_size); \
+ strlcat (buffer, cfg_f, buffer_size); \
+ } \
+ else \
+ { \
+ strlcat (buffer, "graph_", buffer_size); \
+ strlcat (buffer, #field, buffer_size); \
+ strlcat (buffer, "=", buffer_size); \
+ strlcat (buffer, cfg_f, buffer_size); \
+ strlcat (buffer, ";", buffer_size); \
+ strlcat (buffer, "inst_", buffer_size); \
+ strlcat (buffer, #field, buffer_size); \
+ strlcat (buffer, "=", buffer_size); \
+ strlcat (buffer, inst_f, buffer_size); \
+ } \
+} while (0)
+
+ COPY_FIELD(host);
+ strlcat (buffer, ";", buffer_size);
+ COPY_FIELD(plugin);
+ strlcat (buffer, ";", buffer_size);
+ COPY_FIELD(plugin_instance);
+ strlcat (buffer, ";", buffer_size);
+ COPY_FIELD(type);
+ strlcat (buffer, ";", buffer_size);
+ COPY_FIELD(type_instance);
+
+#undef COPY_FIELD
+
+ ident_destroy (cfg_select);
+
+ return (0);
+} /* }}} int inst_get_params */
+
+int inst_append (graph_instance_t *head, graph_instance_t *inst) /* {{{ */
+{
+ graph_instance_t *ptr;
+
+ if ((head == NULL) || (inst == NULL))
+ return (EINVAL);
+
+ ptr = head;
+ while (ptr->next != NULL)
+ ptr = ptr->next;
+
+ ptr->next = inst;
+
+ return (0);
+} /* }}} int inst_append */
+
+int inst_foreach (graph_instance_t *inst, /* {{{ */
+ inst_callback_t cb, void *user_data)
+{
+ graph_instance_t *ptr;
+
+ if ((inst == NULL) || (cb == NULL))
+ return (EINVAL);
+
+ for (ptr = inst; ptr != NULL; ptr = ptr->next)
+ {
+ int status;
+
+ status = (*cb) (ptr, user_data);
+ if (status != 0)
+ return (status);
+ }
+
+ return (0);
+} /* }}} int inst_foreach */
+
++int inst_search (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
++ const char *term, inst_callback_t cb, void *user_data)
++{
++ graph_instance_t *ptr;
++ char buffer[1024];
++ int status;
++
++ if ((inst == NULL) || (cb == NULL))
++ return (EINVAL);
++
++ for (ptr = inst; ptr != NULL; ptr = ptr->next)
++ {
++ status = inst_describe (cfg, ptr, buffer, sizeof (buffer));
++ if (status != 0)
++ {
++ fprintf (stderr, "inst_search: inst_describe failed\n");
++ return (status);
++ }
++
++ /* no match */
++ if (strstr (buffer, term) == NULL)
++ continue;
++
++ /* match */
++ status = (*cb) (ptr, user_data);
++ if (status != 0)
++ return (status);
++ }
++
++ return (0);
++} /* }}} int inst_search */
++
+graph_instance_t *inst_find_matching (graph_instance_t *inst, /* {{{ */
+ const graph_ident_t *ident)
+{
+ graph_instance_t *ptr;
+
+ if ((inst == NULL) || (ident == NULL))
+ return (NULL);
+
+ for (ptr = inst; ptr != NULL; ptr = ptr->next)
+ if (ident_matches (ptr->select, ident))
+ return (ptr);
+
+ return (NULL);
+} /* }}} graph_instance_t *inst_find_matching */
+
+int inst_describe (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
+ char *buffer, size_t buffer_size)
+{
+ graph_ident_t *cfg_select;
+
+ if ((cfg == NULL) || (inst == NULL)
+ || (buffer == NULL) || (buffer_size < 2))
+ return (EINVAL);
+
+ cfg_select = graph_get_selector (cfg);
+ if (cfg_select == NULL)
+ {
+ fprintf (stderr, "inst_describe: graph_get_selector failed\n");
+ return (-1);
+ }
+
+ buffer[0] = 0;
+
+#define CHECK_FIELD(field) do { \
+ if (IS_ANY (ident_get_##field (cfg_select))) \
+ { \
+ if (buffer[0] != 0) \
+ strlcat (buffer, "/", buffer_size); \
+ strlcat (buffer, ident_get_##field (inst->select), buffer_size); \
+ } \
+} while (0)
+
+ CHECK_FIELD (host);
+ CHECK_FIELD (plugin);
+ CHECK_FIELD (plugin_instance);
+ CHECK_FIELD (type);
+ CHECK_FIELD (type_instance);
+
+#undef CHECK_FIELD
+
+ if (buffer[0] == 0)
+ strlcat (buffer, "default", buffer_size);
+
+ ident_destroy (cfg_select);
+
+ return (0);
+} /* }}} int inst_describe */
+
+time_t inst_get_mtime (graph_instance_t *inst) /* {{{ */
+{
+ size_t i;
+ time_t mtime;
+
+ if (inst == NULL)
+ return (0);
+
+ mtime = 0;
+ for (i = 0; i < inst->files_num; i++)
+ {
+ time_t tmp;
+
+ tmp = ident_get_mtime (inst->files[i]);
+ if (mtime < tmp)
+ mtime = tmp;
+ }
+
+ return (mtime);
+} /* }}} time_t inst_get_mtime */
+
+/* vim: set sw=2 sts=2 et fdm=marker : */
--- /dev/null
- /*
- * Data types
- */
- struct graph_instance_s;
- typedef struct graph_instance_s graph_instance_t;
-
- typedef int (*inst_callback_t) (graph_instance_t *inst, void *user_data);
-
+#ifndef GRAPH_INSTANCE_H
+#define GRAPH_INSTANCE_H 1
+
- #include "graph.h"
+#include <time.h>
+
- * Callback types
- */
- /*
++#include "graph_types.h"
+#include "utils_array.h"
+
+/*
+ * Methods
+ */
+graph_instance_t *inst_create (graph_config_t *cfg,
+ const graph_ident_t *ident);
+
+void inst_destroy (graph_instance_t *inst);
+
+int inst_add_file (graph_instance_t *inst, const graph_ident_t *file);
+
+graph_instance_t *inst_get_selected (graph_config_t *cfg);
+
+int inst_get_params (graph_config_t *cfg, graph_instance_t *inst,
+ char *buffer, size_t buffer_size);
+
+int inst_get_rrdargs (graph_config_t *cfg, graph_instance_t *inst,
+ str_array_t *args);
+
+graph_ident_t *inst_get_selector (graph_instance_t *inst);
+
+int inst_append (graph_instance_t *head, graph_instance_t *inst);
+
+int inst_foreach (graph_instance_t *inst,
+ inst_callback_t cb, void *user_data);
+
++int inst_search (graph_config_t *cfg, graph_instance_t *inst,
++ const char *term, inst_callback_t cb,
++ void *user_data);
++
+graph_instance_t *inst_find_matching (graph_instance_t *inst,
+ const graph_ident_t *ident);
+
+int inst_describe (graph_config_t *cfg, graph_instance_t *inst,
+ char *buffer, size_t buffer_size);
+
+time_t inst_get_mtime (graph_instance_t *inst);
+
+#endif /* GRAPH_INSTANCE_H */
+/* vim: set sw=2 sts=2 et fdm=marker : */
--- /dev/null
- int gl_graph_get_all (gl_cfg_callback callback, /* {{{ */
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <inttypes.h>
+#include <string.h>
+#include <time.h>
+#include <errno.h>
+
+#include "graph_list.h"
+#include "graph_ident.h"
+#include "graph_def.h"
+#include "graph_config.h"
+#include "common.h"
+#include "filesystem.h"
+#include "utils_params.h"
+
+#include <fcgiapp.h>
+#include <fcgi_stdio.h>
+
+/*
+ * Defines
+ */
+#define UPDATE_INTERVAL 10
+
+/*
+ * Global variables
+ */
+static graph_config_t **gl_active = NULL;
+static size_t gl_active_num = 0;
+
+static graph_config_t **gl_staging = NULL;
+static size_t gl_staging_num = 0;
+
+static time_t gl_last_update = 0;
+
+/*
+ * Private functions
+ */
+int gl_add_graph_internal (graph_config_t *cfg, /* {{{ */
+ graph_config_t ***gl_array, size_t *gl_array_num)
+{
+ graph_config_t **tmp;
+
+#define ARRAY_PTR (*gl_array)
+#define ARRAY_SIZE (*gl_array_num)
+
+ if (cfg == NULL)
+ return (EINVAL);
+
+ tmp = realloc (ARRAY_PTR, sizeof (*ARRAY_PTR) * (ARRAY_SIZE + 1));
+ if (tmp == NULL)
+ return (ENOMEM);
+ ARRAY_PTR = tmp;
+
+ ARRAY_PTR[ARRAY_SIZE] = cfg;
+ ARRAY_SIZE++;
+
+#undef ARRAY_SIZE
+#undef ARRAY_PTR
+
+ return (0);
+} /* }}} int gl_add_graph_internal */
+
+static int gl_register_file (const graph_ident_t *file, /* {{{ */
+ __attribute__((unused)) void *user_data)
+{
+ graph_config_t *cfg;
+ int num_graphs = 0;
+ size_t i;
+
+ for (i = 0; i < gl_active_num; i++)
+ {
+ graph_config_t *cfg = gl_active[i];
+ int status;
+
+ if (!graph_matches (cfg, file))
+ continue;
+
+ status = graph_add_file (cfg, file);
+ if (status != 0)
+ {
+ /* report error */;
+ }
+ else
+ {
+ num_graphs++;
+ }
+ }
+
+ if (num_graphs == 0)
+ {
+ cfg = graph_create (file);
+ gl_add_graph_internal (cfg, &gl_active, &gl_active_num);
+ graph_add_file (cfg, file);
+ }
+
+ return (0);
+} /* }}} int gl_register_file */
+
+static const char *get_part_from_param (const char *prim_key, /* {{{ */
+ const char *sec_key)
+{
+ const char *val;
+
+ val = param (prim_key);
+ if (val != NULL)
+ return (val);
+
+ return (param (sec_key));
+} /* }}} const char *get_part_from_param */
+
+static int gl_clear_instances (void) /* {{{ */
+{
+ size_t i;
+
+ for (i = 0; i < gl_active_num; i++)
+ graph_clear_instances (gl_active[i]);
+
+ return (0);
+} /* }}} int gl_clear_instances */
+
+
+/*
+ * Global functions
+ */
+int gl_add_graph (graph_config_t *cfg) /* {{{ */
+{
+ return (gl_add_graph_internal (cfg, &gl_staging, &gl_staging_num));
+} /* }}} int gl_add_graph */
+
+int gl_config_submit (void) /* {{{ */
+{
+ graph_config_t **old;
+ size_t old_num;
+ size_t i;
+
+ old = gl_active;
+ old_num = gl_active_num;
+
+ gl_active = gl_staging;
+ gl_active_num = gl_staging_num;
+
+ gl_staging = NULL;
+ gl_staging_num = 0;
+
+ for (i = 0; i < old_num; i++)
+ {
+ graph_destroy (old[i]);
+ old[i] = NULL;
+ }
+ free (old);
+
+ return (0);
+} /* }}} int graph_config_submit */
+
- gl_inst_callback callback;
++int gl_graph_get_all (graph_callback_t callback, /* {{{ */
+ void *user_data)
+{
+ size_t i;
+
+ if (callback == NULL)
+ return (EINVAL);
+
+ gl_update ();
+
+ for (i = 0; i < gl_active_num; i++)
+ {
+ int status;
+
+ status = (*callback) (gl_active[i], user_data);
+ if (status != 0)
+ return (status);
+ }
+
+ return (0);
+} /* }}} int gl_graph_get_all */
+
+graph_config_t *gl_graph_get_selected (void) /* {{{ */
+{
+ const char *host = get_part_from_param ("graph_host", "host");
+ const char *plugin = get_part_from_param ("graph_plugin", "plugin");
+ const char *plugin_instance = get_part_from_param ("graph_plugin_instance", "plugin_instance");
+ const char *type = get_part_from_param ("graph_type", "type");
+ const char *type_instance = get_part_from_param ("graph_type_instance", "type_instance");
+ graph_ident_t *ident;
+ size_t i;
+
+ if ((host == NULL)
+ || (plugin == NULL) || (plugin_instance == NULL)
+ || (type == NULL) || (type_instance == NULL))
+ return (NULL);
+
+ ident = ident_create (host, plugin, plugin_instance, type, type_instance);
+
+ gl_update ();
+
+ for (i = 0; i < gl_active_num; i++)
+ {
+ if (graph_compare (gl_active[i], ident) != 0)
+ continue;
+
+ ident_destroy (ident);
+ return (gl_active[i]);
+ }
+
+ ident_destroy (ident);
+ return (NULL);
+} /* }}} graph_config_t *gl_graph_get_selected */
+
+/* gl_instance_get_all, gl_graph_instance_get_all {{{ */
+struct gl_inst_callback_data /* {{{ */
+{
+ graph_config_t *cfg;
- gl_inst_callback callback, void *user_data)
++ graph_inst_callback_t callback;
+ void *user_data;
+}; /* }}} struct gl_inst_callback_data */
+
+static int gl_inst_callback_handler (graph_instance_t *inst, /* {{{ */
+ void *user_data)
+{
+ struct gl_inst_callback_data *data = user_data;
+
+ return ((*data->callback) (data->cfg, inst, data->user_data));
+} /* }}} int gl_inst_callback_handler */
+
+int gl_graph_instance_get_all (graph_config_t *cfg, /* {{{ */
- int gl_instance_get_all (gl_inst_callback callback, /* {{{ */
++ graph_inst_callback_t callback, void *user_data)
+{
+ struct gl_inst_callback_data data =
+ {
+ cfg,
+ callback,
+ user_data
+ };
+
+ if ((cfg == NULL) || (callback == NULL))
+ return (EINVAL);
+
+ return (inst_foreach (graph_get_instances (cfg),
+ gl_inst_callback_handler, &data));
+} /* }}} int gl_graph_instance_get_all */
+
++int gl_instance_get_all (graph_inst_callback_t callback, /* {{{ */
+ void *user_data)
+{
+ size_t i;
+
+ gl_update ();
+
+ for (i = 0; i < gl_active_num; i++)
+ {
+ int status;
+
+ status = gl_graph_instance_get_all (gl_active[i], callback, user_data);
+ if (status != 0)
+ return (status);
+ }
+
+ return (0);
+} /* }}} int gl_instance_get_all */
+/* }}} gl_instance_get_all, gl_graph_instance_get_all */
+
++int gl_search (const char *term, graph_inst_callback_t callback, /* {{{ */
++ void *user_data)
++{
++ size_t i;
++
++ for (i = 0; i < gl_active_num; i++)
++ {
++ int status;
++
++ status = graph_search (gl_active[i], term,
++ /* callback = */ callback,
++ /* user data = */ user_data);
++ if (status != 0)
++ return (status);
++ }
++
++ return (0);
++} /* }}} int gl_search */
++
+int gl_update (void) /* {{{ */
+{
+ time_t now;
+ int status;
+
+ /*
+ printf ("Content-Type: text/plain\n\n");
+ */
+
+ now = time (NULL);
+
+ if ((gl_last_update + UPDATE_INTERVAL) >= now)
+ return (0);
+
+ graph_read_config ();
+
+ gl_clear_instances ();
+ status = fs_scan (/* callback = */ gl_register_file, /* user data = */ NULL);
+
+ gl_last_update = now;
+
+ return (status);
+} /* }}} int gl_update */
+
+/* vim: set sw=2 sts=2 et fdm=marker : */
--- /dev/null
- * Callback types
- */
- typedef int (*gl_cfg_callback) (graph_config_t *cfg,
- void *user_data);
-
- typedef int (*gl_inst_callback) (graph_config_t *cfg,
- graph_instance_t *inst, void *user_data);
-
- /*
+#ifndef GRAPH_LIST_H
+#define GRAPH_LIST_H 1
+
++#include "graph_types.h"
++#include "graph.h"
+#include "graph_instance.h"
+
+/*
- int gl_graph_get_all (gl_cfg_callback callback,
- void *user_data);
-
+ * Functions
+ */
+int gl_add_graph (graph_config_t *cfg);
+
+int gl_config_submit (void);
+
- int gl_graph_instance_get_all (graph_config_t *cfg,
- gl_inst_callback callback, void *user_data);
+graph_config_t *gl_graph_get_selected (void);
+
- int gl_instance_get_all (gl_inst_callback callback,
++int gl_graph_get_all (graph_callback_t callback, void *user_data);
++
++int gl_graph_instance_get_all (graph_config_t *cfg, graph_inst_callback_t callback,
++ void *user_data);
++
++int gl_instance_get_all (graph_inst_callback_t callback, void *user_data);
+
++int gl_search (const char *search, graph_inst_callback_t callback,
+ void *user_data);
+
+int gl_update (void);
+
+#endif /* GRAPH_LIST_H */
+/* vim: set sw=2 sts=2 et fdm=marker : */
--- /dev/null
--- /dev/null
++#ifndef GRAPH_TYPES_H
++#define GRAPH_TYPES_H 1
++
++/*
++ * Opaque types
++ */
++struct graph_config_s;
++typedef struct graph_config_s graph_config_t;
++
++struct graph_def_s;
++typedef struct graph_def_s graph_def_t;
++
++struct graph_ident_s;
++typedef struct graph_ident_s graph_ident_t;
++
++struct graph_instance_s;
++typedef struct graph_instance_s graph_instance_t;
++
++/*
++ * Callback types
++ */
++typedef int (*graph_callback_t) (graph_config_t *cfg,
++ void *user_data);
++
++typedef int (*graph_inst_callback_t) (graph_config_t *cfg,
++ graph_instance_t *inst, void *user_data);
++
++typedef int (*def_callback_t) (graph_def_t *def,
++ void *user_data);
++
++typedef int (*inst_callback_t) (graph_instance_t *inst,
++ void *user_data);
++
++#endif /* GRAPH_TYPES_H */
++/* vim: set sw=2 sts=2 et fdm=marker : */