diff --git a/check_purearray.pl b/check_purearray.pl index b134d24..1c09282 100755 --- a/check_purearray.pl +++ b/check_purearray.pl @@ -1,67 +1,98 @@ #!/usr/bin/perl +my $Version='0.2'; use Data::Dumper; use REST::Client; use JSON; use Net::SSL; use strict; +use Getopt::Long; ### Config my $cookie_file = "/tmp/cookies.txt"; -# pureadmin create --api-token -my %api_tokens = ( - 'my-pure-array1.company.com' => 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX', - 'my-pure-array2.company.com' => 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX', -); - my $max_system_percent = 10; my $array_warn_percent = 85; my $array_crit_percent = 90; -my $vol_warn_percent = 85; -my $vol_crit_percent = 90; our %ENV; $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0; ### Nagios exit codes -my $OKAY = 0; -my $WARNING = 1; -my $CRITICAL = 2; -my $UNKNOWN = 3; +my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4); ### Parse command line +my $help = undef; +my $host = undef; +my $token = undef; +my $checktype = "space"; +my @valid_types = ("space", "performance"); +my $vol_warn_percent = undef; +my $vol_crit_percent = undef; +my $perf = undef; +my $debug = undef; -my $debug = 0; -my $host; +my @critical; +my @warning; +my @info; +my @perfdata; +my $perfoutput = ""; -for my $arg (@ARGV) { - if ( $arg =~ /--debug/i ) { - $debug++; - } else { - $host = $arg; - } +sub print_usage { + print "Usage: $0 -H -T -w -c [-f] [-d]\n"; } -### Bootstrap - -unless ( $host ) { - print "No hostname given to check\n"; - exit $UNKNOWN +sub help { + print "\nA full REST API is available for automation and monitoring Pure Storage arrays, version $Version\n"; + print "Artistic License 2.0\n\n"; + print_usage(); + print < \$debug, 'debug' => \$debug, + 'h' => \$help, 'help' => \$help, + 'H:s' => \$host, 'hostname:s' => \$host, + 'T:s' => \$token, 'token:s' => \$token, + 'w:s' => \$vol_warn_percent, 'warning:s' => \$vol_warn_percent, + 'c:s' => \$vol_crit_percent, 'critical:s' => \$vol_crit_percent, + 'f' => \$perf, 'perfdata' => \$perf + ); + if (defined ($help) ) { help(); exit $ERRORS{"UNKNOWN"}}; + + if ( ! defined($host) ) # check host and filter + { print_usage(); exit $ERRORS{"UNKNOWN"}} + + if ( ! defined($token) ) # check API token + { print_usage(); exit $ERRORS{"UNKNOWN"}} + + if (!defined($vol_warn_percent) || !defined($vol_crit_percent)) + { print "put warning and critical info!\n"; print_usage(); exit $ERRORS{"UNKNOWN"}} + + if ($vol_warn_percent > $vol_crit_percent) + { print "warning <= critical ! \n";print_usage(); exit $ERRORS{"UNKNOWN"}} } -my @critical; -my @warning; -my @info; +### Bootstrap + +check_options(); ### Start RESTing @@ -90,7 +121,7 @@ unless ( $api_version ) { print "API version 1.3 or 1.4 is not supported by host: $host\n"; - exit $UNKNOWN + exit $ERRORS{"UNKNOWN"} } ### Set the Session Cookie @@ -101,10 +132,10 @@ my $array_info = &api_get("/api/$api_version/array?space=true"); -for my $param (qw/system capacity total/) { +for my $param (qw/system capacity total data_reduction total_reduction/) { next if defined $array_info->{$param}; print "Array data lacks parameter: $param"; - exit $UNKNOWN + exit $ERRORS{"UNKNOWN"} } if ( (100 * $array_info->{system} / $array_info->{capacity}) >= $max_system_percent ) { @@ -114,7 +145,10 @@ } my $array_percent_used = sprintf('%0.2f', (100 * $array_info->{total} / $array_info->{capacity})); -my $message = "Array @ $array_percent_used\%"; +my $array_data_reduction = sprintf('%0.2f', $array_info->{data_reduction}); +my $array_total_reduction = sprintf('%0.2f', $array_info->{total_reduction}); +my $message = "Array @ $array_percent_used\% (Data reduction is $array_data_reduction, Total reduction is $array_total_reduction)"; +my $perfmessage = 'Array=' . $array_info->{total} . 'B;' . $array_info->{capacity}*$array_warn_percent/100 . ';' . $array_info->{capacity}*$array_crit_percent/100 . ';0;' . $array_info->{capacity} . ' Array_total_reduction=' . $array_total_reduction . ';;;0; Array_data_reduction=' . $array_data_reduction . ';;;0;' if ($perf); if ( $array_percent_used > $array_crit_percent ) { push @critical, $message; @@ -123,6 +157,7 @@ } else { push @info, $message; } +push @perfdata, $perfmessage if ($perf); ### Check the volumes @@ -132,14 +167,14 @@ for my $param (qw/total size name/) { next if defined $vol->{$param}; print "Volume data lacks parameter: $param"; - exit $UNKNOWN + exit $ERRORS{"UNKNOWN"} } } for my $vol ( sort { ($b->{total}/$b->{size}) <=> ($a->{total}/$a->{size}) } @$vol_info) { - my $vol_percent_used = sprintf('%0.2f', (100 * $vol->{total} / $vol->{size})); my $message = "$vol->{name} $vol_percent_used\%"; + my $perfmessage = $vol->{name} . '=' . $vol->{total} . 'B;' . $vol->{size}*$array_warn_percent/100 . ';' . $vol->{size}*$array_crit_percent/100 . ';0;' . $vol->{size} if ($perf); if ( $vol_percent_used > $vol_crit_percent ) { push @critical, $message; @@ -148,6 +183,7 @@ } else { push @info, $message; } + push @perfdata, $perfmessage if ($perf); } # Kill the session @@ -155,15 +191,17 @@ $ret = $client->DELETE("/api/$api_version/auth/session"); unlink($cookie_file); +$perfoutput = '|' . join(' ', @perfdata) if ($perf); + if ( scalar(@critical) > 0 ) { - print join(' ', map { '[ '.$_.' ]' } (@critical,@warning)); - exit $CRITICAL; + print 'CRITICAL - API ' . $api_version . ': ' . (shift @info) . ' ' . join(' ', map { '[ '.$_.' ]' } (@critical,@warning)) . $perfoutput; + exit $ERRORS{"CRITICAL"}; } elsif ( scalar(@warning) > 0 ) { - print join(' ', map { '[ '.$_.' ]' } @warning); - exit $WARNING; + print 'WARNING - API ' . $api_version . ': ' . (shift @info) . ' ' . join(' ', map { '[ '.$_.' ]' } @warning). $perfoutput; + exit $ERRORS{"WARNING"}; } else { - print $api_version . ': '.(shift @info).' '.join(' ', map { '[ '.$_.' ]' } @info); - exit $OKAY; + print 'OK - API ' . $api_version . ': ' . (shift @info) . ' ' . join(' ', map { '[ '.$_.' ]' } @info) . $perfoutput; + exit $ERRORS{"OK"}; } ### Subs @@ -175,11 +213,11 @@ sub api_get { my $con = $ret->responseContent(); if ( $num == 500 ) { print "API returned error 500 for '$url' - $con\n"; - exit $UNKNOWN + exit $ERRORS{"UNKNOWN"} } if ( $num != 200 ) { print "API returned code $num for URL '$url'\n"; - exit $UNKNOWN + exit $ERRORS{"UNKNOWN"} } print 'DEBUG: GET ', $url, ' -> ', $num, ":\n", Dumper(from_json($con)), "\n" if $debug; return from_json($con); @@ -193,11 +231,11 @@ sub api_post { my $con = $ret->responseContent(); if ( $num == 500 ) { print "API returned error 500 for '$url' - $con\n"; - exit $UNKNOWN + exit $ERRORS{"UNKNOWN"} } if ( $num != 200 ) { print "API returned code $num for URL '$url'\n"; - exit $UNKNOWN + exit $ERRORS{"UNKNOWN"} } print 'DEBUG: POST ', $url, ' -> ', $num, ":\n", Dumper(from_json($con)), "\n" if $debug; return from_json($con);