2007-03-24 19:49:30 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <getopt.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}
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2007-03-25 15:24:47 +02:00
|
|
|
/* init netlink & hashtables */
|
2007-03-24 19:49:30 +01:00
|
|
|
if (conntrack_init())
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
/* init database connection */
|
|
|
|
if (database_init()) {
|
|
|
|
conntrack_close();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2007-03-25 15:24:47 +02:00
|
|
|
/* start event listener */
|
2007-03-24 19:49:30 +01:00
|
|
|
conntrack_start_event_thread();
|
|
|
|
|
|
|
|
int intervall = config_get_int("global", "intervall", DEFAULT_INTERVALL);
|
|
|
|
while (1) {
|
|
|
|
sleep(intervall);
|
|
|
|
|
2007-03-25 15:24:47 +02:00
|
|
|
/* parse hashes, fill database */
|
2007-03-24 19:49:30 +01:00
|
|
|
database_analyse();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* how to stop? */
|
|
|
|
database_close();
|
|
|
|
conntrack_close();
|
|
|
|
return 0;
|
|
|
|
}
|