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