buildsystem and lib update

This commit is contained in:
Olaf Rempel 2012-12-09 12:26:51 +01:00
parent 7aa19ecade
commit 6746926791
12 changed files with 162 additions and 146 deletions

View File

@ -1,5 +1,5 @@
# Toplevel Makefile
WITH_RRD=yes
WITH_RRD?=yes
PLUGINS := ctstat diskstat hwmon load memory mount netdev random rtstat stat uptime vmstat
#PLUGINS += apache mysql conntrack alixusv ts2 diskstandby
@ -17,20 +17,19 @@ WWW_OWNER = www-data
# ############################
SRC := configfile.c event.c helper.c linebuffer.c logging.c network.c pidfile.c
SRC += plugins.c probe.c sammler.c signals.c sockaddr.c
CFLAGS := -O2 -Wall -MMD -I.
LDFLAGS := -ldl -rdynamic
SRC := $(wildcard *.c)
BUILD_DIR = build
CFLAGS = -O2 -g -pipe -Wall -Wno-unused-result -I.
CFLAGS += -MMD -MF $(BUILD_DIR)/$(*D)/$(*F).d
LDFLAGS = -rdynamic -ldl
# ############################
ifeq ("$(WITH_RRD)", "yes")
SRC += rrdtool.c
CFLAGS += -DWITH_RRD
LDFLAGS_TARGET = -lrrd
TARGET = sammler
else
SRC += rrdtool-fake.c
LDFLAGS_TARGET =
TARGET = sammler_norrd
endif
@ -45,34 +44,36 @@ endif
all: $(TARGET) plugins
$(TARGET): $(SRC:%.c=%.o)
$(TARGET): $(patsubst %,$(BUILD_DIR)/%, $(SRC:.c=.o))
@echo " Linking file: $@"
@$(CC) $(LDFLAGS) $(LDFLAGS_TARGET) $^ -o $@
@$(CC) $^ $(LDFLAGS) $(LDFLAGS_TARGET) -o $@
%.o: %.c
$(BUILD_DIR)/%.o: %.c $(MAKEFILE_LIST)
@echo " Building file: $<"
@$(shell test -d $(BUILD_DIR)/$(*D) || mkdir -p $(BUILD_DIR)/$(*D))
@$(CC) $(CFLAGS) -o $@ -c $<
.PHONY: plugins
plugins: $(PLUGINS:%=plugins/%.o) $(PLUGINS:%=plugins/%.so)
plugins: $(PLUGINS:%=plugins/%.so)
plugins/%.so: plugins/%.o
.SECONDARY: $(patsubst %,$(BUILD_DIR)/%, $(PLUGINS:%=plugins/%.o))
plugins/%.so: $(BUILD_DIR)/plugins/%.o
@echo " Linking file: $@"
@$(CC) $(LDFLAGS) -shared -o $@ $<
plugins/apache.so: plugins/apache.o
plugins/apache.so: $(BUILD_DIR)/plugins/apache.o
@echo " Linking file: $@"
@$(CC) $(LDFLAGS) -shared -lcurl -o $@ $<
plugins/conntrack.so: plugins/conntrack.o
plugins/conntrack.so: $(BUILD_DIR)/plugins/conntrack.o
@echo " Linking file: $@"
@$(CC) $(LDFLAGS) -shared -lnfnetlink -lnetfilter_conntrack -o $@ $<
plugins/mysql.so: plugins/mysql.o plugins/mysql_helper.o
plugins/mysql.so: $(BUILD_DIR)/plugins/mysql.o $(BUILD_DIR)/plugins/mysql_helper.o
@echo " Linking file: $@"
@$(CC) $(LDFLAGS) -shared -lmysqlclient -o $@ $^
plugins/diskstandby.so: plugins/diskstandby.o plugins/sgio.o
plugins/diskstandby.so: $(BUILD_DIR)/plugins/diskstandby.o $(BUILD_DIR)/plugins/sgio.o
@echo " Linking file: $@"
@$(CC) $(LDFLAGS) -shared -o $@ $^
@ -104,6 +105,6 @@ else
endif
clean:
rm -rf *.d plugins/*.d *.o plugins/*.o plugins/*.so $(TARGET)
rm -rf build plugins/*.so $(TARGET)
include $(shell find . -name \*.d 2> /dev/null)
include $(shell find $(BUILD_DIR) -name \*.d 2> /dev/null)

View File

@ -22,11 +22,6 @@
#include <unistd.h>
#include <ctype.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include "configfile.h"
#include "list.h"
#include "logging.h"
@ -90,13 +85,13 @@ int config_parse(const char *config)
{
FILE *fz = fopen(config, "r");
if (fz == NULL) {
log_print(LOG_ERROR, "config_parse(): %s", config);
log_print(LOG_ERROR, "%s(): failed to open config '%s'", __FUNCTION__, config);
return -1;
}
char *line = malloc(BUFSIZE);
if (line == NULL) {
log_print(LOG_ERROR, "config_parse(): out of memory");
log_print(LOG_ERROR, "%s(): out of memory", __FUNCTION__);
fclose(fz);
return -1;
}
@ -113,7 +108,7 @@ int config_parse(const char *config)
char *tok = strtok(line +1, " ]\n");
if (tok == NULL || (section = config_add_section(tok)) == NULL) {
log_print(LOG_WARN, "config_parse(): invalid section in row %d", linenum);
log_print(LOG_WARN, "%s(): invalid section in row %d", __FUNCTION__, linenum);
free(line);
fclose(fz);
return -1;
@ -121,7 +116,7 @@ int config_parse(const char *config)
continue;
} else if (section == NULL) {
log_print(LOG_WARN, "config_parse(): missing section in row %d", linenum);
log_print(LOG_WARN, "%s(): missing section in row %d", __FUNCTION__, linenum);
free(line);
fclose(fz);
return -1;
@ -132,7 +127,7 @@ int config_parse(const char *config)
char *tok2;
while ((tok2 = strtok_r(NULL, " \n", &tmp))) {
if (config_add_tupel(section, tok, tok2) != 0)
log_print(LOG_WARN, "config_parse(): invalid row %d", linenum);
log_print(LOG_WARN, "%s(): invalid row %d", __FUNCTION__, linenum);
}
}
}
@ -233,14 +228,18 @@ int config_get_strings(const char *section_str, const char *option,
struct strtoken * strtokenize(const char *input, const char *delim, int maxfields)
{
struct strtoken *tokens = malloc(sizeof(struct strtoken) +
maxfields * sizeof(char *) +
strlen(input) +1);
(maxfields +1) * sizeof(char *) +
strlen(input));
if (tokens == NULL)
return NULL;
char *ptr = (char *)&tokens->field[maxfields];
strcpy(ptr, input);
tokens->input = input;
tokens->delim = delim;
tokens->maxfields = maxfields;
int i;
char *tmp;

View File

@ -13,6 +13,9 @@ int config_get_strings(const char *section_str, const char *option,
void *privdata);
struct strtoken {
const char *input;
const char *delim;
int maxfields;
int count;
char *field[0];
};

105
event.c
View File

@ -47,7 +47,8 @@ struct event_timeout {
unsigned int flags;
struct timeval intervall;
struct timeval nextrun;
int (*callback)(void *privdata);
int (*callback)(int timerid, void *privdata);
int timerid;
void *privdata;
};
@ -60,13 +61,13 @@ struct event_fd * event_add_fd(
{
/* check valid filediskriptor */
if (fd < 0 || fd > FD_SETSIZE) {
log_print(LOG_ERROR, "event_add_fd(): invalid fd");
log_print(LOG_ERROR, "%s(): invalid fd", __FUNCTION__);
return NULL;
}
/* check valid type (read/write) */
if (!(type & FD_TYPES)) {
log_print(LOG_ERROR, "event_add_fd(): invalid type");
log_print(LOG_ERROR, "%s(): invalid type", __FUNCTION__);
return NULL;
}
@ -74,7 +75,7 @@ struct event_fd * event_add_fd(
if (entry == NULL) {
entry = malloc(sizeof(struct event_fd));
if (entry == NULL) {
log_print(LOG_ERROR, "event_add_fd(): out of memory");
log_print(LOG_ERROR, "%s(): out of memory", __FUNCTION__);
return NULL;
}
@ -171,19 +172,21 @@ static void schedule_nextrun(struct event_timeout *entry, struct timeval *now)
struct event_timeout * event_add_timeout(
struct timeval *timeout,
int (*callback)(void *privdata),
int (*callback)(int timerid, void *privdata),
int timerid,
void *privdata)
{
struct event_timeout *entry;
entry = malloc(sizeof(struct event_timeout));
if (entry == NULL) {
log_print(LOG_ERROR, "event_add_timeout(): out of memory");
log_print(LOG_ERROR, "%s(): out of memory", __FUNCTION__);
return NULL;
}
entry->flags = 0;
memcpy(&entry->intervall, timeout, sizeof(entry->intervall));
entry->callback = callback;
entry->timerid = timerid;
entry->privdata = privdata;
struct timeval now;
@ -193,22 +196,36 @@ struct event_timeout * event_add_timeout(
return entry;
}
struct event_timeout * event_add_timeout_ms(
int timeout_ms,
int (*callback)(int timerid, void *privdata),
int timerid,
void *privdata)
{
struct timeval tv;
tv.tv_sec = timeout_ms / 1000;
tv.tv_usec = (timeout_ms % 1000) * 1000;
return event_add_timeout(&tv, callback, timerid, privdata);
}
void event_remove_timeout(struct event_timeout *entry)
{
/* mark the event as deleted -> remove in select() loop */
entry->flags |= EVENT_DELETE;
}
int event_loop(int (*callback)(void *privdata), void *privdata)
int event_loop(int (*pre_select_cb)(int *maxfd, void *readfds, void *writefds, struct timeval *timeout, void *privdata),
int (*post_select_cb)(int retval, void *readfds, void *writefds, void *privdata),
void *privdata)
{
fd_set *fdsets = malloc(sizeof(fd_set) * 2);
if (fdsets == NULL) {
log_print(LOG_ERROR, "event_loop(): out of memory");
return -1;
}
while (1) {
struct timeval timeout, *timeout_p = NULL;
/* default value if no application timeout is present */
struct timeval timeout = {
.tv_sec = -1,
.tv_usec = -1,
};
if (!list_empty(&event_timeout_list)) {
struct timeval now;
gettimeofday(&now, NULL);
@ -229,7 +246,7 @@ int event_loop(int (*callback)(void *privdata), void *privdata)
list_del(&entry->list);
/* execute callback, when callback returns 0 -> schedule event again */
if (entry->callback(entry->privdata)) {
if (entry->callback(entry->timerid, entry->privdata)) {
free(entry);
} else {
@ -242,14 +259,16 @@ int event_loop(int (*callback)(void *privdata), void *privdata)
/* calc select() timeout */
sub_timeval(&timeout, &entry->nextrun, &now);
timeout_p = &timeout;
}
}
fd_set *readfds = NULL, *writefds = NULL;
struct event_fd *entry, *tmp;
int maxfd = -1;
fd_set readfds, writefds;
FD_ZERO(&readfds);
FD_ZERO(&writefds);
list_for_each_entry_safe(entry, tmp, &event_fd_list, list) {
entry->flags &= ~EVENT_NEW;
@ -259,52 +278,54 @@ int event_loop(int (*callback)(void *privdata), void *privdata)
continue;
}
if (entry->flags & FD_READ) {
if (readfds == NULL) {
readfds = &fdsets[0];
FD_ZERO(readfds);
}
FD_SET(entry->fd, readfds);
}
if (entry->flags & FD_READ)
FD_SET(entry->fd, &readfds);
if (entry->flags & FD_WRITE) {
if (writefds == NULL) {
writefds = &fdsets[1];
FD_ZERO(writefds);
}
FD_SET(entry->fd, writefds);
}
if (entry->flags & FD_WRITE)
FD_SET(entry->fd, &writefds);
maxfd = (entry->fd > maxfd) ? entry->fd : maxfd;
}
maxfd++;
/* exit loop if callback returns true */
if (callback != NULL && callback(privdata) != 0)
if (pre_select_cb != NULL && pre_select_cb(&maxfd, (void *)&readfds, (void *)&writefds, &timeout, privdata) != 0)
break;
int i = select(maxfd +1, readfds, writefds, NULL, timeout_p);
if (i < 0 && errno == EINTR) {
int retval;
if (timeout.tv_sec == -1 && timeout.tv_usec == -1)
retval = select(maxfd, &readfds, &writefds, NULL, NULL);
else
retval = select(maxfd, &readfds, &writefds, NULL, &timeout);
/* exit loop if callback returns true */
if (post_select_cb != NULL && post_select_cb(retval, (void *)&readfds, (void *)&writefds, privdata) != 0)
break;
if (retval < 0 && errno == EINTR) {
errno = 0;
continue;
} else if (i < 0) {
log_print(LOG_ERROR, "event_loop(): select():");
continue;
} else if (i == 0) {
} else if (retval < 0) {
log_print(LOG_ERROR, "%s(): select():", __FUNCTION__);
continue;
}
/* timeout */
if (retval == 0)
continue;
list_for_each_entry(entry, &event_fd_list, list) {
if (((entry->flags & (FD_READ | EVENT_NEW)) == FD_READ) && FD_ISSET(entry->fd, readfds))
if (((entry->flags & (FD_READ | EVENT_NEW)) == FD_READ) && FD_ISSET(entry->fd, &readfds))
if (entry->read_cb(entry->fd, entry->read_priv) != 0)
entry->flags |= EVENT_DELETE;
if (((entry->flags & (FD_WRITE | EVENT_NEW)) == FD_WRITE) && FD_ISSET(entry->fd, writefds))
if (((entry->flags & (FD_WRITE | EVENT_NEW)) == FD_WRITE) && FD_ISSET(entry->fd, &writefds))
if (entry->write_cb(entry->fd, entry->write_priv) != 0)
entry->flags |= EVENT_DELETE;
}
}
free(fdsets);
return 0;
}

13
event.h
View File

@ -32,11 +32,20 @@ void event_remove_fd(struct event_fd *entry);
struct event_timeout * event_add_timeout(
struct timeval *timeout,
int (*callback)(void *privdata),
int (*callback)(int timerid, void *privdata),
int timerid,
void *privdata);
struct event_timeout * event_add_timeout_ms(
int timeout_ms,
int (*callback)(int timerid, void *privdata),
int timerid,
void *privdata);
void event_remove_timeout(struct event_timeout *entry);
int event_loop(int (*callback)(void *privdata), void *privdata);
int event_loop(int (*pre_select_cb)(int *maxfd, void *readfds, void *writefds, struct timeval *timeout, void *privdata),
int (*post_select_cb)(int retval, void *readfds, void *writefds, void *privdata),
void *privdata);
#endif /* _EVENT_H_ */

View File

@ -29,7 +29,7 @@
#define BUFSIZE 8192
static FILE *log_fd = NULL;
static FILE *log_file = NULL;
static int log_prio = LOG_EVERYTIME;
static char *buffer = NULL;
@ -44,12 +44,12 @@ int log_print(int prio, const char *fmt, ...)
if (buffer == NULL) {
buffer = malloc(BUFSIZE);
if (buffer == NULL) {
fprintf(stderr, "log_print(): out of memory\n");
fprintf(stderr, "%s(): out of memory\n", __FUNCTION__);
return -1;
}
}
if (log_fd != NULL) {
if (log_file != NULL) {
time_t tzgr;
time(&tzgr);
@ -62,7 +62,7 @@ int log_print(int prio, const char *fmt, ...)
if (len < 0 || len >= BUFSIZE) {
errno = 0;
return log_print(LOG_ERROR, "log_print: arguments too long");
return log_print(LOG_ERROR, "%s: arguments too long", __FUNCTION__);
}
if (errno) {
@ -70,8 +70,8 @@ int log_print(int prio, const char *fmt, ...)
errno = 0;
}
retval = fprintf((log_fd ? log_fd : stderr), "%s\n", buffer);
fflush(log_fd);
retval = fprintf((log_file ? log_file : stderr), "%s\n", buffer);
fflush(log_file);
return retval;
}
@ -82,25 +82,26 @@ void log_close(void)
buffer = NULL;
}
if (log_fd) {
fclose(log_fd);
log_fd = NULL;
if (log_file) {
fclose(log_file);
log_file = NULL;
}
}
int log_init(const char *logfile)
{
if (log_fd != NULL)
if (log_file != NULL)
log_close();
log_fd = fopen(logfile, "a");
if (log_fd == NULL) {
fprintf(stderr, "log_init(): can not open logfile");
log_file = fopen(logfile, "a");
if (log_file == NULL) {
fprintf(stderr, "%s(): can not open logfile", __FUNCTION__);
return -1;
}
if (fcntl(fileno(log_fd), F_SETFD, FD_CLOEXEC) < 0) {
fprintf(stderr, "log_init(): fcntl(FD_CLOEXEC)");
if (fcntl(fileno(log_file), F_SETFD, FD_CLOEXEC) < 0) {
fprintf(stderr, "%s(): fcntl(FD_CLOEXEC)", __FUNCTION__);
fclose(log_file);
return -1;
}

View File

@ -1,14 +1,18 @@
#ifndef _LOGGING_H_
#define _LOGGING_H_
#define LOG_DEBUG 5
#define LOG_INFO 4
#define LOG_NOTICE 3
#define LOG_WARN 2
#define LOG_ERROR 1
#define LOG_CRIT 0
#define LOG_EMERG 0 /* system is unusable */
#define LOG_ALERT 1 /* action must be taken immediately */
#define LOG_CRIT 2 /* critical conditions */
#define LOG_ERR 3 /* error conditions */
#define LOG_WARNING 4 /* warning conditions */
#define LOG_NOTICE 5 /* normal but significant condition */
#define LOG_INFO 6 /* informational */
#define LOG_DEBUG 7 /* debug-level messages */
#define LOG_EVERYTIME 0
#define LOG_EVERYTIME LOG_EMERG
#define LOG_ERROR LOG_ERR
#define LOG_WARN LOG_WARNING
int log_init(const char *logfile);
void log_close(void);

View File

@ -85,7 +85,7 @@ static int plugin_init_cb(const char *filename, void *privdata)
return 0;
}
static int plugins_probe(void *privdata)
static int plugins_probe(int timerid, void *privdata)
{
time_t now;
time(&now);
@ -117,10 +117,7 @@ int plugin_init(void)
return -1;
}
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
probe_event = event_add_timeout(&tv, plugins_probe, NULL);
probe_event = event_add_timeout_ms(1000, plugins_probe, 0, NULL);
return 0;
}

View File

@ -1,30 +0,0 @@
/***************************************************************************
* 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 <rrdtool.h>
int sammler_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;
}

View File

@ -33,6 +33,7 @@
#include "logging.h"
#include "plugins.h"
#if (WITH_RRD)
#define DEFAULT_STEP 10
#define ARGCMAX 64
@ -239,3 +240,15 @@ int rrd_submit(const char *hostname, const char *pluginname, const char *filenam
free(fullfile);
return 0;
}
#else /* (WITH_RRD) */
int sammler_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;
}
#endif /* (WITH_RRD) */

View File

@ -52,15 +52,13 @@ static int restart_var;
static void trigger_restart(void *privdata)
{
int *restart = (int *)privdata;
*restart = 1;
restart_var = 1;
}
static int check_restart(void *privdata)
static int check_restart(int *maxfd, void *readfds, void *writefds, struct timeval *timeout, void *privdata)
{
int *restart = (int *)privdata;
if (*restart == 1) {
*restart = 0;
if (restart_var == 1) {
restart_var = 0;
return 1;
}
@ -134,7 +132,7 @@ int main(int argc, char *argv[])
}
signal_init();
signal_add_callback(SIGHUP, trigger_restart, (void *)&restart_var);
signal_add_callback(SIGHUP, trigger_restart, NULL);
log_print(LOG_EVERYTIME, "sammler started (pid:%d)", getpid());
@ -152,7 +150,7 @@ int main(int argc, char *argv[])
break;
/* exited on restart / SIGUSR1 */
event_loop(check_restart, (void *)&restart_var);
event_loop(check_restart, NULL, NULL);
plugin_close();

View File

@ -68,7 +68,7 @@ int signal_remove_callback(int signum, int type)
}
if (sigaction(signum, &sig_action, NULL) < 0) {
log_print(LOG_WARN, "signal_remove_callback(): sigaction()");
log_print(LOG_WARN, "%s(): sigaction(%d)", __FUNCTION__, signum);
return -1;
}
@ -81,7 +81,7 @@ int signal_add_callback(int signum, void (*callback)(void *privdata), void *priv
{
struct signal_entry *entry = malloc(sizeof(struct signal_entry));
if (entry == NULL) {
log_print(LOG_WARN, "signal_add_callback(): out of memory");
log_print(LOG_WARN, "%s(): out of memory", __FUNCTION__);
return -1;
}
@ -96,7 +96,7 @@ int signal_add_callback(int signum, void (*callback)(void *privdata), void *priv
};
if (sigaction(signum, &sig_action, NULL) < 0) {
log_print(LOG_WARN, "signal_add_callback(): sigaction()");
log_print(LOG_WARN, "%s(): sigaction(%d)", __FUNCTION__, signum);
list_del(&entry->list);
free(entry);
return -1;
@ -110,7 +110,7 @@ static int sig_event(int fd, void *privdata)
unsigned char signum;
int len = read(fd, &signum, 1);
if (len <= 0) {
log_print(LOG_WARN, "sig_event(): read()");
log_print(LOG_WARN, "%s(): read()", __FUNCTION__);
return -1;
}
@ -132,17 +132,17 @@ static int sig_event(int fd, void *privdata)
int signal_init(void)
{
if (pipe(sig_pipe) < 0) {
log_print(LOG_ERROR, "signal_init(): pipe()");
log_print(LOG_ERROR, "%s(): pipe()", __FUNCTION__);
return -1;
}
if (fcntl(sig_pipe[0], F_SETFD, FD_CLOEXEC) < 0) {
log_print(LOG_WARN, "signal_init(): fcntl(FD_CLOEXEC)");
log_print(LOG_WARN, "%s(): fcntl(FD_CLOEXEC)", __FUNCTION__);
return -1;
}
if (fcntl(sig_pipe[1], F_SETFD, FD_CLOEXEC) < 0) {
log_print(LOG_WARN, "signal_init(): fcntl(FD_CLOEXEC)");
log_print(LOG_WARN, "%s(): fcntl(FD_CLOEXEC)", __FUNCTION__);
return -1;
}