Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 76 lines (62 sloc) 1.451 kB
9cd5f93 nagios check for statsd
Marcus Barczak authored
1 #!/usr/bin/perl -w
2
3 ## quick and dirty nagios check for statsd
4
5 use strict;
6 use IO::Socket;
7 use Getopt::Long;
8
9 use constant {
10 OK => 0,
11 WARN => 1,
12 CRIT => 2
13 };
14
15 # config defaults
16 my $mgmt_addr = '127.0.0.1';
17 my $mgmt_port = 8126;
18
19 # globals
20 my ($metric, $warn, $crit);
21
22 GetOptions(
23 "host=s" => \$mgmt_addr,
24 "port=i" => \$mgmt_port,
25 "metric=s" => \$metric,
26 "warning=i" => \$warn,
27 "crit=i" => \$crit,
28 );
29
30 unless ($metric && $warn && $crit) {
31 print <<USAGE;
32 usage: check_statsd.pl -m <metric> -w <warn> -c <crit> [-H <host>] [-P <port>]
33
34 USAGE
35 exit CRIT;
36 }
37
38 my $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
45 print $handle "stats\r\n";
46
47 my $value;
48 while(<$handle>) {
49 last if /END/;
50 next if !/^$metric:\s+(\d+)/;
51 $value = $1;
52 }
53
54 my ($msg, $code);
55 if (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 }
69 else {
70 $msg = "CRITICAL: metric $metric wasn't found";
71 $code = CRIT;
72 }
73
74 print $msg;
75 exit $code;
Something went wrong with that request. Please try again.