=================================================================== RCS file: /cvs/nagios/check_bioctl/check_bioctl,v retrieving revision 1.10 retrieving revision 1.21 diff -u -r1.10 -r1.21 --- nagios/check_bioctl/check_bioctl 2009/11/12 18:54:38 1.10 +++ nagios/check_bioctl/check_bioctl 2017/02/08 16:31:55 1.21 @@ -1,5 +1,5 @@ #!/usr/bin/perl -T -# $RedRiver: check_bioctl,v 1.9 2009/11/09 20:22:43 andrew Exp $ +# $AFresh1: check_bioctl,v 1.20 2014/06/27 18:59:56 andrew Exp $ ######################################################################## # check_bioctl *** A nagios check for OpenBSD bioctl # @@ -7,10 +7,11 @@ ######################################################################## use strict; use warnings; +use 5.010; -%ENV = (); +local %ENV = (); -use constant NAGIOS_OUTPUT => 1; +my $NAGIOS_OUTPUT = 1; my $License = <<'EOL'; Copyright (c) 2009 Andrew Fresh @@ -27,22 +28,24 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. EOL -my $PROGNAME = "check_bioctl"; +my $PROGNAME = 'check_bioctl'; my $BIOCTL = '/sbin/bioctl'; +my $DOAS = '/usr/bin/doas'; use POSIX; my $PREFIX; + BEGIN { ## no critic 'warnings' no warnings 'uninitialized'; - $PREFIX = "${PREFIX}" || '/usr/local'; # Magic for OpenBSD ports tree + $PREFIX = "${PREFIX}" || '/usr/local'; # Magic for OpenBSD ports tree } use lib $PREFIX . '/libexec/nagios'; use utils qw($TIMEOUT %ERRORS &support); $SIG{'ALRM'} = sub { - print ("ERROR: $PROGNAME timeout\n"); - exit $ERRORS{'UNKNOWN'}; + say "ERROR: $PROGNAME timeout"; + exit $ERRORS{'UNKNOWN'}; }; alarm($TIMEOUT); @@ -63,8 +66,6 @@ Invalid => 'CRITICAL', ); -my $state = 'UNKNOWN'; # tells whether the it is warning, critical, or OK -my %states; # This stores the count of states; my @devices; my $opt_h; my $opt_V; @@ -82,112 +83,122 @@ } if ($opt_V) { - print_revision( $PROGNAME, '$Revision: 1.10 $ ' ); + print_revision(); exit $ERRORS{'OK'}; } -if ( $opt_h || not @devices ) { +if ( $opt_h || !@devices ) { print_help(); exit $ERRORS{'OK'}; } -my %VOLUMES; -foreach my $device (@devices) { - open my $bioctl, '-|', $BIOCTL, $device or die "Couldn't open bioctl: $!"; - my $volume_id; +my %VOLUMES = read_bioctl( \@devices ); +my %STATES = check_status( \%VOLUMES ); - while (<$bioctl>) { - chomp; +my $have_results = 0; +my $state = 'OK'; +foreach my $error ( sort { $ERRORS{$b} <=> $ERRORS{$a} } keys %ERRORS ) { + if ( exists $STATES{$error} ) { + $have_results++; + $state = $error if $ERRORS{$state} < $ERRORS{$error}; - # Do these by columns cuZ that is the easiest for now - my @o = unpack( "A6 A1 A11 A15 A7 A9 A*", $_ ); - next if $o[0] eq 'Volume'; - - foreach (@o) { - s/^\s+//; - s/\s+$//; + if ($NAGIOS_OUTPUT) { + print "$error (" . scalar( @{ $STATES{$error} } ) . ")"; + if ( $error ne 'OK' ) { + print '
'; + print map {" - $_
"} @{ $STATES{$error} }; + } } - - my ( $controller, $id, $status, $size, $dev, $details, $name ) = @o; - my $index = $id; - if ($controller) { - $volume_id = $id; - } else { - $index = "$volume_id.$id"; + print "$error (" . scalar( @{ $STATES{$error} } ) . "):\n"; + print map {" $_\n"} @{ $STATES{$error} }; } + } +} +if ( $have_results == 0 ) { + say 'No results found'; +} +exit $ERRORS{$state}; - $VOLUMES{$device}{$index} = { - type => 'volume', - controller => $controller, - id => $id, - status => $status, - size => $size, - device => $dev, - details => $details, - name => $name, - }; +sub read_bioctl { + my ($devices) = @_; + my %volumes; - if ( $dev =~ /^\d+:\d+/ ) { - $VOLUMES{$device}{$index}{'volume'} - = $VOLUMES{$device}{$volume_id}; + foreach my $d ( @{$devices} ) { + open my $bioctl, q{-|}, $DOAS, $BIOCTL, $d + or die "Couldn't open bioctl: $!\n"; + LINE: while ( my $line = <$bioctl> ) { + my ( $i, $item ) = parse_bioctl_line($line); + next LINE if !defined $i; + $volumes{$d}{$i} = $item; } + ## no critic 'die' + close $bioctl + or die $! + ? "Error closing bioctl pipe: $!\n" + : "Exit status $? from bioctl \n"; + } + foreach my $d ( keys %volumes ) { + foreach my $i ( keys %{ $volumes{$d} } ) { + my $item = $volumes{$d}{$i}; + if ( $item->{device} =~ /^\d+:\d+/xms ) { + $item->{'volume'} = $volumes{$d}{ $item->{volume_id} }; + } + } } - close $bioctl; + + return %volumes; } -foreach my $device ( sort keys %VOLUMES ) { - foreach my $index ( sort keys %{ $VOLUMES{$device} } ) { - my $cur_state - = $Status_Map{ $VOLUMES{$device}{$index}{'status'} } - ? $Status_Map{ $VOLUMES{$device}{$index}{'status'} } - : 'UNKNOWN'; +sub parse_bioctl_line { + ($_) = @_; + chomp; - if ( $VOLUMES{$device}{$index}{'device'} =~ /^\d+:\d/ ) { - push @{ $states{$cur_state} }, - sprintf( - "%5s %-7s %-11s %s", - $VOLUMES{$device}{$index}{'volume'}{'controller'}, - $VOLUMES{$device}{$index}{'device'}, - $VOLUMES{$device}{$index}{'status'}, - $VOLUMES{$device}{$index}{'name'} - ); - } - else { - push @{ $states{$cur_state} }, - sprintf( "%5s %-7s %s", - $VOLUMES{$device}{$index}{'controller'}, - $VOLUMES{$device}{$index}{'device'}, - $VOLUMES{$device}{$index}{'status'} ); - } + my @o = map { s/^\s+|\s+$//g; $_ } split; + return if $o[0] eq 'Volume'; + + state $vid = ''; + state $controller; + + my $index = "$vid.$o[0]"; + if ( $o[0] !~ /^\d+$/ ) { + $controller = shift @o; + $vid = $o[0]; + $index = $vid; } + + 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, + }; } -my $have_results = 0; -$state = 'OK'; -foreach my $error ( sort { $ERRORS{$b} <=> $ERRORS{$a} } keys %ERRORS ) { - if ( exists $states{$error} ) { - $have_results++; - $state = $error if $ERRORS{$state} < $ERRORS{$error}; +sub check_status { + my ($volumes) = @_; - if (NAGIOS_OUTPUT) { - print "$error (" . scalar( @{ $states{$error} } ) . ")"; - if ( $error ne 'OK' ) { - print '
'; - print map {" - $_
"} @{ $states{$error} }; - } + my %states; + 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'; + + push @{ $states{$state} }, + sprintf( + "%5s %-7s %-11s %s", + $volume->{'controller'}, $volume->{'device'}, + $volume->{'status'}, $volume->{'name'} + ); } - else { - print "$error (" . scalar( @{ $states{$error} } ) . "):\n"; - print map {" $_\n"} @{ $states{$error} }; - } } + return %states; } -if ( $have_results == 0 ) { - print "No results found\n"; -} -exit $ERRORS{$state}; sub print_help { print <<"EOL"; @@ -202,15 +213,18 @@ EOL - print_revision( $PROGNAME, '$Revision: 1.10 $' ); + print_revision(); print $License; + + return 1; } - sub print_revision { - my ($prog, $rev) = @_; + my $rev = '$Revision: 1.21 $'; $rev =~ s/^\D+([\d\.]+)\D+$/v$1/xms; - print "$prog $rev\n"; + say "$PROGNAME $rev"; + + return 1; }