retire da-backup checks
[mirror/dsa-nagios.git] / dsa-nagios-checks / checks / dsa-check-raid-3ware
1 #!/usr/bin/perl -Tw
2
3 # Copyright (C) 2006,2008,2009,2016 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
25 # Need to allow /usr/local/bin/tw_cli info c0 u0 status in sudoers:
26 #
27 #  nagios          ALL=(ALL) NOPASSWD: /usr/local/bin/tw_cli info c0 u0 status
28 #
29
30 use strict;
31 use English;
32 use Getopt::Long;
33
34 $ENV{'PATH'} = '/bin:/sbin:/usr/bin:/usr/sbin';
35 delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
36
37 my $TW_CLI = '/usr/local/bin/tw_cli';
38 my $SVN_REVISION_STRING = '$Rev: 410 $';
39 my ($SVN_REVISION) = ($SVN_REVISION_STRING =~ /([0-9]+)/);
40     $SVN_REVISION  = 'unknown' unless defined $SVN_REVISION;
41 my $VERSION = '0.0.0.'.$SVN_REVISION;
42
43 # nagios exit codes
44 my $UNKNOWN = -1;
45 my $OK = 0;
46 my $WARNING = 1;
47 my $CRITICAL = 2;
48
49 my $params = {
50         'no-sudo'    => 0,
51         'controller' => 0,
52         'unit'       => 0
53         };
54
55 Getopt::Long::config('bundling');
56 if (!GetOptions (
57         '--help'                => \$params->{'help'},
58         '--version'             => \$params->{'version'},
59         '--verbose'             => \$params->{'verbose'},
60         '--controller=i'        => \$params->{'controller'},
61         '--unit=i'              => \$params->{'unit'},
62         '--no-sudo'             => \$params->{'no-sudo'},
63         )) {
64         die ("$PROGRAM_NAME: Usage: $PROGRAM_NAME [--help|--version] [--verbose] [--no-sudo] [--controller=<n>] [--unit=<n>]\n");
65 };
66 if ($params->{'help'}) {
67         print "$PROGRAM_NAME: Usage: $PROGRAM_NAME [--help|--version] [--verbose] [--no-sudo] [--controller=<n>] [--unit=<n>]\n";
68         print "Checks status of 3ware raid arrays.\n";
69         exit (0);
70 };
71 if ($params->{'version'}) {
72         print "nagios-check-raid-3ware $VERSION\n";
73         print "nagios check for 3ware raids\n";
74         print "Copyright (c) 2006,2008,2009,2016 Peter Palfrader <peter\@palfrader.org>\n";
75         exit (0);
76 };
77
78 $SIG{'__DIE__'} = sub {
79         print STDERR @_;
80         exit $UNKNOWN;
81 };
82
83 unless (-e $TW_CLI) {
84         print "Cannot find '$TW_CLI'.\n";
85         exit $UNKNOWN;
86 };
87
88 for my $thing (qw{controller unit}) {
89         if ($params->{$thing} =~ m/^([0-9]+)$/) {
90                 $params->{$thing} = $1;
91         } else {
92                 die("Invalid $thing $1.\n");
93         }
94 };
95
96 my @command;
97 push @command, "sudo" if $params->{'no-sudo'};
98 push @command, ($TW_CLI, 'info', "c$params->{'controller'}", "u$params->{'unit'}", "status");
99 my $command = join(' ', @command);
100 print STDERR "Running $command\n" if $params->{'verbose'};
101 open (TW, "-|", @command) or die ("Cannot run $command: $!\n");
102 my @tw=<TW>;
103 close TW;
104 if ($CHILD_ERROR) { # program failed
105         die("$command returned with non-zero exit code: ".($CHILD_ERROR / 256)."\n");
106 };
107
108
109 my $exit = $UNKNOWN;
110 my $msg = '';
111 for my $line (@tw)  {
112         chomp $line;
113         next if $line =~ /^$/;
114         my ($device, $status) = $line =~ m#^(/c[0-9]+/u[0-9]+) status = ([A-Z]+)$#;
115         unless (defined($device) && defined($status)) {
116                 print "Cannot parse line '$line'\n";
117                 exit $UNKNOWN;
118         };
119         if ($status eq 'OK' ||
120             $status eq 'VERIFYING') {
121                 $msg .= ($msg eq '' ? '' : '; '). "$device: $status";
122                 $exit = $exit > $OK ? $exit : $OK;
123         } elsif ($status eq 'REBUILDING') {
124                 $msg .= ($msg eq '' ? '' : '; '). "$device: $status";
125                 $exit = $exit > $WARNING ? $exit : $WARNING;
126         } elsif ($status eq 'DEGRADED') {
127                 $msg .= ($msg eq '' ? '' : '; '). "$device: $status";
128                 $exit = $exit > $CRITICAL ? $exit : $CRITICAL;
129         } elsif ($status eq 'OFFLINE') {
130                 $msg .= ($msg eq '' ? '' : '; '). "$device: $status";
131                 $exit = $exit > $CRITICAL ? $exit : $CRITICAL;
132         } else {
133                 $msg .= ($msg eq '' ? '' : '; '). "$device: UNKNOWN STATUS '$status'";
134                 $exit = $exit > $UNKNOWN ? $exit : $UNKNOWN;
135         };
136 };
137
138 if ($msg eq '') {
139         $msg = "No devices found";
140         die ("exit is not UNKNOWN but $exit") if ($exit != $UNKNOWN);
141 }
142
143 print $msg,"\n";
144 exit $exit;