chiark / gitweb /
add a trivial README
[rrd-graphs.git] / incoming-connections
1 #!/usr/bin/perl -w
2 #
3 # Command line utility for doing some trivial innd log analysis
4
5 # rrd-graphs/incoming-connections
6 #     - part of rrd-graphs, a tool for online graphs
7 # Copyright 2010, 2012 Ian Jackson
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU Affero General Public License as
11 # published by the Free Software Foundation, either version 3 of the
12 # License, or (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU Affero General Public License for more details.
18 #
19 # You should have received a copy of the GNU Affero General Public License
20 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
22
23 use strict qw(refs vars);
24 use Parse::Syslog;
25 use POSIX;
26
27 @ARGV==1 or die;
28
29 our ($site) = @ARGV;
30
31 our @col2num;
32 our %num2col;
33
34 sub report ($$$) {
35     my ($ts,$col,$how) = @_;
36     my $o= '';
37     my $n= 0;
38     foreach (my $i=0; $i<@col2num; $i++) {
39         if (defined $col2num[$i]) {
40             $n++;
41             $o .= $col==$i ? " $how" : " |";
42         } else {
43             $o .= '  ';
44         }
45     }
46     printf "%s %3d %s\n", $ts, $n, $o or die $!;
47 }
48
49 sub event ($$$;$);
50 sub event ($$$;$) {
51     my ($ts, $num, $how, $openclose) = @_;
52     my $col= $num2col{$num};
53     if (!defined $col) {
54         if ($openclose>0) {
55             for ($col=0; defined $col2num[$col]; $col++) { }
56             $num2col{$num}= $col;
57             $col2num[$col]= $num;
58             report($ts, $col, $how);
59         } else {
60             event($ts, $num, '?', +1);
61             $col= $num2col{$num};
62             report($ts, $col, $how);
63         }
64     } else {
65         report($ts, $col, $how);
66     }
67     if ($openclose < 0) {
68         delete $num2col{$num};
69         $col2num[$col]= undef;
70     }
71 }
72
73 my $in= new IO::File '<& STDIN';
74 my $slp= Parse::Syslog->new($in);
75 while (my $sl= $slp->next) {
76     next unless $sl->{program} eq 'innd';
77     $_= $sl->{text};
78     s/^([^ :]+)// or next;
79 #print STDERR ">$1<\n";
80     next unless $1 eq $site;
81
82     my $timestamp= strftime "%Y-%m-%d %H:%M:%S", localtime $sl->{timestamp};
83
84 #print STDERR "eventing\n";
85     if (m/^\:(\d+) inactive\b/) {
86         event($timestamp, $1, ':',  0);
87     } elsif (m/^\:(\d+) checkpoint\b/) {
88         event($timestamp, $1, 'I',  0);
89     } elsif (m/^\:(\d+) NCmode\b/) {
90         event($timestamp, $1, '#',  0);
91     } elsif (m/^\:(\d+) closed\b/) {
92         event($timestamp, $1, '^', -1);
93     } elsif (m/^ connected (\d+)\b/) {
94         event($timestamp, $1, 'v', +1);
95     }
96 #print STDERR "evented\n";
97 }