From d09c70d2aaa92491acbe3a3400550653f230ac79 Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Fri, 1 May 2009 21:24:12 +0200 Subject: [PATCH] move config parsing to init() functions --- Makefile | 11 ++++++----- include/rrdtool.h | 5 +---- probe.c | 15 ++++++--------- rrdtool-fake.c | 30 ++++++++++++++++++++++++++++++ rrdtool.c | 12 ++++++++---- sammler.c | 4 ++++ 6 files changed, 55 insertions(+), 22 deletions(-) create mode 100644 rrdtool-fake.c diff --git a/Makefile b/Makefile index c4ae41e..57957fb 100644 --- a/Makefile +++ b/Makefile @@ -28,6 +28,7 @@ ifeq ("$(WITH_RRD)", "yes") LDFLAGS += -lrrd TARGET = sammler else + SRC += rrdtool-fake.c TARGET = sammler_norrd endif @@ -36,7 +37,7 @@ endif all: $(TARGET) plugins $(TARGET): $(SRC:%.c=%.o) - @echo " Linking file: $@" + @echo " Linking file: $@" @$(CC) $(LDFLAGS) $^ -o $@ %.o: %.c @@ -47,19 +48,19 @@ $(TARGET): $(SRC:%.c=%.o) plugins: $(PLUGINS:%=plugins/%.o) $(PLUGINS:%=plugins/%.so) plugins/%.so: plugins/%.o - @echo " Linking file: $@" + @echo " Linking file: $@" @$(CC) $(LDFLAGS) -shared -o $@ $< plugins/apache.so: plugins/apache.o - @echo " Linking file: $@" + @echo " Linking file: $@" @$(CC) $(LDFLAGS) -shared -o -lcurl -o $@ $< plugins/conntrack.so: plugins/conntrack.o - @echo " Linking file: $@" + @echo " Linking file: $@" @$(CC) $(LDFLAGS) -shared -o -lnfnetlink -lnetfilter_conntrack -o $@ $< plugins/mysql.so: plugins/mysql.o plugins/mysql_helper.o - @echo " Linking file: $@" + @echo " Linking file: $@" @$(CC) $(LDFLAGS) -shared -o -lmysqlclient -o $@ $< install: all diff --git a/include/rrdtool.h b/include/rrdtool.h index 3eb22aa..e6bccd7 100644 --- a/include/rrdtool.h +++ b/include/rrdtool.h @@ -1,10 +1,7 @@ #ifndef _RRDTOOL_H_ #define _RRDTOOL_H_ -#ifdef WITH_RRD +int rrd_init(void); int rrd_submit(const char *hostname, const char *pluginname, const char *filename, int ds_id, const char *data); -#else -#define rrd_submit(hostname, plugin, filename, ds_id, data) -#endif #endif /* _RRDTOOL_H_ */ diff --git a/probe.c b/probe.c index 29dade7..d38194f 100644 --- a/probe.c +++ b/probe.c @@ -14,6 +14,7 @@ #define SUBMIT_NET_ONLY 0x01 static int submit_flags; +static const char *hostname; int probe_init(void) { @@ -21,20 +22,16 @@ int probe_init(void) if (!strncmp(fwd_only, "true", 4)) submit_flags |= SUBMIT_NET_ONLY; + static char hostname_buf[32]; + if (gethostname(hostname_buf, sizeof(hostname_buf)) != 0) + strcpy(hostname_buf, "localhost"); + + hostname = config_get_string("global", "hostname", hostname_buf); return 0; } int probe_submit(struct sammler_plugin *plugin, const char *filename, int ds_id, const char *fmt, ... ) { - static const char *hostname = NULL; - if (hostname == NULL) { - static char hostname_buf[32]; - if (gethostname(hostname_buf, sizeof(hostname_buf)) != 0) - strcpy(hostname_buf, "localhost"); - - hostname = config_get_string("global", "hostname", hostname_buf); - } - char *buffer = malloc(BUFSIZE); if (buffer == NULL) { log_print(LOG_ERROR, "probe_submit: out of memory"); diff --git a/rrdtool-fake.c b/rrdtool-fake.c new file mode 100644 index 0000000..b51cda8 --- /dev/null +++ b/rrdtool-fake.c @@ -0,0 +1,30 @@ +/*************************************************************************** + * Copyright (C) 05/2009 by Olaf Rempel * + * razzor@kopf-tisch.de * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ +#include + +int rrd_init(void) +{ + return 0; +} + +int rrd_submit(const char *hostname, const char *pluginname, const char *filename, int ds_id, const char *data) +{ + return 0; +} diff --git a/rrdtool.c b/rrdtool.c index bb7a651..5c1d226 100644 --- a/rrdtool.c +++ b/rrdtool.c @@ -48,6 +48,14 @@ struct rra_cb_data { int *pos; }; +static const char *rrd_dir; + +int rrd_init(void) +{ + rrd_dir = config_get_string("global", "rrd_dir", "."); + return 0; +} + static int append_rra_config(const char *parameter, void *privdata) { struct rra_cb_data *data = (struct rra_cb_data *)privdata; @@ -201,10 +209,6 @@ static int create_parent_dirs(char *filename) int rrd_submit(const char *hostname, const char *pluginname, const char *filename, int ds_id, const char *data) { - static const char *rrd_dir = NULL; - if (rrd_dir == NULL) - rrd_dir = config_get_string("global", "rrd_dir", "."); - char *fullfile = malloc(BUFSIZE); if (fullfile == NULL) { log_print(LOG_ERROR, "rrd_submit: out of memory"); diff --git a/sammler.c b/sammler.c index 851b00b..1aa84db 100644 --- a/sammler.c +++ b/sammler.c @@ -31,6 +31,7 @@ #include "event.h" #include "logging.h" #include "network.h" +#include "rrdtool.h" #include "plugins.h" #include "probe.h" @@ -103,6 +104,9 @@ int main(int argc, char *argv[]) if (net_init()) exit(1); + if (rrd_init()) + exit(1); + if (plugin_init()) { net_close(); exit(1);