X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=graph_list.c;h=3edcb3284ca468a4df8b844e51e74ca56295f410;hb=964b6bdd9fc86ed17e00e01abbe423166326b45d;hp=fc63620f7b5e9f5b36b78a3eb015f67e6736ff7f;hpb=a38bcd4f039e3354add408ad44f592b709335fef;p=collection4.git diff --git a/graph_list.c b/graph_list.c index fc63620..3edcb32 100644 --- a/graph_list.c +++ b/graph_list.c @@ -10,6 +10,7 @@ #include "graph_list.h" #include "graph_ident.h" #include "graph_def.h" +#include "graph_config.h" #include "common.h" #include "utils_params.h" @@ -61,10 +62,18 @@ struct graph_config_s /* {{{ */ 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; @@ -183,6 +192,12 @@ static graph_instance_t *instance_create (graph_config_t *cfg, /* {{{ */ 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; @@ -254,20 +269,24 @@ static int graph_add_file (graph_config_t *cfg, const graph_ident_t *file) /* {{ 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; @@ -285,7 +304,10 @@ static graph_config_t *graph_create (const graph_ident_t *selector) /* {{{ */ 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; @@ -342,70 +364,13 @@ static int register_file (const graph_ident_t *file) /* {{{ */ 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; @@ -526,81 +491,200 @@ static const char *get_part_from_param (const char *prim_key, /* {{{ */ 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_ident_get_rrdargs */ +} /* }}} int gl_instance_get_rrdargs_cb */ + +/* + * Config functions + */ +static int config_get_string (const oconfig_item_t *ci, char **ret_str) +{ + char *tmp; + + if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) + return (EINVAL); + + tmp = strdup (ci->values[0].value.string); + if (tmp == NULL) + return (ENOMEM); + + free (*ret_str); + *ret_str = tmp; + + return (0); +} /* }}} int config_get_string */ /* * Global functions */ +int graph_config_add (const oconfig_item_t *ci) +{ + char *host = NULL; + char *plugin = NULL; + char *plugin_instance = NULL; + char *type = NULL; + char *type_instance = NULL; + graph_config_t *cfg = NULL; + int i; + + cfg = graph_create (/* selector = */ NULL); + if (cfg == NULL) + return (ENOMEM); + + for (i = 0; i < ci->children_num; i++) + { + oconfig_item_t *child; + + child = ci->children + i; + + if (strcasecmp ("Host", child->key) == 0) + config_get_string (child, &host); + else if (strcasecmp ("Plugin", child->key) == 0) + config_get_string (child, &plugin); + else if (strcasecmp ("PluginInstance", child->key) == 0) + config_get_string (child, &plugin_instance); + else if (strcasecmp ("Type", child->key) == 0) + config_get_string (child, &type); + else if (strcasecmp ("TypeInstance", child->key) == 0) + config_get_string (child, &type_instance); + else if (strcasecmp ("Title", child->key) == 0) + config_get_string (child, &cfg->title); + else if (strcasecmp ("VerticalLabel", child->key) == 0) + config_get_string (child, &cfg->vertical_label); + /* TODO: DEFs! */ + } /* for */ + + cfg->select = ident_create (host, plugin, plugin_instance, + type, type_instance); + if (cfg->select == NULL) + { + graph_destroy (cfg); + return (EINVAL); + } + + 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) { @@ -767,24 +851,18 @@ int gl_graph_instance_get_all (graph_config_t *cfg, /* {{{ */ 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 */ @@ -824,7 +902,9 @@ int gl_instance_get_rrdargs (graph_config_t *cfg, /* {{{ */ 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); @@ -835,30 +915,30 @@ int gl_instance_get_rrdargs (graph_config_t *cfg, /* {{{ */ 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) /* {{{ */ @@ -886,9 +966,7 @@ int gl_update (void) /* {{{ */ 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; @@ -899,7 +977,7 @@ int gl_update (void) /* {{{ */ status = foreach_host (callback_host, &gl); - /* print_graphs (); */ + gl_last_update = now; return (status); } /* }}} int gl_update */