NewsStats.pm: Fix $VERSION and $PackageVersion.
[usenet/newsstats.git] / feedlog.pl
CommitLineData
2832c235
TH
1#! /usr/bin/perl -W
2#
3# feedlog.pl
4#
5# This script will log headers and other data to a database
6# for further analysis by parsing a feed from INN.
7#
8# It is part of the NewsStats package.
9#
10# Copyright (c) 2010 Thomas Hochstein <thh@inter.net>
11#
12# It can be redistributed and/or modified under the same terms under
13# which Perl itself is published.
14
15BEGIN {
16 our $VERSION = "0.01";
17 use File::Basename;
18 push(@INC, dirname($0));
19}
20use strict;
21
22use NewsStats;
23
24use Sys::Syslog qw(:standard :macros);
25
26use Date::Format;
27use DBI;
28
29################################# Main program #################################
30
31### read commandline options
32my %Options = &ReadOptions('qd');
33
34### read configuration
35my %Conf = %{ReadConfig('newsstats.conf')};
36
37### init syslog
38openlog($MySelf, 'nofatal,pid', LOG_NEWS);
39syslog(LOG_NOTICE, "$MyVersion starting up.") if !$Options{'q'};
40
41### init database
42my $DBHandle = InitDB(\%Conf,0);
43if (!$DBHandle) {
44 syslog(LOG_CRIT, 'Database connection failed: %s', $DBI::errstr);
45 while (1) {}; # go into endless loop to suppress further errors and respawning
46};
47my $DBQuery = $DBHandle->prepare(sprintf("INSERT INTO %s.%s (day,date,mid,timestamp,token,size,peer,path,newsgroups,headers) VALUES (?,?,?,?,?,?,?,?,?,?)",$Conf{'DBDatabase'},$Conf{'DBTableRaw'}));
48
49### main loop
50while (<>) {
51 chomp;
52 # catch empty lines trailing or leading
53 if ($_ eq '') {
54 next;
55 }
56 # first line contains: mid, timestamp, token, size, peer, Path, Newsgroups
57 my ($Mid, $Timestamp, $Token, $Size, $Peer, $Path, $Newsgroups) = split;
58 # remaining lines contain headers
59 my $Headers = "";
60 while (<>) {
61 chomp;
62 # empty line terminates this article
63 if ($_ eq '') {
64 last;
65 }
66 # collect headers
67 $Headers .= $_."\n" ;
68 }
69
70 # parse timestamp to day (YYYY-MM-DD) and to MySQL timestamp
71 my $Day = time2str("%Y-%m-%d", $Timestamp);
72 my $Date = time2str("%Y-%m-%d %H:%M:%S", $Timestamp);
73
74 # write to database
75 if (!$DBQuery->execute($Day, $Date, $Mid, $Timestamp, $Token, $Size, $Peer, $Path, $Newsgroups, $Headers)) {
76 syslog(LOG_ERR, 'Database error: %s', $DBI::errstr);
77 };
78 $DBQuery->finish;
79
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'};
81}
82
83### close handles
84$DBHandle->disconnect;
85syslog(LOG_NOTICE, "$MySelf closing down.") if !$Options{'q'};
86closelog();
87
88__END__
89
90################################ Documentation #################################
91
92=head1 NAME
93
94feedlog - log data from an INN feed to a database
95
96=head1 SYNOPSIS
97
98B<feedlog> [B<-Vhdq>]
99
100=head1 REQUIREMENTS
101
102See doc/README: Perl 5.8.x itself and the following modules from CPAN:
103
104=over 2
105
106=item -
107
108Config::Auto
109
110=item -
111
112Date::Format
113
114=item -
115
116DBI
117
118=back
119
120=head1 DESCRIPTION
121
122This script will log overview data and complete headers to a database
123table for further examination by parsing a feed from INN. It will
124parse that information and write it to a mysql database table in real
125time.
126
127All reporting is done to I<syslog> via I<news> facility. If B<feedlog>
128fails to initiate a database connection at startup, it will log to
129I<syslog> with I<CRIT> priority and go in an endless loop, as
130terminating would only result in a rapid respawn.
131
132=head2 Configuration
133
134F<feedlog.pl> will read its configuration from F<newsstats.conf> which
135should be present in the same directory via Config::Auto.
136
137See doc/INSTALL for an overview of possible configuration options.
138
139=head1 OPTIONS
140
141=over 3
142
143=item B<-V> (version)
144
145Print out version and copyright information on B<yapfaq> and exit.
146
147=item B<-h> (help)
148
149Print this man page and exit.
150
151=item B<-d> (debug)
152
153Output debugging information to STDERR while parsing STDIN. You'll
154find that information most probably in your B<INN> F<errlog> file.
155
156=item B<-q> (quiet)
157
158Suppress logging to syslog.
159
160=back
161
162=head1 INSTALLATION
163
164See doc/INSTALL.
165
166=head1 EXAMPLES
167
168Set up a feed like that in your B<INN> F<newsfeeds> file:
169
170 ## gather statistics for NewsStats
171 newsstats!
172 :!*,de.*
173 :Tc,WmtfbsPNH,Ac:/path/to/feedlog.pl
174
175See doc/INSTALL for further information.
176
177=head1 FILES
178
179=over 4
180
181=item F<feedlog.pl>
182
183The script itself.
184
185=item F<NewsStats.pm>
186
187Library functions for the NewsStats package.
188
189=item F<newsstats.conf>
190
191Runtime configuration file for B<yapfaq>.
192
193=back
194
195=head1 BUGS
196
197Please report any bugs or feature requests to the author or use the
198bug tracker at L<http://bugs.th-h.de/>!
199
200=head1 SEE ALSO
201
202=over 2
203
204=item -
205
206doc/README
207
208=item -
209
210doc/INSTALL
211
212=back
213
214This script is part of the B<NewsStats> package.
215
216=head1 AUTHOR
217
218Thomas Hochstein <thh@inter.net>
219
220=head1 COPYRIGHT AND LICENSE
221
222Copyright (c) 2010 Thomas Hochstein <thh@inter.net>
223
224This program is free software; you may redistribute it and/or modify it
225under the same terms as Perl itself.
226
227=cut
This page took 0.02004 seconds and 4 git commands to generate.