#!/usr/bin/perl # # $Id: nagios_check_twcli 700 2007-10-30 18:08:03Z root $ use strict; use lib "/usr/local/libexec/nagios"; use English; use utils qw (%ERRORS &print_revision &support); my $tw_cli = '/usr/local/sbin/tw_cli'; my @units_fail = (); my @units_ok = (); my %to_check = (); my %unit_types = (); my $check_all = 0; if ( int(@ARGV) == 0 or $ARGV[0] eq '-h' ) { print "Usage: $0 (-a | cX[:uX] [cX[:uX] ..])\n"; print "\t-a\tCheck all units on all controllers\n"; print "\tcX\tController specifier (eg. c0, c1..)\n"; print "\tuX\tUnit number (eg. u0, u1..)\n"; print "When no units are specified after a colon, all units will be checked.\n"; exit $ERRORS{'UNKNOWN'}; } if ( $ARGV[0] eq '-a' ) { $check_all = 1; } else { while ( int(@ARGV) > 0 ) { unless ( $ARGV[0] =~ /^(c\d+)(?::(u\d+))?$/ ) { print "Invalid characters in unit list, bailing out\n"; exit $ERRORS{'UNKNOWN'}; } if ( defined $2 ) { unless ( $to_check{$1} eq "all" ) { defined $to_check{$1} or $to_check{$1} = (); push @{$to_check{$1}}, $2; } } else { $to_check{$1} = "all"; } shift @ARGV; } } if ( $UID != 0 ) { print "NOT ROOT: this check is supposed to run with UID=0\n"; exit $ERRORS{'UNKNOWN'} } my $controllers = &get_controllers; if ( $check_all == 1 ) { foreach my $controller (keys %$controllers) { $to_check{$controller} = "all"; } } else { foreach my $controller (keys %to_check) { unless ( defined $controllers->{$controller} ) { print "Unknown controller: $controller, bailing out!\n"; exit $ERRORS{'CRITICAL'}; } } } foreach my $controller (keys %to_check) { my $units = &get_units ($controller); my @units_to_check; if ( $to_check{$controller} eq "all" ) { @units_to_check = keys %$units; } else { @units_to_check = @{$to_check{$controller}}; } foreach my $unit ( @units_to_check ) { if ( $units->{$unit} eq 'OK' or $units->{$unit} eq 'VERIFYING' ) { push @units_ok, "${controller}:${unit}"; } else { push @units_fail, "${controller}:${unit}"; } } } if ( int(@units_fail) == 0 ) { print "OK - all 3ware units are operational:"; foreach my $unit (@units_ok) { print " $unit (${unit_types{$unit}})"; } print "\n"; exit $ERRORS{'OK'}; } else { print "CRITICAL - the following 3ware units are degraded or missing:"; foreach my $unit (@units_fail) { print " $unit (${unit_types{$unit}})"; } print "\n"; exit $ERRORS{'CRITICAL'}; } sub get_controllers { unless (open TW_CLI, "$tw_cli show|") { print "Could not exec tw_cli: $!\n"; exit $ERRORS{'UNKNOWN'}; } my $ret = {}; while () { if ( /^(c\d)+\s+(\S+)\s/ ) { $ret->{$1} = $2; } } if ( $? != 0 ) { printf "tw_cli returned error code %d\n", $?; exit $ERRORS{'UNKNOWN'}; } close TW_CLI; return $ret; } sub get_units { my $controller = shift or return undef; unless (open TW_CLI, "$tw_cli /${controller} show unitstatus|") { print "Could not exec tw_cli: $!\n"; exit $ERRORS{'UNKNOWN'}; } my $ret = {}; while () { if ( /^(u\d+)\s+(\S+)\s+(\w+)\s/ ) { $ret->{$1} = $3; $unit_types{"${controller}:$1"} = $2; } } if ( $? != 0 ) { printf "tw_cli returned error code %d\n", $?; exit $ERRORS{'UNKNOWN'}; } close TW_CLI; return $ret; }