]> git.sesse.net Git - remoteglot/blobdiff - varnishcount.pl
Make serve-analysis.js capable of handling e.g. /analysis2.pl.
[remoteglot] / varnishcount.pl
index 82356d68176585139faae5fac4709d8c6e92caef..35e1402d24085444c753b477d3e0d1240eadb525 100755 (executable)
@@ -1,21 +1,41 @@
 #! /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);
+my $url = $ARGV[0] // "/analysis.pl";  # Technically an URL regex, not an URL.
+my $port = $ARGV[1] // 5000;
 
-open my $fh, "-|", "varnishncsa -F '%{%s}t %U %q tffb=%{Varnish:time_firstbyte}x' -q 'ReqURL ~ \"^/analysis.pl\"'"
+open my $fh, "-|", "varnishncsa -F '%{%s}t %U %q tffb=%{Varnish:time_firstbyte}x' -q 'ReqURL ~ \"^$url\"'"
        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+) $url \?ims=\d+&unique=(.*) tffb=(.*)# or return;
        $uniques{$2} = {
                last_seen => $1 + $3,
                grace => undef,
@@ -57,5 +77,5 @@ sub output {
 
        my $num_viewers = scalar keys %uniques; 
        printf "%d entries in hash, mtime=$mtime\n", scalar keys %uniques;
-       LWP::Simple::get('http://127.0.0.1:5000/override-num-viewers?num=' . $num_viewers);     
+       LWP::Simple::get('http://127.0.0.1:' . $port . '/override-num-viewers?num=' . $num_viewers);    
 }