From 273218612ef437dc58a0357efd5a791bfa858524 Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Sat, 7 Oct 2006 20:37:30 +0200 Subject: [PATCH] plugin init/fini functions --- p_ctstat.c | 32 +++++++++++++++++++++++--------- p_load.c | 9 +++++---- p_memory.c | 31 ++++++++++++++++++++++--------- p_mount.c | 5 +++-- p_netdev.c | 32 +++++++++++++++++++++++--------- p_random.c | 9 +++++---- p_rtstat.c | 32 +++++++++++++++++++++++--------- p_stat.c | 31 ++++++++++++++++++++++--------- p_uptime.c | 9 +++++---- p_vmstat.c | 31 ++++++++++++++++++++++--------- plugins.c | 22 +++++++++++++--------- plugins.h | 4 +++- 12 files changed, 169 insertions(+), 78 deletions(-) diff --git a/p_ctstat.c b/p_ctstat.c index 354d4b1..27e9104 100644 --- a/p_ctstat.c +++ b/p_ctstat.c @@ -51,24 +51,20 @@ static char * get_ds(int ds_id) return ds_def; } -static void probe(void) +static char *buffer; + +static int probe(void) { FILE *fp; - char *buffer, *val[16], filename[16]; + char *val[16], filename[16]; unsigned long long arr[16]; int i, len, cpu = 0; - buffer = malloc(BUFSIZE); - if (buffer == NULL) { - log_print(LOG_WARN, "plugin ctstat: out of memory"); - return; - } - fp = fopen("/proc/net/stat/ip_conntrack", "r"); if (fp == NULL) { log_print(LOG_WARN, "plugin ctstat"); free(buffer); - return; + return -1; } while (fgets(buffer, BUFSIZE, fp) != NULL) { @@ -95,12 +91,30 @@ static void probe(void) cpu++; } fclose(fp); + 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, .probe = &probe, .get_ds = &get_ds, }; diff --git a/p_load.c b/p_load.c index 626f3e9..43ffe8d 100644 --- a/p_load.c +++ b/p_load.c @@ -35,7 +35,7 @@ static char * get_ds(int ds_id) return ds_def; } -static void probe(void) +static int probe(void) { FILE *fp; char buffer[32]; @@ -44,21 +44,22 @@ static void probe(void) fp = fopen("/proc/loadavg", "r"); if (fp == NULL) { log_print(LOG_WARN, "plugin load"); - return; + return -1; } if (fgets(buffer, sizeof(buffer), fp) == NULL) { log_print(LOG_WARN, "plugin load"); fclose(fp); - return; + return -1; } fclose(fp); if (strsplit(buffer, val, 3) != 3) - return; + return -1; probe_submit(&plugin, "load.rrd", 0, "%s:%s:%s", val[0], val[1], val[2]); + return 0; } struct sammler_plugin plugin = { diff --git a/p_memory.c b/p_memory.c index 67e61a3..3da6c16 100644 --- a/p_memory.c +++ b/p_memory.c @@ -65,25 +65,20 @@ struct meminfo_ { unsigned long long swapfree; }; -static void probe(void) +static char *buffer; + +static int probe(void) { FILE *fp; - char *buffer; struct meminfo_ meminfo; memset(&meminfo, 0, sizeof(meminfo)); - buffer = malloc(BUFSIZE); - if (buffer == NULL) { - log_print(LOG_WARN, "plugin memory: out of memory"); - return; - } - fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { log_print(LOG_WARN, "plugin memory"); free(buffer); - return; + return -1; } while (fgets(buffer, BUFSIZE, fp) != NULL) { @@ -114,12 +109,30 @@ static void probe(void) meminfo.swaptotal, meminfo.swapfree); fclose(fp); + 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, .probe = &probe, .get_ds = &get_ds, }; diff --git a/p_mount.c b/p_mount.c index cd5c400..a174cf5 100644 --- a/p_mount.c +++ b/p_mount.c @@ -37,7 +37,7 @@ static char * get_ds(int ds_id) return ds_def; } -static void probe(void) +static int probe(void) { FILE *fp; struct mntent *mnt; @@ -48,7 +48,7 @@ static void probe(void) fp = setmntent("/etc/mtab", "r"); if (fp == NULL) { log_print(LOG_WARN, "plugin mount"); - return; + return -1; } while ((mnt = getmntent(fp)) != NULL) { @@ -98,6 +98,7 @@ static void probe(void) fs.f_bfree * (fs.f_bsize /1024)); } endmntent(fp); + return 0; } struct sammler_plugin plugin = { diff --git a/p_netdev.c b/p_netdev.c index 4bbad75..e85b102 100644 --- a/p_netdev.c +++ b/p_netdev.c @@ -39,24 +39,20 @@ static char * get_ds(int ds_id) return ds_def; } -static void probe(void) +static char *buffer; + +static int probe(void) { FILE *fp; - char *buffer, *stats, *device; + char *stats, *device; char *val[16], filename[32]; int len; - buffer = malloc(BUFSIZE); - if (buffer == NULL) { - log_print(LOG_WARN, "plugin netdev: out of memory"); - return; - } - fp = fopen("/proc/net/dev", "r"); if (fp == NULL) { log_print(LOG_WARN, "plugin netdev"); free(buffer); - return; + return -1; } while (fgets(buffer, BUFSIZE, fp) != NULL) { @@ -84,12 +80,30 @@ static void probe(void) val[0], val[8], val[1], val[9]); } fclose(fp); + 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, .probe = &probe, .get_ds = &get_ds, }; diff --git a/p_random.c b/p_random.c index 7a391cc..b418639 100644 --- a/p_random.c +++ b/p_random.c @@ -33,7 +33,7 @@ static char * get_ds(int ds_id) return ds_def; } -static void probe(void) +static int probe(void) { FILE *fp; char buffer[32]; @@ -42,21 +42,22 @@ static void probe(void) fp = fopen("/proc/sys/kernel/random/entropy_avail", "r"); if (fp == NULL) { log_print(LOG_WARN, "plugin random"); - return; + return -1; } if (fgets(buffer, sizeof(buffer), fp) == NULL) { log_print(LOG_WARN, "plugin random"); fclose(fp); - return; + return -1; } fclose(fp); if (strsplit(buffer, val, 1) != 1) - return; + return -1; probe_submit(&plugin, "random.rrd", 0, "%s", val[0]); + return 0; } struct sammler_plugin plugin = { diff --git a/p_rtstat.c b/p_rtstat.c index 8ba3670..c642df8 100644 --- a/p_rtstat.c +++ b/p_rtstat.c @@ -67,24 +67,20 @@ static char * get_ds(int ds_id) } } -static void probe(void) +static char *buffer; + +static int probe(void) { FILE *fp; - char *buffer, *val[17], filename[16]; + char *val[17], filename[16]; unsigned long long arr[17]; int i, len, cpu = 0; - buffer = malloc(BUFSIZE); - if (buffer == NULL) { - log_print(LOG_WARN, "plugin rtstat: out of memory"); - return; - } - fp = fopen("/proc/net/stat/rt_cache", "r"); if (fp == NULL) { log_print(LOG_WARN, "plugin rtstat"); free(buffer); - return; + return -1; } while (fgets(buffer, BUFSIZE, fp) != NULL) { @@ -118,12 +114,30 @@ static void probe(void) cpu++; } fclose(fp); + 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, .probe = &probe, .get_ds = &get_ds, }; diff --git a/p_stat.c b/p_stat.c index ebb75c5..b6e66b4 100644 --- a/p_stat.c +++ b/p_stat.c @@ -67,25 +67,20 @@ struct proc_ { unsigned long long fork; }; -static void probe(void) +static char *buffer; + +static int probe(void) { FILE *fp; - char *buffer; struct proc_ proc; memset(&proc, 0, sizeof(proc)); - buffer = malloc(BUFSIZE); - if (buffer == NULL) { - log_print(LOG_WARN, "plugin stat: out of memory"); - return; - } - fp = fopen("/proc/stat", "r"); if (fp == NULL) { log_print(LOG_WARN, "plugin stat"); free(buffer); - return; + return -1; } while (fgets(buffer, BUFSIZE, fp) != NULL) { @@ -134,12 +129,30 @@ static void probe(void) proc.intr, proc.ctxt, proc.fork); fclose(fp); + 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, .probe = &probe, .get_ds = &get_ds, }; diff --git a/p_uptime.c b/p_uptime.c index c75dc33..87d029d 100644 --- a/p_uptime.c +++ b/p_uptime.c @@ -34,7 +34,7 @@ static char * get_ds(int ds_id) return ds_def; } -static void probe(void) +static int probe(void) { FILE *fp; char buffer[32]; @@ -43,21 +43,22 @@ static void probe(void) fp = fopen("/proc/uptime", "r"); if (fp == NULL) { log_print(LOG_WARN, "plugin uptime"); - return; + return -1; } if (fgets(buffer, sizeof(buffer), fp) == NULL) { log_print(LOG_WARN, "plugin uptime"); fclose(fp); - return; + return -1; } fclose(fp); if (strsplit(buffer, val, 2) != 2) - return; + return -1; probe_submit(&plugin, "uptime.rrd", 0, "%s:%s", val[0], val[1]); + return 0; } struct sammler_plugin plugin = { diff --git a/p_vmstat.c b/p_vmstat.c index 7efe60f..a635f2e 100644 --- a/p_vmstat.c +++ b/p_vmstat.c @@ -48,25 +48,20 @@ struct vmstat_ { unsigned long long pgfault; }; -static void probe(void) +static char *buffer; + +static int probe(void) { FILE *fp; - char *buffer; struct vmstat_ vmstat; memset(&vmstat, 0, sizeof(vmstat)); - buffer = malloc(BUFSIZE); - if (buffer == NULL) { - log_print(LOG_WARN, "plugin vmstat: out of memory"); - return; - } - fp = fopen("/proc/vmstat", "r"); if (fp == NULL) { log_print(LOG_WARN, "plugin vmstat"); free(buffer); - return; + return -1; } while (fgets(buffer, BUFSIZE, fp) != NULL) { @@ -91,12 +86,30 @@ static void probe(void) vmstat.pgalloc_dma, vmstat.pgfree, vmstat.pgfault); fclose(fp); + 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, .probe = &probe, .get_ds = &get_ds, }; diff --git a/plugins.c b/plugins.c index 8e503b5..ceff11b 100644 --- a/plugins.c +++ b/plugins.c @@ -39,7 +39,7 @@ static LIST_HEAD(plugin_list); static int plugin_flags; -static void plugin_load(char *filename) +static int plugin_load(char *filename) { struct sammler_plugin *plugin = NULL; static char *plugin_dir; @@ -54,14 +54,14 @@ static void plugin_load(char *filename) buffer = malloc(BUFSIZE); if (buffer == NULL) { log_print(LOG_ERROR, "plugin_load: out of memory"); - return; + return -1; } len = snprintf(buffer, BUFSIZE, "%s/%s", plugin_dir, filename); if (len < 0 || len >= BUFSIZE) { log_print(LOG_ERROR, "plugin_load: file name too long: %s/%s", plugin_dir, filename); free(buffer); - return; + return -1; } dlerror(); @@ -69,24 +69,28 @@ static void plugin_load(char *filename) if (tmp == NULL) { log_print(LOG_ERROR, "plugin_load: dlopen: %s", dlerror()); free(buffer); - return; + return -1; } + free(buffer); + plugin = dlsym(tmp, "plugin"); if (plugin == NULL) { log_print(LOG_ERROR, "plugin_load: failed to load '%s'", filename); dlclose(tmp); - free(buffer); - return; + return -1; } log_print(LOG_INFO, "Plugin '%s' loaded", plugin->name); plugin->lastprobe = 0; - list_add_tail(&plugin->list, &plugin_list); + if (plugin->init != NULL && (plugin->init() != 0)) { + log_print(LOG_ERROR, "Plugin '%s': init failed", plugin->name); + return -1; + } - free(buffer); - return; + list_add_tail(&plugin->list, &plugin_list); + return 0; } void plugin_init(int flags) diff --git a/plugins.h b/plugins.h index 64e3233..f340ce2 100644 --- a/plugins.h +++ b/plugins.h @@ -12,7 +12,9 @@ struct sammler_plugin { char *name; unsigned int interval; unsigned long lastprobe; - void (*probe) (void); + int (*init) (void); + int (*fini) (void); + int (*probe) (void); char * (*get_ds) (int ds_id); };