b47e3dbc6f5b0986fe84868bf929a2e8bcf8c770
[icelog.git] / iceaccessd
1 #!/usr/bin/perl -w
2 #
3 # Copyright (c) 2002 Ivan Kohler
4 # All rights reserved.
5 # This program is free software; you can redistribute it and/or modify it under
6 # the same terms as Perl itself.
7 #
8 # ivan-icelog@420.am
9
10 use strict;
11 use vars qw($eof);
12 #use Date::Parse;
13 use Time::Local;
14
15 my $c = 0;
16 my %mon =
17   map { ( $_ => $c++ ) } qw( jan feb mar apr may jun jul aug sep oct nov dec );
18
19 $|=1;
20 $eof=0;
21
22 my( $file, $pos ) = @ARGV;
23 open(FILE,"<$file") or die "Can't open $file: $!";
24 seek(FILE,$pos,0) or die "Can't seek: $!";
25
26 $SIG{'HUP'} = sub { print "EOF\n"; exit; };
27 #$SIG{'HUP'} = sub { $eof=time; }; # set an alarm
28
29 while (1) {
30
31   while (<FILE>) {
32     next if /^$/;
33     #rootwood.haze.st - - [24/Dec/2001:15:33:50 -0800] "GET / HTTP/1.0" 200 1388544 "-" "xmms/1.2.5" 89
34     /^
35       ([\w\-\.]+)\ -\ -\  #hostname
36       \[(\d{2})\/(\w{3})\/(\d{4}):(\d{2}):(\d{2}):(\d{2})\ -(\d{4})\]\  #date
37       "GET\ ([\/\w\-\.]+)\ HTTP\/\d\.\d"\  # request string
38       (\d{3})\  #staus resonse
39       (\d+)\  #bytes
40       "([^"]*)"\  #referer
41       "([^"]*)"\  #user agent
42       (\d+)  #seconds connected
43     $/xo or do {
44       die "unparsable line: $_";
45       next;
46     };
47
48     #warn "ok: $_";
49
50     my $hostname = $1;
51     my( $mday, $mon, $year, $hours, $min, $sec, $zone ) =
52       (    $2,   $3,    $4,     $5,   $6,   $7,    $8 );
53
54     my $mountpoint = $9;
55     my $status = $10;
56     my $bytes = $11;
57     my $referer = $12;
58     my $useragent = $13;
59     my $seconds = $14;
60
61     $SIG{HUP} = sub { $eof=1 };
62     print join("\t",
63       tell FILE,
64       hostname   => $hostname,
65       start      => timelocal($sec, $min, $hours, $mday, $mon{lc($mon)}, $year),
66       mountpoint => $mountpoint,
67       bytes      => $bytes,
68       useragent  => $useragent,
69       seconds    => $seconds,
70     ), "\n";
71     $SIG{'HUP'} = sub { print "EOF\n"; exit; };
72     if ( $eof ) {
73       print "EOF\n";
74       exit;
75     }
76
77   }
78
79   sleep 1;
80   seek(FILE,0,1);
81 }
82