forked from statsd/statsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_statsd.pl
executable file
·75 lines (62 loc) · 1.42 KB
/
check_statsd.pl
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/perl -w
## quick and dirty nagios check for statsd
use strict;
use IO::Socket;
use Getopt::Long;
use constant {
OK => 0,
WARN => 1,
CRIT => 2
};
# config defaults
my $mgmt_addr = '127.0.0.1';
my $mgmt_port = 8126;
# globals
my ($metric, $warn, $crit);
GetOptions(
"host=s" => \$mgmt_addr,
"port=i" => \$mgmt_port,
"metric=s" => \$metric,
"warning=i" => \$warn,
"crit=i" => \$crit,
);
unless ($metric && $warn && $crit) {
print <<USAGE;
usage: check_statsd.pl -m <metric> -w <warn> -c <crit> [-H <host>] [-P <port>]
USAGE
exit CRIT;
}
my $handle = IO::Socket::INET->new(
Proto => 'tcp',
PeerAddr => $mgmt_addr,
PeerPort => $mgmt_port,
) || die "Couldn't open statsd management connection; $mgmt_addr:$mgmt_port";
# make stats request
print $handle "stats\r\n";
my $value;
while(<$handle>) {
last if /END/;
next if !/^$metric:\s+(\d+)/;
$value = $1;
}
my ($msg, $code);
if (defined $value) {
if ($value > $warn) {
$msg = "WARNING: $metric value $value exceeds warning threshold $warn";
$code = WARN;
}
elsif ($value > $crit) {
$msg = "CRITICAL: $metric value $value exceeds critical threshold $crit";
$code = CRIT;
}
else {
$msg = "OK: $metric = $value";
$code = OK;
}
}
else {
$msg = "CRITICAL: metric $metric wasn't found";
$code = CRIT;
}
print $msg;
exit $code;