From: Florian Forster Date: Sat, 19 Jun 2010 06:06:50 +0000 (+0200) Subject: Merge branch 'search' X-Git-Tag: v4.0.0~236 X-Git-Url: https://git.octo.it/?p=collection4.git;a=commitdiff_plain;h=2c85f876c1b9d5c161694cfb03cf20773b74c844;hp=da630143f2fd8a152ec71a9c7ed8b0ae17081fa4 Merge branch 'search' Conflicts: src/graph_def.c src/graph_ident.h src/graph_instance.h --- diff --git a/collection.conf b/collection.conf index ede1458..eb33fe0 100644 --- a/collection.conf +++ b/collection.conf @@ -91,6 +91,7 @@ Title "Diskspace" VerticalLabel "Bytes" + ShowZero true TypeInstance "used" @@ -129,6 +130,8 @@ TypeInstance "/all/" Title "Diskspace (Inodes)" + VerticalLabel "Inodes" + ShowZero true TypeInstance "used" @@ -164,13 +167,16 @@ TypeInstance "/any/" Title "Diskspace (legacy)" + VerticalLabel "Bytes" + ShowZero true DSName "used" - Legend "Used " + Legend "Used" Color "ff0000" Area true #Stack true + Format "%5.1lf%s" DSName "free" @@ -178,6 +184,7 @@ Color "00bf00" Area true Stack true + Format "%5.1lf%s" @@ -274,6 +281,7 @@ Title "Memory utilization" VerticalLabel "Bytes" + ShowZero true TypeInstance "free" diff --git a/src/Makefile.am b/src/Makefile.am index de0cb7e..9b235ba 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -15,6 +15,7 @@ collection_fcgi_SOURCES = main.c \ 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 \ diff --git a/src/action_list_graphs.c b/src/action_list_graphs.c index 395d8a1..b360c1d 100644 --- a/src/action_list_graphs.c +++ b/src/action_list_graphs.c @@ -5,6 +5,7 @@ #include "action_list_graphs.h" #include "graph.h" +#include "graph_ident.h" #include "graph_list.h" #include "utils_params.h" @@ -62,13 +63,33 @@ static int list_graphs_json (void) /* {{{ */ 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, - __attribute__((unused)) void *user_data) + void *user_data) { + callback_data_t *data = user_data; char params[1024]; char desc[1024]; + if (data->cfg != cfg) + { + if (data->cfg != NULL) + printf (" \n"); + + memset (desc, 0, sizeof (desc)); + graph_get_title (cfg, desc, sizeof (desc)); + + printf ("
  • %s\n
      \n", desc); + + data->cfg = cfg; + } + memset (params, 0, sizeof (params)); inst_get_params (cfg, inst, params, sizeof (params)); @@ -81,27 +102,20 @@ static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */ return (0); } /* }}} int print_graph_inst_html */ -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 ("
    • %s\n
        \n", buffer); - gl_graph_instance_get_all (cfg, print_graph_inst_html, /* user_data = */ NULL); - printf ("
    • \n"); - - return (0); -} /* }}} int print_graph_html */ - -static int list_graphs_html (void) /* {{{ */ +static int list_graphs_html (const char *term) /* {{{ */ { + callback_data_t data = { NULL }; printf ("Content-Type: text/html\n\n"); printf ("
        \n"); - gl_graph_get_all (print_graph_html, /* user_data = */ NULL); + 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 ("
      \n"); + printf ("
    \n"); return (0); @@ -120,7 +134,7 @@ int action_list_graphs (void) /* {{{ */ if (strcmp ("json", format) == 0) return (list_graphs_json ()); else - return (list_graphs_html ()); + return (list_graphs_html (param ("search"))); } /* }}} int action_list_graphs */ /* vim: set sw=2 sts=2 et fdm=marker : */ diff --git a/src/graph.c b/src/graph.c index 154af77..2809ebb 100644 --- a/src/graph.c +++ b/src/graph.c @@ -28,6 +28,7 @@ struct graph_config_s /* {{{ */ char *title; char *vertical_label; + _Bool show_zero; graph_def_t *defs; @@ -154,6 +155,8 @@ int graph_config_add (const oconfig_item_t *ci) /* {{{ */ 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 */ @@ -247,6 +250,57 @@ _Bool graph_matches (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */ 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)) @@ -284,6 +338,12 @@ int graph_get_rrdargs (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */ array_append (args, cfg->vertical_label); } + if (cfg->show_zero) + { + array_append (args, "-l"); + array_append (args, "0"); + } + return (0); } /* }}} int graph_get_rrdargs */ diff --git a/src/graph.h b/src/graph.h index 6e9ae07..57a866f 100644 --- a/src/graph.h +++ b/src/graph.h @@ -1,15 +1,7 @@ #ifndef GRAPH_H #define GRAPH_H 1 -/* - * 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" +#include "graph_types.h" #include "oconfig.h" #include "utils_array.h" @@ -37,6 +29,9 @@ 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); diff --git a/src/graph_def.c b/src/graph_def.c index 927b3fe..cecc8f0 100644 --- a/src/graph_def.c +++ b/src/graph_def.c @@ -5,6 +5,7 @@ #include "graph_def.h" #include "graph.h" #include "graph_config.h" +#include "graph_ident.h" #include "common.h" #include "oconfig.h" @@ -329,13 +330,13 @@ int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */ (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 : "%lg"); + index, (def->format != NULL) ? def->format : "%6.2lf"); free (file); diff --git a/src/graph_def.h b/src/graph_def.h index b2cd228..a4bb7fd 100644 --- a/src/graph_def.h +++ b/src/graph_def.h @@ -1,14 +1,7 @@ #ifndef GRAPH_DEF_H #define GRAPH_DEF_H 1 -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" +#include "graph_types.h" #include "utils_array.h" #include "oconfig.h" diff --git a/src/graph_ident.h b/src/graph_ident.h index af86c9d..e2f2ba6 100644 --- a/src/graph_ident.h +++ b/src/graph_ident.h @@ -2,6 +2,7 @@ #define GRAPH_IDENT_H 1 #include +#include "graph_types.h" #define ANY_TOKEN "/any/" #define ALL_TOKEN "/all/" @@ -9,9 +10,6 @@ #define IS_ANY(str) (((str) != NULL) && (strcasecmp (ANY_TOKEN, (str)) == 0)) #define IS_ALL(str) (((str) != NULL) && (strcasecmp (ALL_TOKEN, (str)) == 0)) -struct graph_ident_s; -typedef struct graph_ident_s graph_ident_t; - graph_ident_t *ident_create (const char *host, const char *plugin, const char *plugin_instance, const char *type, const char *type_instance); @@ -42,7 +40,7 @@ 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); + const graph_ident_t *ident); char *ident_to_string (const graph_ident_t *ident); char *ident_to_file (const graph_ident_t *ident); diff --git a/src/graph_instance.c b/src/graph_instance.c index bad0bcd..90d1753 100644 --- a/src/graph_instance.c +++ b/src/graph_instance.c @@ -4,6 +4,7 @@ #include #include "graph_instance.h" +#include "graph_def.h" #include "graph_ident.h" #include "graph_list.h" #include "common.h" @@ -415,6 +416,38 @@ int inst_foreach (graph_instance_t *inst, /* {{{ */ 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) { diff --git a/src/graph_instance.h b/src/graph_instance.h index 72ab66a..f1a4486 100644 --- a/src/graph_instance.h +++ b/src/graph_instance.h @@ -1,23 +1,12 @@ #ifndef GRAPH_INSTANCE_H #define GRAPH_INSTANCE_H 1 -/* - * 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); - #include -#include "graph.h" +#include "graph_types.h" #include "utils_array.h" /* - * Callback types - */ -/* * Methods */ graph_instance_t *inst_create (graph_config_t *cfg, @@ -42,6 +31,10 @@ 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); diff --git a/src/graph_list.c b/src/graph_list.c index 965fcba..3c8d375 100644 --- a/src/graph_list.c +++ b/src/graph_list.c @@ -153,7 +153,7 @@ int gl_config_submit (void) /* {{{ */ return (0); } /* }}} int graph_config_submit */ -int gl_graph_get_all (gl_cfg_callback callback, /* {{{ */ +int gl_graph_get_all (graph_callback_t callback, /* {{{ */ void *user_data) { size_t i; @@ -211,7 +211,7 @@ graph_config_t *gl_graph_get_selected (void) /* {{{ */ struct gl_inst_callback_data /* {{{ */ { graph_config_t *cfg; - gl_inst_callback callback; + graph_inst_callback_t callback; void *user_data; }; /* }}} struct gl_inst_callback_data */ @@ -224,7 +224,7 @@ static int gl_inst_callback_handler (graph_instance_t *inst, /* {{{ */ } /* }}} int gl_inst_callback_handler */ int gl_graph_instance_get_all (graph_config_t *cfg, /* {{{ */ - gl_inst_callback callback, void *user_data) + graph_inst_callback_t callback, void *user_data) { struct gl_inst_callback_data data = { @@ -240,7 +240,7 @@ int gl_graph_instance_get_all (graph_config_t *cfg, /* {{{ */ gl_inst_callback_handler, &data)); } /* }}} int gl_graph_instance_get_all */ -int gl_instance_get_all (gl_inst_callback callback, /* {{{ */ +int gl_instance_get_all (graph_inst_callback_t callback, /* {{{ */ void *user_data) { size_t i; @@ -260,6 +260,25 @@ int gl_instance_get_all (gl_inst_callback callback, /* {{{ */ } /* }}} 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; diff --git a/src/graph_list.h b/src/graph_list.h index c17d6fc..e6b6888 100644 --- a/src/graph_list.h +++ b/src/graph_list.h @@ -1,33 +1,27 @@ #ifndef GRAPH_LIST_H #define GRAPH_LIST_H 1 +#include "graph_types.h" +#include "graph.h" #include "graph_instance.h" /* - * 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); - -/* * Functions */ int gl_add_graph (graph_config_t *cfg); int gl_config_submit (void); -int gl_graph_get_all (gl_cfg_callback callback, - void *user_data); - graph_config_t *gl_graph_get_selected (void); -int gl_graph_instance_get_all (graph_config_t *cfg, - gl_inst_callback callback, void *user_data); +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_instance_get_all (gl_inst_callback callback, +int gl_search (const char *search, graph_inst_callback_t callback, void *user_data); int gl_update (void); diff --git a/src/graph_types.h b/src/graph_types.h new file mode 100644 index 0000000..623e671 --- /dev/null +++ b/src/graph_types.h @@ -0,0 +1,35 @@ +#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 : */