8bbb7ebe5a4ce1698203a82dd0f38bda2e1112ed
[mirror/dsa-nagios.git] / dsa-nagios-checks / checks / dsa-check-hpacucli
1 #!/usr/bin/perl -w
2
3 # check _physical_ disk status of disks on HP smart array controllers
4 # requires hpacucli
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 hpacucli $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 hpacucli controller all show\n");
96 };
97
98 if (scalar @controllers == 0) {
99         if ($params->{'no-controller-ok'}) {
100                 print "No smartarray controllers found with hpacucli\n";
101                 exit $CODE{'OK'}
102         } else {
103                 print "UNKNOWN: No smartarray controllers found with hpacucli\n";
104                 exit $CODE{'UNKNOWN'}
105         }
106 };
107
108 my @resultstr;
109
110 for my $slot (sort @controllers) {
111         my $pds = runcmd("controller slot=$slot pd all show");
112         my @drives;
113         my $nodrives = 0;
114         my %status;
115         for (@$pds) {
116                 chomp;
117                 next if /^$/;
118                 next if (/^\S.*in Slot $slot/);
119                 next if /^ *array [A-Z]$/;
120                 next if /^ *unassigned/;
121                 if (/^ *(array [A-Z]) \(Failed\)$/) {
122                         record('CRITICAL');
123                         push @{$status{'Failed'}}, $1;
124                 } elsif (/^Error: The specified controller does not have any physical drives on it.$/) {
125                         $nodrives = 1;
126                 } elsif (/^ *physicaldrive (\S+) .* (OK|Predictive Failure|Failed|Rebuilding)(?:, (?:active )?spare)?\)$/) {
127                         my $drive = $1;
128                         my $status = $2;
129                         push @{$status{$status}}, $drive;
130                         if ($status eq 'OK') {
131                         } elsif ($status eq 'Predictive Failure' ||
132                                  $status eq 'Rebuilding') {
133                                 record('WARNING');
134                         } elsif ($status eq 'Failed') {
135                                 record('CRITICAL');
136                         } else {
137                                 record('UNKNOWN');
138                         };      
139                         push @drives, $drive;
140                 } else {
141                         die ("Cannot read line '$_' gotten from hpacucli controller slot=$slot pd all show\n");
142                 };
143         };
144
145         # Check that all drives have the proper transfer speed.
146         # sometimes stuff breaks and they fall back to 10mb/sec.
147         for my $drive (@drives) {
148                 # skip drives that are known to have failed
149                 next if (exists $status{'Failed'} && grep {$drive eq $_} @{$status{'Failed'}});
150                 my $type;
151                 if ($drive =~ /^[0-9]+:[0-9]+$/) { # scsi drives
152                         $type = 'SCSI';
153                 } elsif ($drive =~ /^[0-9]+[EI]:[0-9]+:[0-9]+$/) { # SAS
154                         $type = 'SAS';
155                 } elsif ($drive =~ /^[0-9]+[C]:[0-9]+:[0-9]+$/) { # New 6GBPS SAS
156                         $type = 'SAS+';
157                 } else {
158                         # I'm not going to run pass arguments of unknown form to the shell..
159                         warn ("Unknown diskdrive ID $drive\n");
160                         next;
161                 }
162
163                 my $pd = runcmd("controller slot=$slot pd $drive show");
164                 while (defined $pd->[0] && !($pd->[0] =~ /physicaldrive/)) {
165                         shift @$pd;
166                 };
167                 shift @$pd;
168                 my %value;
169                 for (@$pd) {
170                         if (m/^\s*(.*?):\s*(.*?)\s*$/) {
171                                 $value{$1} = $2;
172                         }
173                 }
174
175                 my $key;
176                 my $expected;
177                 if ($type eq 'SCSI') {
178                         $key = 'Transfer Speed';
179                         if (!defined $value{'Transfer Mode'}) {
180                                 record('WARNING');
181                                 push @{$status{'unknown transfer mode'}}, $drive;
182                                 next;
183                         } elsif ($value{'Transfer Mode'} eq 'Ultra 3 Wide') {
184                                 $expected = '160 MB/Sec';
185                         } elsif ($value{'Transfer Mode'} eq 'Ultra 320 Wide') {
186                                 $expected = '320 MB/Sec';
187                         } else {
188                                 record('WARNING');
189                                 push @{$status{'unknown transfer mode'}}, $drive."(".$value{'Transfer Mode'}.")";
190                                 next;
191                         };
192                 } elsif ($type eq 'SAS' || $type eq 'SAS+') {
193                         $key = 'PHY Transfer Rate';
194                         if ($value{'PHY Count'} eq '2') {
195                                 if (defined($value{'Redundant Path(s)'})) {
196                                         $expected = [ '3.0GBPS, 3.0GBPS', '6.0GBPS, 6.0GBPS' ];
197                                 } else {
198                                         $expected = [ '3.0GBPS, Unknown', 'Unknown, 3.0GBPS',
199                                                       '6.0GBPS, Unknown', 'Unknown, 6.0GBPS' ];
200                                 }
201                         } else {
202                                 $expected = [ '3.0GBPS', '6.0GBPS' ];
203                         }
204                 } else {
205                         warn "Should not be here.  Do not know what to do with type '$type'\n";
206                         next;
207                 }
208
209                 if ($params->{'ignore-transfer-speed'}) {
210                         if (grep { $drive eq $_ } @{$params->{'ignore-transfer-speed'}}) {
211                                 push @{$status{'ignored transfer speed'}}, $drive."(".$value{$key}.")";
212                                 next;
213                         };
214                 };
215                 if (!defined $value{$key}) {
216                         record('WARNING');
217                         push @{$status{'unknown transfer speed'}}, $drive;
218                 } elsif (ref($expected) eq 'ARRAY') {
219                         if (scalar(grep { uc($value{$key}) eq uc($_) } @$expected) == 0) {
220                                 record('WARNING');
221                                 push @{$status{'bad transfer speed'}}, $drive."(".$value{$key}.")";
222                         };
223                 } elsif (uc($value{$key}) ne uc($expected)) {
224                         record('WARNING');
225                         push @{$status{'bad transfer speed'}}, $drive."(".$value{$key}.")";
226                 };
227         };
228
229         if ($nodrives && scalar keys %status > 0) {
230                 push @resultstr, "Slot $slot: have no drives but status results?";
231                 record('UNKNOWN');
232                 next;
233         } elsif ($nodrives) {
234                 push @resultstr, "Slot $slot: no drives";
235                 next;
236         };
237
238         my $cst = runcmd("controller slot=$slot show status");
239         for (@$cst) {
240                 chomp;
241                 next if /^$/;
242                 next if (/^\S.*in Slot $slot/);
243                 if (/^ *(.*) Status: (.*)$/) {
244                         my $system = $1;
245                         my $status = $2;
246                         push @{$status{$status}}, $system;
247                         if ($status ne 'OK') {
248                                 next if ($params->{'no-battery'} && $system eq 'Cache');
249                                 next if ($params->{'no-battery'} && $system eq 'Battery/Capacitor');
250                                 record('WARNING');
251                         };
252                 } else {
253                         die ("Cannot read line '$_' gotten from hpacucli controller slot=$slot show status\n");
254                 };
255         };
256
257         my $status = join(" - ", (map { $_.": ".join(", ", @{$status{$_}}) } keys %status));
258         push @resultstr, "Slot $slot: $status";
259 };
260
261 print "$EXITCODE: ", join(" --- ", @resultstr), "\n";
262 exit $CODE{$EXITCODE};