diff --git a/Makefile b/Makefile index dddc161..976a2e7 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ WITH_CURL = yes SAMMLER_SRC := sammler.c configfile.c event.c helper.c logging.c network.c plugins.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 +CFLAGS := -O2 -Wall -fno-stack-protector LDFLAGS := -ldl -rdynamic # ############################ diff --git a/network.c b/network.c index 96a3884..36a4637 100644 --- a/network.c +++ b/network.c @@ -30,13 +30,13 @@ static LIST_HEAD(fwd_list); static char *tx_buf, *rx_buf; static int tx_pos; -int net_submit(const char *hostname, const char *plugin, const char *filename, int ds_id, const char *data) +int net_submit(const char *hostname, struct sammler_plugin *plugin, const char *filename, int ds_id, const char *data) { if (list_empty(&fwd_list)) return 0; int size = snprintf(tx_buf + tx_pos, BUFSIZE - tx_pos, "%s:%s:%s:%d %s\n", - hostname, plugin, filename, ds_id, data); + hostname, plugin->name, filename, ds_id, data); if (size < 0 || size >= BUFSIZE - tx_pos) { log_print(LOG_ERROR, "net_submit(): arguments too long"); @@ -69,19 +69,27 @@ static int net_receive(int socket, void *privdata) while ((delim = memchr(rx_buf + rx_pos, '\n', size - rx_pos))) { *delim = '\0'; - char *data[2], *part[4]; + char *data[2]; int ret = strsplit(rx_buf + rx_pos, " ", data, 2); if (ret != 2) { - log_print(LOG_ERROR, "net_receive() abort data-split"); + log_print(LOG_ERROR, "net_receive(): abort data-split"); continue; } + char *part[4]; ret = strsplit(data[0], ":", part, 4); if (ret != 4) { - log_print(LOG_ERROR, "net_receive() abort header-split"); + log_print(LOG_ERROR, "net_receive(): abort header-split"); continue; } - rrd_submit(part[0], part[1], part[2], atoi(part[3]), data[1]); + + struct sammler_plugin *plugin = plugin_lookup(part[1]); + if (plugin == NULL) { + log_print(LOG_ERROR, "net_receive(): plugin not found (%s)", part[1]); + continue; + } + + rrd_submit(part[0], plugin, part[2], atoi(part[3]), data[1]); rx_pos = (delim - rx_buf) +1; } diff --git a/network.h b/network.h index 1812015..c14309e 100644 --- a/network.h +++ b/network.h @@ -5,7 +5,7 @@ int net_init(void); -int net_submit(const char *hostname, const char *plugin, const char *filename, int ds_id, const char *data); +int net_submit(const char *hostname, struct sammler_plugin *plugin, const char *filename, int ds_id, const char *data); void net_submit_flush(void); #endif /* _NETWORK_H_ */ diff --git a/plugins.c b/plugins.c index 8c9bc9f..6739258 100644 --- a/plugins.c +++ b/plugins.c @@ -175,10 +175,10 @@ int probe_submit(struct sammler_plugin *plugin, const char *filename, int ds_id, return -1; } - net_submit(hostname, plugin->name, filename, ds_id, buffer); + net_submit(hostname, plugin, filename, ds_id, buffer); if (!(submit_flags & SUBMIT_NET_ONLY)) - rrd_submit(hostname, plugin->name, filename, ds_id, buffer); + rrd_submit(hostname, plugin, filename, ds_id, buffer); free(buffer); return 0; diff --git a/rrdtool.c b/rrdtool.c index bd0d702..8541e2f 100644 --- a/rrdtool.c +++ b/rrdtool.c @@ -48,7 +48,7 @@ struct rra_cb_data { int *pos; }; -static int append_rra_config_cb(const char *parameter, void *privdata) +static int append_rra_config(const char *parameter, void *privdata) { struct rra_cb_data *data = (struct rra_cb_data *)privdata; @@ -62,114 +62,78 @@ static int append_rra_config_cb(const char *parameter, void *privdata) return 0; } -static int append_rra_config(char *buffer, int size, int *pos) +static int do_rrd(int (*rrd_func)(int, char **), char *cmd) { - struct rra_cb_data data = { - .buffer = buffer, - .size = size, - .pos = pos, - }; - - return config_get_strings("global", "rra", append_rra_config_cb, &data); -} - -static int do_rrd(int mode, const char *cmd) -{ - int argc; char *argv[ARGCMAX]; - - argc = strsplit(cmd, " \t\n", argv, ARGCMAX -1); + int argc = strsplit(cmd, " \t\n", argv, ARGCMAX -1); argv[argc] = NULL; optind = 0; rrd_clear_error(); - if (mode == RRDTOOL_CREATE) { - if (rrd_create(argc, argv) == -1) { - errno = 0; - log_print(LOG_ERROR, "rrd_create failed: %s: %s", - argv[1], rrd_get_error()); - return -1; - } - - } else if (mode == RRDTOOL_UPDATE) { - if (rrd_update(argc, argv) == -1) { - errno = 0; - log_print(LOG_ERROR, "rrd_update failed: %s: %s", - argv[1], rrd_get_error()); - return -1; - } + int retval = rrd_func(argc, argv); + if (retval == -1) { + errno = 0; + log_print(LOG_ERROR, "rrd_func failed: %s", rrd_get_error()); } - return 0; + + free(cmd); + return retval; } -static int rrd_create_file(const char *filename, const char *plugin_name, int ds_id) +static int rrd_create_file(const char *filename, struct sammler_plugin *plugin, int ds_id) { - struct sammler_plugin *plugin; - int pos, step, retval; - const char *ds_def; - char *buffer; - - plugin = plugin_lookup(plugin_name); - if (plugin == NULL) { - log_print(LOG_ERROR, "Plugin not found (%s)", plugin_name); - return -1; - } - - ds_def = plugin->get_ds(ds_id); + const char *ds_def = plugin->get_ds(ds_id); if (ds_def == NULL) { log_print(LOG_ERROR, "No vaild DS found (%s:%d)", plugin->name, ds_id); return -1; } - buffer = malloc(ARGVSIZE); + char *buffer = malloc(ARGVSIZE); if (buffer == NULL) { log_print(LOG_ERROR, "append_ds_config: out of memory"); return -1; } - step = plugin->interval; - - pos = snprintf(buffer, ARGVSIZE, "create %s -s %d %s ", filename, step, ds_def); + int step = plugin->interval; + int pos = snprintf(buffer, ARGVSIZE, "create %s -s %d %s ", filename, step, ds_def); if (pos < 0 || pos >= ARGVSIZE) { log_print(LOG_ERROR, "rrd_create_file: arguments too long"); free(buffer); return -1; } - if (append_rra_config(buffer, ARGVSIZE, &pos) <= 0) { + struct rra_cb_data data = { + .buffer = buffer, + .size = ARGVSIZE, + .pos = &pos, + }; + + int cnt = config_get_strings("global", "rra", append_rra_config, &data); + if (cnt <= 0) { free(buffer); return -1; } - retval = do_rrd(RRDTOOL_CREATE, buffer); - free(buffer); - - return retval; + return do_rrd(&rrd_create, buffer); } static int rrd_update_file(const char *filename, const char *values) { - int pos, retval; - char *buffer; - - buffer = malloc(ARGVSIZE); + char *buffer = malloc(ARGVSIZE); if (buffer == NULL) { log_print(LOG_ERROR, "append_ds_config: out of memory"); return -1; } - pos = snprintf(buffer, ARGVSIZE, "update %s %lu:%s", filename, time(NULL), values); + int pos = snprintf(buffer, ARGVSIZE, "update %s %lu:%s", filename, time(NULL), values); if (pos < 0 || pos >= ARGVSIZE) { log_print(LOG_ERROR, "rrd_update_file: arguments too long"); free(buffer); return -1; } - retval = do_rrd(RRDTOOL_UPDATE, buffer); - free(buffer); - - return retval; + return do_rrd(&rrd_update, buffer); } static int check_create_dir(const char *dir) @@ -197,9 +161,9 @@ static int check_create_dir(const char *dir) static int create_parent_dirs(char *filename) { - char *lastslash, *nextslash = filename; + char *nextslash = filename; - lastslash = strrchr(filename, '/'); + char *lastslash = strrchr(filename, '/'); if (lastslash == NULL) { log_print(LOG_ERROR, "create_parent_dirs: invalid file name"); return -1; @@ -229,30 +193,26 @@ static int create_parent_dirs(char *filename) return 0; } -int rrd_submit(const char *hostname, const char *plugin, const char *filename, int ds_id, const char *data) +int rrd_submit(const char *hostname, struct sammler_plugin *plugin, const char *filename, int ds_id, const char *data) { - struct stat statbuf; static const char *rrd_dir = NULL; - - char *fullfile; - int len; - if (rrd_dir == NULL) rrd_dir = config_get_string("global", "rrd_dir", "."); - fullfile = malloc(BUFSIZE); + char *fullfile = malloc(BUFSIZE); if (fullfile == NULL) { log_print(LOG_ERROR, "rrd_submit: out of memory"); return -1; } - len = snprintf(fullfile, BUFSIZE, "%s/%s/%s", rrd_dir, hostname, filename); + int len = snprintf(fullfile, BUFSIZE, "%s/%s/%s", rrd_dir, hostname, filename); if (len < 0 || len >= BUFSIZE) { log_print(LOG_ERROR, "rrd_submit: arguments too long"); free(fullfile); return -1; } + struct stat statbuf; if (stat(fullfile, &statbuf) == -1) { if (errno == ENOENT) { errno = 0; diff --git a/rrdtool.h b/rrdtool.h index ce15c5a..7ca7855 100644 --- a/rrdtool.h +++ b/rrdtool.h @@ -2,7 +2,7 @@ #define _RRDTOOL_H_ #ifdef WITH_RRD -int rrd_submit(const char *hostname, const char *plugin, const char *filename, int ds_id, const char *data); +int rrd_submit(const char *hostname, struct sammler_plugin *plugin, const char *filename, int ds_id, const char *data); #else #define rrd_submit(hostname, plugin, filename, ds_id, data) #endif diff --git a/sammler.conf b/sammler.conf index 25cef2a..3f0da5c 100644 --- a/sammler.conf +++ b/sammler.conf @@ -17,8 +17,8 @@ plugin p_vmstat.so plugin p_uptime.so plugin p_netdev.so plugin p_mount.so -#plugin p_ctstat.so -#plugin p_rtstat.so +plugin p_ctstat.so +plugin p_rtstat.so plugin p_random.so plugin p_mysql.so plugin p_apache.so