[project @ peter@palfrader.org-20080507195426-zzl3uubufrngcsjs]
[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)(?:, 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                                 record('WARNING');
103                         } elsif ($status eq 'Failed') {
104                                 record('CRITICAL');
105                         } else {
106                                 record('UNKNOWN');
107                         };      
108                 } else {
109                         die ("Cannot read line '$_' gotten from hpacucli controller slot=$slot pd all show\n");
110                 };
111         };
112
113         if ($nodrives && scalar keys %status > 0) {
114                 push @resultstr, "Slot $slot: have no drives but status results?";
115                 record('UNKNOWN');
116                 next;
117         } elsif ($nodrives) {
118                 push @resultstr, "Slot $slot: no drives";
119                 next;
120         };
121
122         my $cst = runcmd("controller slot=$slot show status");
123         for (@$cst) {
124                 chomp;
125                 next if /^$/;
126                 next if (/^\S.*in Slot $slot/);
127                 if (/^ *(.*) Status: (.*)$/) {
128                         my $system = $1;
129                         my $status = $2;
130                         push @{$status{$status}}, $system;
131                         if ($status ne 'OK') {
132                                 record('WARNING');
133                         };
134                 } else {
135                         die ("Cannot read line '$_' gotten from hpacucli controller slot=$slot show status\n");
136                 };
137         };
138
139         my $status = join("; ", (map { $_.": ".join(", ", @{$status{$_}}) } keys %status));
140         push @resultstr, "Slot $slot: $status";
141 };
142
143 print "$EXITCODE: ", join(" - ", @resultstr), "\n";
144 exit $CODE{$EXITCODE};