[project @ peter@palfrader.org-20080509125524-spvutul11i214x23]
[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 $nodrives = 0;
88         my %status;
89         for (@$pds) {
90                 chomp;
91                 next if /^$/;
92                 next if /^ *array [A-Z]$/;
93                 next if (/^\S.*in Slot $slot/);
94                 if (/^Error: The specified controller does not have any physical drives on it.$/) {
95                         $nodrives = 1;
96                 } elsif (/^ *physicaldrive (\S+) .* (OK|Predictive Failure|Failed|Rebuilding)(?:, spare)?\)$/) {
97                         my $drive = $1;
98                         my $status = $2;
99                         push @{$status{$status}}, $drive;
100                         if ($status eq 'OK') {
101                         } elsif ($status eq 'Predictive Failure' ||
102                                  $status eq 'Rebuilding') {
103                                 record('WARNING');
104                         } elsif ($status eq 'Failed') {
105                                 record('CRITICAL');
106                         } else {
107                                 record('UNKNOWN');
108                         };      
109                 } else {
110                         die ("Cannot read line '$_' gotten from hpacucli controller slot=$slot pd all show\n");
111                 };
112         };
113
114         if ($nodrives && scalar keys %status > 0) {
115                 push @resultstr, "Slot $slot: have no drives but status results?";
116                 record('UNKNOWN');
117                 next;
118         } elsif ($nodrives) {
119                 push @resultstr, "Slot $slot: no drives";
120                 next;
121         };
122
123         my $cst = runcmd("controller slot=$slot show status");
124         for (@$cst) {
125                 chomp;
126                 next if /^$/;
127                 next if (/^\S.*in Slot $slot/);
128                 if (/^ *(.*) Status: (.*)$/) {
129                         my $system = $1;
130                         my $status = $2;
131                         push @{$status{$status}}, $system;
132                         if ($status ne 'OK') {
133                                 record('WARNING');
134                         };
135                 } else {
136                         die ("Cannot read line '$_' gotten from hpacucli controller slot=$slot show status\n");
137                 };
138         };
139
140         my $status = join(" - ", (map { $_.": ".join(", ", @{$status{$_}}) } keys %status));
141         push @resultstr, "Slot $slot: $status";
142 };
143
144 print "$EXITCODE: ", join(" --- ", @resultstr), "\n";
145 exit $CODE{$EXITCODE};