hpacucli does not know about kernel 3.x, but with setarch from util-linux we can...
[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 use POSIX qw(uname);
33
34 # nagios exit codes
35 my %CODE = (
36         'OK'            => 0,
37         'WARNING'       => 1,
38         'CRITICAL'      => 2,
39         'UNKNOWN'       => 3
40 );
41
42 my $EXITCODE = 'OK';
43
44 $SIG{'__DIE__'} = sub {
45         print STDERR @_;
46         exit $CODE{'UNKNOWN'};
47 };
48
49 sub runcmd($) {
50         my ($cmd) = @_;
51         my ($os, $host, $kernel, $date, $arch) = uname();
52         if ($kernel =~ m/^3(.*)/) {
53                 $cmd = "sudo setarch x86_64 --uname-2.6 hpacucli $cmd";
54         } else {
55                 $cmd = "sudo hpacucli $cmd";
56         }
57         open(FH, $cmd."|") or die ("Cannot run $cmd: $!");
58         my @lines = <FH>;
59         close FH;
60         die ("no results from $cmd\n") if (scalar @lines == 0);
61         return \@lines;
62 }
63
64 sub record($) {
65         my ($newexit) = @_;
66         die "code $newexit not defined\n" unless defined $CODE{$newexit};
67
68         if ($CODE{$newexit} > $CODE{$EXITCODE}) {
69                 $EXITCODE = $newexit;
70         };
71 }
72
73 my $usage = "$PROGRAM_NAME: Usage: $PROGRAM_NAME [--no-battery] [--ignore-transfer-speed=<pd> [--ignore-transfer-speed=<pd> ...]]\n";
74 my $params;
75 Getopt::Long::Configure('bundling');
76 if (!GetOptions (
77         '--help'                      => \$params->{'help'},
78         '--no-battery'                => \$params->{'no-battery'},
79         '--ignore-transfer-speed=s@'  => \$params->{'ignore-transfer-speed'},
80         )) {
81         die ($usage);
82 };
83 if ($params->{'help'}) {
84         print $usage;
85         exit (0);
86 };
87 die ($usage) unless (scalar @ARGV == 0);
88
89 my $ctrlallshow = runcmd("controller all show");
90 my @controllers;
91 for (@$ctrlallshow) {
92         chomp;
93         next if /^$/;
94         if (/in Slot ([0-9a-z]+)/) {
95                 push @controllers, $1;
96                 next;
97         };
98         die ("Cannot read line '$_' gotten from hpacucli controller all show\n");
99 };
100
101 if (scalar @controllers == 0) {
102         print "UNKNONW: No smartarray controllers found with hpacucli\n";
103         exit $CODE{'UNKNOWN'}
104 };
105
106 my @resultstr;
107
108 for my $slot (sort @controllers) {
109         my $pds = runcmd("controller slot=$slot pd all show");
110         my @drives;
111         my $nodrives = 0;
112         my %status;
113         for (@$pds) {
114                 chomp;
115                 next if /^$/;
116                 next if (/^\S.*in Slot $slot/);
117                 next if /^ *array [A-Z]$/;
118                 next if /^ *unassigned/;
119                 if (/^ *(array [A-Z]) \(Failed\)$/) {
120                         record('CRITICAL');
121                         push @{$status{'Failed'}}, $1;
122                 } elsif (/^Error: The specified controller does not have any physical drives on it.$/) {
123                         $nodrives = 1;
124                 } elsif (/^ *physicaldrive (\S+) .* (OK|Predictive Failure|Failed|Rebuilding)(?:, (?:active )?spare)?\)$/) {
125                         my $drive = $1;
126                         my $status = $2;
127                         push @{$status{$status}}, $drive;
128                         if ($status eq 'OK') {
129                         } elsif ($status eq 'Predictive Failure' ||
130                                  $status eq 'Rebuilding') {
131                                 record('WARNING');
132                         } elsif ($status eq 'Failed') {
133                                 record('CRITICAL');
134                         } else {
135                                 record('UNKNOWN');
136                         };      
137                         push @drives, $drive;
138                 } else {
139                         die ("Cannot read line '$_' gotten from hpacucli controller slot=$slot pd all show\n");
140                 };
141         };
142
143         # Check that all drives have the proper transfer speed.
144         # sometimes stuff breaks and they fall back to 10mb/sec.
145         for my $drive (@drives) {
146                 # skip drives that are known to have failed
147                 next if (exists $status{'Failed'} && grep {$drive eq $_} @{$status{'Failed'}});
148                 my $type;
149                 if ($drive =~ /^[0-9]+:[0-9]+$/) { # scsi drives
150                         $type = 'SCSI';
151                 } elsif ($drive =~ /^[0-9]+[EI]:[0-9]+:[0-9]+$/) { # SAS
152                         $type = 'SAS';
153                 } elsif ($drive =~ /^[0-9]+[C]:[0-9]+:[0-9]+$/) { # New 6GBPS SAS
154                         $type = 'SAS+';
155                 } else {
156                         # I'm not going to run pass arguments of unknown form to the shell..
157                         warn ("Unknown diskdrive ID $drive\n");
158                         next;
159                 }
160
161                 my $pd = runcmd("controller slot=$slot pd $drive show");
162                 while (defined $pd->[0] && !($pd->[0] =~ /physicaldrive/)) {
163                         shift @$pd;
164                 };
165                 shift @$pd;
166                 my %value;
167                 for (@$pd) {
168                         if (m/^\s*(.*?):\s*(.*?)\s*$/) {
169                                 $value{$1} = $2;
170                         }
171                 }
172
173                 my $key;
174                 my $expected;
175                 if ($type eq 'SCSI') {
176                         $key = 'Transfer Speed';
177                         if (!defined $value{'Transfer Mode'}) {
178                                 record('WARNING');
179                                 push @{$status{'unknown transfer mode'}}, $drive;
180                                 next;
181                         } elsif ($value{'Transfer Mode'} eq 'Ultra 3 Wide') {
182                                 $expected = '160 MB/Sec';
183                         } elsif ($value{'Transfer Mode'} eq 'Ultra 320 Wide') {
184                                 $expected = '320 MB/Sec';
185                         } else {
186                                 record('WARNING');
187                                 push @{$status{'unknown transfer mode'}}, $drive."(".$value{'Transfer Mode'}.")";
188                                 next;
189                         };
190                 } elsif ($type eq 'SAS' || $type eq 'SAS+') {
191                         $key = 'PHY Transfer Rate';
192                         if ($value{'PHY Count'} eq '2') {
193                                 if (defined($value{'Redundant Path(s)'})) {
194                                         $expected = [ '3.0GBPS, 3.0GBPS', '6.0GBPS, 6.0GBPS' ];
195                                 } else {
196                                         $expected = [ '3.0GBPS, Unknown', 'Unknown, 3.0GBPS',
197                                                       '6.0GBPS, Unknown', 'Unknown, 6.0GBPS' ];
198                                 }
199                         } else {
200                                 $expected = [ '3.0GBPS', '6.0GBPS' ];
201                         }
202                 } else {
203                         warn "Should not be here.  Do not know what to do with type '$type'\n";
204                         next;
205                 }
206
207                 if ($params->{'ignore-transfer-speed'}) {
208                         if (grep { $drive eq $_ } @{$params->{'ignore-transfer-speed'}}) {
209                                 push @{$status{'ignored transfer speed'}}, $drive."(".$value{$key}.")";
210                                 next;
211                         };
212                 };
213                 if (!defined $value{$key}) {
214                         record('WARNING');
215                         push @{$status{'unknown transfer speed'}}, $drive;
216                 } elsif (ref($expected) eq 'ARRAY') {
217                         if (scalar(grep { $value{$key} eq $_ } @$expected) == 0) {
218                                 record('WARNING');
219                                 push @{$status{'bad transfer speed'}}, $drive."(".$value{$key}.")";
220                         };
221                 } elsif ($value{$key} ne $expected) {
222                         record('WARNING');
223                         push @{$status{'bad transfer speed'}}, $drive."(".$value{$key}.")";
224                 };
225         };
226
227         if ($nodrives && scalar keys %status > 0) {
228                 push @resultstr, "Slot $slot: have no drives but status results?";
229                 record('UNKNOWN');
230                 next;
231         } elsif ($nodrives) {
232                 push @resultstr, "Slot $slot: no drives";
233                 next;
234         };
235
236         my $cst = runcmd("controller slot=$slot show status");
237         for (@$cst) {
238                 chomp;
239                 next if /^$/;
240                 next if (/^\S.*in Slot $slot/);
241                 if (/^ *(.*) Status: (.*)$/) {
242                         my $system = $1;
243                         my $status = $2;
244                         push @{$status{$status}}, $system;
245                         if ($status ne 'OK') {
246                                 next if ($params->{'no-battery'} && $system eq 'Cache');
247                                 next if ($params->{'no-battery'} && $system eq 'Battery/Capacitor');
248                                 record('WARNING');
249                         };
250                 } else {
251                         die ("Cannot read line '$_' gotten from hpacucli controller slot=$slot show status\n");
252                 };
253         };
254
255         my $status = join(" - ", (map { $_.": ".join(", ", @{$status{$_}}) } keys %status));
256         push @resultstr, "Slot $slot: $status";
257 };
258
259 print "$EXITCODE: ", join(" --- ", @resultstr), "\n";
260 exit $CODE{$EXITCODE};