5 # This script will log headers and other data to a database
6 # for further analysis by parsing a feed from INN.
8 # It is part of the NewsStats package.
10 # Copyright (c) 2010-2012 Thomas Hochstein <thh@inter.net>
12 # It can be redistributed and/or modified under the same terms under
13 # which Perl itself is published.
16 our $VERSION = "0.01";
18 push(@INC, dirname($0));
24 use Sys::Syslog qw(:standard :macros);
28 use Getopt::Long qw(GetOptions);
29 Getopt::Long::config ('bundling');
31 ################################# Main program #################################
33 ### read commandline options
34 my ($OptDebug,$OptQuiet);
35 GetOptions ('d|debug!' => \$OptDebug,
36 'q|test!' => \$OptQuiet,
37 'h|help' => \&ShowPOD,
38 'V|version' => \&ShowVersion) or exit 1;
40 ### read configuration
41 my %Conf = %{ReadConfig($HomePath.'/newsstats.conf')};
44 openlog($0, 'nofatal,pid', LOG_NEWS);
45 syslog(LOG_NOTICE, "$MyVersion starting up.") if !$OptQuiet;
48 my $DBHandle = InitDB(\%Conf,0);
50 syslog(LOG_CRIT, 'Database connection failed: %s', $DBI::errstr);
51 while (1) {}; # go into endless loop to suppress further errors and respawning
53 my $DBQuery = $DBHandle->prepare(sprintf("INSERT INTO %s.%s (day,date,mid,
54 timestamp,token,size,peer,path,
56 VALUES (?,?,?,?,?,?,?,?,?,?)",
57 $Conf{'DBDatabase'},$Conf{'DBTableRaw'}));
62 # catch empty lines trailing or leading
66 # first line contains: mid, timestamp, token, size, peer, Path, Newsgroups
67 my ($Mid, $Timestamp, $Token, $Size, $Peer, $Path, $Newsgroups) = split;
68 # remaining lines contain headers
72 # empty line terminates this article
80 # parse timestamp to day (YYYY-MM-DD) and to MySQL timestamp
81 my $Day = time2str("%Y-%m-%d", $Timestamp);
82 my $Date = time2str("%Y-%m-%d %H:%M:%S", $Timestamp);
85 if (!$DBQuery->execute($Day, $Date, $Mid, $Timestamp, $Token, $Size, $Peer,
86 $Path, $Newsgroups, $Headers)) {
87 syslog(LOG_ERR, 'Database error: %s', $DBI::errstr);
91 warn sprintf("-----\nDay: %s\nDate: %s\nMID: %s\nTS: %s\nToken: %s\n".
92 "Size: %s\nPeer: %s\nPath: %s\nNewsgroups: %s\nHeaders: %s\n",
93 $Day, $Date, $Mid, $Timestamp, $Token, $Size, $Peer, $Path,
94 $Newsgroups, $Headers) if $OptDebug;
98 $DBHandle->disconnect;
99 syslog(LOG_NOTICE, "$0 closing down.") if !$OptQuiet;
104 ################################ Documentation #################################
108 feedlog - log data from an INN feed to a database
112 B<feedlog> [B<-Vhdq>]
120 This script will log overview data and complete headers to a database
121 table for further examination by parsing a feed from INN. It will
122 parse that information and write it to a mysql database table in real
125 All reporting is done to I<syslog> via I<news> facility. If B<feedlog>
126 fails to initiate a database connection at startup, it will log to
127 I<syslog> with I<CRIT> priority and go in an endless loop, as
128 terminating would only result in a rapid respawn.
132 B<feedlog> will read its configuration from F<newsstats.conf> which
133 should be present in the same directory via Config::Auto.
135 See L<doc/INSTALL> for an overview of possible configuration options.
141 =item B<-V>, B<--version>
143 Print out version and copyright information and exit.
145 =item B<-h>, B<--help>
147 Print this man page and exit.
149 =item B<-d>, B<--debug>
151 Output debugging information to STDERR while parsing STDIN. You'll
152 find that information most probably in your B<INN> F<errlog> file.
154 =item B<-q>, B<--quiet>
156 Suppress logging to syslog.
166 Set up a feed like that in your B<INN> F<newsfeeds> file:
168 ## gather statistics for NewsStats
171 :Tc,WmtfbsPNH,Ac:/path/to/feedlog.pl
173 See L<doc/INSTALL> for further information.
183 =item F<NewsStats.pm>
185 Library functions for the NewsStats package.
187 =item F<newsstats.conf>
189 Runtime configuration file.
195 Please report any bugs or feature requests to the author or use the
196 bug tracker at L<http://bugs.th-h.de/>!
212 This script is part of the B<NewsStats> package.
216 Thomas Hochstein <thh@inter.net>
218 =head1 COPYRIGHT AND LICENSE
220 Copyright (c) 2010-2012 Thomas Hochstein <thh@inter.net>
222 This program is free software; you may redistribute it and/or modify it
223 under the same terms as Perl itself.