Looks like I lost a sudo during testing
[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 # Need to allow /usr/local/bin/tw_cli info c0 u0 status in sudoers:
6 #
7 #  nagios          ALL=(ALL) NOPASSWD: /usr/sbin/mpt-status -s
8 #
9
10 use strict;
11 use English;
12 use Getopt::Long;
13
14 $ENV{'PATH'} = '/bin:/sbin:/usr/bin:/usr/sbin';
15 delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
16
17 my $MPT_STATUS = 'sudo /usr/sbin/mpt-status';
18
19 # nagios exit codes
20 my $UNKNOWN = -1;
21 my $OK = 0;
22 my $WARNING = 1;
23 my $CRITICAL = 2;
24
25 $SIG{'__DIE__'} = sub {
26         print STDERR @_;
27         exit $UNKNOWN;
28 };
29
30 unless (-e $MPT_STATUS) {
31         print "Cannot find '$MPT_STATUS'.\n";
32         exit $UNKNOWN;
33 };
34
35 my $command = "$MPT_STATUS -s";
36 open (MPT, "$command|") or die ("Cannot run $command: $!\n");
37 my @tw=<MPT>;
38 close MPT;
39
40 my $command_exitcode = $CHILD_ERROR;
41
42 my $exit = $UNKNOWN;
43 my $msg = '';
44 for my $line (@tw)  {
45         chomp $line;
46         next if $line =~ /^$/;
47         my ($device, $num, $status) = $line =~ m/^(log_id|vol_id|phys_id)\s+([0-9]+)\s+(.*)$/;
48         unless (defined($device) && defined($num) && defined($status)) {
49                 print "Cannot parse line '$line'\n";
50                 exit $UNKNOWN;
51         };
52         if ($status eq 'OPTIMAL' ||
53             $status eq 'ONLINE') {
54                 $msg .= ($msg eq '' ? '' : '; '). "$device $num: $status";
55                 $exit = $exit > $OK ? $exit : $OK;
56         } else {
57                 $msg .= ($msg eq '' ? '' : '; '). "$device $num: $status";
58                 $exit = $exit > $CRITICAL ? $exit : $CRITICAL;
59         };
60 };
61
62 if ($msg eq '') {
63         $msg = "No devices found";
64         die ("exit is not UNKNOWN but $exit") if ($exit != $UNKNOWN);
65 }
66
67 if ($command_exitcode && ($exit == $OK || $exit == $UNKNOWN)) {
68         $exit = $UNKNOWN;
69         $msg .= ($msg eq '' ? '' : '; '). "$command returned with non-zero exit code: ".($command_exitcode / 256);
70 }
71
72 print $msg,"\n";
73 exit $exit;