Move files into specific directories in source
[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 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 (/^\S.*in Slot $slot/);
94                 next if /^ *array [A-Z]$/;
95                 if (/^ *(array [A-Z]) \(Failed\)$/) {
96                         record('CRITICAL');
97                         push @{$status{'Failed'}}, $1;
98                 } elsif (/^Error: The specified controller does not have any physical drives on it.$/) {
99                         $nodrives = 1;
100                 } elsif (/^ *physicaldrive (\S+) .* (OK|Predictive Failure|Failed|Rebuilding)(?:, spare)?\)$/) {
101                         my $drive = $1;
102                         my $status = $2;
103                         push @{$status{$status}}, $drive;
104                         if ($status eq 'OK') {
105                         } elsif ($status eq 'Predictive Failure' ||
106                                  $status eq 'Rebuilding') {
107                                 record('WARNING');
108                         } elsif ($status eq 'Failed') {
109                                 record('CRITICAL');
110                         } else {
111                                 record('UNKNOWN');
112                         };      
113                         push @drives, $drive;
114                 } else {
115                         die ("Cannot read line '$_' gotten from hpacucli controller slot=$slot pd all show\n");
116                 };
117         };
118
119         # Check that all drives have the proper transfer speed.
120         # sometimes stuff breaks and they fall back to 10mb/sec.
121         for my $drive (@drives) {
122                 # skip drives that are known to have failed
123                 next if (exists $status{'Failed'} && grep {$drive eq $_} @{$status{'Failed'}});
124                 my $type;
125                 if ($drive =~ /^[0-9]+:[0-9]+$/) { # scsi drives
126                         $type = 'SCSI';
127                 } elsif ($drive =~ /^[0-9]+I:[0-9]+:[0-9]+$/) { # SAS
128                         $type = 'SAS';
129                 } else {
130                         # I'm not going to run pass arguments of unknown form to the shell..
131                         warn ("Unknown diskdrive ID $drive\n");
132                         next;
133                 }
134
135                 my $pd = runcmd("controller slot=$slot pd $drive show");
136                 while (defined $pd->[0] && !($pd->[0] =~ /physicaldrive/)) {
137                         shift @$pd;
138                 };
139                 shift @$pd;
140                 my %value;
141                 for (@$pd) {
142                         if (m/^\s*(.*?):\s*(.*?)\s*$/) {
143                                 $value{$1} = $2;
144                         }
145                 }
146
147                 my $key;
148                 my $expected;
149                 if ($type eq 'SCSI') {
150                         $key = 'Transfer Speed';
151                         if (!defined $value{'Transfer Mode'}) {
152                                 record('WARNING');
153                                 push @{$status{'unknown transfer mode'}}, $drive;
154                                 next;
155                         } elsif ($value{'Transfer Mode'} eq 'Ultra 3 Wide') {
156                                 $expected = '160 MB/Sec';
157                         } elsif ($value{'Transfer Mode'} eq 'Ultra 320 Wide') {
158                                 $expected = '320 MB/Sec';
159                         } else {
160                                 record('WARNING');
161                                 push @{$status{'unknown transfer mode'}}, $drive."(".$value{'Transfer Mode'}.")";
162                                 next;
163                         };
164                 } elsif ($type eq 'SAS') {
165                         $key = 'PHY Transfer Rate';
166                         if ($value{'PHY Count'} eq '2') {
167                                 $expected = '3.0GBPS, Unknown';
168                         } else {
169                                 $expected = '3.0GBPS';
170                         }
171                 } else {
172                         warn "Should not be here.  Do not know what to do with type '$type'\n";
173                         next;
174                 }
175
176                 if (!defined $value{$key}) {
177                         record('WARNING');
178                         push @{$status{'unknown transfer speed'}}, $drive;
179                 } elsif ($value{$key} ne $expected) {
180                         record('WARNING');
181                         push @{$status{'bad transfer speed'}}, $drive."(".$value{$key}.")";
182                 };
183         };
184
185         if ($nodrives && scalar keys %status > 0) {
186                 push @resultstr, "Slot $slot: have no drives but status results?";
187                 record('UNKNOWN');
188                 next;
189         } elsif ($nodrives) {
190                 push @resultstr, "Slot $slot: no drives";
191                 next;
192         };
193
194         my $cst = runcmd("controller slot=$slot show status");
195         for (@$cst) {
196                 chomp;
197                 next if /^$/;
198                 next if (/^\S.*in Slot $slot/);
199                 if (/^ *(.*) Status: (.*)$/) {
200                         my $system = $1;
201                         my $status = $2;
202                         push @{$status{$status}}, $system;
203                         if ($status ne 'OK') {
204                                 record('WARNING');
205                         };
206                 } else {
207                         die ("Cannot read line '$_' gotten from hpacucli controller slot=$slot show status\n");
208                 };
209         };
210
211         my $status = join(" - ", (map { $_.": ".join(", ", @{$status{$_}}) } keys %status));
212         push @resultstr, "Slot $slot: $status";
213 };
214
215 print "$EXITCODE: ", join(" --- ", @resultstr), "\n";
216 exit $CODE{$EXITCODE};