3 # Copyright (C) 2006,2008,2009,2016 Peter Palfrader <peter@palfrader.org>
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:
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
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.
25 # Need to allow /usr/local/bin/tw_cli info c0 u0 status in sudoers:
27 # nagios ALL=(ALL) NOPASSWD: /usr/local/bin/tw_cli info c0 u0 status
34 $ENV{'PATH'} = '/bin:/sbin:/usr/bin:/usr/sbin';
35 delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
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;
55 Getopt::Long::config('bundling');
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'},
64 die ("$PROGRAM_NAME: Usage: $PROGRAM_NAME [--help|--version] [--verbose] [--no-sudo] [--controller=<n>] [--unit=<n>]\n");
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";
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";
78 $SIG{'__DIE__'} = sub {
84 print "Cannot find '$TW_CLI'.\n";
88 for my $thing (qw{controller unit}) {
89 if ($params->{$thing} =~ m/^([0-9]+)$/) {
90 $params->{$thing} = $1;
92 die("Invalid $thing $1.\n");
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");
104 if ($CHILD_ERROR) { # program failed
105 die("$command returned with non-zero exit code: ".($CHILD_ERROR / 256)."\n");
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";
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;
133 $msg .= ($msg eq '' ? '' : '; '). "$device: UNKNOWN STATUS '$status'";
134 $exit = $exit > $UNKNOWN ? $exit : $UNKNOWN;
139 $msg = "No devices found";
140 die ("exit is not UNKNOWN but $exit") if ($exit != $UNKNOWN);