retire da-backup checks
[mirror/dsa-nagios.git] / dsa-nagios-checks / checks / dsa-check-raid-aacraid
1 #!/usr/bin/perl -w
2
3 # Copyright (c) 2008,2009 Peter Palfrader <peter@palfrader.org>
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24 use strict;
25 use File::Temp qw/ tempdir /;
26
27 # nagios exit codes
28 my %CODE = (
29         'OK'            => 0,
30         'WARNING'       => 1,
31         'CRITICAL'      => 2,
32         'UNKNOWN'       => 3
33 );
34
35 my $EXITCODE = 'OK';
36
37 $SIG{'__DIE__'} = sub {
38         print STDERR @_;
39         exit $CODE{'UNKNOWN'};
40 };
41
42 sub runcmd($) {
43         my ($cmd) = @_;
44         $cmd = "sudo arcconf $cmd";
45         open(FH, $cmd."|") or die ("Cannot run $cmd: $!");
46         my @lines = <FH>;
47         close FH;
48         die ("no results from $cmd\n") if (scalar @lines == 0);
49         return \@lines;
50 }
51
52 sub record($) {
53         my ($newexit) = @_;
54         die "code $newexit not defined\n" unless defined $CODE{$newexit};
55
56         if ($CODE{$newexit} > $CODE{$EXITCODE}) {
57                 $EXITCODE = $newexit;
58         };
59 }
60
61 # arcconf puts crap into a $PWD/UcliEvt.log file.
62 my $dir = tempdir( "/tmp/check-aacraid-XXXXXXX", CLEANUP => 1 );
63 chdir ($dir) or die ("Cannot chdir $dir: $!\n");
64
65 my $ctrl = 1;
66 my @resultstr;
67 my $numcontrollers = 1;
68 while ($ctrl <= $numcontrollers) {
69         my $lds = runcmd("GETCONFIG $ctrl LD");
70         my %status;
71         my $ld = "unknown";
72         my @extrastatus = ();
73         for (@$lds) {
74                 chomp;
75                 if (/^Controllers found: *([0-9]+)/) {
76                         $numcontrollers = $1;
77                 } elsif (/^Logical device number (.*)/) {
78                         $ld = "LD$1";
79                 } elsif (/^ *Status of logical device *: *(.*)/) {
80                         my $status = $1;
81                         if ($status ne 'Optimal') {
82                                 record('WARNING');
83                         };
84                         push @{$status{$status}}, $ld;
85                 };
86         };
87
88         my $adi = runcmd("GETCONFIG $ctrl AD");
89         for (@$adi) {
90                 chomp;
91                 if (/^ *Controller Status *: *(.*)/) {
92                         my $status = $1;
93                         if ($status ne 'Optimal') {
94                                 record('WARNING');
95                         };
96                         push @{$status{$status}}, "controller";
97                 } elsif (/^ *Defunct disk drive count *: *(.*)/) {
98                         my $count  = $1;
99                         if ($count > 0) {
100                                 record('WARNING');
101                                 push @extrastatus, "$count defunct drives";
102                         }
103                 } elsif (m#^ *Logical devices/Failed/Degraded *: (.*)/(.*)/(.*)#) {
104                         my $total = $1;
105                         my $failed = $2;
106                         my $degraded = $3;
107                         push @extrastatus, "$total/$failed/$degraded LDs total/failed/degraded";
108                         if ($failed > 0) {
109                                 record('CRITICAL');
110                         } elsif ($degraded > 0) {
111                                 record('WARNING');
112                         }
113                 } elsif (/.*Controller Battery Information/) {
114                         last;
115                 };
116         };
117         # in Controller Battery Information
118         for (@$adi) {
119                 chomp;
120                 if (/^ *Status *: *(.*)/) {
121                         my $status = $1;
122                         if ($status eq 'Not Installed') {
123                                 next;
124                         } elsif ($status ne 'Optimal' && $status ne 'ZMM Optimal') {
125                                 record('WARNING');
126                         };
127                         push @{$status{$status}}, 'Battery';
128                 }
129         }
130
131         my $status = join(" - ", (map { $_.": ".join(", ", @{$status{$_}}) } keys %status));
132         if (scalar @extrastatus > 0) {
133                 $status .= ", ".join(", ", @extrastatus);
134         }
135         push @resultstr, "Ctrl $ctrl: $status";
136         $ctrl++;
137 };
138
139 if ( -e 'UcliEvt.log' ) {
140         unlink('UcliEvt.log') or die ("Cannot unlink UcliEvt.log: $!\n");
141 }
142
143 # Need to chdir out of tempdir in order to rm it in new perl
144 chdir ('/') or die ("Cannot chdir /: $!\n");
145
146 print "$EXITCODE: ", join(" --- ", @resultstr), "\n";
147 exit $CODE{$EXITCODE};