From f16b9a8169fad98efff4013980728612504350ca Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Sun, 1 Apr 2007 14:30:05 +0200 Subject: [PATCH] create probe.[ch] --- Makefile | 2 +- network.h | 1 + p_apache.c | 1 + p_ctstat.c | 1 + p_load.c | 1 + p_memory.c | 1 + p_mount.c | 1 + p_mysql.c | 1 + p_netdev.c | 1 + p_random.c | 1 + p_rtstat.c | 1 + p_stat.c | 1 + p_uptime.c | 1 + p_vmstat.c | 1 + plugins.c | 56 +++++++----------------------------------------------- plugins.h | 3 --- rrdtool.c | 2 +- sammler.c | 19 ++++++++++++------ 18 files changed, 35 insertions(+), 60 deletions(-) diff --git a/Makefile b/Makefile index 976a2e7..21be0d4 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ WITH_CURL = yes # ############################ -SAMMLER_SRC := sammler.c configfile.c event.c helper.c logging.c network.c plugins.c +SAMMLER_SRC := sammler.c configfile.c event.c helper.c logging.c network.c plugins.c probe.c PLUGIN_SRC := p_ctstat.c p_load.c p_memory.c p_mount.c p_netdev.c p_random.c PLUGIN_SRC += p_rtstat.c p_stat.c p_uptime.c p_vmstat.c CFLAGS := -O2 -Wall -fno-stack-protector diff --git a/network.h b/network.h index f373d19..d99dab8 100644 --- a/network.h +++ b/network.h @@ -4,6 +4,7 @@ #include "plugins.h" int net_init(void); +void net_close(void); int net_submit(const char *hostname, const char *pluginname, const char *filename, int ds_id, const char *data); void net_submit_flush(void); diff --git a/p_apache.c b/p_apache.c index 3cc3f92..aa34569 100644 --- a/p_apache.c +++ b/p_apache.c @@ -28,6 +28,7 @@ #include "helper.h" #include "list.h" #include "plugins.h" +#include "probe.h" #define BUFSIZE 8192 diff --git a/p_ctstat.c b/p_ctstat.c index e2200f9..7dc0321 100644 --- a/p_ctstat.c +++ b/p_ctstat.c @@ -23,6 +23,7 @@ #include "helper.h" #include "plugins.h" +#include "probe.h" struct sammler_plugin plugin; diff --git a/p_load.c b/p_load.c index 1b19ac7..7298bc8 100644 --- a/p_load.c +++ b/p_load.c @@ -22,6 +22,7 @@ #include "helper.h" #include "plugins.h" +#include "probe.h" struct sammler_plugin plugin; diff --git a/p_memory.c b/p_memory.c index b4a80df..9bd2dbb 100644 --- a/p_memory.c +++ b/p_memory.c @@ -22,6 +22,7 @@ #include #include "plugins.h" +#include "probe.h" #define DS_MEMORY 1 #define DS_SWAP 2 diff --git a/p_mount.c b/p_mount.c index b797e55..a569d8b 100644 --- a/p_mount.c +++ b/p_mount.c @@ -24,6 +24,7 @@ #include #include "plugins.h" +#include "probe.h" struct sammler_plugin plugin; diff --git a/p_mysql.c b/p_mysql.c index b093121..5edb047 100644 --- a/p_mysql.c +++ b/p_mysql.c @@ -25,6 +25,7 @@ #include "helper.h" #include "list.h" #include "plugins.h" +#include "probe.h" #include "p_mysql_helper.h" diff --git a/p_netdev.c b/p_netdev.c index 64f8138..a4f290e 100644 --- a/p_netdev.c +++ b/p_netdev.c @@ -23,6 +23,7 @@ #include "helper.h" #include "plugins.h" +#include "probe.h" struct sammler_plugin plugin; diff --git a/p_random.c b/p_random.c index 2ee1d14..a13f75b 100644 --- a/p_random.c +++ b/p_random.c @@ -22,6 +22,7 @@ #include "helper.h" #include "plugins.h" +#include "probe.h" struct sammler_plugin plugin; diff --git a/p_rtstat.c b/p_rtstat.c index 5ba28b4..044003a 100644 --- a/p_rtstat.c +++ b/p_rtstat.c @@ -23,6 +23,7 @@ #include "helper.h" #include "plugins.h" +#include "probe.h" #define DS_STAT 1 #define DS_GC 2 diff --git a/p_stat.c b/p_stat.c index 7609089..ff561e9 100644 --- a/p_stat.c +++ b/p_stat.c @@ -23,6 +23,7 @@ #include "helper.h" #include "plugins.h" +#include "probe.h" #define DS_CPU 1 #define DS_PROC 2 diff --git a/p_uptime.c b/p_uptime.c index 11344c3..1d0538d 100644 --- a/p_uptime.c +++ b/p_uptime.c @@ -22,6 +22,7 @@ #include "helper.h" #include "plugins.h" +#include "probe.h" struct sammler_plugin plugin; diff --git a/p_vmstat.c b/p_vmstat.c index d79163f..0e61313 100644 --- a/p_vmstat.c +++ b/p_vmstat.c @@ -22,6 +22,7 @@ #include #include "plugins.h" +#include "probe.h" struct sammler_plugin plugin; diff --git a/plugins.c b/plugins.c index 8c9bc9f..b9cc573 100644 --- a/plugins.c +++ b/plugins.c @@ -22,27 +22,17 @@ #include #include #include -#include #include #include -#include "list.h" - -#include "plugins.h" #include "configfile.h" #include "event.h" #include "logging.h" #include "network.h" -#include "rrdtool.h" - -#define BUFSIZE 512 - -#define SUBMIT_NET_ONLY 0x01 +#include "plugins.h" static LIST_HEAD(plugin_list); -static int submit_flags; - static char *scratchpad; static int plugin_init_cb(const char *filename, void *privdata) @@ -118,11 +108,15 @@ static int plugins_probe(void *privdata) int plugin_init(void) { int bufsize = 0; - config_get_strings("global", "plugin", plugin_init_cb, &bufsize); + int cnt = config_get_strings("global", "plugin", plugin_init_cb, &bufsize); + 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"); + log_print(LOG_ERROR, "plugin_init(): out of memory"); return -1; } @@ -130,10 +124,6 @@ int plugin_init(void) list_for_each_entry(plugin, &plugin_list, list) plugin->buffer = scratchpad; - const char *fwd_only = config_get_string("global", "forward_only", NULL); - if (fwd_only == NULL || strncmp(fwd_only, "true", 4)) - submit_flags |= SUBMIT_NET_ONLY; - struct timeval tv; tv.tv_sec = 1; tv.tv_usec = 0; @@ -151,35 +141,3 @@ struct sammler_plugin * plugin_lookup(const char *name) } return NULL; } - -int probe_submit(struct sammler_plugin *plugin, const char *filename, int ds_id, const char *fmt, ... ) -{ - static const char *hostname = NULL; - if (hostname == NULL) - hostname = config_get_string("global", "hostname", "localhost"); - - char *buffer = malloc(BUFSIZE); - if (buffer == NULL) { - log_print(LOG_ERROR, "probe_submit: out of memory"); - return -1; - } - - va_list az; - va_start(az, fmt); - int len = vsnprintf(buffer, BUFSIZE, fmt, az); - va_end(az); - - if (len < 0 || len >= BUFSIZE) { - log_print(LOG_ERROR, "probe_submit: %s arguments too long", plugin->name); - free(buffer); - return -1; - } - - net_submit(hostname, plugin->name, filename, ds_id, buffer); - - if (!(submit_flags & SUBMIT_NET_ONLY)) - rrd_submit(hostname, plugin->name, filename, ds_id, buffer); - - free(buffer); - return 0; -} diff --git a/plugins.h b/plugins.h index 49605c6..05a9938 100644 --- a/plugins.h +++ b/plugins.h @@ -25,9 +25,6 @@ struct sammler_plugin { }; int plugin_init(void); - struct sammler_plugin * plugin_lookup(const char *name); -int probe_submit(struct sammler_plugin *plugin, const char *filename, int ds_id, const char *fmt, ... ); - #endif /* _PLUGINS_H_ */ diff --git a/rrdtool.c b/rrdtool.c index 39caca4..af3a175 100644 --- a/rrdtool.c +++ b/rrdtool.c @@ -74,7 +74,7 @@ static int do_rrd(int (*rrd_func)(int, char **), char *cmd) int retval = rrd_func(argc, argv); if (retval == -1) { errno = 0; - log_print(LOG_ERROR, "rrd_func failed: %s", rrd_get_error()); + log_print(LOG_ERROR, "rrd_func failed: %s: %s", argv[1], rrd_get_error()); } free(cmd); diff --git a/sammler.c b/sammler.c index e0e2c92..455c305 100644 --- a/sammler.c +++ b/sammler.c @@ -32,6 +32,7 @@ #include "logging.h" #include "network.h" #include "plugins.h" +#include "probe.h" #define DEFAULT_CONFIG "sammler.conf" #define DEFAULT_LOGFILE "sammler.log" @@ -71,7 +72,7 @@ int main(int argc, char *argv[]) break; case '?': /* error */ - exit(-1); + exit(1); break; default: /* unknown / all options parsed */ @@ -81,14 +82,14 @@ int main(int argc, char *argv[]) /* parse config file */ if (config_parse(config)) - exit(-1); + exit(1); /* check logfile */ const char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE); if (logfile != NULL && debug == 0) { /* start logging */ if (log_init(logfile)) - exit(-1); + exit(1); /* zum daemon mutieren */ daemon(-1, 0); @@ -97,10 +98,16 @@ int main(int argc, char *argv[]) const char *hostname = config_get_string("global", "hostname", "localhost"); log_print(LOG_EVERYTIME, "sammler (pid:%d) started on host '%s'", getpid(), hostname); - if (net_init()) - exit(-1); + if (probe_init()) + exit(1); - plugin_init(); + if (net_init()) + exit(1); + + if (plugin_init()) { + net_close(); + exit(1); + } event_loop(); return 0;