No longer install nrpe_dsa.cfg
[mirror/dsa-nagios.git] / dsa-nagios-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 = '/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 = "sudo $MPT_STATUS -s";
36 open (MPT, "$command|") or die ("Cannot run $command: $!\n");
37 my @tw=<MPT>;
38 close MPT;
39 if ($CHILD_ERROR) { # program failed
40         die("$command returned with non-zero exit code: ".($CHILD_ERROR / 256)."\n");
41 };
42
43
44 my $exit = $UNKNOWN;
45 my $msg = '';
46 for my $line (@tw)  {
47         chomp $line;
48         next if $line =~ /^$/;
49         my ($device, $num, $status) = $line =~ m#^(log_id|vol_id|phys_id)\s+([0-9]+)\s+(.*)$#;
50         unless (defined($device) && defined($num) && defined($status)) {
51                 print "Cannot parse line '$line'\n";
52                 exit $UNKNOWN;
53         };
54         if ($status eq 'OPTIMAL' ||
55             $status eq 'ONLINE') {
56                 $msg .= ($msg eq '' ? '' : '; '). "$device $num: $status";
57                 $exit = $exit > $OK ? $exit : $OK;
58         } else {
59                 $msg .= ($msg eq '' ? '' : '; '). "$device $num: $status";
60                 $exit = $exit > $CRITICAL ? $exit : $CRITICAL;
61         };
62 };
63
64 if ($msg eq '') {
65         $msg = "No devices found";
66         die ("exit is not UNKNOWN but $exit") if ($exit != $UNKNOWN);
67 }
68
69 print $msg,"\n";
70 exit $exit;