From b6660f01127b75ea1acf61a192931165318f4fb1 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Wed, 14 May 2014 02:35:45 +0200 Subject: Initial commit --- stats.pl | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100755 stats.pl (limited to 'stats.pl') diff --git a/stats.pl b/stats.pl new file mode 100755 index 0000000..5dd65a9 --- /dev/null +++ b/stats.pl @@ -0,0 +1,55 @@ +#!/usr/bin/perl -w + +use strict; +use AnyEvent; +use AnyEvent::Socket; +use AnyEvent::Handle; +use Storable; +use Data::Dumper; + +# timeout in seconds +my $timeout = 10; + +# socket +my $socket = '/tmp/stats.sock'; + +my $cv = AnyEvent->condvar; + +sub my_die { + print "UNKNOWN "; + print @_; + print "\n"; + + exit 3; +} + +tcp_connect "unix/", $socket, sub { + my ($fh) = @_ or my_die("Unable to connect to socket: $!");; + my $h; $h = new AnyEvent::Handle + fh => $fh, + on_eof => sub { + $h->destroy; + my_die('Connection closed.'); + }, + on_read => sub { + $h->push_read(line => sub { + my ($h, $line) = @_; + + # read stats data + my $stats = Storable::thaw($line); + + print "OK | ham=$stats->{ham};;;0 spam=$stats->{spam};;;0 block=$stats->{block};;;0\n"; + $cv->send; + }); + }; +}; + +# general timeout +my $t; $t = AnyEvent->timer( + after => $timeout, + cb => sub { + my_die("Timeout after $timeout seconds."); + undef $t; + }); + +$cv->recv; -- cgit v1.2.3-1-g7c22