#include "graph_list.h"
#include "graph_ident.h"
#include "graph_def.h"
+#include "graph_config.h"
#include "common.h"
#include "utils_params.h"
graph_config_t *next;
}; /* }}} struct graph_config_s */
+struct def_callback_data_s
+{
+ graph_instance_t *inst;
+ str_array_t *args;
+};
+typedef struct def_callback_data_s def_callback_data_t;
+
/*
* Global variables
*/
static graph_config_t *graph_config_head = NULL;
+static graph_config_t *graph_config_staging = NULL;
static time_t gl_last_update = 0;
i->select = ident_copy_with_selector (cfg->select, file,
IDENT_FLAG_REPLACE_ANY);
+ if (i->select == NULL)
+ {
+ DEBUG ("instance_create: ident_copy_with_selector returned NULL.\n");
+ free (i);
+ return (NULL);
+ }
i->files = NULL;
i->files_num = 0;
return (instance_add_file (inst, file));
} /* }}} int graph_add_file */
-static int graph_append (graph_config_t *cfg) /* {{{ */
+static int graph_append (graph_config_t **head, /* {{{ */
+ graph_config_t *cfg)
{
graph_config_t *last;
if (cfg == NULL)
return (EINVAL);
- if (graph_config_head == NULL)
+ if (head == NULL)
+ head = &graph_config_head;
+
+ if (*head == NULL)
{
- graph_config_head = cfg;
+ *head = cfg;
return (0);
}
- last = graph_config_head;
+ last = *head;
while (last->next != NULL)
last = last->next;
return (NULL);
memset (cfg, 0, sizeof (*cfg));
- cfg->select = ident_clone (selector);
+ if (selector != NULL)
+ cfg->select = ident_clone (selector);
+ else
+ cfg->select = NULL;
cfg->title = NULL;
cfg->vertical_label = NULL;
if (num_graphs == 0)
{
cfg = graph_create (file);
- graph_append (cfg);
+ graph_append (/* head = */ NULL, cfg);
graph_add_file (cfg, file);
}
return (0);
} /* }}} int register_file */
-static int FIXME_graph_create_from_file (const char *host, /* {{{ */
- const char *plugin, const char *plugin_instance,
- const char *type, const char *type_instance,
- const char *title)
-{
- graph_config_t *cfg;
-
- cfg = malloc (sizeof (*cfg));
- if (cfg == NULL)
- return (ENOMEM);
- memset (cfg, 0, sizeof (*cfg));
-
- cfg->select = ident_create (host, plugin, plugin_instance, type, type_instance);
-
- cfg->title = NULL;
- if (title != NULL)
- cfg->title = strdup (title);
- cfg->vertical_label = NULL;
- cfg->instances = NULL;
- cfg->next = NULL;
-
- graph_append (cfg);
-
- return (0);
-} /* }}} int FIXME_graph_create_from_file */
-
-/* FIXME: Actually read the config file here. */
-static int read_graph_config (void) /* {{{ */
-{
- if (graph_config_head != NULL)
- return (0);
-
- FIXME_graph_create_from_file (ANY_TOKEN, "cpu", ANY_TOKEN, "cpu", ALL_TOKEN,
- "CPU {instance} usage");
- FIXME_graph_create_from_file (ANY_TOKEN, "memory", "", "memory", ALL_TOKEN,
- "Memory usage");
- FIXME_graph_create_from_file (ANY_TOKEN, "swap", "", "swap", ALL_TOKEN,
- "Swap");
- FIXME_graph_create_from_file (ANY_TOKEN, ANY_TOKEN, ANY_TOKEN, "ps_state", ALL_TOKEN,
- "Processes");
- FIXME_graph_create_from_file (ANY_TOKEN, "cpu", ALL_TOKEN, "cpu", "idle",
- "CPU idle overview");
-
- return (0);
-} /* }}} int read_graph_config */
-
-static void gl_clear (void) /* {{{ */
-{
- graph_config_t *cfg;
-
- cfg = graph_config_head;
- graph_config_head = NULL;
- graph_destroy (cfg);
-
- gl_last_update = 0;
-} /* }}} void gl_clear */
-
static int callback_type (const char *type, void *user_data) /* {{{ */
{
gl_ident_stage_t *gl;
return (param (sec_key));
} /* }}} const char *get_part_from_param */
-int gl_ident_get_rrdargs (graph_config_t *cfg, /* {{{ */
- graph_instance_t *inst,
- graph_ident_t *ident,
- str_array_t *args)
+/* Create one DEF for each data source in the file. Called by
+ * "gl_inst_get_default_defs" for each file. */
+static graph_def_t *gl_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) || (inst == NULL) || (ident == NULL) || (args == NULL))
- return (EINVAL);
+ if ((cfg == NULL) || (ident == NULL))
+ return (def_head);
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_default_defs: ident_to_file returned NULL.\n");
+ return (def_head);
}
- DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
+ DEBUG ("gl_ident_get_default_defs: file = %s;\n", file);
status = ds_list_from_rrd_file (file, &dses_num, &dses);
if (status != 0)
{
free (file);
- return (status);
+ return (def_head);
}
for (i = 0; i < dses_num; i++)
{
- int index;
-
- DEBUG ("gl_ident_get_rrdargs: ds[%lu] = %s;\n", (unsigned long) i, dses[i]);
-
- index = array_argc (args);
-
- /* CDEFs */
- array_append_format (args, "DEF:def_%04i_min=%s:%s:MIN",
- index, file, dses[i]);
- array_append_format (args, "DEF:def_%04i_avg=%s:%s:AVERAGE",
- index, file, dses[i]);
- array_append_format (args, "DEF:def_%04i_max=%s:%s:MAX",
- index, file, dses[i]);
- /* 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, "LINE1:def_%04i_avg#%06"PRIx32":%s",
- index, get_random_color (), dses[i]);
- array_append_format (args, "GPRINT:vdef_%04i_min:%%lg min,", index);
- array_append_format (args, "GPRINT:vdef_%04i_avg:%%lg avg,", index);
- array_append_format (args, "GPRINT:vdef_%04i_max:%%lg max,", index);
- array_append_format (args, "GPRINT:vdef_%04i_lst:%%lg last\\l", index);
+ 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 (dses);
free (file);
+ return (defs);
+} /* }}} int gl_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
+ * "gl_instance_get_rrdargs" if no DEFs are available from the configuration.
+ * */
+static graph_def_t *gl_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 = gl_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 *gl_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 int gl_clear_instances (void) /* {{{ */
+{
+ graph_config_t *cfg;
+
+ for (cfg = graph_config_head; cfg != NULL; cfg = cfg->next)
+ {
+ instance_destroy (cfg->instances);
+ cfg->instances = NULL;
+ }
+
return (0);
-} /* }}} int gl_ident_get_rrdargs */
+} /* }}} int gl_clear_instances */
+
+
+/*
+ * 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
*/
+
+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 ("DEF", child->key) == 0)
+ def_config (cfg, child);
+ } /* for */
+
+ graph_append (&graph_config_staging, cfg);
+
+ return (0);
+} /* }}} graph_config_add */
+
+int graph_config_submit (void) /* {{{ */
+{
+ graph_config_t *tmp;
+
+ tmp = graph_config_head;
+ graph_config_head = graph_config_staging;
+ graph_config_staging = NULL;
+
+ graph_destroy (tmp);
+
+ return (0);
+} /* }}} int graph_config_submit */
+
int gl_instance_get_params (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
char *buffer, size_t buffer_size)
{
int gl_graph_get_title (graph_config_t *cfg, /* {{{ */
char *buffer, size_t buffer_size)
{
- char *str;
-
if ((cfg == NULL) || (buffer == NULL) || (buffer_size < 1))
return (EINVAL);
- if (cfg->title != NULL)
- str = cfg->title;
- else
- str = ident_to_string (cfg->select);
+ if (cfg->title == NULL)
+ cfg->title = ident_to_string (cfg->select);
- if (str == NULL)
+ if (cfg->title == NULL)
return (ENOMEM);
- strncpy (buffer, str, buffer_size);
+ strncpy (buffer, cfg->title, buffer_size);
buffer[buffer_size - 1] = 0;
- free (str);
-
return (0);
} /* }}} int gl_graph_get_title */
return (ident_clone (cfg->select));
} /* }}} graph_ident_t *gl_graph_get_selector */
+int gl_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 gl_graph_add_def */
+
+
int gl_instance_get_all (gl_inst_callback callback, /* {{{ */
void *user_data)
{
graph_instance_t *inst,
str_array_t *args)
{
- size_t i;
+ def_callback_data_t data = { inst, args };
+ graph_def_t *default_defs;
+ int status;
if ((cfg == NULL) || (inst == NULL) || (args == NULL))
return (EINVAL);
array_append (args, cfg->title);
}
- for (i = 0; i < inst->files_num; i++)
+ if (cfg->vertical_label != NULL)
{
- graph_def_t *def;
- int status;
+ array_append (args, "-v");
+ array_append (args, cfg->vertical_label);
+ }
- def = def_search (cfg->defs, inst->files[i]);
- if (def == NULL)
- {
- def = def_create (cfg, inst->files[i]);
- if (def == NULL)
- return (-1);
-
- if (cfg->defs == NULL)
- cfg->defs = def;
- else
- def_append (cfg->defs, def);
- }
+ if (cfg->defs == NULL)
+ {
+ default_defs = gl_inst_get_default_defs (cfg, inst);
- status = def_get_rrdargs (def, inst->files[i], args);
- if (status != 0)
- return (status);
+ if (default_defs == NULL)
+ return (-1);
+
+ status = def_foreach (default_defs, gl_instance_get_rrdargs_cb, &data);
+
+ if (default_defs != NULL)
+ def_destroy (default_defs);
+ }
+ else
+ {
+ status = def_foreach (cfg->defs, gl_instance_get_rrdargs_cb, &data);
}
- return (0);
+ return (status);
} /* }}} int gl_instance_get_rrdargs */
graph_ident_t *gl_instance_get_selector (graph_instance_t *inst) /* {{{ */
return (ident_clone (inst->select));
} /* }}} graph_ident_t *gl_instance_get_selector */
-/* DELETEME */
-
int gl_update (void) /* {{{ */
{
time_t now;
if ((gl_last_update + UPDATE_INTERVAL) >= now)
return (0);
- gl_clear ();
-
- read_graph_config ();
+ graph_read_config ();
memset (&gl, 0, sizeof (gl));
gl.host = NULL;
gl.type = NULL;
gl.type_instance = NULL;
+ gl_clear_instances ();
status = foreach_host (callback_host, &gl);
- /* print_graphs (); */
+ gl_last_update = now;
return (status);
} /* }}} int gl_update */