[project @ peter@palfrader.org-20080722225740-scxy8ry0d8swsc7y]
[mirror/dsa-nagios.git] / dsa-nagios-nrpe-config / 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 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
31 # nagios exit codes
32 my %CODE = (
33         'OK'            => 0,
34         'WARNING'       => 1,
35         'CRITICAL'      => 2,
36         'UNKNOWN'       => 3
37 );
38
39 my $EXITCODE = 'OK';
40
41 $SIG{'__DIE__'} = sub {
42         print STDERR @_;
43         exit $CODE{'UNKNOWN'};
44 };
45
46 sub runcmd($) {
47         my ($cmd) = @_;
48         $cmd = "sudo hpacucli $cmd";
49         open(FH, $cmd."|") or die ("Cannot run $cmd: $!");
50         my @lines = <FH>;
51         close FH;
52         die ("no results from $cmd\n") if (scalar @lines == 0);
53         return \@lines;
54 }
55
56 sub record($) {
57         my ($newexit) = @_;
58         die "code $newexit not defined\n" unless defined $CODE{$newexit};
59
60         if ($CODE{$newexit} > $CODE{$EXITCODE}) {
61                 $EXITCODE = $newexit;
62         };
63 }
64
65
66 my $ctrlallshow = runcmd("controller all show");
67 my @controllers;
68 for (@$ctrlallshow) {
69         chomp;
70         next if /^$/;
71         if (/in Slot ([0-9]+) /) {
72                 push @controllers, $1;
73                 next;
74         };
75         die ("Cannot read line '$_' gotten from hpacucli controller all show\n");
76 };
77
78 if (scalar @controllers == 0) {
79         print "UNKNONW: No smartarray controllers found with hpacucli\n";
80         exit $CODE{'UNKNOWN'}
81 };
82
83 my @resultstr;
84
85 for my $slot (sort @controllers) {
86         my $pds = runcmd("controller slot=$slot pd all show");
87         my @drives;
88         my $nodrives = 0;
89         my %status;
90         for (@$pds) {
91                 chomp;
92                 next if /^$/;
93                 next if /^ *array [A-Z]$/;
94                 next if (/^\S.*in Slot $slot/);
95                 if (/^Error: The specified controller does not have any physical drives on it.$/) {
96                         $nodrives = 1;
97                 } elsif (/^ *physicaldrive (\S+) .* (OK|Predictive Failure|Failed|Rebuilding)(?:, spare)?\)$/) {
98                         my $drive = $1;
99                         my $status = $2;
100                         push @{$status{$status}}, $drive;
101                         if ($status eq 'OK') {
102                         } elsif ($status eq 'Predictive Failure' ||
103                                  $status eq 'Rebuilding') {
104                                 record('WARNING');
105                         } elsif ($status eq 'Failed') {
106                                 record('CRITICAL');
107                         } else {
108                                 record('UNKNOWN');
109                         };      
110                         push @drives, $drive;
111                 } else {
112                         die ("Cannot read line '$_' gotten from hpacucli controller slot=$slot pd all show\n");
113                 };
114         };
115
116         # Check that all drives have the proper transfer speed.
117         # sometimes stuff breaks and they fall back to 10mb/sec.
118         for my $drive (@drives) {
119                 my $type;
120                 if ($drive =~ /^[0-9]+:[0-9]+$/) { # scsi drives
121                         $type = 'SCSI';
122                 } elsif ($drive =~ /^[0-9]+I:[0-9]+:[0-9]+$/) { # SAS
123                         $type = 'SAS';
124                 } else {
125                         # I'm not going to run pass arguments of unknown form to the shell..
126                         warn ("Unknown diskdrive ID $drive\n");
127                         next;
128                 }
129
130                 my $pd = runcmd("controller slot=$slot pd $drive show");
131                 while (defined $pd->[0] && !($pd->[0] =~ /physicaldrive/)) {
132                         shift @$pd;
133                 };
134                 shift @$pd;
135                 my %value;
136                 for (@$pd) {
137                         if (m/^\s*(.*?):\s*(.*?)\s*$/) {
138                                 $value{$1} = $2;
139                         }
140                 }
141
142                 my $key;
143                 my $expected;
144                 if ($type eq 'SCSI') {
145                         $key = 'Transfer Speed';
146                         if (!defined $value{'Transfer Mode'}) {
147                                 record('WARNING');
148                                 push @{$status{'unknown transfer mode'}}, $drive;
149                                 next;
150                         } elsif ($value{'Transfer Mode'} eq 'Ultra 3 Wide') {
151                                 $expected = '160 MB/Sec';
152                         } elsif ($value{'Transfer Mode'} eq 'Ultra 320 Wide') {
153                                 $expected = '320 MB/Sec';
154                         } else {
155                                 record('WARNING');
156                                 push @{$status{'unknown transfer mode'}}, $drive."(".$value{'Transfer Mode'}.")";
157                                 next;
158                         };
159                 } elsif ($type eq 'SAS') {
160                         $key = 'PHY Transfer Rate';
161                         $expected = '3.0GBPS';
162                 } else {
163                         warn "Should not be here.  Do not know what to do with type '$type'\n";
164                         next;
165                 }
166
167                 if (!defined $value{$key}) {
168                         record('WARNING');
169                         push @{$status{'unknown transfer speed'}}, $drive;
170                 } elsif ($value{$key} ne $expected) {
171                         record('WARNING');
172                         push @{$status{'bad transfer speed'}}, $drive."(".$value{$key}.")";
173                 };
174         };
175
176         if ($nodrives && scalar keys %status > 0) {
177                 push @resultstr, "Slot $slot: have no drives but status results?";
178                 record('UNKNOWN');
179                 next;
180         } elsif ($nodrives) {
181                 push @resultstr, "Slot $slot: no drives";
182                 next;
183         };
184
185         my $cst = runcmd("controller slot=$slot show status");
186         for (@$cst) {
187                 chomp;
188                 next if /^$/;
189                 next if (/^\S.*in Slot $slot/);
190                 if (/^ *(.*) Status: (.*)$/) {
191                         my $system = $1;
192                         my $status = $2;
193                         push @{$status{$status}}, $system;
194                         if ($status ne 'OK') {
195                                 record('WARNING');
196                         };
197                 } else {
198                         die ("Cannot read line '$_' gotten from hpacucli controller slot=$slot show status\n");
199                 };
200         };
201
202         my $status = join(" - ", (map { $_.": ".join(", ", @{$status{$_}}) } keys %status));
203         push @resultstr, "Slot $slot: $status";
204 };
205
206 print "$EXITCODE: ", join(" --- ", @resultstr), "\n";
207 exit $CODE{$EXITCODE};