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