[BACK]Return to check_bioctl CVS log [TXT][DIR] Up to [local] / nagios / check_bioctl

Diff for /nagios/check_bioctl/check_bioctl between version 1.9 and 1.14

version 1.9, 2009/11/09 20:22:43 version 1.14, 2009/11/23 21:58:04
Line 1 
Line 1 
 #!/usr/bin/perl -T  #!/usr/bin/perl -T
 # $RedRiver: check_bioctl,v 1.8 2009/11/09 18:11:33 andrew Exp $  # $RedRiver: check_bioctl,v 1.13 2009/11/23 21:57:31 andrew Exp $
 ########################################################################  ########################################################################
 # check_bioctl *** A nagios check for OpenBSD bioctl  # check_bioctl *** A nagios check for OpenBSD bioctl
 #  #
Line 8 
Line 8 
 use strict;  use strict;
 use warnings;  use warnings;
   
 %ENV = ();  local %ENV = ();
   
 use constant NAGIOS_OUTPUT => 1;  my $NAGIOS_OUTPUT = 1;
   
 my $License = <<'EOL';  my $License = <<'EOL';
 Copyright (c) 2009 Andrew Fresh <andrew@afresh1.com>  Copyright (c) 2009 Andrew Fresh <andrew@afresh1.com>
Line 27 
Line 27 
 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 EOL  EOL
   
   my $PROGNAME = 'check_bioctl';
   my $BIOCTL   = '/sbin/bioctl';
   
 use POSIX;  use POSIX;
 use lib "/usr/local/libexec/nagios";  my $PREFIX;
   
   BEGIN {
       ## no critic 'warnings'
       no warnings 'uninitialized';
       $PREFIX = "${PREFIX}" || '/usr/local';    # Magic for OpenBSD ports tree
   }
   use lib $PREFIX . '/libexec/nagios';
 use utils qw($TIMEOUT %ERRORS &support);  use utils qw($TIMEOUT %ERRORS &support);
   
   $SIG{'ALRM'} = sub {
       print "ERROR: $PROGNAME timeout\n";
       exit $ERRORS{'UNKNOWN'};
   };
   alarm($TIMEOUT);
   
 use Getopt::Long;  use Getopt::Long;
 Getopt::Long::Configure('bundling');  Getopt::Long::Configure('bundling');
   
 my $PROGNAME = "check_bioctl";  
 my $BIOCTL   = '/sbin/bioctl';  
   
 # This maps the status we get from bioctl to something nagios can use  # This maps the status we get from bioctl to something nagios can use
 my %Status_Map = (  my %Status_Map = (
     Online      => 'OK',      Online      => 'OK',
Line 52 
Line 65 
 );  );
   
 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 @devices;  my @devices;
 my $opt_h;  my $opt_h;
 my $opt_V;  my $opt_V;
Line 74 
Line 86 
     exit $ERRORS{'OK'};      exit $ERRORS{'OK'};
 }  }
   
 if ( $opt_h || not @devices ) {  if ( $opt_h || !@devices ) {
     print_help();      print_help();
     exit $ERRORS{'OK'};      exit $ERRORS{'OK'};
 }  }
   
 my %VOLUMES;  my %VOLUMES = read_bioctl( \@devices );
 foreach my $device (@devices) {  my %STATES  = check_status( \%VOLUMES );
     open my $bioctl, '-|', $BIOCTL, $device or die "Couldn't open bioctl: $!";  
     my $volume_id;  
   
     while (<$bioctl>) {  my $have_results = 0;
         chomp;  $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};
   
           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} };
           }
       }
   }
   if ( $have_results == 0 ) {
       print "No results found\n";
   }
   exit $ERRORS{$state};
   
   sub read_bioctl {
       my ($devices) = @_;
       my %volumes;
   
       foreach my $d ( @{$devices} ) {
           open my $bioctl, q{-|}, $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} };
               }
           }
       }
   
       return %volumes;
   }
   
   {
       my $vid;
   
       sub parse_bioctl_line {
           my ($line) = @_;
           chomp $line;
   
         # Do these by columns cuZ that is the easiest for now          # Do these by columns cuZ that is the easiest for now
         my @o = unpack( "A6 A1 A11 A15 A7 A9 A*", $_ );          my @o = unpack( "A6 A1 A11 A15 A7 A9 A*", $line );
         next if $o[0] eq 'Volume';          return if $o[0] eq 'Volume';
   
         foreach (@o) {          foreach (@o) {
             s/^\s+//;              s/^\s+//xms;
             s/\s+$//;              s/\s+$//xms;
         }          }
   
         my ( $controller, $id, $status, $size, $dev, $details, $name ) = @o;          my ( $controller, $id, $status, $size, $dev, $details, $name ) = @o;
         my $index = $id;          my $index = $id;
         if ($controller) {          if ($controller) {
             $volume_id = $id;              $vid = $id;
         }          }
         else {          else {
             $index = "$volume_id.$id";              $index = "$vid.$id";
         }          }
   
         $VOLUMES{$device}{$index} = {          my %item = (
             type       => 'volume',              type       => 'volume',
             controller => $controller,              controller => $controller,
             id         => $id,              id         => $id,
Line 114 
Line 184 
             device     => $dev,              device     => $dev,
             details    => $details,              details    => $details,
             name       => $name,              name       => $name,
         };              volume_id  => $vid,
           );
   
         if ( $dev =~ /^\d+:\d+/ ) {          return $index, \%item;
             $VOLUMES{$device}{$index}{'volume'}  
                 = $VOLUMES{$device}{$volume_id};  
         }  
   
     }      }
     close $bioctl;  
 }  }
   
 foreach my $device ( sort keys %VOLUMES ) {  sub check_status {
     foreach my $index ( sort keys %{ $VOLUMES{$device} } ) {      my ($volumes) = @_;
         my $cur_state  
             = $Status_Map{ $VOLUMES{$device}{$index}{'status'} }  
             ? $Status_Map{ $VOLUMES{$device}{$index}{'status'} }  
             : 'UNKNOWN';  
   
         if ( $VOLUMES{$device}{$index}{'device'} =~ /^\d+:\d/ ) {      my %states;
             push @{ $states{$cur_state} },      foreach my $device ( sort keys %{$volumes} ) {
                 sprintf(          foreach my $index ( sort keys %{ $volumes->{$device} } ) {
                 "%5s %-7s %-11s %s",              my $cur_volume = $volumes->{$device}->{$index};
                 $VOLUMES{$device}{$index}{'volume'}{'controller'},              my $cur_state  = $Status_Map{ $cur_volume->{'status'} }
                 $VOLUMES{$device}{$index}{'device'},                  || 'UNKNOWN';
                 $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 $have_results = 0;              if ( $cur_volume->{'device'} =~ /^\d+:\d/xms ) {
 $state = 'OK';                  push @{ $states{$cur_state} },
 foreach my $error ( sort { $ERRORS{$b} <=> $ERRORS{$a} } keys %ERRORS ) {                      sprintf(
     if ( exists $states{$error} ) {                      "%5s %-7s %-11s %s",
         $have_results++;                      $cur_volume->{'volume'}{'controller'},
         $state = $error if $ERRORS{$state} < $ERRORS{$error};                      $cur_volume->{'device'},
                       $cur_volume->{'status'},
         if (NAGIOS_OUTPUT) {                      $cur_volume->{'name'}
             print "$error (" . scalar( @{ $states{$error} } ) . ")";                      );
             if ( $error ne 'OK' ) {  
                 print '<br>';  
                 print map {" - $_<br>"} @{ $states{$error} };  
             }              }
               else {
                   push @{ $states{$cur_state} },
                       sprintf( "%5s %-7s %s",
                       $cur_volume->{'controller'},
                       $cur_volume->{'device'},
                       $cur_volume->{'status'} );
               }
         }          }
         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 {  sub print_help {
     print <<"EOL";      print <<"EOL";
Line 193 
Line 239 
     print_revision( $PROGNAME, '$Revision$' );      print_revision( $PROGNAME, '$Revision$' );
   
     print $License;      print $License;
   
       return 1;
 }  }
   
   
 sub print_revision {  sub print_revision {
     my ($prog, $rev) = @_;      my ( $prog, $rev ) = @_;
     $rev =~ s/^\D+([\d\.]+)\D+$/v$1/xms;      $rev =~ s/^\D+([\d\.]+)\D+$/v$1/xms;
   
     print "$prog $rev\n";      print "$prog $rev\n";
   
       return 1;
 }  }

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.14

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>