diff --git a/.gitignore b/.gitignore index 960d78f..54cbdba 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ *.o -*.so *.d sammler sammler.log diff --git a/Makefile b/Makefile index 21be0d4..6dc2105 100644 --- a/Makefile +++ b/Makefile @@ -1,17 +1,13 @@ # Toplevel Makefile +WITH_RRD=yes -WITH_RRD = yes - -WITH_MYSQL = yes - -WITH_CURL = yes +PLUGINS := ctstat load memory mount netdev random rtstat stat uptime vmstat +PLUGINS += apache mysql # ############################ 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 +CFLAGS := -O2 -Wall -fno-stack-protector -Iinclude LDFLAGS := -ldl -rdynamic # ############################ @@ -22,17 +18,10 @@ ifeq ("$(WITH_RRD)", "yes") LDFLAGS += -lrrd endif -ifeq ("$(WITH_MYSQL)", "yes") - PLUGIN_SRC += p_mysql.c -endif - -ifeq ("$(WITH_CURL)", "yes") - PLUGIN_SRC += p_apache.c -endif - # ############################ -all: sammler $(PLUGIN_SRC:%.c=%.so) +all: sammler + make -C plugins PLUGINS="$(PLUGINS)" sammler: $(SAMMLER_SRC:%.c=%.o) $(CC) $(LDFLAGS) $^ -o $@ @@ -43,19 +32,8 @@ sammler: $(SAMMLER_SRC:%.c=%.o) %.o: %.c $(CC) $(CFLAGS) -o $@ -c $< -%_sh.o: %.c - $(CC) $(CFLAGS) -fPIC -o $@ -c $< - -p_apache.so: p_apache_sh.o - $(LD) -shared -lcurl -o $@ $^ - -p_mysql.so: p_mysql_sh.o p_mysql_helper_sh.o - $(LD) -shared -lmysqlclient -o $@ $^ - -%.so: %_sh.o - $(LD) -shared -o $@ $< - clean: - rm -rf *.d *.o *.so sammler + rm -rf *.d *.o sammler + make -C plugins clean --include $(SAMMLER_SRC:.c=.d) $(PLUGIN_SRC:.c=.d) +-include $(SAMMLER_SRC:%.c=%.d) diff --git a/configfile.h b/include/configfile.h similarity index 100% rename from configfile.h rename to include/configfile.h diff --git a/event.h b/include/event.h similarity index 100% rename from event.h rename to include/event.h diff --git a/helper.h b/include/helper.h similarity index 100% rename from helper.h rename to include/helper.h diff --git a/list.h b/include/list.h similarity index 100% rename from list.h rename to include/list.h diff --git a/logging.h b/include/logging.h similarity index 100% rename from logging.h rename to include/logging.h diff --git a/network.h b/include/network.h similarity index 100% rename from network.h rename to include/network.h diff --git a/plugins.h b/include/plugins.h similarity index 100% rename from plugins.h rename to include/plugins.h diff --git a/include/probe.h b/include/probe.h new file mode 100644 index 0000000..875a60a --- /dev/null +++ b/include/probe.h @@ -0,0 +1,9 @@ +#ifndef _PROBE_H_ +#define _PROBE_H_ + +#include "plugins.h" + +int probe_init(void); +int probe_submit(struct sammler_plugin *plugin, const char *filename, int ds_id, const char *fmt, ... ); + +#endif /* _PROBE_H_ */ diff --git a/rrdtool.h b/include/rrdtool.h similarity index 100% rename from rrdtool.h rename to include/rrdtool.h diff --git a/plugins/.gitignore b/plugins/.gitignore new file mode 100644 index 0000000..fd9c310 --- /dev/null +++ b/plugins/.gitignore @@ -0,0 +1,3 @@ +*.o +*.so +*.d diff --git a/plugins/Makefile b/plugins/Makefile new file mode 100644 index 0000000..7542961 --- /dev/null +++ b/plugins/Makefile @@ -0,0 +1,26 @@ +CFLAGS := -O2 -Wall -fno-stack-protector -I../include +LDFLAGS := -ldl -rdynamic + +# ############################ + +all: $(PLUGINS:%=%.so) + +%.d: %.c + $(CC) $(CFLAGS) -MM -c $< -o $@ + +%.o: %.c + $(CC) $(CFLAGS) -fPIC -o $@ -c $< + +apache.so: apache.o + $(LD) -shared -lcurl -o $@ $^ + +mysql.so: mysql.o mysql_helper.o + $(LD) -shared -lmysqlclient -o $@ $^ + +%.so: %.o + $(LD) -shared -o $@ $< + +clean: + rm -rf *.d *.o *.so + +-include $(PLUGINS:%=%.d) diff --git a/p_apache.c b/plugins/apache.c similarity index 100% rename from p_apache.c rename to plugins/apache.c diff --git a/p_ctstat.c b/plugins/ctstat.c similarity index 100% rename from p_ctstat.c rename to plugins/ctstat.c diff --git a/p_load.c b/plugins/load.c similarity index 100% rename from p_load.c rename to plugins/load.c diff --git a/p_memory.c b/plugins/memory.c similarity index 100% rename from p_memory.c rename to plugins/memory.c diff --git a/p_mount.c b/plugins/mount.c similarity index 100% rename from p_mount.c rename to plugins/mount.c diff --git a/p_mysql.c b/plugins/mysql.c similarity index 99% rename from p_mysql.c rename to plugins/mysql.c index 5edb047..87290c6 100644 --- a/p_mysql.c +++ b/plugins/mysql.c @@ -27,7 +27,7 @@ #include "plugins.h" #include "probe.h" -#include "p_mysql_helper.h" +#include "mysql_helper.h" #define DS_TRAFFIC 1 #define DS_COMMANDS 2 diff --git a/p_mysql_helper.c b/plugins/mysql_helper.c similarity index 99% rename from p_mysql_helper.c rename to plugins/mysql_helper.c index 2c1faee..f6601ab 100644 --- a/p_mysql_helper.c +++ b/plugins/mysql_helper.c @@ -5,7 +5,7 @@ #include #include "logging.h" -#include "p_mysql_helper.h" +#include "mysql_helper.h" void * init_connection(const char *host, const char *user, const char *pass) { diff --git a/p_mysql_helper.h b/plugins/mysql_helper.h similarity index 89% rename from p_mysql_helper.h rename to plugins/mysql_helper.h index 248b971..07ef582 100644 --- a/p_mysql_helper.h +++ b/plugins/mysql_helper.h @@ -1,5 +1,5 @@ -#ifndef _P_MYSQL_HELPER_H_ -#define _P_MYSQL_HELPER_H_ +#ifndef _MYSQL_HELPER_H_ +#define _MYSQL_HELPER_H_ #include @@ -32,4 +32,4 @@ int close_connection(void *mysql); int get_stats(void *mysql, struct mysql_stats *stats); -#endif /* _P_MYSQL_HELPER_H_ */ +#endif /* _MYSQL_HELPER_H_ */ diff --git a/p_netdev.c b/plugins/netdev.c similarity index 100% rename from p_netdev.c rename to plugins/netdev.c diff --git a/p_random.c b/plugins/random.c similarity index 100% rename from p_random.c rename to plugins/random.c diff --git a/p_rtstat.c b/plugins/rtstat.c similarity index 100% rename from p_rtstat.c rename to plugins/rtstat.c diff --git a/p_stat.c b/plugins/stat.c similarity index 100% rename from p_stat.c rename to plugins/stat.c diff --git a/p_uptime.c b/plugins/uptime.c similarity index 100% rename from p_uptime.c rename to plugins/uptime.c diff --git a/p_vmstat.c b/plugins/vmstat.c similarity index 100% rename from p_vmstat.c rename to plugins/vmstat.c diff --git a/probe.c b/probe.c new file mode 100644 index 0000000..d5fa93c --- /dev/null +++ b/probe.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include + +#include "configfile.h" +#include "logging.h" +#include "network.h" +#include "rrdtool.h" + +#define BUFSIZE 512 + +#define SUBMIT_NET_ONLY 0x01 + +static int submit_flags; + +int probe_init(void) +{ + const char *fwd_only = config_get_string("global", "forward_only", "false"); + if (fwd_only == NULL || strncmp(fwd_only, "true", 4)) + submit_flags |= SUBMIT_NET_ONLY; + + 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) + 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/sammler.conf b/sammler.conf index 3f0da5c..ac187a6 100644 --- a/sammler.conf +++ b/sammler.conf @@ -9,19 +9,19 @@ logfile sammler.log rrd_dir rrd -plugin_dir . -plugin p_stat.so -plugin p_load.so -plugin p_memory.so -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_random.so -plugin p_mysql.so -plugin p_apache.so +plugin_dir plugins +plugin stat.so +plugin load.so +plugin memory.so +plugin vmstat.so +plugin uptime.so +plugin netdev.so +plugin mount.so +plugin ctstat.so +plugin rtstat.so +plugin random.so +plugin mysql.so +plugin apache.so # 1h(10s), 12h(1min), 48h(2min), 14d(15min), 4w(60min), 2y(12h) rra RRA:MIN:0.1:1:360 RRA:AVERAGE:0.1:1:360 RRA:MAX:0.1:1:360