| 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 |
| 15 | BEGIN {\r |
| 16 | our $VERSION = "0.01";\r |
| 17 | use File::Basename;\r |
| 18 | push(@INC, dirname($0));\r |
| 19 | }\r |
| 20 | use strict;\r |
| 21 | \r |
| 22 | use NewsStats;\r |
| 23 | \r |
| 24 | use Sys::Syslog qw(:standard :macros);\r |
| 25 | \r |
| 26 | use Date::Format;\r |
| 27 | use DBI;\r |
| 28 | \r |
| 29 | ################################# Main program #################################\r |
| 30 | \r |
| 31 | ### read commandline options\r |
| 32 | my %Options = &ReadOptions('qd');\r |
| 33 | \r |
| 34 | ### read configuration\r |
| 35 | my %Conf = %{ReadConfig('newsstats.conf')};\r |
| 36 | \r |
| 37 | ### init syslog\r |
| 38 | openlog($MySelf, 'nofatal,pid', LOG_NEWS);\r |
| 39 | syslog(LOG_NOTICE, "$MyVersion starting up.") if !$Options{'q'};\r |
| 40 | \r |
| 41 | ### init database\r |
| 42 | my $DBHandle = InitDB(\%Conf,0);\r |
| 43 | if (!$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 |
| 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'}));\r |
| 48 | \r |
| 49 | ### main loop\r |
| 50 | while (<>) {\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 |
| 85 | syslog(LOG_NOTICE, "$MySelf closing down.") if !$Options{'q'};\r |
| 86 | closelog();\r |
| 87 | \r |