[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.10 and 1.11

version 1.10, 2009/11/12 18:54:38 version 1.11, 2009/11/23 21:45:58
Line 1 
Line 1 
 #!/usr/bin/perl -T  #!/usr/bin/perl -T
 # $RedRiver: check_bioctl,v 1.9 2009/11/09 20:22:43 andrew Exp $  # $RedRiver: check_bioctl,v 1.10 2009/11/12 18:54:38 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 = ();  use 5.010;
   
 use constant NAGIOS_OUTPUT => 1;  local %ENV = ();
   
   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>
 Permission to use, copy, modify, and distribute this software for any  Permission to use, copy, modify, and distribute this software for any
Line 27 
Line 29 
 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 $PROGNAME = 'check_bioctl';
 my $BIOCTL   = '/sbin/bioctl';  my $BIOCTL   = '/sbin/bioctl';
   
 use POSIX;  use POSIX;
 my $PREFIX;  my $PREFIX;
   
 BEGIN {  BEGIN {
     ## no critic 'warnings'      ## no critic 'warnings'
     no warnings 'uninitialized';      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 lib $PREFIX . '/libexec/nagios';
 use utils qw($TIMEOUT %ERRORS &support);  use utils qw($TIMEOUT %ERRORS &support);
   
 $SIG{'ALRM'} = sub {  $SIG{'ALRM'} = sub {
         print ("ERROR: $PROGNAME timeout\n");      say "ERROR: $PROGNAME timeout";
         exit $ERRORS{'UNKNOWN'};      exit $ERRORS{'UNKNOWN'};
 };  };
 alarm($TIMEOUT);  alarm($TIMEOUT);
   
Line 64 
Line 67 
 );  );
   
 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 86 
Line 88 
     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};
   
         # Do these by columns cuZ that is the easiest for now          if ($NAGIOS_OUTPUT) {
         my @o = unpack( "A6 A1 A11 A15 A7 A9 A*", $_ );              print "$error (" . scalar( @{ $STATES{$error} } ) . ")";
         next if $o[0] eq 'Volume';              if ( $error ne 'OK' ) {
                   print '<br>';
         foreach (@o) {                  print map {" - $_<br>"} @{ $STATES{$error} };
             s/^\s+//;              }
             s/\s+$//;  
         }          }
   
         my ( $controller, $id, $status, $size, $dev, $details, $name ) = @o;  
         my $index = $id;  
         if ($controller) {  
             $volume_id = $id;  
         }  
         else {          else {
             $index = "$volume_id.$id";              print "$error (" . scalar( @{ $STATES{$error} } ) . "):\n";
               print map {"    $_\n"} @{ $STATES{$error} };
         }          }
       }
   }
   if ( $have_results == 0 ) {
       print "No results found\n";
   }
   exit $ERRORS{$state};
   
         $VOLUMES{$device}{$index} = {  sub read_bioctl {
             type       => 'volume',      my ($devices) = @_;
             controller => $controller,      my %volumes;
             id         => $id,  
             status     => $status,  
             size       => $size,  
             device     => $dev,  
             details    => $details,  
             name       => $name,  
         };  
   
         if ( $dev =~ /^\d+:\d+/ ) {      foreach my $d ( @{$devices} ) {
             $VOLUMES{$device}{$index}{'volume'}          open my $bioctl, q{-|}, $BIOCTL, $d
                 = $VOLUMES{$device}{$volume_id};              or die "Couldn't open bioctl: $!\n";
           while ( my $line = <$bioctl> ) {
               my ( $i, $item ) = parse_bioctl_line($line);
               $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}{ $i->{volume_id} };
               }
           }
     }      }
     close $bioctl;  
       return %volumes;
 }  }
   
 foreach my $device ( sort keys %VOLUMES ) {  sub parse_bioctl_line {
     foreach my $index ( sort keys %{ $VOLUMES{$device} } ) {      my ($line) = @_;
         my $cur_state      state $vid;
             = $Status_Map{ $VOLUMES{$device}{$index}{'status'} }      chomp $line;
             ? $Status_Map{ $VOLUMES{$device}{$index}{'status'} }  
             : 'UNKNOWN';  
   
         if ( $VOLUMES{$device}{$index}{'device'} =~ /^\d+:\d/ ) {      # Do these by columns cuZ that is the easiest for now
             push @{ $states{$cur_state} },      my @o = unpack( "A6 A1 A11 A15 A7 A9 A*", $line );
                 sprintf(      next if $o[0] eq 'Volume';
                 "%5s %-7s %-11s %s",  
                 $VOLUMES{$device}{$index}{'volume'}{'controller'},      foreach (@o) {
                 $VOLUMES{$device}{$index}{'device'},          s/^\s+//xms;
                 $VOLUMES{$device}{$index}{'status'},          s/\s+$//xms;
                 $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 ( $controller, $id, $status, $size, $dev, $details, $name ) = @o;
       my $index = $id;
       if ($controller) {
           $vid = $id;
       }
       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;
 }  }
   
 my $have_results = 0;  sub check_status {
 $state = 'OK';      my ($volumes) = @_;
 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) {      my %states;
             print "$error (" . scalar( @{ $states{$error} } ) . ")";      foreach my $device ( sort keys %{$volumes} ) {
             if ( $error ne 'OK' ) {          foreach my $index ( sort keys %{ $volumes->{$device} } ) {
                 print '<br>';              my $cur_volume = $volumes->{$device}->{$index};
                 print map {" - $_<br>"} @{ $states{$error} };              my $cur_state  = $Status_Map{ $cur_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'} );
               }
         }          }
         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 205 
Line 237 
     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";      say "$prog $rev";
   
       return 1;
 }  }

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.11

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