retire da-backup checks
[mirror/dsa-nagios.git] / dsa-nagios-checks / checks / dsa-check-raid-mpt
1 #!/usr/bin/perl -Tw
2
3 # Copyright (C) 2008 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 # Need to allow /usr/local/bin/tw_cli info c0 u0 status in sudoers:
25 #
26 #  nagios          ALL=(ALL) NOPASSWD: /usr/sbin/mpt-status -s
27 #
28
29 use strict;
30 use English;
31 use Getopt::Long;
32
33 $ENV{'PATH'} = '/bin:/sbin:/usr/bin:/usr/sbin';
34 delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
35
36 my $MPT_STATUS = '/usr/sbin/mpt-status';
37
38 # nagios exit codes
39 my $UNKNOWN = -1;
40 my $OK = 0;
41 my $WARNING = 1;
42 my $CRITICAL = 2;
43
44 $SIG{'__DIE__'} = sub {
45         print STDERR @_;
46         exit $UNKNOWN;
47 };
48
49 unless (-e $MPT_STATUS) {
50         print "Cannot find '$MPT_STATUS'.\n";
51         exit $UNKNOWN;
52 };
53
54 my $command = "sudo $MPT_STATUS -s";
55 open (MPT, "$command|") or die ("Cannot run $command: $!\n");
56 my @tw=<MPT>;
57 close MPT;
58
59 my $command_exitcode = $CHILD_ERROR;
60
61 my $exit = $UNKNOWN;
62 my $msg = '';
63 for my $line (@tw)  {
64         chomp $line;
65         next if $line =~ /^$/;
66         my ($device, $num, $status) = $line =~ m/^(log_id|vol_id|phys_id)\s+([0-9]+)\s+(.*)$/;
67         unless (defined($device) && defined($num) && defined($status)) {
68                 print "Cannot parse line '$line'\n";
69                 exit $UNKNOWN;
70         };
71         if ($status eq 'OPTIMAL' ||
72             $status eq 'ONLINE') {
73                 $msg .= ($msg eq '' ? '' : '; '). "$device $num: $status";
74                 $exit = $exit > $OK ? $exit : $OK;
75         } else {
76                 $msg .= ($msg eq '' ? '' : '; '). "$device $num: $status";
77                 $exit = $exit > $CRITICAL ? $exit : $CRITICAL;
78         };
79 };
80
81 if ($msg eq '') {
82         $msg = "No devices found";
83         die ("exit is not UNKNOWN but $exit") if ($exit != $UNKNOWN);
84 }
85
86 if ($command_exitcode && ($exit == $OK || $exit == $UNKNOWN)) {
87         $exit = $UNKNOWN;
88         $msg .= ($msg eq '' ? '' : '; '). "$command returned with non-zero exit code: ".($command_exitcode / 256);
89 }
90
91 print $msg,"\n";
92 exit $exit;