From 4ff80b5e2415d172f9cafb25f1f35b99204115c4 Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Sun, 1 Apr 2007 15:23:09 +0200 Subject: [PATCH] remove plugin scratchpad memory --- include/plugins.h | 6 ++--- plugins.c | 63 +++++++++++++++++++++-------------------------- plugins/ctstat.c | 24 +++++++++++++++--- plugins/memory.c | 44 ++++++++++++++++++++++----------- plugins/netdev.c | 24 +++++++++++++++--- plugins/rtstat.c | 24 +++++++++++++++--- plugins/stat.c | 40 +++++++++++++++++++++--------- plugins/vmstat.c | 40 +++++++++++++++++++++--------- 8 files changed, 177 insertions(+), 88 deletions(-) diff --git a/include/plugins.h b/include/plugins.h index 05a9938..9e85463 100644 --- a/include/plugins.h +++ b/include/plugins.h @@ -8,15 +8,15 @@ struct sammler_plugin { struct list_head list; + void *dlhandle; const char *name; /* timing */ unsigned int interval; time_t lastprobe; - /* scratchpad memory */ - int bufsize; - char *buffer; + /* flags */ + int flags; int (*init) (void); int (*fini) (void); diff --git a/plugins.c b/plugins.c index b9cc573..d627378 100644 --- a/plugins.c +++ b/plugins.c @@ -31,58 +31,56 @@ #include "network.h" #include "plugins.h" -static LIST_HEAD(plugin_list); +#define FLAGS_ACTIVE 0x01 -static char *scratchpad; +static LIST_HEAD(plugin_list); static int plugin_init_cb(const char *filename, void *privdata) { - int *bufsize = (int *)privdata; - static const char *plugin_dir; if (plugin_dir == NULL) plugin_dir = config_get_string("global", "plugin_dir", "."); - char *buffer = malloc(PATH_MAX); - if (buffer == NULL) { + char *fullname = malloc(PATH_MAX); + if (fullname == NULL) { log_print(LOG_ERROR, "plugin_load: out of memory"); return -1; } - int len = snprintf(buffer, PATH_MAX, "%s/%s", plugin_dir, filename); + int len = snprintf(fullname, PATH_MAX, "%s/%s", plugin_dir, filename); if (len < 0 || len >= PATH_MAX) { log_print(LOG_ERROR, "plugin_load: file name too long: %s/%s", plugin_dir, filename); - free(buffer); + free(fullname); return -1; } dlerror(); - void *tmp = dlopen(buffer, RTLD_NOW); - if (tmp == NULL) { + void *dlhandle = dlopen(fullname, RTLD_NOW); + if (dlhandle == NULL) { log_print(LOG_ERROR, "plugin_load: dlopen: %s", dlerror()); - free(buffer); + free(fullname); return -1; } - free(buffer); + free(fullname); - struct sammler_plugin *plugin = dlsym(tmp, "plugin"); + struct sammler_plugin *plugin = dlsym(dlhandle, "plugin"); if (plugin == NULL) { log_print(LOG_ERROR, "plugin_load: failed to load '%s'", filename); - dlclose(tmp); + dlclose(dlhandle); + return -1; + } + + if (plugin->init != NULL && (plugin->init() != 0)) { + log_print(LOG_ERROR, "Plugin '%s': init failed", plugin->name); + dlclose(dlhandle); return -1; } log_print(LOG_INFO, "Plugin '%s' loaded", plugin->name); plugin->lastprobe = 0; - - if (plugin->init != NULL && (plugin->init() != 0)) { - log_print(LOG_ERROR, "Plugin '%s': init failed", plugin->name); - return -1; - } - - if (plugin->bufsize > *bufsize) - *bufsize = plugin->bufsize; + plugin->flags = FLAGS_ACTIVE; + plugin->dlhandle = dlhandle; list_add_tail(&plugin->list, &plugin_list); return 0; @@ -95,8 +93,14 @@ static int plugins_probe(void *privdata) struct sammler_plugin *plugin; list_for_each_entry(plugin, &plugin_list, list) { + if (!(plugin->flags & FLAGS_ACTIVE)) + continue; + if (plugin->lastprobe + plugin->interval <= now) { - plugin->probe(); + if (plugin->probe() != 0) { + log_print(LOG_ERROR, "plugin_probe(): plugin %s disabled", plugin->name); + plugin->flags &= ~FLAGS_ACTIVE; + } plugin->lastprobe = now; } } @@ -107,23 +111,12 @@ static int plugins_probe(void *privdata) int plugin_init(void) { - int bufsize = 0; - int cnt = config_get_strings("global", "plugin", plugin_init_cb, &bufsize); + int cnt = config_get_strings("global", "plugin", plugin_init_cb, NULL); if (cnt == 0) { log_print(LOG_ERROR, "plugin_init(): no working plugins"); return -1; } - scratchpad = malloc(bufsize); - if (scratchpad == NULL) { - log_print(LOG_ERROR, "plugin_init(): out of memory"); - return -1; - } - - struct sammler_plugin *plugin; - list_for_each_entry(plugin, &plugin_list, list) - plugin->buffer = scratchpad; - struct timeval tv; tv.tv_sec = 1; tv.tv_usec = 0; diff --git a/plugins/ctstat.c b/plugins/ctstat.c index 7dc0321..4cc47ab 100644 --- a/plugins/ctstat.c +++ b/plugins/ctstat.c @@ -25,7 +25,10 @@ #include "plugins.h" #include "probe.h" +#define BUFSIZE 1024 + struct sammler_plugin plugin; +static char *buffer; static const char *ds_def = { "DS:entries:GAUGE:15:0:U " @@ -64,11 +67,11 @@ static int probe(void) return -1; } - while (fgets(plugin.buffer, plugin.bufsize, fp) != NULL) { - if (!strncmp(plugin.buffer, "entries", 7)) + while (fgets(buffer, BUFSIZE, fp) != NULL) { + if (!strncmp(buffer, "entries", 7)) continue; - if (strsplit(plugin.buffer, " \t\n", val, 16) != 16) + if (strsplit(buffer, " \t\n", val, 16) != 16) continue; for (i = 0; i < 16; i++) @@ -91,10 +94,23 @@ static int probe(void) return 0; } +static int init(void) +{ + buffer = malloc(BUFSIZE); + return (buffer == NULL) ? -1 : 0; +} + +static int fini(void) +{ + free(buffer); + return 0; +} + struct sammler_plugin plugin = { .name = "ctstat", .interval = 10, - .bufsize = 1024, + .init = &init, + .fini = &fini, .probe = &probe, .get_ds = &get_ds, }; diff --git a/plugins/memory.c b/plugins/memory.c index 9bd2dbb..b7497f4 100644 --- a/plugins/memory.c +++ b/plugins/memory.c @@ -27,7 +27,10 @@ #define DS_MEMORY 1 #define DS_SWAP 2 +#define BUFSIZE 1024 + struct sammler_plugin plugin; +static char *buffer; static const char *mem_ds_def = { "DS:total:GAUGE:15:0:U " @@ -77,24 +80,24 @@ static int probe(void) return -1; } - while (fgets(plugin.buffer, plugin.bufsize, fp) != NULL) { - if (!strncmp(plugin.buffer, "MemTotal:", 9)) - meminfo.memtotal = atoll(plugin.buffer + 10); + while (fgets(buffer, BUFSIZE, fp) != NULL) { + if (!strncmp(buffer, "MemTotal:", 9)) + meminfo.memtotal = atoll(buffer + 10); - else if (!strncmp(plugin.buffer, "MemFree:", 8)) - meminfo.memfree = atoll(plugin.buffer + 9); + else if (!strncmp(buffer, "MemFree:", 8)) + meminfo.memfree = atoll(buffer + 9); - else if (!strncmp(plugin.buffer, "Buffers:", 8)) - meminfo.buffers = atoll(plugin.buffer + 9); + else if (!strncmp(buffer, "Buffers:", 8)) + meminfo.buffers = atoll(buffer + 9); - else if (!strncmp(plugin.buffer, "Cached:", 7)) - meminfo.cached = atoll(plugin.buffer + 8); + else if (!strncmp(buffer, "Cached:", 7)) + meminfo.cached = atoll(buffer + 8); - else if (!strncmp(plugin.buffer, "SwapTotal:", 10)) - meminfo.swaptotal = atoll(plugin.buffer + 11); + else if (!strncmp(buffer, "SwapTotal:", 10)) + meminfo.swaptotal = atoll(buffer + 11); - else if (!strncmp(plugin.buffer, "SwapFree:", 9)) - meminfo.swapfree = atoll(plugin.buffer + 10); + else if (!strncmp(buffer, "SwapFree:", 9)) + meminfo.swapfree = atoll(buffer + 10); } probe_submit(&plugin, "memory.rrd", DS_MEMORY, "%llu:%llu:%llu:%llu", @@ -108,10 +111,23 @@ static int probe(void) return 0; } +static int init(void) +{ + buffer = malloc(BUFSIZE); + return (buffer == NULL) ? -1 : 0; +} + +static int fini(void) +{ + free(buffer); + return 0; +} + struct sammler_plugin plugin = { .name = "memory", .interval = 10, - .bufsize = 1024, + .init = &init, + .fini = &fini, .probe = &probe, .get_ds = &get_ds, }; diff --git a/plugins/netdev.c b/plugins/netdev.c index a4f290e..3732bfd 100644 --- a/plugins/netdev.c +++ b/plugins/netdev.c @@ -25,7 +25,10 @@ #include "plugins.h" #include "probe.h" +#define BUFSIZE 1024 + struct sammler_plugin plugin; +static char *buffer; static const char *ds_def = { "DS:byte_in:COUNTER:15:0:U " @@ -52,14 +55,14 @@ static int probe(void) return -1; } - while (fgets(plugin.buffer, plugin.bufsize, fp) != NULL) { + while (fgets(buffer, BUFSIZE, fp) != NULL) { - if (!(stats = strchr(plugin.buffer, ':'))) + if (!(stats = strchr(buffer, ':'))) continue; *stats++ = '\0'; - device = plugin.buffer; + device = buffer; while (*device == ' ') device++; @@ -80,10 +83,23 @@ static int probe(void) return 0; } +static int init(void) +{ + buffer = malloc(BUFSIZE); + return (buffer == NULL) ? -1 : 0; +} + +static int fini(void) +{ + free(buffer); + return 0; +} + struct sammler_plugin plugin = { .name = "netdev", .interval = 10, - .bufsize = 1024, + .init = &init, + .fini = &fini, .probe = &probe, .get_ds = &get_ds, }; diff --git a/plugins/rtstat.c b/plugins/rtstat.c index 044003a..fd83362 100644 --- a/plugins/rtstat.c +++ b/plugins/rtstat.c @@ -28,7 +28,10 @@ #define DS_STAT 1 #define DS_GC 2 +#define BUFSIZE 1024 + struct sammler_plugin plugin; +static char *buffer; static const char *ds_def_stat = { "DS:in_hit:DERIVE:15:0:U " @@ -80,11 +83,11 @@ static int probe(void) return -1; } - while (fgets(plugin.buffer, plugin.bufsize, fp) != NULL) { - if (!strncmp(plugin.buffer, "entries", 7)) + while (fgets(buffer, BUFSIZE, fp) != NULL) { + if (!strncmp(buffer, "entries", 7)) continue; - if (strsplit(plugin.buffer, " \t\n", val, 17) != 17) + if (strsplit(buffer, " \t\n", val, 17) != 17) continue; for (i = 0; i < 17; i++) @@ -114,10 +117,23 @@ static int probe(void) return 0; } +static int init(void) +{ + buffer = malloc(BUFSIZE); + return (buffer == NULL) ? -1 : 0; +} + +static int fini(void) +{ + free(buffer); + return 0; +} + struct sammler_plugin plugin = { .name = "rtstat", .interval = 10, - .bufsize = 1024, + .init = &init, + .fini = &fini, .probe = &probe, .get_ds = &get_ds, }; diff --git a/plugins/stat.c b/plugins/stat.c index ff561e9..2077b43 100644 --- a/plugins/stat.c +++ b/plugins/stat.c @@ -28,7 +28,10 @@ #define DS_CPU 1 #define DS_PROC 2 +#define BUFSIZE 1024 + struct sammler_plugin plugin; +static char *buffer; static const char *cpu_ds_def = { "DS:user:COUNTER:15:0:U " @@ -80,13 +83,13 @@ static int probe(void) return -1; } - while (fgets(plugin.buffer, plugin.bufsize, fp) != NULL) { - if (!strncmp(plugin.buffer, "cpu", 3)) { + while (fgets(buffer, BUFSIZE, fp) != NULL) { + if (!strncmp(buffer, "cpu", 3)) { char *val[9], filename[16]; int numfields, len, cpu; - if ((plugin.buffer[3] >= '0') && (plugin.buffer[3] <= '9')) { - cpu = atoi(plugin.buffer +3); + if ((buffer[3] >= '0') && (buffer[3] <= '9')) { + cpu = atoi(buffer +3); len = snprintf(filename, sizeof(filename), "cpu-%d.rrd", cpu); if (len < 0 || len >= sizeof(filename)) continue; @@ -95,7 +98,7 @@ static int probe(void) strncpy(filename, "cpu.rrd", sizeof(filename)); } - numfields = strsplit(plugin.buffer, " \t\n", val, 9); + numfields = strsplit(buffer, " \t\n", val, 9); if (numfields < 5) continue; @@ -110,14 +113,14 @@ static int probe(void) val[1], val[2], val[3], val[4], val[5], val[6], val[7], val[8]); - } else if (!strncmp(plugin.buffer, "intr", 4)) { - proc.intr = atoll(plugin.buffer + 5); + } else if (!strncmp(buffer, "intr", 4)) { + proc.intr = atoll(buffer + 5); - } else if (!strncmp(plugin.buffer, "ctxt", 4)) { - proc.ctxt = atoll(plugin.buffer + 5); + } else if (!strncmp(buffer, "ctxt", 4)) { + proc.ctxt = atoll(buffer + 5); - } else if (!strncmp(plugin.buffer, "processes", 9)) { - proc.fork = atoll(plugin.buffer + 10); + } else if (!strncmp(buffer, "processes", 9)) { + proc.fork = atoll(buffer + 10); } } @@ -128,10 +131,23 @@ static int probe(void) return 0; } +static int init(void) +{ + buffer = malloc(BUFSIZE); + return (buffer == NULL) ? -1 : 0; +} + +static int fini(void) +{ + free(buffer); + return 0; +} + struct sammler_plugin plugin = { .name = "stat", .interval = 10, - .bufsize = 1024, + .init = &init, + .fini = &fini, .probe = &probe, .get_ds = &get_ds, }; diff --git a/plugins/vmstat.c b/plugins/vmstat.c index 0e61313..08097ba 100644 --- a/plugins/vmstat.c +++ b/plugins/vmstat.c @@ -24,7 +24,10 @@ #include "plugins.h" #include "probe.h" +#define BUFSIZE 1024 + struct sammler_plugin plugin; +static char *buffer; static const char *ds_def = { "DS:pgalloc_high:DERIVE:15:0:U " @@ -60,21 +63,21 @@ static int probe(void) return -1; } - while (fgets(plugin.buffer, plugin.bufsize, fp) != NULL) { - if (!strncmp(plugin.buffer, "pgalloc_high", 12)) - vmstat.pgalloc_high = atoll(plugin.buffer + 13); + while (fgets(buffer, BUFSIZE, fp) != NULL) { + if (!strncmp(buffer, "pgalloc_high", 12)) + vmstat.pgalloc_high = atoll(buffer + 13); - else if (!strncmp(plugin.buffer, "pgalloc_normal", 14)) - vmstat.pgalloc_normal = atoll(plugin.buffer + 15); + else if (!strncmp(buffer, "pgalloc_normal", 14)) + vmstat.pgalloc_normal = atoll(buffer + 15); - else if (!strncmp(plugin.buffer, "pgalloc_dma", 11)) - vmstat.pgalloc_dma = atoll(plugin.buffer + 12); + else if (!strncmp(buffer, "pgalloc_dma", 11)) + vmstat.pgalloc_dma = atoll(buffer + 12); - else if (!strncmp(plugin.buffer, "pgfree", 6)) - vmstat.pgfree = atoll(plugin.buffer + 7); + else if (!strncmp(buffer, "pgfree", 6)) + vmstat.pgfree = atoll(buffer + 7); - else if (!strncmp(plugin.buffer, "pgfault", 7)) - vmstat.pgfault = atoll(plugin.buffer + 8); + else if (!strncmp(buffer, "pgfault", 7)) + vmstat.pgfault = atoll(buffer + 8); } probe_submit(&plugin, "vmstat.rrd", 0, "%llu:%llu:%llu:%llu:%llu", @@ -85,10 +88,23 @@ static int probe(void) return 0; } +static int init(void) +{ + buffer = malloc(BUFSIZE); + return (buffer == NULL) ? -1 : 0; +} + +static int fini(void) +{ + free(buffer); + return 0; +} + struct sammler_plugin plugin = { .name = "vmstat", .interval = 10, - .bufsize = 1024, + .init = &init, + .fini = &fini, .probe = &probe, .get_ds = &get_ds, };