summaryrefslogtreecommitdiffstats
path: root/stats.pl
blob: 5dd65a91b907039998bc54c424d3839952e719de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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;