5 # This script will log headers and other data to a database
\r
6 # for further analysis by parsing a feed from INN.
\r
8 # It is part of the NewsStats package.
\r
10 # Copyright (c) 2010 Thomas Hochstein <thh@inter.net>
\r
12 # It can be redistributed and/or modified under the same terms under
\r
13 # which Perl itself is published.
\r
16 our $VERSION = "0.01";
\r
18 push(@INC, dirname($0));
\r
24 use Sys::Syslog qw(:standard :macros);
\r
29 ################################# Main program #################################
\r
31 ### read commandline options
\r
32 my %Options = &ReadOptions('qd');
\r
34 ### read configuration
\r
35 my %Conf = %{ReadConfig('newsstats.conf')};
\r
38 openlog($MySelf, 'nofatal,pid', LOG_NEWS);
\r
39 syslog(LOG_NOTICE, "$MyVersion starting up.") if !$Options{'q'};
\r
42 my $DBHandle = InitDB(\%Conf,0);
\r
44 syslog(LOG_CRIT, 'Database connection failed: %s', $DBI::errstr);
\r
45 while (1) {}; # go into endless loop to suppress further errors and respawning
\r
47 my $DBQuery = $DBHandle->prepare(sprintf("INSERT INTO %s.%s (day,date,mid,timestamp,token,size,peer,path,newsgroups,headers) VALUES (?,?,?,?,?,?,?,?,?,?)",$Conf{'DBDatabase'},$Conf{'DBTableRaw'}));
\r
52 # catch empty lines trailing or leading
\r
56 # first line contains: mid, timestamp, token, size, peer, Path, Newsgroups
\r
57 my ($Mid, $Timestamp, $Token, $Size, $Peer, $Path, $Newsgroups) = split;
\r
58 # remaining lines contain headers
\r
62 # empty line terminates this article
\r
67 $Headers .= $_."\n" ;
\r
70 # parse timestamp to day (YYYY-MM-DD) and to MySQL timestamp
\r
71 my $Day = time2str("%Y-%m-%d", $Timestamp);
\r
72 my $Date = time2str("%Y-%m-%d %H:%M:%S", $Timestamp);
\r
75 if (!$DBQuery->execute($Day, $Date, $Mid, $Timestamp, $Token, $Size, $Peer, $Path, $Newsgroups, $Headers)) {
\r
76 syslog(LOG_ERR, 'Database error: %s', $DBI::errstr);
\r
80 warn sprintf("-----\nDay: %s\nDate: %s\nMID: %s\nTS: %s\nToken: %s\nSize: %s\nPeer: %s\nPath: %s\nNewsgroups: %s\nHeaders: %s\n",$Day, $Date, $Mid, $Timestamp, $Token, $Size, $Peer, $Path, $Newsgroups, $Headers) if !$Options{'d'};
\r
84 $DBHandle->disconnect;
\r
85 syslog(LOG_NOTICE, "$MySelf closing down.") if !$Options{'q'};
\r