version 1.1, 2006/07/27 01:02:43 |
version 1.8, 2009/11/09 18:11:33 |
|
|
#!/usr/bin/perl -T |
#!/usr/bin/perl -T |
# $RedRiver$ |
# $RedRiver: check_bioctl,v 1.7 2009/11/09 18:00:09 andrew Exp $ |
######################################################################## |
######################################################################## |
# check_bioctl *** A nagios check for OpenBSD bioctl |
# check_bioctl *** A nagios check for OpenBSD bioctl |
# |
# |
# 2006.07.26 #*#*# andrew fresh <andrew@mad-techies.org> |
# 2006.07.26 #*#*# andrew fresh <andrew@mad-techies.org> |
######################################################################## |
######################################################################## |
# TODO: |
|
# Really need real documentation. |
|
######################################################################## |
|
use strict; |
use strict; |
use warnings; |
use warnings; |
|
|
#use Data::Dumper; |
|
|
|
%ENV = (); |
%ENV = (); |
|
|
use constant NAGIOS_OUTPUT => 1; |
use constant NAGIOS_OUTPUT => 1; |
|
|
Getopt::Long::Configure('bundling'); |
Getopt::Long::Configure('bundling'); |
|
|
my $PROGNAME = "check_bioctl"; |
my $PROGNAME = "check_bioctl"; |
my $BIOCTL = '/sbin/bioctl'; |
my $BIOCTL = '/sbin/bioctl'; |
|
|
|
# This maps the status we get from bioctl to something nagios can use |
my %Status_Map = ( |
my %Status_Map = ( |
Online => 'OK', |
Online => 'OK', |
Offline => 'WARNING', |
Offline => 'CRITICAL', |
Degraded => 'CRITICAL', |
Degraded => 'CRITICAL', |
Failed => 'CRITICAL', |
Failed => 'CRITICAL', |
Building => 'WARNING', |
Building => 'WARNING', |
Rebuild => 'WARNING', |
Rebuild => 'WARNING', |
'Hot spare' => 'OK', |
'Hot spare' => 'OK', |
Unused => 'OK', |
Unused => 'OK', |
Scrubbing => 'WARNING', |
Scrubbing => 'WARNING', |
Invalid => 'CRITICAL', |
Invalid => 'CRITICAL', |
); |
); |
|
|
|
my $state = 'UNKNOWN'; # tells whether the it is warning, critical, or OK |
my $state = 'UNKNOWN'; # tells whether the it is warning, critical, or OK |
my %states; # This stores the count of states; |
my %states; # This stores the count of states; |
my @devices; |
my $device; |
|
my $opt_h; |
my $opt_h; |
my $opt_V; |
my $opt_V; |
|
|
#Option checking |
#Option checking |
my $status = GetOptions( |
my $status = GetOptions( |
"version|V" => \$opt_V, |
"version|V" => \$opt_V, |
"help|h" => \$opt_h, |
"help|h" => \$opt_h, |
"device|d=s" => \$device, |
"device|d=s" => \@devices, |
); |
); |
|
|
if ($status == 0) { |
if ( $status == 0 ) { |
print_help() ; |
print_help(); |
exit $ERRORS{'OK'}; |
exit $ERRORS{'OK'}; |
} |
} |
|
|
if ($opt_V) { |
if ($opt_V) { |
print_revision($PROGNAME,'$Revision$ '); |
print_revision( $PROGNAME, '$Revision$ ' ); |
exit $ERRORS{'OK'}; |
exit $ERRORS{'OK'}; |
} |
} |
|
|
if ($opt_h || not $device) { |
if ( $opt_h || not @devices ) { |
print_help(); |
print_help(); |
exit $ERRORS{'OK'}; |
exit $ERRORS{'OK'}; |
} |
} |
|
|
my %DISKS; |
|
my %VOLUMES; |
my %VOLUMES; |
open my $bioctl, "-|", $BIOCTL, $device or die "Couldn't open bioctl: $!"; |
foreach my $device (@devices) { |
my $volume_id; |
open my $bioctl, '-|', $BIOCTL, $device or die "Couldn't open bioctl: $!"; |
while (<$bioctl>) { |
my $volume_id; |
chomp; |
|
# Do these by columns cuZ that is the easiest for now |
|
# Volume Status Size Device |
|
my @o = unpack("A6 A1 A11 A15 A7 A9 A*", $_); |
|
next if $o[0] eq 'Volume'; |
|
|
|
foreach (@o) { |
while (<$bioctl>) { |
s/^\s+//; |
chomp; |
s/\s+$//; |
|
} |
|
|
|
#print Dumper \@o; |
# 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'; |
|
|
my ($controller, $id, $status, $size, $dev, $details, $name) = @o; |
foreach (@o) { |
|
s/^\s+//; |
|
s/\s+$//; |
|
} |
|
|
if ($controller) { |
my ( $controller, $id, $status, $size, $dev, $details, $name ) = @o; |
$volume_id = $id; |
my $index = $id; |
|
if ($controller) { |
|
$volume_id = $id; |
|
} |
|
else { |
|
$index = "$volume_id.$id"; |
|
} |
|
|
$VOLUMES{$volume_id} = { |
$VOLUMES{$device}{$index} = { |
controller => $controller, |
type => 'volume', |
id => $volume_id, |
controller => $controller, |
status => $status, |
id => $id, |
size => $size, |
status => $status, |
device => $dev, |
size => $size, |
details => $details, |
device => $dev, |
name => $name, |
details => $details, |
} |
name => $name, |
} |
}; |
|
|
if ($dev =~ /^\d+:\d+/) { |
if ( $dev =~ /^\d+:\d+/ ) { |
$DISKS{$volume_id}{$id} = { |
$VOLUMES{$device}{$index}{'volume'} |
volume => $VOLUMES{$volume_id}, |
= $VOLUMES{$device}{$volume_id}; |
id => $id, |
} |
status => $status, |
|
size => $size, |
|
device => $dev, |
|
details => $details, |
|
name => $name, |
|
}; |
|
} |
|
|
|
|
} |
|
close $bioctl; |
} |
} |
close $bioctl; |
|
|
|
#print Dumper \%VOLUMES, \%DISKS; |
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'; |
|
|
foreach my $volume (sort keys %VOLUMES) { |
if ( $VOLUMES{$device}{$index}{'device'} =~ /^\d+:\d/ ) { |
next if $VOLUMES{$volume}{'device'} =~ /^\d+:\d+/; |
push @{ $states{$cur_state} }, |
my $cur_state = $Status_Map{ $VOLUMES{$volume}{'status'} } ? |
sprintf( |
$Status_Map{ $VOLUMES{$volume}{'status'} } : |
"%5s %-7s %-11s %s", |
'UNKNOWN'; |
$VOLUMES{$device}{$index}{'volume'}{'controller'}, |
push @{ $states{$cur_state} }, sprintf("%5s %-7s %s", |
$VOLUMES{$device}{$index}{'device'}, |
$VOLUMES{$volume}{'controller'}, |
$VOLUMES{$device}{$index}{'status'}, |
$VOLUMES{$volume}{'device'}, |
$VOLUMES{$device}{$index}{'name'} |
$VOLUMES{$volume}{'status'} |
); |
); |
} |
|
else { |
|
push @{ $states{$cur_state} }, |
|
sprintf( "%5s %-7s %s", |
|
$VOLUMES{$device}{$index}{'controller'}, |
|
$VOLUMES{$device}{$index}{'device'}, |
|
$VOLUMES{$device}{$index}{'status'} ); |
|
} |
|
} |
} |
} |
|
|
foreach my $volume (sort keys %DISKS) { |
|
foreach my $disk (sort keys %{ $DISKS{$volume} }) { |
|
my $cur_state = $Status_Map{ $DISKS{$volume}{$disk}{'status'} } ? |
|
$Status_Map{ $DISKS{$volume}{$disk}{'status'} } : |
|
'UNKNOWN'; |
|
push @{ $states{$cur_state} }, sprintf("%5s %-7s %-11s %s", |
|
$DISKS{$volume}{$disk}{'volume'}{'controller'}, |
|
$DISKS{$volume}{$disk}{'device'}, |
|
$DISKS{$volume}{$disk}{'status'}, |
|
$DISKS{$volume}{$disk}{'name'} |
|
); |
|
} |
|
} |
|
|
|
#print Dumper \%states; |
|
|
|
$state = 'OK'; |
|
my $have_results = 0; |
my $have_results = 0; |
foreach my $error (sort { $ERRORS{$a} <=> $ERRORS{$b} } keys %ERRORS) { |
$state = 'OK'; |
if (exists $states{$error}) { |
foreach my $error ( sort { $ERRORS{$b} <=> $ERRORS{$a} } keys %ERRORS ) { |
$have_results++; |
if ( exists $states{$error} ) { |
$state = $error; |
$have_results++; |
} |
$state = $error if $ERRORS{$state} < $ERRORS{$error}; |
|
|
|
if (NAGIOS_OUTPUT) { |
|
print "$error (" . scalar( @{ $states{$error} } ) . ")"; |
|
if ( $error ne 'OK' ) { |
|
print '<br>'; |
|
print map {" - $_<br>"} @{ $states{$error} }; |
|
} |
|
} |
|
else { |
|
print "$error (" . scalar( @{ $states{$error} } ) . "):\n"; |
|
print map {" $_\n"} @{ $states{$error} }; |
|
} |
|
} |
} |
} |
foreach my $error (sort { $ERRORS{$b} <=> $ERRORS{$a} } keys %ERRORS) { |
if ( $have_results == 0 ) { |
if (exists $states{$error}) { |
print "No results found\n"; |
if (NAGIOS_OUTPUT) { |
|
print "$error (" . scalar(@{ $states{ $error } }) . ")"; |
|
unless ($error eq 'OK') { |
|
print '<br>'; |
|
print map { " - $_<br>" } @{ $states{ $error } }; |
|
} |
|
} else { |
|
print "$error (" . scalar(@{ $states{ $error } }) . "):\n"; |
|
print map { " $_\n" } @{ $states{ $error } }; |
|
} |
|
} |
|
} |
} |
if ($have_results == 0) { |
|
print "No results found\n"; |
|
} |
|
exit $ERRORS{$state}; |
exit $ERRORS{$state}; |
|
|
sub print_help { |
sub print_help { |
print <<EOL; |
print <<EOL; |
$PROGNAME plugin for Nagios monitors bioctl on OpenBSD |
$PROGNAME plugin for Nagios monitors bioctl on OpenBSD |
$PROGNAME -d <device> |
$PROGNAME -d <device> [ -d <device2> [ -d ... ] ] |
|
|
Usage: |
Usage: |
-d, --device=DEVICE |
-d, --device=DEVICE |
DEVICE to check. Can either be a disk, as in sd0 |
DEVICE to check. Can be any device that bioctl(8) accepts |
or a raid card like ami0 |
|
-h (--help) usage help |
-h (--help) usage help |
-V (--version) version information |
-V (--version) version information |
|
|
EOL |
EOL |
|
|
print_revision($PROGNAME, '$Revision$'); |
print_revision( $PROGNAME, '$Revision$' ); |
} |
} |
|
|