ctstats/ctstats.c

140 lines
3.6 KiB
C

/***************************************************************************
* Copyright (C) 07/2007 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; version 2 of the License *
* *
* 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 <signal.h>
#include <errno.h>
#include "configfile.h"
#include "conntrack.h"
#include "database.h"
#include "logging.h"
#define DEFAULT_CONFIG "ctstats.conf"
#define DEFAULT_LOGFILE "ctstats.log"
#define DEFAULT_INTERVALL 300
static struct option opts[] = {
{"config", 1, 0, 'c'},
{"debug", 0, 0, 'd'},
{"help", 0, 0, 'h'},
{0, 0, 0, 0}
};
static int sig_received = 0;
static void my_sighandler(int s)
{
sig_received = 1;
}
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: ctstat [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);
/* init netlink & hashtables */
if (conntrack_init())
exit(1);
/* init database connection */
if (database_init()) {
conntrack_close();
exit(1);
}
/* check logfile */
const char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE);
if (logfile != NULL && debug == 0) {
/* start logging */
if (log_init(logfile))
exit(1);
/* zum daemon mutieren */
daemon(-1, 0);
}
log_print(LOG_EVERYTIME, "ctstats started (pid: %d)", getpid());
signal(SIGINT, my_sighandler);
signal(SIGTERM, my_sighandler);
/* start event listener */
conntrack_start_event_thread();
int intervall = config_get_int("global", "intervall", DEFAULT_INTERVALL);
struct timeval tv;
tv.tv_sec = intervall;
tv.tv_usec = 0;
while (sig_received == 0) {
select(0, NULL, NULL, NULL, &tv);
errno = 0;
tv.tv_sec = intervall;
tv.tv_usec = 0;
database_analyse();
}
database_close();
conntrack_close();
log_close();
return 0;
}