forked from statsd/statsd
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request statsd#42 from ickymettle/master
added nagios plugin for checking statsd
- Loading branch information
Showing
1 changed file
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; |