Move files into specific directories in source
[mirror/dsa-nagios.git] / dsa-nagios-checks / checks / dsa-check-raid-3ware
1 #!/usr/bin/perl -Tw
2
3 # Copyright (C) 2006 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/local/bin/tw_cli info c0 u0 status
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 $TW_CLI = '/usr/local/bin/tw_cli';
18 my $SVN_REVISION_STRING = '$Rev: 313 $';
19 my ($SVN_REVISION) = ($SVN_REVISION_STRING =~ /([0-9]+)/);
20     $SVN_REVISION  = 'unknown' unless defined $SVN_REVISION;
21 my $VERSION = '0.0.0.'.$SVN_REVISION;
22
23 # nagios exit codes
24 my $UNKNOWN = -1;
25 my $OK = 0;
26 my $WARNING = 1;
27 my $CRITICAL = 2;
28
29 my $params = {
30         'no-sudo'    => 0,
31         'controller' => 0,
32         'unit'       => 0
33         };
34
35 Getopt::Long::config('bundling');
36 if (!GetOptions (
37         '--help'                => \$params->{'help'},
38         '--version'             => \$params->{'version'},
39         '--verbose'             => \$params->{'verbose'},
40         '--controller=i'        => \$params->{'controller'},
41         '--unit=i'              => \$params->{'unit'},
42         '--no-sudo'             => \$params->{'no-sudo'},
43         )) {
44         die ("$PROGRAM_NAME: Usage: $PROGRAM_NAME [--help|--version] [--verbose] [--no-sudo] [--controller=<n>] [--unit=<n>]\n");
45 };
46 if ($params->{'help'}) {
47         print "$PROGRAM_NAME: Usage: $PROGRAM_NAME [--help|--version] [--verbose] [--no-sudo] [--controller=<n>] [--unit=<n>]\n";
48         print "Checks status of 3ware raid arrays.\n";
49         exit (0);
50 };
51 if ($params->{'version'}) {
52         print "nagios-check-raid-3ware $VERSION\n";
53         print "nagios check for 3ware raids\n";
54         print "Copyright (c) 2006 Peter Palfrader <peter\@palfrader.org>\n";
55         exit (0);
56 };
57
58 $SIG{'__DIE__'} = sub {
59         print STDERR @_;
60         exit $UNKNOWN;
61 };
62
63 unless (-e $TW_CLI) {
64         print "Cannot find '$TW_CLI'.\n";
65         exit $UNKNOWN;
66 };
67
68 my $sudo = $params->{'no-sudo'} ? '' : 'sudo ';
69 my $command = "$sudo $TW_CLI info c$params->{'controller'} u$params->{'unit'} status";
70 print STDERR "Running $command\n" if $params->{'verbose'};
71 open (TW, "$command|") or die ("Cannot run $command: $!\n");
72 my @tw=<TW>;
73 close TW;
74 if ($CHILD_ERROR) { # program failed
75         die("$command returned with non-zero exit code: ".($CHILD_ERROR / 256)."\n");
76 };
77
78
79 my $exit = $UNKNOWN;
80 my $msg = '';
81 for my $line (@tw)  {
82         chomp $line;
83         next if $line =~ /^$/;
84         my ($device, $status) = $line =~ m#^(/c[0-9]+/u[0-9]+) status = ([A-Z]+)$#;
85         unless (defined($device) && defined($status)) {
86                 print "Cannot parse line '$line'\n";
87                 exit $UNKNOWN;
88         };
89         if ($status eq 'OK' ||
90             $status eq 'VERIFYING') {
91                 $msg .= ($msg eq '' ? '' : '; '). "$device: $status";
92                 $exit = $exit > $OK ? $exit : $OK;
93         } elsif ($status eq 'DEGRADED') {
94                 $msg .= ($msg eq '' ? '' : '; '). "$device: $status";
95                 $exit = $exit > $CRITICAL ? $exit : $CRITICAL;
96         } elsif ($status eq 'OFFLINE') {
97                 $msg .= ($msg eq '' ? '' : '; '). "$device: $status";
98                 $exit = $exit > $CRITICAL ? $exit : $CRITICAL;
99         } else {
100                 $msg .= ($msg eq '' ? '' : '; '). "$device: UNKNOWN STATUS '$status'";
101                 $exit = $exit > $UNKNOWN ? $exit : $UNKNOWN;
102         };
103 };
104
105 if ($msg eq '') {
106         $msg = "No devices found";
107         die ("exit is not UNKNOWN but $exit") if ($exit != $UNKNOWN);
108 }
109
110 print $msg,"\n";
111 exit $exit;