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