3 # Program for updating an rrd with info from innduct and inn logs
4 # Needs to be run once inside a lock
6 # rrd-graphs/newstailer - part of rrd-graphs, a tool for online graphs
7 # Copyright 2010, 2012 Ian Jackson
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.
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.
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/>.
24 # with-lock-ex -f data/news/lock sh -xc \
25 # "rm data/news/*.rrd; ./newstailer -Odata/news/ -D \
26 # /var/log/news/news.notice.{6,5,4,3,2,1}.gz /var/log/news/news.notice{.0,} ''"
28 use strict qw(refs vars);
43 open DEBUG, ">/dev/null" or die $!;
45 while (@ARGV && $ARGV[0] =~ m/^\-./) {
49 if (s/^-h//) { $dohosts{$_}=1; last; }
50 elsif (s/^-O//) { $outpfx=$_; last; }
51 elsif (s/^-s//) { $sep=$_; last; }
52 elsif (s/^-i([1-9]\d{0,5})$//) { $interval= $1; }
53 elsif (s/^-D/-/) { $debug++; }
54 else { die "bad option $_"; }
59 my ($sysname, $nodename) = POSIX::uname();
60 die unless defined $nodename;
61 $dohosts{$nodename}= 1;
65 my $totail= pop @ARGV;
67 if ($debug) { open DEBUG, ">& STDOUT" or die $!; }
75 DstArguments => "7200:0:U",
77 Archives => [ [ 3600*4, 60 ], # 4hr, 1min resolution
78 [ 3600*25, 180 ], # 25h, 3min resolution
79 [ 86400*8, 600 ], # 8d, 10min resolution
80 [ 86400*7*14, 3600*2 ], # 14wks, 2hr resolution
81 [ 86400*366, 3600*6 ], # 1yr, 6hr resolution
82 [ 86400*366*3, 3600*24 ] ], # 3yr, 1d resolution
85 our @fields_in= qw(seconds accepted refused rejected duplicate
86 accepted_size duplicate_size);
88 Fields => \@fields_in,
92 our @fields_out= qw(missing offered deferred
93 accepted unwanted rejected body_missing);
95 Fields => \@fields_out,
100 our ($time,$host,$peer,$conn,$stats);
102 sub create_rrd ($$) {
103 my ($inout, $path) = @_;
105 my $details= $details{$inout};
107 my @sargs= ($path, '--start','now-1y', '--step',$details->{Step});
109 push @largs, "DS:$_:ABSOLUTE:$details->{DstArguments}"
110 foreach @{ $details{$inout}{Fields} };
111 foreach (@{ $details->{Archives} }) {
112 my ($whole,$reso) = @$_;
113 my $steps= $reso / $details->{Step};
114 my $rows= $whole / $reso;
115 push @largs, "RRA:AVERAGE:$details->{Xff}:$steps:$rows";
117 print DEBUG join(" \\\n ", "creating @sargs", @largs),"\n";
118 RRDs::create(@sargs,@largs);
119 my $err= RRDs::error;
120 die "$err [@sargs @largs]" if defined $err;
123 sub get_rrd_info ($$) {
124 my ($rrdupdate, $path) = @_;
125 my $h= RRDs::info($path);
126 die "$path $! ".(RRDs::error) unless $h;
127 die "$path ?" unless $h->{'last_update'};
128 $rrdupdate->{DoneUpto}= $h->{'last_update'};
131 sub find_or_create_rrd ($) {
134 Path => "${outpfx}${host}${sep}${peer}_${inout}.rrd",
136 if (stat $rrd->{Path}) {
137 get_rrd_info($rrd, $rrd->{Path});
139 $!==&ENOENT or die "$rrd->{Path} $!";
140 create_rrd($inout, $rrd->{Path});
147 our @rrd_blockedupdates;
148 our $rrd_blockedupdate_time;
150 sub update_rrd ($$) {
151 my ($inout,$vals) = @_;
153 my $rrd= $rrds{$host,$peer,$inout};
155 $rrd= $rrds{$host,$peer,$inout}= find_or_create_rrd($inout);
156 $rrd->{Template}= join ':', @{ $details{$inout}{Fields} };
158 return if $time <= $rrd->{DoneUpto};
160 my $blocked= $rrd->{BlockedUpdate};
161 if (defined $blocked) {
162 for (my $ix=0; $ix<@$vals; $ix++) {
163 my $old= $blocked->[$ix];
164 my $new= $vals->[$ix];
165 $blocked->[$ix]= ($old eq 'U' || $new eq 'U') ? 'U' : $old + $new;
169 $rrd->{BlockedUpdate}= $vals;
170 $rrd_blockedupdate_time= $time;
171 push @rrd_blockedupdates, $rrd;
174 sub actually_update_rrds () {
175 return unless defined $rrd_blockedupdate_time;
176 return if $time == $rrd_blockedupdate_time;
178 while (my $rrd= shift @rrd_blockedupdates) {
179 my $vals= $rrd->{BlockedUpdate};
181 delete $rrd->{BlockedUpdate};
183 my @args= ($rrd->{Path}, '--template',$rrd->{Template},
184 join(':',$rrd_blockedupdate_time,@$vals));
185 print DEBUG "update @args\n" if $debug>=2;
187 my $err= RRDs::error;
188 die "$err [@args]" if defined $err;
191 $rrd_blockedupdate_time= undef;
196 sub inbound_connected () {
197 print DEBUG "inbound $time connected $host $peer $conn\n" if $debug>=2;
198 $in_conns{$host,$peer,$conn} = [ (0) x @fields_in ];
200 sub inbound_closed () {
201 print DEBUG "inbound $time closed $host $peer $conn\n" if $debug>=2;
202 delete $in_conns{$host,$peer,$conn};
204 sub inbound_stats () {
206 s/(?<=[a-z]) (?=[a-z])/_/g;
207 my $hpc= $in_conns{$host,$peer,$conn};
209 print DEBUG "inbound $time UNKNOWN $host $peer $conn $stats\n";
210 $in_conns{$host,$peer,$conn}= $hpc= [ (undef) x @fields_in ];
212 print DEBUG "inbound $time stats $host $peer $conn $stats\n"
216 while (s/^([a-z_]+) (\d+)\s//) { $s{$1}= $2; }
218 foreach my $f (@fields_in) {
220 if (!defined $this) {
225 my $last= $hpc->[@v];
227 push @v, defined($last) ? $this - $last : 'U';
229 update_rrd('in',\@v);
232 sub outbound_stats () {
233 print DEBUG "outbound $time stats $host $peer $stats\n" if $debug>=2;
235 s/missing(?=\=\d+ \()/body_missing/;
238 while (s/ ([a-z]\w+)\=(\d+) / /) { $s{$1}= $2; }
240 foreach my $f (@fields_out) {
242 push @v, defined($this) ? $this : 'U';
244 update_rrd('out',\@v);
249 my $parser= new Parse::Syslog $object, repeat=>0, arrayref=>1;
250 my $host_re= '[-.0-9a-z]+';
251 my $conn_re= '[1-9]\d{0,5}';
252 my ($process,$pid,$msg,$cc,$sl);
253 while ($sl= $parser->next) {
254 ($time,$host,$process,$pid,$msg) = @$sl;
255 actually_update_rrds();
256 next unless exists $dohosts{$host};
257 print DEBUG "logfile ",
258 join("|", map { defined($_) ? $_ : "<undef>" } @$sl), "\n"
260 if ($process eq 'innd' && !defined $pid) {
261 if (($peer,$conn) = $msg =~
262 m/^($host_re) connected ($conn_re)(?: streaming allowed)?$/) {
264 } elsif (($peer,$conn,$cc,$stats) = $msg =~
265 m/^($host_re):($conn_re) (closed|checkpoint) (seconds .*)$/) {
267 inbound_closed() if $cc eq 'closed';
269 } elsif ($process eq 'innduct') {
270 if (($peer,$stats) = $msg =~
271 m/^($host_re)\| notice: (?:completed|processed) \S+ (read=.*)$/) {
278 #seconds (\d+) accepted (\d+) refused (\d+) rejected (\d+) duplicate (\d+) accepted size (\d+) duplicate size (\d+)
281 foreach my $staticpath (@ARGV) {
282 if ($staticpath =~ m/\.gz$/) {
283 my $fh= new IO::Handle;
284 open $fh, '-|', 'gunzip', '-c', '--', $staticpath or die $!;
286 !$fh->error or die "$staticpath $!";
287 $!=0;$?=0; close $fh or die "$staticpath $! $?";
289 my $fh= new IO::File $staticpath, '<' or die $!;
291 !$fh->error or die "$staticpath $!";
292 close $fh or die "$staticpath $!";
296 exit 0 if $totail eq '';
298 my $tailer= new File::Tail name=>$totail,
299 interval=>$interval, adjustafter=>2, ignore_nonexistant=>1, tail=>-1