diff --git a/ctstats.c b/ctstats.c index 8ccaf1c..b448ee1 100644 --- a/ctstats.c +++ b/ctstats.c @@ -71,6 +71,17 @@ int main(int argc, char *argv[]) exit(1); } + /* check logfile */ + 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); + } + /* start event listener */ conntrack_start_event_thread(); diff --git a/ctstats.sql b/ctstats.sql new file mode 100644 index 0000000..fcc38df --- /dev/null +++ b/ctstats.sql @@ -0,0 +1,19 @@ +CREATE DATABASE `ctstats` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci; +USE `ctstats`; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `stats` +-- + +CREATE TABLE IF NOT EXISTS `stats` ( + `timestamp` int(10) unsigned NOT NULL, + `srcip` int(10) unsigned NOT NULL default '0', + `proto` int(10) unsigned NOT NULL default '0', + `dport` int(10) unsigned NOT NULL default '0', + `srcbytes` int(10) unsigned NOT NULL default '0', + `dstbytes` int(10) unsigned NOT NULL default '0', + `count` int(10) unsigned NOT NULL default '0', + PRIMARY KEY (`timestamp`,`srcip`,`proto`,`dport`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1; diff --git a/database.c b/database.c index 3adb761..eac8fd3 100644 --- a/database.c +++ b/database.c @@ -15,18 +15,16 @@ static MYSQL *dbh; static void purge_hash_cb(struct hash_entry *entry, void *privdata) { - struct in_addr src_ip = { .s_addr = entry->src_ip }; + int long *now = (long *)privdata; char query[256]; int len = snprintf(query, sizeof(query), - "INSERT INTO stats SET srcip='%s', proto='%u', dport='%u', srcbytes='%llu', dstbytes='%llu', count='%u'", - inet_ntoa(src_ip), entry->protonum, ntohs(entry->dst_port), + "INSERT INTO stats SET timestamp='%lu', srcip='%u', proto='%u', dport='%u', srcbytes='%llu', dstbytes='%llu', count='%u'", + *now, ntohl(entry->src_ip), entry->protonum, ntohs(entry->dst_port), entry->src_bytes, entry->dst_bytes, entry->count); if (mysql_real_query(dbh, query, len +1) != 0) log_print(LOG_WARN, "purge_hash_cb: mysql_real_query(): %s", mysql_error(dbh)); - - log_print(LOG_DEBUG, query); } int database_analyse(void) @@ -39,7 +37,8 @@ int database_analyse(void) return 0; } - purge_hash(hash, purge_hash_cb, NULL); + long now = time(NULL); + purge_hash(hash, purge_hash_cb, (void *)&now); return 0; }