3dfd4880c73eafc88be9f4d5a994a96b4fe15a83
[mirror/dsa-nagios.git] / dsa-nagios-checks / checks / dsa-check-hpssacli
1 #!/usr/bin/perl -w
2
3 # check _physical_ disk status of disks on HP smart array controllers
4 # requires hpssacli
5 #
6 # does _not_ check raid status.  use arrayprobe for that.
7
8 # Copyright (c) 2008,2009,2010,2011 Peter Palfrader <peter@palfrader.org>
9 #
10 # Permission is hereby granted, free of charge, to any person obtaining
11 # a copy of this software and associated documentation files (the
12 # "Software"), to deal in the Software without restriction, including
13 # without limitation the rights to use, copy, modify, merge, publish,
14 # distribute, sublicense, and/or sell copies of the Software, and to
15 # permit persons to whom the Software is furnished to do so, subject to
16 # the following conditions:
17 #
18 # The above copyright notice and this permission notice shall be
19 # included in all copies or substantial portions of the Software.
20 #
21 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
29 use strict;
30 use English;
31 use Getopt::Long;
32
33 # nagios exit codes
34 my %CODE = (
35         'OK'            => 0,
36         'WARNING'       => 1,
37         'CRITICAL'      => 2,
38         'UNKNOWN'       => 3
39 );
40
41 my $EXITCODE = 'OK';
42
43 $SIG{'__DIE__'} = sub {
44         print @_;
45         exit $CODE{'UNKNOWN'};
46 };
47
48 sub runcmd($) {
49         my ($cmd) = @_;
50         $cmd = "sudo hpssacli $cmd";
51         open(FH, $cmd."|") or die ("Cannot run $cmd: $!");
52         my @lines = <FH>;
53         close FH;
54         die ("no results from $cmd\n") if (scalar @lines == 0);
55         return \@lines;
56 }
57
58 sub record($) {
59         my ($newexit) = @_;
60         die "code $newexit not defined\n" unless defined $CODE{$newexit};
61
62         if ($CODE{$newexit} > $CODE{$EXITCODE}) {
63                 $EXITCODE = $newexit;
64         };
65 }
66
67 my $usage = "$PROGRAM_NAME: Usage: $PROGRAM_NAME [--no-battery] [--ignore-controller=<regex>] [--no-controller-ok] [--ignore-transfer-speed=<pd> [--ignore-transfer-speed=<pd> ...]]\n";
68 my $params;
69 Getopt::Long::Configure('bundling');
70 if (!GetOptions (
71         '--help'                      => \$params->{'help'},
72         '--no-battery'                => \$params->{'no-battery'},
73         '--no-controller-ok'          => \$params->{'no-controller-ok'},
74         '--ignore-controller=s'       => \$params->{'ignore-controller'},
75         '--ignore-transfer-speed=s@'  => \$params->{'ignore-transfer-speed'},
76         )) {
77         die ($usage);
78 };
79 if ($params->{'help'}) {
80         print $usage;
81         exit (0);
82 };
83 die ($usage) unless (scalar @ARGV == 0);
84
85 my $ctrlallshow = runcmd("controller all show");
86 my @controllers;
87 for (@$ctrlallshow) {
88         chomp;
89         next if /^$/;
90         next if ($params->{'ignore-controller'} && /$params->{'ignore-controller'}/);
91         if (/in Slot ([0-9a-z]+)/) {
92                 push @controllers, $1;
93                 next;
94         };
95         die ("Cannot read line '$_' gotten from hpssacli controller all show\n");
96 };
97
98 if (scalar @controllers == 0) {
99         if ($params->{'no-controller-ok'}) {
100                 print "No smartarray controllers found with hpssacli\n";
101                 exit $CODE{'OK'}
102         } else {
103                 print "UNKNOWN: No smartarray controllers found with hpssacli\n";
104                 exit $CODE{'UNKNOWN'}
105         }
106 };
107
108 my @resultstr;
109
110 for my $slot (sort @controllers) {
111         my @drives;
112         my $nodrives = 0;
113         my %status;
114         my @freetext;
115
116         my $ldallshow = runcmd("controller slot=$slot ld all show");
117         my @logicaldrives;
118         for (@$ldallshow) {
119                 chomp;
120                 next if /^$/;
121                 next if (/^\S.*in Slot $slot/);
122                 next if /^ *array [A-Z]$/;
123                 if (/logicaldrive ([0-9a-z]+)/) {
124                         push @logicaldrives, $1;
125                         next;
126                 } elsif (/^Error: The specified device does not have any logical drives.$/) {
127                         $nodrives = 1;
128                 } else {
129                         die ("Cannot read line '$_' gotten from hpssacli controller slot = $slot logicaldrive all show\n");
130                 }
131         };
132
133         # check logicaldrives
134         for my $logicaldrive (sort @logicaldrives) {
135                 my $lds = runcmd("controller slot=$slot ld $logicaldrive show");
136                 for (@$lds) {
137                         chomp;
138                         next if /^$/;
139                         if (/^ *Parity Initialization Status: (Initialization Completed|Initialization Failed|Rebuilding)$/) {
140                                 my $status = $1;
141                                 if ($status eq 'Initialization Completed') {
142                                         push @{$status{'OK'}}, "Parity LD$logicaldrive";
143                                 } elsif ($status eq 'Rebuilding') {
144                                         push @{$status{'Failed'}}, "Parity LD$logicaldrive";
145                                         record('WARNING');
146                                 } elsif ($status eq 'Initialization Failed') {
147                                         push @{$status{'Failed'}}, "Parity LD$logicaldrive";
148                                         record('CRITICAL');
149                                 } else {
150                                         record('UNKNOWN');
151                                 }
152                         }
153                         if (/^ *LD Acceleration Method: (.*)$/) {
154                                 my $status = $1;
155                                 # can at least be "Controller Cache" or HP SSD Smart Path", both OK
156                                 if ($status eq 'All disabled') {
157                                         push @{$status{'Acceleration method'}}, "LD$logicaldrive disabled";
158                                         record('WARNING');
159                                 }
160                         }
161                 }
162         }
163
164         if (!$nodrives && scalar @logicaldrives == 0) {
165                 push @resultstr, "Slot $slot: unexpectedly, found no logical drives in list.";
166                 record('UNKNOWN');
167         } elsif ($nodrives && scalar keys %status > 0) {
168                 push @resultstr, "Slot $slot: have no logical drives but status results?";
169                 record('UNKNOWN');
170                 next;
171         } elsif ($nodrives) {
172                 push @resultstr, "Slot $slot: no logical drives";
173         };
174
175
176         my $pds = runcmd("controller slot=$slot pd all show");
177         for (@$pds) {
178                 chomp;
179                 next if /^$/;
180                 next if (/^\S.*in Slot $slot/);
181                 next if /^ *array [A-Z]$/;
182                 next if /^ *unassigned/;
183                 if (/^ *HBA Drives/) {
184                         # HBA mode implies no logical drives, thus reset the "drives found" check and proceed with
185                         # checking physical drives.
186                         $nodrives = 0;
187                         next;
188                 }
189                 if (/^ *(array [A-Z]) \(Failed\)$/) {
190                         record('CRITICAL');
191                         push @{$status{'Failed'}}, $1;
192                 } elsif (/^Error: The specified controller does not have any physical drives on it.$/) {
193                         $nodrives = 1;
194                 } elsif (/^ *physicaldrive (\S+) .* (OK|Predictive Failure|Failed|Rebuilding)(?:, (?:active )?spare)?\)$/) {
195                         my $drive = $1;
196                         my $status = $2;
197                         push @{$status{$status}}, $drive;
198                         if ($status eq 'OK') {
199                         } elsif ($status eq 'Predictive Failure' ||
200                                  $status eq 'Rebuilding') {
201                                 record('WARNING');
202                         } elsif ($status eq 'Failed') {
203                                 record('CRITICAL');
204                         } else {
205                                 record('UNKNOWN');
206                         };      
207                         push @drives, $drive;
208                 } else {
209                         die ("Cannot read line '$_' gotten from hpssacli controller slot=$slot pd all show\n");
210                 };
211         };
212
213         # Check that all drives have the proper transfer speed.
214         # sometimes stuff breaks and they fall back to 10mb/sec.
215         for my $drive (@drives) {
216                 # skip drives that are known to have failed
217                 next if (exists $status{'Failed'} && grep {$drive eq $_} @{$status{'Failed'}});
218                 my $type;
219                 if ($drive =~ /^[0-9]+:[0-9]+$/) { # scsi drives
220                         $type = 'SCSI';
221                 } elsif ($drive =~ /^[0-9]+[EI]:[0-9]+:[0-9]+$/) { # SAS
222                         $type = 'SAS';
223                 } elsif ($drive =~ /^[0-9]+[C]:[0-9]+:[0-9]+$/) { # New 6GBPS SAS
224                         $type = 'SAS+';
225                 } else {
226                         # I'm not going to run pass arguments of unknown form to the shell..
227                         warn ("Unknown diskdrive ID $drive\n");
228                         next;
229                 }
230
231                 my $pd = runcmd("controller slot=$slot pd $drive show");
232                 while (defined $pd->[0] && !($pd->[0] =~ /physicaldrive/)) {
233                         shift @$pd;
234                 };
235                 shift @$pd;
236                 my %value;
237                 for (@$pd) {
238                         if (m/^\s*(.*?):\s*(.*?)\s*$/) {
239                                 $value{$1} = $2;
240                         }
241                 }
242
243                 my $key;
244                 my $expected;
245                 if ($type eq 'SCSI') {
246                         $key = 'Transfer Speed';
247                         if (!defined $value{'Transfer Mode'}) {
248                                 record('WARNING');
249                                 push @{$status{'unknown transfer mode'}}, $drive;
250                                 next;
251                         } elsif ($value{'Transfer Mode'} eq 'Ultra 3 Wide') {
252                                 $expected = '160 MB/Sec';
253                         } elsif ($value{'Transfer Mode'} eq 'Ultra 320 Wide') {
254                                 $expected = '320 MB/Sec';
255                         } else {
256                                 record('WARNING');
257                                 push @{$status{'unknown transfer mode'}}, $drive."(".$value{'Transfer Mode'}.")";
258                                 next;
259                         };
260                 } elsif ($type eq 'SAS' || $type eq 'SAS+') {
261                         $key = 'PHY Transfer Rate';
262                         if ($value{'Interface Type'} eq 'SATA') {
263                                 $expected = [ '1.5Gbps', '3.0Gbps', '6.0Gbps' ];
264                         } elsif ($value{'PHY Count'} eq '2') {
265                                 if (defined($value{'Redundant Path(s)'})) {
266                                         $expected = [ '3.0GBPS, 3.0GBPS', '6.0GBPS, 6.0GBPS',
267                                                       '12.0GBPS, 12.0GBPS' ];
268                                 } else {
269                                         $expected = [ '3.0GBPS, Unknown', 'Unknown, 3.0GBPS',
270                                                       '6.0GBPS, Unknown', 'Unknown, 6.0GBPS',
271                                                       '12.0GBPS, Unknown', 'Unknown, 12.0GBPS' ];
272                                 }
273                         } else {
274                                 $expected = [ '3.0GBPS', '6.0GBPS', '12.0GBPS' ];
275                         }
276                 } else {
277                         warn "Should not be here.  Do not know what to do with type '$type'\n";
278                         next;
279                 }
280
281                 if ($params->{'ignore-transfer-speed'}) {
282                         if (grep { $drive eq $_ } @{$params->{'ignore-transfer-speed'}}) {
283                                 push @{$status{'ignored transfer speed'}}, $drive."(".$value{$key}.")";
284                                 next;
285                         };
286                 };
287                 if (!defined $value{$key}) {
288                         record('WARNING');
289                         push @{$status{'unknown transfer speed'}}, $drive;
290                 } elsif (ref($expected) eq 'ARRAY') {
291                         if (scalar(grep { uc($value{$key}) eq uc($_) } @$expected) == 0) {
292                                 record('WARNING');
293                                 push @{$status{'bad transfer speed'}}, $drive."(".$value{$key}.")";
294                         };
295                 } elsif (uc($value{$key}) ne uc($expected)) {
296                         record('WARNING');
297                         push @{$status{'bad transfer speed'}}, $drive."(".$value{$key}.")";
298                 };
299         };
300
301         if ($nodrives && scalar keys %status > 0) {
302                 push @resultstr, "Slot $slot: have no drives but status results?";
303                 record('UNKNOWN');
304                 next;
305         } elsif ($nodrives) {
306                 push @resultstr, "Slot $slot: no drives";
307                 next;
308         };
309
310         my $cst = runcmd("controller slot=$slot show detail");
311         for (@$cst) {
312                 chomp;
313                 next if /^$/;
314                 next if (/^\S.*in Slot $slot/);
315                 if (/^ *(Controller|Cache|Battery\/Capacitor) Status: (.*)$/) {
316                         my $system = $1;
317                         my $status = $2;
318
319                         if ($system eq 'Cache') {
320                                 # Can be:
321                                 # - 'OK'
322                                 # - 'Not Configured' (for e.g. HP SSD Smart Path)
323                                 # - 'Permanently Disabled'
324                                 # - ...?
325                                 next if $status =~ /^(OK|Not Configured)$/;
326                         }
327
328                         push @freetext, "$system: $status";
329                         if ($status ne 'OK') {
330                                 next if ($params->{'no-battery'} && $system eq 'Battery/Capacitor');
331                                 record('WARNING');
332                         };
333                 } elsif (/^ *(Cache Status Details): (Cable Error)/) {
334                         push @freetext, $2;
335                         record('CRITICAL');
336                 } elsif (/^ *(Battery\/Capacitor Count): (.*)/) {
337                         next if $params->{'no-battery'} || int($2) > 0;
338                         push @freetext, "Battery count: $2";
339                         record('CRITICAL');
340                 };
341         };
342
343         my $status = join(" - ", ((map { $_.": ".join(", ", @{$status{$_}}) } keys %status), @freetext));
344
345         push @resultstr, "Slot $slot: $status";
346 };
347
348 print "$EXITCODE: ", join(" --- ", @resultstr), "\n";
349 exit $CODE{$EXITCODE};