retire da-backup checks
[mirror/dsa-nagios.git] / dsa-nagios-checks / checks / dsa-check-hpacucli-enclosure
1 #!/usr/bin/perl -w
2
3 # check enclosure status
4
5 # Copyright (c) 2008,2009,2010 Peter Palfrader <peter@palfrader.org>
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining
8 # a copy of this software and associated documentation files (the
9 # "Software"), to deal in the Software without restriction, including
10 # without limitation the rights to use, copy, modify, merge, publish,
11 # distribute, sublicense, and/or sell copies of the Software, and to
12 # permit persons to whom the Software is furnished to do so, subject to
13 # the following conditions:
14 #
15 # The above copyright notice and this permission notice shall be
16 # included in all copies or substantial portions of the Software.
17 #
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 use strict;
27 use English;
28 use Getopt::Long;
29
30 # nagios exit codes
31 my %CODE = (
32         'OK'            => 0,
33         'WARNING'       => 1,
34         'CRITICAL'      => 2,
35         'UNKNOWN'       => 3
36 );
37
38 my $EXITCODE = 'OK';
39
40 $SIG{'__DIE__'} = sub {
41         print STDERR @_;
42         exit $CODE{'UNKNOWN'};
43 };
44
45 sub runcmd($) {
46         my ($cmd) = @_;
47         $cmd = "sudo hpacucli $cmd";
48         open(FH, $cmd."|") or die ("Cannot run $cmd: $!");
49         my @lines = <FH>;
50         close FH;
51         die ("no results from $cmd\n") if (scalar @lines == 0);
52         return \@lines;
53 }
54
55 sub record($) {
56         my ($newexit) = @_;
57         die "code $newexit not defined\n" unless defined $CODE{$newexit};
58
59         if ($CODE{$newexit} > $CODE{$EXITCODE}) {
60                 $EXITCODE = $newexit;
61         };
62 }
63
64 my $usage = "$PROGRAM_NAME: Usage: $PROGRAM_NAME <controller slot> <enclosure>\n";
65 my $params;
66 Getopt::Long::Configure('bundling');
67 if (!GetOptions (
68         '--help'                      => \$params->{'help'},
69         )) {
70         die ($usage);
71 };
72 if ($params->{'help'}) {
73         print $usage;
74         exit (0);
75 };
76 die ($usage) unless (scalar @ARGV == 2);
77 my $slot = shift;
78 my $enc = shift;
79
80 my @resultstr;
81 my %status;
82 my $status = runcmd("controller slot=$slot enclosure $enc show detail");
83 for (@$status) {
84         chomp;
85         next if /^$/;
86         next if (/^\S.*in Slot $slot/);
87         next if (/^   \S.*at Port/);
88         last if (/^   \S/);
89
90         if (m/^      (Fan Status|Temperature Status):\s*(.*?)\s*$/) {
91                         my $system = $1;
92                         my $status = $2;
93                         push @{$status{$status}}, $system;
94                         if ($status ne 'OK') {
95                                 record('WARNING');
96                         };
97         } elsif (m/^      (Power Supply Status):\s*(.*?)\s*$/) {
98                         my $system = $1;
99                         my $status = $2;
100                         push @{$status{$status}}, $system;
101                         if ($status ne 'Redundant') {
102                                 record('WARNING');
103                         };
104         } elsif (m/^      (Active Path|Standby Path):\s*(.*?),\s*(.*?)\s*$/) {
105                         my $system = $1;
106                         my $detail = $2;
107                         my $status = $3;
108                         push @{$status{$status}}, $system."($detail)";
109                         if ($status ne 'OK') {
110                                 record('WARNING');
111                         };
112         } elsif (m/^(Error): (The specified device does not have a Storage Enclosure identified by.*)/) {
113                         my $status = $1;
114                         my $detail = "$enc: Unidentified Storage Enclosure";
115                         push @{$status{$status}}, $detail;
116                         record('CRITICAL');
117         } elsif (m/^(Error): (.*)/) {
118                         my $status = $1;
119                         my $detail = $2;
120                         push @{$status{$status}}, $detail;
121                         record('CRITICAL');
122         }
123
124 };
125 $status = join(" - ", (map { $_.": ".join(", ", @{$status{$_}}) } keys %status));
126 push @resultstr, "Slot $slot: $status";
127
128 print "$EXITCODE: ", join(" --- ", @resultstr), "\n";
129 exit $CODE{$EXITCODE};