From b94e64cd456b739886f12b029db7aecd15b6139b Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Sat, 31 Mar 2007 23:21:31 +0200 Subject: [PATCH] global plugin scratchpad memory --- Makefile | 2 +- p_apache.c | 4 ++-- p_ctstat.c | 29 ++++------------------------- p_memory.c | 49 ++++++++++++++----------------------------------- p_mysql.c | 4 ++-- p_netdev.c | 29 ++++------------------------- p_rtstat.c | 29 ++++------------------------- p_stat.c | 45 ++++++++++++--------------------------------- p_vmstat.c | 45 ++++++++++++--------------------------------- plugins.c | 28 ++++++++++++++++++++++++---- plugins.h | 13 +++++++++++-- 11 files changed, 90 insertions(+), 187 deletions(-) diff --git a/Makefile b/Makefile index df8b73d..527ec17 100644 --- a/Makefile +++ b/Makefile @@ -58,4 +58,4 @@ p_mysql.so: p_mysql_sh.o p_mysql_helper_sh.o clean: rm -rf *.d *.o *.so sammler -#-include $(SAMMLER_SRC:.c=.d) $(PLUGIN_SRC:.c=.d) +-include $(SAMMLER_SRC:.c=.d) $(PLUGIN_SRC:.c=.d) diff --git a/p_apache.c b/p_apache.c index 078cdfc..3cc3f92 100644 --- a/p_apache.c +++ b/p_apache.c @@ -31,6 +31,8 @@ #define BUFSIZE 8192 +struct sammler_plugin plugin; + struct server_entry { struct list_head list; CURL *handle; @@ -53,8 +55,6 @@ struct stats { uint64_t idle_workers; }; -struct sammler_plugin plugin; - static char *rx_buf; static int rx_pos; diff --git a/p_ctstat.c b/p_ctstat.c index 34767eb..e2200f9 100644 --- a/p_ctstat.c +++ b/p_ctstat.c @@ -24,8 +24,6 @@ #include "helper.h" #include "plugins.h" -#define BUFSIZE 1024 - struct sammler_plugin plugin; static const char *ds_def = { @@ -52,8 +50,6 @@ static const char * get_ds(int ds_id) return ds_def; } -static char *buffer; - static int probe(void) { FILE *fp; @@ -67,11 +63,11 @@ static int probe(void) return -1; } - while (fgets(buffer, BUFSIZE, fp) != NULL) { - if (!strncmp(buffer, "entries", 7)) + while (fgets(plugin.buffer, plugin.bufsize, fp) != NULL) { + if (!strncmp(plugin.buffer, "entries", 7)) continue; - if (strsplit(buffer, " \t\n", val, 16) != 16) + if (strsplit(plugin.buffer, " \t\n", val, 16) != 16) continue; for (i = 0; i < 16; i++) @@ -94,27 +90,10 @@ static int probe(void) return 0; } -static int init(void) -{ - buffer = malloc(BUFSIZE); - if (buffer == NULL) { - log_print(LOG_WARN, "plugin ctstat: out of memory"); - return -1; - } - return 0; -} - -static int fini(void) -{ - free(buffer); - return 0; -} - struct sammler_plugin plugin = { .name = "ctstat", .interval = 10, - .init = &init, - .fini = &fini, + .bufsize = 1024, .probe = &probe, .get_ds = &get_ds, }; diff --git a/p_memory.c b/p_memory.c index c0fd9e8..b4a80df 100644 --- a/p_memory.c +++ b/p_memory.c @@ -23,8 +23,6 @@ #include "plugins.h" -#define BUFSIZE 1024 - #define DS_MEMORY 1 #define DS_SWAP 2 @@ -65,8 +63,6 @@ struct meminfo_ { unsigned long long swapfree; }; -static char *buffer; - static int probe(void) { FILE *fp; @@ -80,24 +76,24 @@ static int probe(void) return -1; } - while (fgets(buffer, BUFSIZE, fp) != NULL) { - if (!strncmp(buffer, "MemTotal:", 9)) - meminfo.memtotal = atoll(buffer + 10); + while (fgets(plugin.buffer, plugin.bufsize, fp) != NULL) { + if (!strncmp(plugin.buffer, "MemTotal:", 9)) + meminfo.memtotal = atoll(plugin.buffer + 10); - else if (!strncmp(buffer, "MemFree:", 8)) - meminfo.memfree = atoll(buffer + 9); + else if (!strncmp(plugin.buffer, "MemFree:", 8)) + meminfo.memfree = atoll(plugin.buffer + 9); - else if (!strncmp(buffer, "Buffers:", 8)) - meminfo.buffers = atoll(buffer + 9); + else if (!strncmp(plugin.buffer, "Buffers:", 8)) + meminfo.buffers = atoll(plugin.buffer + 9); - else if (!strncmp(buffer, "Cached:", 7)) - meminfo.cached = atoll(buffer + 8); + else if (!strncmp(plugin.buffer, "Cached:", 7)) + meminfo.cached = atoll(plugin.buffer + 8); - else if (!strncmp(buffer, "SwapTotal:", 10)) - meminfo.swaptotal = atoll(buffer + 11); + else if (!strncmp(plugin.buffer, "SwapTotal:", 10)) + meminfo.swaptotal = atoll(plugin.buffer + 11); - else if (!strncmp(buffer, "SwapFree:", 9)) - meminfo.swapfree = atoll(buffer + 10); + else if (!strncmp(plugin.buffer, "SwapFree:", 9)) + meminfo.swapfree = atoll(plugin.buffer + 10); } probe_submit(&plugin, "memory.rrd", DS_MEMORY, "%llu:%llu:%llu:%llu", @@ -111,27 +107,10 @@ static int probe(void) return 0; } -static int init(void) -{ - buffer = malloc(BUFSIZE); - if (buffer == NULL) { - log_print(LOG_WARN, "plugin memory: out of memory"); - return -1; - } - return 0; -} - -static int fini(void) -{ - free(buffer); - return 0; -} - struct sammler_plugin plugin = { .name = "memory", .interval = 10, - .init = &init, - .fini = &fini, + .bufsize = 1024, .probe = &probe, .get_ds = &get_ds, }; diff --git a/p_mysql.c b/p_mysql.c index 1919cc8..b093121 100644 --- a/p_mysql.c +++ b/p_mysql.c @@ -33,6 +33,8 @@ #define DS_QCACHE 3 #define DS_THREADS 4 +struct sammler_plugin plugin; + struct server_entry { struct list_head list; void *mysql; @@ -93,8 +95,6 @@ static const char * get_ds(int ds_id) } } -struct sammler_plugin plugin; - static int probe(void) { struct mysql_stats stats; diff --git a/p_netdev.c b/p_netdev.c index b4a221f..64f8138 100644 --- a/p_netdev.c +++ b/p_netdev.c @@ -24,8 +24,6 @@ #include "helper.h" #include "plugins.h" -#define BUFSIZE 1024 - struct sammler_plugin plugin; static const char *ds_def = { @@ -40,8 +38,6 @@ static const char * get_ds(int ds_id) return ds_def; } -static char *buffer; - static int probe(void) { FILE *fp; @@ -55,14 +51,14 @@ static int probe(void) return -1; } - while (fgets(buffer, BUFSIZE, fp) != NULL) { + while (fgets(plugin.buffer, plugin.bufsize, fp) != NULL) { - if (!(stats = strchr(buffer, ':'))) + if (!(stats = strchr(plugin.buffer, ':'))) continue; *stats++ = '\0'; - device = buffer; + device = plugin.buffer; while (*device == ' ') device++; @@ -83,27 +79,10 @@ static int probe(void) return 0; } -static int init(void) -{ - buffer = malloc(BUFSIZE); - if (buffer == NULL) { - log_print(LOG_WARN, "plugin netdev: out of memory"); - return -1; - } - return 0; -} - -static int fini(void) -{ - free(buffer); - return 0; -} - struct sammler_plugin plugin = { .name = "netdev", .interval = 10, - .init = &init, - .fini = &fini, + .bufsize = 1024, .probe = &probe, .get_ds = &get_ds, }; diff --git a/p_rtstat.c b/p_rtstat.c index c3a4d7a..5ba28b4 100644 --- a/p_rtstat.c +++ b/p_rtstat.c @@ -24,8 +24,6 @@ #include "helper.h" #include "plugins.h" -#define BUFSIZE 1024 - #define DS_STAT 1 #define DS_GC 2 @@ -68,8 +66,6 @@ static const char * get_ds(int ds_id) } } -static char *buffer; - static int probe(void) { FILE *fp; @@ -83,11 +79,11 @@ static int probe(void) return -1; } - while (fgets(buffer, BUFSIZE, fp) != NULL) { - if (!strncmp(buffer, "entries", 7)) + while (fgets(plugin.buffer, plugin.bufsize, fp) != NULL) { + if (!strncmp(plugin.buffer, "entries", 7)) continue; - if (strsplit(buffer, " \t\n", val, 17) != 17) + if (strsplit(plugin.buffer, " \t\n", val, 17) != 17) continue; for (i = 0; i < 17; i++) @@ -117,27 +113,10 @@ static int probe(void) return 0; } -static int init(void) -{ - buffer = malloc(BUFSIZE); - if (buffer == NULL) { - log_print(LOG_WARN, "plugin rtstat: out of memory"); - return -1; - } - return 0; -} - -static int fini(void) -{ - free(buffer); - return 0; -} - struct sammler_plugin plugin = { .name = "rtstat", .interval = 10, - .init = &init, - .fini = &fini, + .bufsize = 1024, .probe = &probe, .get_ds = &get_ds, }; diff --git a/p_stat.c b/p_stat.c index 71cad77..7609089 100644 --- a/p_stat.c +++ b/p_stat.c @@ -24,8 +24,6 @@ #include "helper.h" #include "plugins.h" -#define BUFSIZE 1024 - #define DS_CPU 1 #define DS_PROC 2 @@ -68,8 +66,6 @@ struct proc_ { unsigned long long fork; }; -static char *buffer; - static int probe(void) { FILE *fp; @@ -83,13 +79,13 @@ static int probe(void) return -1; } - while (fgets(buffer, BUFSIZE, fp) != NULL) { - if (!strncmp(buffer, "cpu", 3)) { + while (fgets(plugin.buffer, plugin.bufsize, fp) != NULL) { + if (!strncmp(plugin.buffer, "cpu", 3)) { char *val[9], filename[16]; int numfields, len, cpu; - if ((buffer[3] >= '0') && (buffer[3] <= '9')) { - cpu = atoi(buffer +3); + if ((plugin.buffer[3] >= '0') && (plugin.buffer[3] <= '9')) { + cpu = atoi(plugin.buffer +3); len = snprintf(filename, sizeof(filename), "cpu-%d.rrd", cpu); if (len < 0 || len >= sizeof(filename)) continue; @@ -98,7 +94,7 @@ static int probe(void) strncpy(filename, "cpu.rrd", sizeof(filename)); } - numfields = strsplit(buffer, " \t\n", val, 9); + numfields = strsplit(plugin.buffer, " \t\n", val, 9); if (numfields < 5) continue; @@ -113,14 +109,14 @@ static int probe(void) val[1], val[2], val[3], val[4], val[5], val[6], val[7], val[8]); - } else if (!strncmp(buffer, "intr", 4)) { - proc.intr = atoll(buffer + 5); + } else if (!strncmp(plugin.buffer, "intr", 4)) { + proc.intr = atoll(plugin.buffer + 5); - } else if (!strncmp(buffer, "ctxt", 4)) { - proc.ctxt = atoll(buffer + 5); + } else if (!strncmp(plugin.buffer, "ctxt", 4)) { + proc.ctxt = atoll(plugin.buffer + 5); - } else if (!strncmp(buffer, "processes", 9)) { - proc.fork = atoll(buffer + 10); + } else if (!strncmp(plugin.buffer, "processes", 9)) { + proc.fork = atoll(plugin.buffer + 10); } } @@ -131,27 +127,10 @@ static int probe(void) return 0; } -static int init(void) -{ - buffer = malloc(BUFSIZE); - if (buffer == NULL) { - log_print(LOG_WARN, "plugin stat: out of memory"); - return -1; - } - return 0; -} - -static int fini(void) -{ - free(buffer); - return 0; -} - struct sammler_plugin plugin = { .name = "stat", .interval = 10, - .init = &init, - .fini = &fini, + .bufsize = 1024, .probe = &probe, .get_ds = &get_ds, }; diff --git a/p_vmstat.c b/p_vmstat.c index 33e81d0..d79163f 100644 --- a/p_vmstat.c +++ b/p_vmstat.c @@ -23,8 +23,6 @@ #include "plugins.h" -#define BUFSIZE 1024 - struct sammler_plugin plugin; static const char *ds_def = { @@ -48,8 +46,6 @@ struct vmstat_ { unsigned long long pgfault; }; -static char *buffer; - static int probe(void) { FILE *fp; @@ -63,21 +59,21 @@ static int probe(void) return -1; } - while (fgets(buffer, BUFSIZE, fp) != NULL) { - if (!strncmp(buffer, "pgalloc_high", 12)) - vmstat.pgalloc_high = atoll(buffer + 13); + while (fgets(plugin.buffer, plugin.bufsize, fp) != NULL) { + if (!strncmp(plugin.buffer, "pgalloc_high", 12)) + vmstat.pgalloc_high = atoll(plugin.buffer + 13); - else if (!strncmp(buffer, "pgalloc_normal", 14)) - vmstat.pgalloc_normal = atoll(buffer + 15); + else if (!strncmp(plugin.buffer, "pgalloc_normal", 14)) + vmstat.pgalloc_normal = atoll(plugin.buffer + 15); - else if (!strncmp(buffer, "pgalloc_dma", 11)) - vmstat.pgalloc_dma = atoll(buffer + 12); + else if (!strncmp(plugin.buffer, "pgalloc_dma", 11)) + vmstat.pgalloc_dma = atoll(plugin.buffer + 12); - else if (!strncmp(buffer, "pgfree", 6)) - vmstat.pgfree = atoll(buffer + 7); + else if (!strncmp(plugin.buffer, "pgfree", 6)) + vmstat.pgfree = atoll(plugin.buffer + 7); - else if (!strncmp(buffer, "pgfault", 7)) - vmstat.pgfault = atoll(buffer + 8); + else if (!strncmp(plugin.buffer, "pgfault", 7)) + vmstat.pgfault = atoll(plugin.buffer + 8); } probe_submit(&plugin, "vmstat.rrd", 0, "%llu:%llu:%llu:%llu:%llu", @@ -88,27 +84,10 @@ static int probe(void) return 0; } -static int init(void) -{ - buffer = malloc(BUFSIZE); - if (buffer == NULL) { - log_print(LOG_WARN, "plugin vmstat: out of memory"); - return -1; - } - return 0; -} - -static int fini(void) -{ - free(buffer); - return 0; -} - struct sammler_plugin plugin = { .name = "vmstat", .interval = 10, - .init = &init, - .fini = &fini, + .bufsize = 1024, .probe = &probe, .get_ds = &get_ds, }; diff --git a/plugins.c b/plugins.c index 76ec02d..f3a6814 100644 --- a/plugins.c +++ b/plugins.c @@ -40,8 +40,11 @@ static LIST_HEAD(plugin_list); static int plugin_flags; +static char *scratchpad; + static int plugin_init_cb(const char *filename, void *privdata) { + int *bufsize = (int *)privdata; struct sammler_plugin *plugin = NULL; static const char *plugin_dir; @@ -90,22 +93,39 @@ static int plugin_init_cb(const char *filename, void *privdata) return -1; } + if (plugin->bufsize > *bufsize) + *bufsize = plugin->bufsize; + list_add_tail(&plugin->list, &plugin_list); return 0; } -void plugin_init(int flags) +int plugin_init(int flags) { - config_get_strings("global", "plugin", plugin_init_cb, NULL); + struct sammler_plugin *plugin; + int bufsize = 0; + + config_get_strings("global", "plugin", plugin_init_cb, &bufsize); plugin_flags = flags; + + scratchpad = malloc(bufsize); + if (scratchpad == NULL) { + log_print(LOG_ERROR, "plugin_init: out of memory"); + return -1; + } + + list_for_each_entry(plugin, &plugin_list, list) + plugin->buffer = scratchpad; + + return 0; } void plugins_probe(void) { struct sammler_plugin *plugin; - unsigned long now; + time_t now; - now = time(NULL); + time(&now); list_for_each_entry(plugin, &plugin_list, list) { if (plugin->lastprobe + plugin->interval <= now) { diff --git a/plugins.h b/plugins.h index 900e2cd..5906f60 100644 --- a/plugins.h +++ b/plugins.h @@ -1,6 +1,8 @@ #ifndef _PLUGINS_H_ #define _PLUGINS_H_ +#include + #include "list.h" #include "logging.h" @@ -10,15 +12,22 @@ struct sammler_plugin { struct list_head list; const char *name; + + /* timing */ unsigned int interval; - unsigned long lastprobe; + time_t lastprobe; + + /* scratchpad memory */ + int bufsize; + char *buffer; + int (*init) (void); int (*fini) (void); int (*probe) (void); const char * (*get_ds) (int ds_id); }; -void plugin_init(int flags); +int plugin_init(int flags); void plugins_probe(void);