905435d1a068f5bf8fff30fefeeadd9a2d682670
[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-cache] [--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-cache'              => \$params->{'ignore-cache'},
75         '--ignore-controller=s'       => \$params->{'ignore-controller'},
76         '--ignore-transfer-speed=s@'  => \$params->{'ignore-transfer-speed'},
77         )) {
78         die ($usage);
79 };
80 if ($params->{'help'}) {
81         print $usage;
82         exit (0);
83 };
84 die ($usage) unless (scalar @ARGV == 0);
85
86 my $ctrlallshow = runcmd("controller all show detail");
87 my $slot;
88 my %controllers;
89 for (@$ctrlallshow) {
90         chomp;
91         next if /^$/;
92         next if ($params->{'ignore-controller'} && /$params->{'ignore-controller'}/);
93         if (/in Slot ([0-9a-z]+)/) {
94                 $slot = $1;
95                 $controllers{$slot} = ();
96         } elsif (/^ *(Controller|Cache|Battery\/Capacitor) Status: (.*)$/) {
97                 my $system = $1;
98                 my $status = $2;
99
100                 if ($system eq 'Cache') {
101                         # Can be:
102                         # - 'OK'
103                         # - 'Not Configured' (for e.g. HP SSD Smart Path)
104                         # - 'Permanently Disabled'
105                         # - ...?
106                         next if $status =~ /^(OK|Not Configured)$/;
107                         if ($params->{'ignore-cache'}) {
108                                 push @{$controllers{$slot}}, "$system: $status (ignored)";
109                                 next;
110                         }
111                 }
112
113                 push @{$controllers{$slot}}, "$system: $status";
114                 if ($status ne 'OK') {
115                         next if ($params->{'no-battery'} && $system eq 'Battery/Capacitor');
116                         record('WARNING');
117                 };
118         } elsif (/^ *(Cache Status Details): (Cable Error)/) {
119                 push @{$controllers{$slot}}, $2;
120                 record('CRITICAL');
121         } elsif (/^ *(Battery\/Capacitor Count): (.*)/) {
122                 next if $params->{'no-battery'} || int($2) > 0;
123                 push @{$controllers{$slot}}, "Battery count: $2";
124                 record('CRITICAL');
125         };
126 };
127
128 if (scalar keys %controllers == 0) {
129         if ($params->{'no-controller-ok'}) {
130                 print "No smartarray controllers found with hpssacli\n";
131                 exit $CODE{'OK'}
132         } else {
133                 print "UNKNOWN: No smartarray controllers found with hpssacli\n";
134                 exit $CODE{'UNKNOWN'}
135         }
136 };
137
138 my @resultstr;
139
140 for my $slot (sort keys %controllers) {
141         my $nodrives = 0;
142         my %status;
143
144         # check logicaldrives
145         my $logicaldrive;
146         my @logicaldrives;
147         my $lds = runcmd("controller slot=$slot ld all show detail");
148         for (@$lds) {
149                 chomp;
150                 next if /^$/;
151                 if (/Logical Drive: ([0-9a-z]+)/) {
152                         $logicaldrive = $1;
153                         push @logicaldrives, $logicaldrive;
154                 } elsif (/^Error: The specified device does not have any logical drives.$/) {
155                         $nodrives = 1;
156                 } elsif (/^ *Parity Initialization Status: (Initialization Completed|Initialization Failed|Rebuilding)$/) {
157                         my $status = $1;
158                         if ($status eq 'Initialization Completed') {
159                                 push @{$status{'OK'}}, "Parity LD$logicaldrive";
160                         } elsif ($status eq 'Rebuilding') {
161                                 push @{$status{'Failed'}}, "Parity LD$logicaldrive";
162                                 record('WARNING');
163                         } elsif ($status eq 'Initialization Failed') {
164                                 push @{$status{'Failed'}}, "Parity LD$logicaldrive";
165                                 record('CRITICAL');
166                         } else {
167                                 record('UNKNOWN');
168                         }
169                 } elsif (/^ *LD Acceleration Method: (.*)$/) {
170                         my $status = $1;
171                         # can at least be "Controller Cache" or HP SSD Smart Path", both OK
172                         if ($status eq 'All disabled') {
173                                 push @{$status{'Acceleration method'}}, "LD$logicaldrive disabled";
174                                 record('WARNING');
175                         }
176                 }
177         }
178
179         if (!$nodrives && scalar @logicaldrives == 0) {
180                 push @resultstr, "Slot $slot: unexpectedly, found no logical drives in list.";
181                 record('UNKNOWN');
182         } elsif ($nodrives && scalar keys %status > 0) {
183                 push @resultstr, "Slot $slot: have no logical drives but status results?";
184                 record('UNKNOWN');
185                 next;
186         } elsif ($nodrives) {
187                 push @resultstr, "Slot $slot: no logical drives";
188         };
189
190         my $pds = runcmd("controller slot=$slot pd all show detail");
191         my $drive;
192         my %drives;
193         for (@$pds) {
194                 chomp;
195                 next if /^$/;
196                 next if (/^\S.*in Slot $slot/);
197                 next if /^ *array [A-Z]$/;
198                 next if /^ *unassigned/;
199                 if (/^ *HBA Drives/) {
200                         # HBA mode implies no logical drives, thus reset the "drives found" check and proceed with
201                         # checking physical drives.
202                         $nodrives = 0;
203                         next;
204                 }
205                 if (/^ *(array [A-Z]) \(Failed\)$/) {
206                         record('CRITICAL');
207                         push @{$status{'Failed'}}, $1;
208                 } elsif (/^Error: The specified controller does not have any physical drives on it.$/) {
209                         $nodrives = 1;
210                 } elsif (/^ *physicaldrive (\S+)/) {
211                         $drive = $1;
212                         $drives{$drive} = {};
213                 } elsif (defined $drive && m/^\s*(.*?):\s*(.*?)\s*$/) {
214                         $drives{$drive}{$1} = $2;
215                 } else {
216                         die ("Cannot read line '$_' gotten from hpssacli controller slot=$slot pd all show\n");
217                 }
218         };
219
220         # Check that all drives have the proper transfer speed.
221         # sometimes stuff breaks and they fall back to 10mb/sec.
222         for my $drive (sort keys %drives) {
223                 my $value = $drives{$drive};
224                 my $status = $value->{'Status'};
225                 push @{$status{$status}}, $drive;
226                 if ($status eq 'OK') {
227                 } elsif ($status eq 'Predictive Failure' ||
228                          $status eq 'Rebuilding') {
229                         record('WARNING');
230                 } elsif ($status eq 'Failed') {
231                         record('CRITICAL');
232                         # skip drives that are known to have failed
233                         next;
234                 } else {
235                         record('UNKNOWN');
236                 }
237
238                 my $type;
239                 if ($drive =~ /^[0-9]+:[0-9]+$/) { # scsi drives
240                         $type = 'SCSI';
241                 } elsif ($drive =~ /^[0-9]+[EI]:[0-9]+:[0-9]+$/) { # SAS
242                         $type = 'SAS';
243                 } elsif ($drive =~ /^[0-9]+[C]:[0-9]+:[0-9]+$/) { # New 6GBPS SAS
244                         $type = 'SAS+';
245                 } else {
246                         warn ("Unknown diskdrive ID $drive\n");
247                         next;
248                 }
249
250                 my $key;
251                 my $expected;
252                 if ($type eq 'SCSI') {
253                         $key = 'Transfer Speed';
254                         if (!defined $value->{'Transfer Mode'}) {
255                                 record('WARNING');
256                                 push @{$status{'unknown transfer mode'}}, $drive;
257                                 next;
258                         } elsif ($value->{'Transfer Mode'} eq 'Ultra 3 Wide') {
259                                 $expected = '160 MB/Sec';
260                         } elsif ($value->{'Transfer Mode'} eq 'Ultra 320 Wide') {
261                                 $expected = '320 MB/Sec';
262                         } else {
263                                 record('WARNING');
264                                 push @{$status{'unknown transfer mode'}}, $drive."(".$value->{'Transfer Mode'}.")";
265                                 next;
266                         };
267                 } elsif ($type eq 'SAS' || $type eq 'SAS+') {
268                         $key = 'PHY Transfer Rate';
269                         if ($value->{'Interface Type'} eq 'SATA') {
270                                 $expected = [ '1.5Gbps', '3.0Gbps', '6.0Gbps' ];
271                         } elsif ($value->{'PHY Count'} eq '2') {
272                                 if (defined($value->{'Redundant Path(s)'})) {
273                                         $expected = [ '3.0GBPS, 3.0GBPS', '6.0GBPS, 6.0GBPS',
274                                                       '12.0GBPS, 12.0GBPS' ];
275                                 } else {
276                                         $expected = [ '3.0GBPS, Unknown', 'Unknown, 3.0GBPS',
277                                                       '6.0GBPS, Unknown', 'Unknown, 6.0GBPS',
278                                                       '12.0GBPS, Unknown', 'Unknown, 12.0GBPS' ];
279                                 }
280                         } else {
281                                 $expected = [ '3.0GBPS', '6.0GBPS', '12.0GBPS' ];
282                         }
283                 } else {
284                         warn "Should not be here.  Do not know what to do with type '$type'\n";
285                         next;
286                 }
287
288                 if ($params->{'ignore-transfer-speed'}) {
289                         if (grep { $drive eq $_ } @{$params->{'ignore-transfer-speed'}}) {
290                                 push @{$status{'ignored transfer speed'}}, $drive."(".$value->{$key}.")";
291                                 next;
292                         };
293                 };
294                 if (!defined $value->{$key}) {
295                         record('WARNING');
296                         push @{$status{'unknown transfer speed'}}, $drive;
297                 } elsif (ref($expected) eq 'ARRAY') {
298                         if (scalar(grep { uc($value->{$key}) eq uc($_) } @$expected) == 0) {
299                                 record('WARNING');
300                                 push @{$status{'bad transfer speed'}}, $drive."(".$value->{$key}.")";
301                         };
302                 } elsif (uc($value->{$key}) ne uc($expected)) {
303                         record('WARNING');
304                         push @{$status{'bad transfer speed'}}, $drive."(".$value->{$key}.")";
305                 };
306         };
307
308         if ($nodrives && scalar keys %status > 0) {
309                 push @resultstr, "Slot $slot: have no drives but status results?";
310                 record('UNKNOWN');
311                 next;
312         } elsif ($nodrives) {
313                 push @resultstr, "Slot $slot: no drives";
314                 next;
315         };
316
317         my $status = join(" - ", ((map { $_.": ".join(", ", @{$status{$_}}) } keys %status), @{$controllers{$slot}}));
318
319         push @resultstr, "Slot $slot: $status";
320 };
321
322 print "$EXITCODE: ", join(" --- ", @resultstr), "\n";
323 exit $CODE{$EXITCODE};