summaryrefslogtreecommitdiffstats
path: root/stats.pl
blob: 19a1b5f0255524a3445c269829d97fe95a0675b7 (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
#!/usr/bin/perl -w

use strict;
use AnyEvent;
use AnyEvent::Socket;
use AnyEvent::Handle;
use Storable;
use File::HomeDir;

# timeout in seconds
my $timeout = 10;

# socket
my $socket = File::HomeDir->my_home . '/var/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 $data;
    my $h; $h = new AnyEvent::Handle
        fh => $fh,
        on_eof => sub {
            # read stats data
            my $stats = Storable::thaw($data);

            print "OK | ham=$stats->{ham};;;0 spam=$stats->{spam};;;0 block=$stats->{block};;;0\n";
            $cv->send;
        },
        on_read => sub {
            $h->push_read(line => sub {
                my ($h, $line) = @_;
                $data .= "$line\n";
            });
        };
};

# general timeout
my $t; $t = AnyEvent->timer(
    after => $timeout,
    cb => sub {
        my_die("Timeout after $timeout seconds.");
        undef $t;
    });

$cv->recv;