2006-06-13 21:34:36 +02:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 06/2006 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 <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <sys/stat.h>
|
2006-09-30 17:10:32 +02:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <signal.h>
|
2006-06-13 21:34:36 +02:00
|
|
|
|
2006-08-03 19:49:07 +02:00
|
|
|
#include "configfile.h"
|
2006-06-13 21:34:36 +02:00
|
|
|
#include "logging.h"
|
2006-09-30 20:27:56 +02:00
|
|
|
#include "network.h"
|
2006-06-13 21:34:36 +02:00
|
|
|
#include "plugins.h"
|
|
|
|
|
|
|
|
#define DEFAULT_CONFIG "sammler.conf"
|
|
|
|
#define DEFAULT_LOGFILE "sammler.log"
|
|
|
|
|
|
|
|
static struct option opts[] = {
|
|
|
|
{"config", 1, 0, 'c'},
|
|
|
|
{"debug", 0, 0, 'd'},
|
|
|
|
{"help", 0, 0, 'h'},
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char *config = DEFAULT_CONFIG;
|
|
|
|
int code, arg = 0, debug = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
code = getopt_long(argc, argv, "c:dh", opts, &arg);
|
|
|
|
|
|
|
|
switch (code) {
|
|
|
|
case 'c': /* config */
|
|
|
|
config = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd': /* debug */
|
|
|
|
debug = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'h': /* help */
|
|
|
|
printf("Usage: sammler [options]\n"
|
|
|
|
"Options: \n"
|
|
|
|
" --config -c configfile use this configfile\n"
|
|
|
|
" --debug -d do not fork and log to stderr\n"
|
|
|
|
" --help -h this help\n"
|
|
|
|
"\n");
|
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?': /* error */
|
|
|
|
exit(-1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* unknown / all options parsed */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (code != -1);
|
|
|
|
|
|
|
|
/* parse config file */
|
|
|
|
if (!config_parse(config))
|
|
|
|
exit(-1);
|
|
|
|
|
|
|
|
/* check logfile */
|
|
|
|
char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE);
|
2006-06-22 20:52:54 +02:00
|
|
|
if (logfile != NULL && debug == 0) {
|
2006-06-13 21:34:36 +02:00
|
|
|
/* start logging */
|
|
|
|
if (!log_init(logfile))
|
|
|
|
exit(-1);
|
|
|
|
|
|
|
|
/* zum daemon mutieren */
|
|
|
|
daemon(-1, 0);
|
|
|
|
}
|
|
|
|
|
2006-08-03 19:49:07 +02:00
|
|
|
char *hostname = config_get_string("global", "hostname", "localhost");
|
|
|
|
log_print(LOG_EVERYTIME, "sammler (pid:%d) started on host '%s'", getpid(), hostname);
|
2006-06-13 21:34:36 +02:00
|
|
|
|
2006-09-30 17:10:32 +02:00
|
|
|
fd_set fdsel, fdcpy;
|
|
|
|
FD_ZERO(&fdsel);
|
|
|
|
|
2006-09-30 20:27:56 +02:00
|
|
|
int srv_sock = net_init_srv();
|
|
|
|
if (srv_sock != -1)
|
|
|
|
FD_SET(srv_sock, &fdsel);
|
|
|
|
|
|
|
|
int probe_flags = 0;
|
|
|
|
if (net_init_cli())
|
|
|
|
probe_flags |= PLUGIN_NET;
|
|
|
|
|
|
|
|
char *fwd_only = config_get_string("global", "forward_only", NULL);
|
|
|
|
if (fwd_only == NULL || strncmp(fwd_only, "true", 4))
|
|
|
|
probe_flags |= PLUGIN_RRD;
|
|
|
|
|
|
|
|
plugin_init(probe_flags);
|
|
|
|
|
2006-09-30 17:10:32 +02:00
|
|
|
struct timeval tv;
|
2006-09-30 20:27:56 +02:00
|
|
|
tv.tv_sec = 0;
|
2006-09-30 17:10:32 +02:00
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
2006-06-13 21:34:36 +02:00
|
|
|
while (1) {
|
2006-09-30 17:10:32 +02:00
|
|
|
memcpy(&fdcpy, &fdsel, sizeof(fdsel));
|
|
|
|
|
2006-09-30 20:27:56 +02:00
|
|
|
int i = select(FD_SETSIZE, &fdcpy, NULL, NULL, &tv);
|
2006-09-30 17:10:32 +02:00
|
|
|
if (i == -1) {
|
|
|
|
log_print(LOG_ERROR, "select()");
|
|
|
|
|
|
|
|
/* timeout */
|
|
|
|
} else if (i == 0) {
|
|
|
|
plugins_probe();
|
|
|
|
tv.tv_sec = 1;
|
|
|
|
tv.tv_usec = 0;
|
2006-09-30 20:27:56 +02:00
|
|
|
|
|
|
|
} else if (FD_ISSET(srv_sock, &fdsel)) {
|
|
|
|
net_receive(srv_sock);
|
2006-09-30 17:10:32 +02:00
|
|
|
}
|
2006-06-13 21:34:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|