doc
[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       ([\w\-]+)\  #identd!! 
37       ([\w\-]+)\  #authname
38       \[(\d{2})\/(\w{3})\/(\d{4}):(\d{2}):(\d{2}):(\d{2})\ -(\d{4})\]\  #date
39       "GET\ ([\/\w\-\.]+)\ HTTP\/\d\.\d"\  # request string
40       (\d{3})\  #staus resonse
41       (\d+)\  #bytes
42       "([^"]*)"\  #referer
43       "([^"]*)"\  #user agent
44       (\d+)  #seconds connected
45     $/xo or do {
46       die "unparsable line: $_";
47       next;
48     };
49
50     #warn "ok: $_";
51
52     my $hostname = $1;
53     my( $mday, $mon, $year, $hours, $min, $sec, $zone ) =
54       (    $4,   $5,    $6,     $7,   $8,   $9,   $10 );
55
56     my $mountpoint = $11;
57     my $status = $12;
58     my $bytes = $13;
59     my $referer = $14;
60     my $useragent = $15;
61     my $seconds = $16;
62
63     $SIG{HUP} = sub { $eof=1 };
64     print join("\t",
65       tell FILE,
66       hostname   => $hostname,
67       start      => timelocal($sec, $min, $hours, $mday, $mon{lc($mon)}, $year),
68       mountpoint => $mountpoint,
69       bytes      => $bytes,
70       useragent  => $useragent,
71       seconds    => $seconds,
72     ), "\n";
73     $SIG{'HUP'} = sub { print "EOF\n"; exit; };
74     if ( $eof ) {
75       print "EOF\n";
76       exit;
77     }
78
79   }
80
81   sleep 1;
82   seek(FILE,0,1);
83 }
84