-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_purearray.pl
executable file
·215 lines (169 loc) · 5.07 KB
/
check_purearray.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/perl
use Data::Dumper;
use REST::Client;
use JSON;
use Net::SSL;
use strict;
### 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;
### Parse command line
my $debug = 0;
my $host;
for my $arg (@ARGV) {
if ( $arg =~ /--debug/i ) {
$debug++;
} else {
$host = $arg;
}
}
### Bootstrap
unless ( $host ) {
print "No hostname given to check\n";
exit $UNKNOWN
}
my $token = $api_tokens{$host};
unless ( $token ) {
print "No API token for host $host\n";
exit $UNKNOWN
}
my @critical;
my @warning;
my @info;
### Start RESTing
my $client = REST::Client->new( follow => 1 );
$client->setHost('https://'.$host);
$client->addHeader('Content-Type', 'application/json');
$client->getUseragent()->cookie_jar({ file => $cookie_file });
$client->getUseragent()->ssl_opts(verify_hostname => 0);
### Check for API 1.4 support
my $ref = &api_get('/api/api_version');
my %api_versions;
for my $version (@{$ref->{version}}) {
$api_versions{$version}++;
}
my $api_version = $api_versions{'1.4'} ? '1.4' :
$api_versions{'1.3'} ? '1.3' :
$api_versions{'1.1'} ? '1.1' :
$api_versions{'1.0'} ? '1.0' :
undef;
unless ( $api_version ) {
print "API version 1.3 or 1.4 is not supported by host: $host\n";
exit $UNKNOWN
}
### Set the Session Cookie
my $ret = &api_post("/api/$api_version/auth/session", { api_token => $token });
### Check the Array overall
my $array_info = &api_get("/api/$api_version/array?space=true");
for my $param (qw/system capacity total/) {
next if defined $array_info->{$param};
print "Array data lacks parameter: $param";
exit $UNKNOWN
}
if ( (100 * $array_info->{system} / $array_info->{capacity}) >= $max_system_percent ) {
my $percent = sprintf('%0.2f%%', (100 * $array_info->{system} / $array_info->{capacity}));
my $usage = human_readable_bytes($array_info->{system});
push @warning, "System space in use: $usage / $percent";
}
my $array_percent_used = sprintf('%0.2f', (100 * $array_info->{total} / $array_info->{capacity}));
my $message = "Array @ $array_percent_used\%";
if ( $array_percent_used > $array_crit_percent ) {
push @critical, $message;
} elsif ( $array_percent_used > $array_warn_percent ) {
push @warning, $message;
} else {
push @info, $message;
}
### Check the volumes
my $vol_info = &api_get("/api/$api_version/volume?space=true");
for my $vol (@$vol_info) {
for my $param (qw/total size name/) {
next if defined $vol->{$param};
print "Volume data lacks parameter: $param";
exit $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\%";
if ( $vol_percent_used > $vol_crit_percent ) {
push @critical, $message;
} elsif ( $vol_percent_used > $vol_warn_percent ) {
push @warning, $message;
} else {
push @info, $message;
}
}
# Kill the session
$ret = $client->DELETE("/api/$api_version/auth/session");
unlink($cookie_file);
if ( scalar(@critical) > 0 ) {
print join(' ', map { '[ '.$_.' ]' } (@critical,@warning));
exit $CRITICAL;
} elsif ( scalar(@warning) > 0 ) {
print join(' ', map { '[ '.$_.' ]' } @warning);
exit $WARNING;
} else {
print $api_version . ': '.(shift @info).' '.join(' ', map { '[ '.$_.' ]' } @info);
exit $OKAY;
}
### Subs
sub api_get {
my $url = shift @_;
my $ret = $client->GET($url);
my $num = $ret->responseCode();
my $con = $ret->responseContent();
if ( $num == 500 ) {
print "API returned error 500 for '$url' - $con\n";
exit $UNKNOWN
}
if ( $num != 200 ) {
print "API returned code $num for URL '$url'\n";
exit $UNKNOWN
}
print 'DEBUG: GET ', $url, ' -> ', $num, ":\n", Dumper(from_json($con)), "\n" if $debug;
return from_json($con);
}
sub api_post {
my $url = shift @_;
my $con = shift @_;
my $ret = $client->POST($url, to_json($con));
my $num = $ret->responseCode();
my $con = $ret->responseContent();
if ( $num == 500 ) {
print "API returned error 500 for '$url' - $con\n";
exit $UNKNOWN
}
if ( $num != 200 ) {
print "API returned code $num for URL '$url'\n";
exit $UNKNOWN
}
print 'DEBUG: POST ', $url, ' -> ', $num, ":\n", Dumper(from_json($con)), "\n" if $debug;
return from_json($con);
}
sub human_readable_bytes {
my $raw = shift @_;
if ( $raw > 500_000_000_000 ) {
return sprintf('%.2f TB', ($raw/1_000_000_000_000));
} elsif ( $raw > 500_000_000 ) {
return sprintf('%.2f GB', ($raw/1_000_000_000));
} else {
return sprintf('%.2f MB', ($raw/1_000_000));
}
}