Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added command-line parameters and performance data output #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 87 additions & 49 deletions check_purearray.pl
Original file line number Diff line number Diff line change
@@ -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 <host> -T <api-token> -w <warning level> -c <critical level> [-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 <<EOT;
-H, --hostname=HOST
Name or IP address of array to check
-T, --token=UUID
API token (pureadmin create --api-token)
-w, --warning=INTEGER
Array/volume warning percent
-c, --critical=INTEGER
Array/volume critical percent
-f, --perfdata
Performance data output
-d, --debug
Print extra debugging information
EOT
}

my $token = $api_tokens{$host};

unless ( $token ) {
print "No API token for host $host\n";
exit $UNKNOWN
sub check_options {
Getopt::Long::Configure ("bundling");
GetOptions(
'd' => \$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

Expand Down Expand Up @@ -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
Expand All @@ -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 ) {
Expand All @@ -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;
Expand All @@ -123,6 +157,7 @@
} else {
push @info, $message;
}
push @perfdata, $perfmessage if ($perf);

### Check the volumes

Expand All @@ -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;
Expand All @@ -148,22 +183,25 @@
} else {
push @info, $message;
}
push @perfdata, $perfmessage if ($perf);
}

# Kill the session

$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
Expand All @@ -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);
Expand All @@ -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);
Expand Down