UNPKG

1.53 kBapplication/x-perlView Raw
1#!/usr/bin/perl -w
2
3## quick and dirty nagios check for statsd
4
5use strict;
6use IO::Socket;
7use Getopt::Long;
8
9use constant {
10 OK => 0,
11 WARN => 1,
12 CRIT => 2
13};
14
15# config defaults
16my $mgmt_addr = '127.0.0.1';
17my $mgmt_port = 8126;
18
19# globals
20my ($metric, $warn, $crit);
21
22GetOptions(
23 "host=s" => \$mgmt_addr,
24 "port=i" => \$mgmt_port,
25 "metric=s" => \$metric,
26 "warning=i" => \$warn,
27 "crit=i" => \$crit,
28);
29
30unless ($metric && $warn && $crit) {
31 print <<USAGE;
32usage: check_statsd.pl -m <metric> -w <warn> -c <crit> [-H <host>] [-P <port>]
33
34USAGE
35 exit CRIT;
36}
37
38my $handle = IO::Socket::INET->new(
39 Proto => 'tcp',
40 PeerAddr => $mgmt_addr,
41 PeerPort => $mgmt_port,
42) || die "Couldn't open statsd management connection; $mgmt_addr:$mgmt_port";
43
44# make stats request
45print $handle "stats\r\n";
46
47my $value;
48while(<$handle>) {
49 last if /END/;
50 next if !/^$metric:\s+(\d+)/;
51 $value = $1;
52}
53
54my ($msg, $code);
55if (defined $value) {
56 if ($value > $warn) {
57 $msg = "WARNING: $metric value $value exceeds warning threshold $warn";
58 $code = WARN;
59 }
60 elsif ($value > $crit) {
61 $msg = "CRITICAL: $metric value $value exceeds critical threshold $crit";
62 $code = CRIT;
63 }
64 else {
65 $msg = "OK: $metric = $value";
66 $code = OK;
67 }
68}
69else {
70 $msg = "CRITICAL: metric $metric wasn't found";
71 $code = CRIT;
72}
73
74print $msg;
75exit $code;