| 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-2012 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 | use Getopt::Long qw(GetOptions); |
| 29 | Getopt::Long::config ('bundling'); |
| 30 | |
| 31 | ################################# Main program ################################# |
| 32 | |
| 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; |
| 39 | |
| 40 | ### read configuration |
| 41 | my %Conf = %{ReadConfig($HomePath.'/newsstats.conf')}; |
| 42 | |
| 43 | ### init syslog |
| 44 | openlog($0, 'nofatal,pid', LOG_NEWS); |
| 45 | syslog(LOG_NOTICE, "$MyVersion starting up.") if !$OptQuiet; |
| 46 | |
| 47 | ### init database |
| 48 | my $DBHandle = InitDB(\%Conf,0); |
| 49 | if (!$DBHandle) { |
| 50 | syslog(LOG_CRIT, 'Database connection failed: %s', $DBI::errstr); |
| 51 | while (1) {}; # go into endless loop to suppress further errors and respawning |
| 52 | }; |
| 53 | my $DBQuery = $DBHandle->prepare(sprintf("INSERT INTO %s.%s (day,date,mid, |
| 54 | timestamp,token,size,peer,path, |
| 55 | newsgroups,headers) |
| 56 | VALUES (?,?,?,?,?,?,?,?,?,?)", |
| 57 | $Conf{'DBDatabase'},$Conf{'DBTableRaw'})); |
| 58 | |
| 59 | ### main loop |
| 60 | while (<>) { |
| 61 | chomp; |
| 62 | # catch empty lines trailing or leading |
| 63 | if ($_ eq '') { |
| 64 | next; |
| 65 | } |
| 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 |
| 69 | my $Headers = ""; |
| 70 | while (<>) { |
| 71 | chomp; |
| 72 | # empty line terminates this article |
| 73 | if ($_ eq '') { |
| 74 | last; |
| 75 | } |
| 76 | # collect headers |
| 77 | $Headers .= $_."\n" ; |
| 78 | } |
| 79 | |
| 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); |
| 83 | |
| 84 | # write to database |
| 85 | if (!$DBQuery->execute($Day, $Date, $Mid, $Timestamp, $Token, $Size, $Peer, |
| 86 | $Path, $Newsgroups, $Headers)) { |
| 87 | syslog(LOG_ERR, 'Database error: %s', $DBI::errstr); |
| 88 | }; |
| 89 | $DBQuery->finish; |
| 90 | |
| 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; |
| 95 | } |
| 96 | |
| 97 | ### close handles |
| 98 | $DBHandle->disconnect; |
| 99 | syslog(LOG_NOTICE, "$0 closing down.") if !$OptQuiet; |
| 100 | closelog(); |
| 101 | |
| 102 | __END__ |
| 103 | |
| 104 | ################################ Documentation ################################# |
| 105 | |
| 106 | =head1 NAME |
| 107 | |
| 108 | feedlog - log data from an INN feed to a database |
| 109 | |
| 110 | =head1 SYNOPSIS |
| 111 | |
| 112 | B<feedlog> [B<-Vhdq>] |
| 113 | |
| 114 | =head1 REQUIREMENTS |
| 115 | |
| 116 | See L<doc/README>. |
| 117 | |
| 118 | =head1 DESCRIPTION |
| 119 | |
| 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 |
| 123 | time. |
| 124 | |
| 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. |
| 129 | |
| 130 | =head2 Configuration |
| 131 | |
| 132 | B<feedlog> will read its configuration from F<newsstats.conf> which |
| 133 | should be present in the same directory via Config::Auto. |
| 134 | |
| 135 | See L<doc/INSTALL> for an overview of possible configuration options. |
| 136 | |
| 137 | =head1 OPTIONS |
| 138 | |
| 139 | =over 3 |
| 140 | |
| 141 | =item B<-V>, B<--version> |
| 142 | |
| 143 | Print out version and copyright information and exit. |
| 144 | |
| 145 | =item B<-h>, B<--help> |
| 146 | |
| 147 | Print this man page and exit. |
| 148 | |
| 149 | =item B<-d>, B<--debug> |
| 150 | |
| 151 | Output debugging information to STDERR while parsing STDIN. You'll |
| 152 | find that information most probably in your B<INN> F<errlog> file. |
| 153 | |
| 154 | =item B<-q>, B<--quiet> |
| 155 | |
| 156 | Suppress logging to syslog. |
| 157 | |
| 158 | =back |
| 159 | |
| 160 | =head1 INSTALLATION |
| 161 | |
| 162 | See L<doc/INSTALL.> |
| 163 | |
| 164 | =head1 EXAMPLES |
| 165 | |
| 166 | Set up a feed like that in your B<INN> F<newsfeeds> file: |
| 167 | |
| 168 | ## gather statistics for NewsStats |
| 169 | newsstats! |
| 170 | :!*,de.* |
| 171 | :Tc,WmtfbsPNH,Ac:/path/to/feedlog.pl |
| 172 | |
| 173 | See L<doc/INSTALL> for further information. |
| 174 | |
| 175 | =head1 FILES |
| 176 | |
| 177 | =over 4 |
| 178 | |
| 179 | =item F<feedlog.pl> |
| 180 | |
| 181 | The script itself. |
| 182 | |
| 183 | =item F<NewsStats.pm> |
| 184 | |
| 185 | Library functions for the NewsStats package. |
| 186 | |
| 187 | =item F<newsstats.conf> |
| 188 | |
| 189 | Runtime configuration file. |
| 190 | |
| 191 | =back |
| 192 | |
| 193 | =head1 BUGS |
| 194 | |
| 195 | Please report any bugs or feature requests to the author or use the |
| 196 | bug tracker at L<http://bugs.th-h.de/>! |
| 197 | |
| 198 | =head1 SEE ALSO |
| 199 | |
| 200 | =over 2 |
| 201 | |
| 202 | =item - |
| 203 | |
| 204 | L<doc/README> |
| 205 | |
| 206 | =item - |
| 207 | |
| 208 | L<doc/INSTALL> |
| 209 | |
| 210 | =back |
| 211 | |
| 212 | This script is part of the B<NewsStats> package. |
| 213 | |
| 214 | =head1 AUTHOR |
| 215 | |
| 216 | Thomas Hochstein <thh@inter.net> |
| 217 | |
| 218 | =head1 COPYRIGHT AND LICENSE |
| 219 | |
| 220 | Copyright (c) 2010-2012 Thomas Hochstein <thh@inter.net> |
| 221 | |
| 222 | This program is free software; you may redistribute it and/or modify it |
| 223 | under the same terms as Perl itself. |
| 224 | |
| 225 | =cut |