=================================================================== RCS file: /cvs/nagios/check_bioctl/check_bioctl,v retrieving revision 1.11 retrieving revision 1.20 diff -u -r1.11 -r1.20 --- nagios/check_bioctl/check_bioctl 2009/11/23 21:45:58 1.11 +++ nagios/check_bioctl/check_bioctl 2014/06/27 19:59:56 1.20 @@ -1,5 +1,5 @@ #!/usr/bin/perl -T -# $RedRiver: check_bioctl,v 1.10 2009/11/12 18:54:38 andrew Exp $ +# $AFresh1: check_bioctl,v 1.19 2011/12/27 02:23:57 andrew Exp $ ######################################################################## # check_bioctl *** A nagios check for OpenBSD bioctl # @@ -7,12 +7,11 @@ ######################################################################## use strict; use warnings; - use 5.010; local %ENV = (); -my $NAGIOS_OUTPUT => 1; +my $NAGIOS_OUTPUT = 1; my $License = <<'EOL'; Copyright (c) 2009 Andrew Fresh @@ -31,6 +30,7 @@ my $PROGNAME = 'check_bioctl'; my $BIOCTL = '/sbin/bioctl'; +my $SUDO = '/usr/bin/sudo'; use POSIX; my $PREFIX; @@ -66,7 +66,6 @@ Invalid => 'CRITICAL', ); -my $state = 'UNKNOWN'; # tells whether the it is warning, critical, or OK my @devices; my $opt_h; my $opt_V; @@ -84,7 +83,7 @@ } if ($opt_V) { - print_revision( $PROGNAME, '$Revision: 1.11 $ ' ); + print_revision(); exit $ERRORS{'OK'}; } @@ -97,7 +96,7 @@ my %STATES = check_status( \%VOLUMES ); my $have_results = 0; -$state = 'OK'; +my $state = 'OK'; foreach my $error ( sort { $ERRORS{$b} <=> $ERRORS{$a} } keys %ERRORS ) { if ( exists $STATES{$error} ) { $have_results++; @@ -117,7 +116,7 @@ } } if ( $have_results == 0 ) { - print "No results found\n"; + say 'No results found'; } exit $ERRORS{$state}; @@ -126,10 +125,11 @@ my %volumes; foreach my $d ( @{$devices} ) { - open my $bioctl, q{-|}, $BIOCTL, $d + open my $bioctl, q{-|}, $SUDO, $BIOCTL, $d or die "Couldn't open bioctl: $!\n"; - while ( my $line = <$bioctl> ) { + LINE: while ( my $line = <$bioctl> ) { my ( $i, $item ) = parse_bioctl_line($line); + next LINE if !defined $i; $volumes{$d}{$i} = $item; } ## no critic 'die' @@ -143,7 +143,7 @@ foreach my $i ( keys %{ $volumes{$d} } ) { my $item = $volumes{$d}{$i}; if ( $item->{device} =~ /^\d+:\d+/xms ) { - $item->{'volume'} = $volumes{$d}{ $i->{volume_id} }; + $item->{'volume'} = $volumes{$d}{ $item->{volume_id} }; } } } @@ -152,70 +152,49 @@ } sub parse_bioctl_line { - my ($line) = @_; - state $vid; - chomp $line; + ($_) = @_; + chomp; - # Do these by columns cuZ that is the easiest for now - my @o = unpack( "A6 A1 A11 A15 A7 A9 A*", $line ); - next if $o[0] eq 'Volume'; + my @o = map { s/^\s+|\s+$//g; $_ } split; + return if $o[0] eq 'Volume'; - foreach (@o) { - s/^\s+//xms; - s/\s+$//xms; - } + state $vid = ''; + state $controller; - my ( $controller, $id, $status, $size, $dev, $details, $name ) = @o; - my $index = $id; - if ($controller) { - $vid = $id; + my $index = "$vid.$o[0]"; + if ( $o[0] !~ /^\d+$/ ) { + $controller = shift @o; + $vid = $o[0]; + $index = $vid; } - else { - $index = "$vid.$id"; - } - my %item = ( - type => 'volume', - controller => $controller, - id => $id, - status => $status, - size => $size, - device => $dev, - details => $details, - name => $name, - volume_id => $vid, - ); - - return $index, \%item; + return $index, { + controller => $controller, + volume_id => $vid, + id => shift @o, + status => shift @o, + size => shift @o, + device => shift @o, + name => shift @o, + description => join ' ', @o, + }; } sub check_status { my ($volumes) = @_; my %states; - foreach my $device ( sort keys %{$volumes} ) { - foreach my $index ( sort keys %{ $volumes->{$device} } ) { - my $cur_volume = $volumes->{$device}->{$index}; - my $cur_state = $Status_Map{ $cur_volume->{'status'} } - || 'UNKNOWN'; + foreach my $d ( sort keys %{$volumes} ) { + foreach my $i ( sort { $a <=> $b } keys %{ $volumes->{$d} } ) { + my $volume = $volumes->{$d}->{$i}; + my $state = $Status_Map{ $volume->{'status'} } || 'UNKNOWN'; - if ( $cur_volume->{'device'} =~ /^\d+:\d/xms ) { - push @{ $states{$cur_state} }, - sprintf( - "%5s %-7s %-11s %s", - $cur_volume->{'volume'}{'controller'}, - $cur_volume->{'device'}, - $cur_volume->{'status'}, - $cur_volume->{'name'} - ); - } - else { - push @{ $states{$cur_state} }, - sprintf( "%5s %-7s %s", - $cur_volume->{'controller'}, - $cur_volume->{'device'}, - $cur_volume->{'status'} ); - } + push @{ $states{$state} }, + sprintf( + "%5s %-7s %-11s %s", + $volume->{'controller'}, $volume->{'device'}, + $volume->{'status'}, $volume->{'name'} + ); } } return %states; @@ -234,7 +213,7 @@ EOL - print_revision( $PROGNAME, '$Revision: 1.11 $' ); + print_revision(); print $License; @@ -242,10 +221,10 @@ } sub print_revision { - my ( $prog, $rev ) = @_; + my $rev = '$Revision: 1.20 $'; $rev =~ s/^\D+([\d\.]+)\D+$/v$1/xms; - say "$prog $rev"; + say "$PROGNAME $rev"; return 1; }