summaryrefslogtreecommitdiffstats
path: root/stats.pl
diff options
context:
space:
mode:
Diffstat (limited to 'stats.pl')
-rwxr-xr-xstats.pl55
1 files changed, 55 insertions, 0 deletions
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;