Created
March 28, 2012 09:28
-
-
Save tagomoris/2225030 to your computer and use it in GitHub Desktop.
nagios plugin to check growthforecast values
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
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use lib q!/Users/tagomoris/Documents/nagios-scripts/extlib/lib/perl5!; | |
use List::Util qw!!; | |
use Getopt::Long; | |
Getopt::Long::Configure ("no_ignore_case"); | |
use JSON::XS; | |
use Furl; | |
use Try::Tiny; | |
my $url = "http://growthforecast.hostname.local"; | |
my $ua = "Nagios-check-growthforecast/0.01"; | |
my $mode = "gauge"; | |
my $time_scale = "sh"; | |
my $direction = 'u'; # default: upward (greater number is more dangerous.) | |
my $column; # undef: single column only | |
my $item; # '/service/section/name' | |
my $sampling = 3; | |
my $warning; | |
my $critical; | |
my $upward; | |
my $downward; | |
GetOptions( | |
"u|url=s" => \$url, | |
"m|mode=s" => \$mode, | |
"t|timescale=s" => \$time_scale, | |
"C|column=s" => \$column, | |
"i=s" => \$item, | |
"s=i" => \$sampling, | |
"w=i" => \$warning, | |
"c=i" => \$critical, | |
"A=s" => \$ua, | |
"U" => \$upward, | |
"D" => \$downward, | |
); | |
sub usage { | |
print "usage: $0 -i /service/section/name [ -m GMODE ] [ -t T ] [ -U or -D ] [ -C COLUMN_NAME ] [ -s SAMPLES ] -w 80 -c 95\n"; | |
exit 3 | |
} | |
usage() unless defined $item and defined $warning and defined $critical and $item =~ m!/[-._a-zA-Z0-9]+/[-._a-zA-Z0-9]+/[-._a-zA-Z0-9]+$!; | |
if ($upward) { | |
$direction = 'u'; | |
} | |
elsif ($downward) { | |
$direction = 'd'; | |
} | |
my $furl = Furl->new( | |
agent => $ua, | |
timeout => 5, | |
); | |
my $json_uri = $url . '/xport' . $item . '?t=' . $time_scale; | |
my $res = $furl->get($json_uri); | |
unless ($res->is_success) { | |
printf "UNKNOWN failed to get json URI: %s\n", $json_uri; | |
exit 3; | |
} | |
my $data; | |
my $parse_error; | |
try { | |
$data = decode_json($res->content); | |
} catch { | |
# failed to parse content | |
$parse_error = $_; | |
}; | |
if ($parse_error) { | |
printf "UNKNOWN failed to parse JSON response: %s\n", $parse_error; | |
exit 3; | |
} | |
# $data = +{ | |
# "column_names":["node_remain_min"], | |
# "step":600, | |
# "columns":1, | |
# "end_timestamp":1332753600, | |
# "start_timestamp":1332634800, | |
# "rows":[ | |
# [null], | |
# [1315792848604.1], | |
# [1313090081639.67], | |
# ] | |
# }; | |
if (not defined $column and $data->{columns} ne 1) { | |
printf "UNKNOWN column name not specified for graph with %d columns\n", $data->{columns}; | |
exit 3; | |
} | |
my $attribute_index; | |
if (not defined $column) { | |
$attribute_index = 0; | |
} | |
else { | |
for(my $i = 0 ; $i < $data->{columns} ; $i++) { | |
if ($column eq $data->{column_names}->[$i]) { | |
$attribute_index = $i; | |
last; | |
} | |
} | |
if (not defined $attribute_index) { | |
printf "UNKNOWN column name %s not found in response columns: %s\n", $column, join(',', @{$data->{column_names}}); | |
exit 3; | |
} | |
} | |
my @sample_values = (); | |
my $rows = scalar(@{$data->{rows}}); | |
for (my $i = 1 ; $i <= $rows; $i++) { | |
if (not defined ($data->{rows}->[-1 * $i] && $data->{rows}->[-1 * $i]->[$attribute_index])) { | |
next; | |
} | |
push @sample_values, $data->{rows}->[-1 * $i]->[$attribute_index]; | |
last if scalar(@sample_values) == $sampling; | |
} | |
unless (@sample_values) { | |
print "WARNING no one valid values found in response\n"; | |
exit 1; | |
} | |
my $sampled = scalar(@sample_values); | |
my $avg; | |
{ | |
no warnings 'once'; | |
$avg = int( (List::Util::reduce { $a + $b } @sample_values) / $sampled ); | |
} | |
if ($direction eq 'u') { | |
if ($avg > $critical) { | |
printf "CRITICAL sampled average value %d > %d (critical threshold)\n", $avg, $critical; | |
exit 2; | |
} | |
if ($avg > $warning) { | |
printf "WARNING sampled average value %d > %d (warning threshold)\n", $avg, $warning; | |
exit 1; | |
} | |
} | |
elsif ($direction eq 'd') { | |
if ($avg < $critical) { | |
printf "CRITICAL sampled average value %d < %d (critical threshold)\n", $avg, $critical; | |
exit 2; | |
} | |
if ($avg < $warning) { | |
printf "WARNING sampled average value %d < %d (warning threshold)\n", $avg, $warning; | |
exit 1; | |
} | |
} | |
printf "OK sampled value %d\n", $avg; | |
exit 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment