Commit | Line | Data |
---|---|---|
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 | ||
15 | BEGIN { | |
16 | our $VERSION = "0.01"; | |
17 | use File::Basename; | |
18 | push(@INC, dirname($0)); | |
19 | } | |
20 | use strict; | |
21 | ||
22 | use NewsStats; | |
23 | ||
24 | use Sys::Syslog qw(:standard :macros); | |
25 | ||
26 | use Date::Format; | |
27 | use DBI; | |
28 | ||
29 | ################################# Main program ################################# | |
30 | ||
31 | ### read commandline options | |
32 | my %Options = &ReadOptions('qd'); | |
33 | ||
34 | ### read configuration | |
35 | my %Conf = %{ReadConfig('newsstats.conf')}; | |
36 | ||
37 | ### init syslog | |
38 | openlog($MySelf, 'nofatal,pid', LOG_NEWS); | |
39 | syslog(LOG_NOTICE, "$MyVersion starting up.") if !$Options{'q'}; | |
40 | ||
41 | ### init database | |
42 | my $DBHandle = InitDB(\%Conf,0); | |
43 | if (!$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 | }; | |
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'})); | |
48 | ||
49 | ### main loop | |
50 | while (<>) { | |
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; | |
85 | syslog(LOG_NOTICE, "$MySelf closing down.") if !$Options{'q'}; | |
86 | closelog(); | |
87 | ||
88 | __END__ | |
89 | ||
90 | ################################ Documentation ################################# | |
91 | ||
92 | =head1 NAME | |
93 | ||
94 | feedlog - log data from an INN feed to a database | |
95 | ||
96 | =head1 SYNOPSIS | |
97 | ||
98 | B<feedlog> [B<-Vhdq>] | |
99 | ||
100 | =head1 REQUIREMENTS | |
101 | ||
102 | See doc/README: Perl 5.8.x itself and the following modules from CPAN: | |
103 | ||
104 | =over 2 | |
105 | ||
106 | =item - | |
107 | ||
108 | Config::Auto | |
109 | ||
110 | =item - | |
111 | ||
112 | Date::Format | |
113 | ||
114 | =item - | |
115 | ||
116 | DBI | |
117 | ||
118 | =back | |
119 | ||
120 | =head1 DESCRIPTION | |
121 | ||
122 | This script will log overview data and complete headers to a database | |
123 | table for further examination by parsing a feed from INN. It will | |
124 | parse that information and write it to a mysql database table in real | |
125 | time. | |
126 | ||
127 | All reporting is done to I<syslog> via I<news> facility. If B<feedlog> | |
128 | fails to initiate a database connection at startup, it will log to | |
129 | I<syslog> with I<CRIT> priority and go in an endless loop, as | |
130 | terminating would only result in a rapid respawn. | |
131 | ||
132 | =head2 Configuration | |
133 | ||
134 | F<feedlog.pl> will read its configuration from F<newsstats.conf> which | |
135 | should be present in the same directory via Config::Auto. | |
136 | ||
137 | See doc/INSTALL for an overview of possible configuration options. | |
138 | ||
139 | =head1 OPTIONS | |
140 | ||
141 | =over 3 | |
142 | ||
143 | =item B<-V> (version) | |
144 | ||
145 | Print out version and copyright information on B<yapfaq> and exit. | |
146 | ||
147 | =item B<-h> (help) | |
148 | ||
149 | Print this man page and exit. | |
150 | ||
151 | =item B<-d> (debug) | |
152 | ||
153 | Output debugging information to STDERR while parsing STDIN. You'll | |
154 | find that information most probably in your B<INN> F<errlog> file. | |
155 | ||
156 | =item B<-q> (quiet) | |
157 | ||
158 | Suppress logging to syslog. | |
159 | ||
160 | =back | |
161 | ||
162 | =head1 INSTALLATION | |
163 | ||
164 | See doc/INSTALL. | |
165 | ||
166 | =head1 EXAMPLES | |
167 | ||
168 | Set 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 | ||
175 | See doc/INSTALL for further information. | |
176 | ||
177 | =head1 FILES | |
178 | ||
179 | =over 4 | |
180 | ||
181 | =item F<feedlog.pl> | |
182 | ||
183 | The script itself. | |
184 | ||
185 | =item F<NewsStats.pm> | |
186 | ||
187 | Library functions for the NewsStats package. | |
188 | ||
189 | =item F<newsstats.conf> | |
190 | ||
191 | Runtime configuration file for B<yapfaq>. | |
192 | ||
193 | =back | |
194 | ||
195 | =head1 BUGS | |
196 | ||
197 | Please report any bugs or feature requests to the author or use the | |
198 | bug tracker at L<http://bugs.th-h.de/>! | |
199 | ||
200 | =head1 SEE ALSO | |
201 | ||
202 | =over 2 | |
203 | ||
204 | =item - | |
205 | ||
206 | doc/README | |
207 | ||
208 | =item - | |
209 | ||
210 | doc/INSTALL | |
211 | ||
212 | =back | |
213 | ||
214 | This script is part of the B<NewsStats> package. | |
215 | ||
216 | =head1 AUTHOR | |
217 | ||
218 | Thomas Hochstein <thh@inter.net> | |
219 | ||
220 | =head1 COPYRIGHT AND LICENSE | |
221 | ||
222 | Copyright (c) 2010 Thomas Hochstein <thh@inter.net> | |
223 | ||
224 | This program is free software; you may redistribute it and/or modify it | |
225 | under the same terms as Perl itself. | |
226 | ||
227 | =cut |