X-Git-Url: https://git.sesse.net/?p=remoteglot;a=blobdiff_plain;f=varnishcount.pl;h=7411b054c0a1d010b790794fb1a569f8fc8d19b1;hp=82356d68176585139faae5fac4709d8c6e92caef;hb=d34e567b41978f046f705dc6cb302547fd766b2a;hpb=d5d7dc50fe8f44711a2e4d52563ff40db029e819 diff --git a/varnishcount.pl b/varnishcount.pl index 82356d6..7411b05 100755 --- a/varnishcount.pl +++ b/varnishcount.pl @@ -1,21 +1,38 @@ #! /usr/bin/perl -use Time::HiRes; +use AnyEvent; +use AnyEvent::Handle; +use EV; use LWP::Simple; require 'config.pm'; use strict; use warnings; no warnings qw(once); -$SIG{ALRM} = sub { output(); }; -Time::HiRes::alarm(1.0, 1.0); - open my $fh, "-|", "varnishncsa -F '%{%s}t %U %q tffb=%{Varnish:time_firstbyte}x' -q 'ReqURL ~ \"^/analysis.pl\"'" or die "varnishncsa: $!"; my %uniques = (); -while (<$fh>) { - chomp; - m#(\d+) /analysis.pl \?ims=\d+&unique=(.*) tffb=(.*)# or next; +my $ev = AnyEvent::Handle->new( + fh => $fh, + on_read => sub { + my ($hdl) = @_; + $hdl->push_read( + line => sub { + my ($hdl, $line, $eof) = @_; + handle_line($line); + } + ); + }, +); +my $ev2 = AnyEvent->timer( + interval => 1.0, + cb => \&output +); +EV::run; + +sub handle_line { + my $line = shift; + $line =~ m#(\d+) /analysis.pl \?ims=\d+&unique=(.*) tffb=(.*)# or return; $uniques{$2} = { last_seen => $1 + $3, grace => undef,