chiark / gitweb /
wip newstailer; need to handle date conversion urgh
[rrd-graphs.git] / newstailer
1 #!/usr/bin/perl -w
2
3 use strict qw(refs vars);
4 use POSIX;
5
6 our $polltime= 60;
7 our $logfile= "/var/log/news.notice";
8
9 our ($loghandle, $logfile_devino);
10
11 sub current_devino () { return join '.', (stat _)[0..1]; }
12
13 our %months;
14 $months{ (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec))[$_] }= $_
15     foreach qw(0..11);
16
17 sub processline_core ($) {
18     local ($_) = @_;
19     my ($mon,$dom,$h,$m,$s) = 
20         s/^ ([A-Z][a-z]{2}) [ ]
21             ([ 013]\d) [ ]
22             ([ 0-2]\d) : (\d\d) : (\d\d) [ ]
23             [-0-9a-z]+ /x;
24     return "date re not matched" unless defined $mon;
25     $mon= $months{$mon};
26     return "unknown month $mon" unless defined $mon;
27
28     my $isdst= 0;
29     $ts= mktime($s,$m,$h, $dom,$current_year, 0,0, $isdst);
30     $last_ts= 
31 }
32
33 sub backlog () {
34     my $fh= new IO::File '<', "$logfile.0";
35     if (!$fh) {
36         die "$logfile.0 $!" unless $!==&ENOENT;
37         return;
38     }
39     while (defined(my $l= <$fh>)) {
40         $l =~ s/\n$// or last;
41         processline($l);
42     }
43     close $fh;
44 }
45     
46 sub logpoll () {
47     my $lbuf= '';
48     my $waited_eol= 0;
49     
50     for (;;) {
51         sleep($polltime);
52
53         if (!$loghandle) {
54             $loghandle= new IO::File '<', $logfile;
55             if (!$loghandle) {
56                 die "$logfile $!" unless $!==&ENOENT;
57                 next;
58             }
59             stat $loghandle or die $!;
60             $logfile_devino= join '.', (stat _)[0..1];
61         }
62
63         for (;;) {
64             if ($lbuf =~ s/\n$//) {
65                 processline($l);
66                 $lbuf= '';
67             }
68             my $xtra= <$loghandle>;
69             if (!defined $xtra) {
70                 die "$logfile $!" if $loghandle->error;
71                 last;
72             }
73             $lbuf .= $xtra;
74         }
75
76         if (stat $logfile) {
77             next if $logfile_devino eq current_devino;
78         } else {
79             die "$logfile $!" unless $!==&ENOENT;
80         }
81
82         # current file is no longer right
83         if (length $lbuf) {
84             # wait for the rest of the line ?
85             next unless $waited_eol++ > 3;
86         }
87         $lbuf= '';
88         close $loghandle;
89         $loghandle= undef;
90     }
91 }
92
93 backlog();
94 logpoll();