[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.7 and 1.20

version 1.7, 2009/11/09 18:00:09 version 1.20, 2014/06/27 19:59:56
Line 1 
Line 1 
 #!/usr/bin/perl -T  #!/usr/bin/perl -T
 # $RedRiver: check_bioctl,v 1.6 2009/11/09 17:58:29 andrew Exp $  # $AFresh1: check_bioctl,v 1.19 2011/12/27 02:23:57 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@afresh1.com>
 ########################################################################  ########################################################################
 # TODO:  
 #   Really need real documentation.  
 ########################################################################  
 use strict;  use strict;
 use warnings;  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 <andrew@afresh1.com>
   Permission to use, copy, modify, and distribute this software for any
   purpose with or without fee is hereby granted, provided that the above
   copyright notice and this permission notice appear in all copies.
   
   THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
   WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
   MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
   ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   EOL
   
   my $PROGNAME = 'check_bioctl';
   my $BIOCTL   = '/sbin/bioctl';
   my $SUDO     = '/usr/bin/sudo';
   
 use POSIX;  use POSIX;
 use lib "/usr/local/libexec/nagios";  my $PREFIX;
 use utils qw($TIMEOUT %ERRORS &print_revision &support);  
   
   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);
   
   $SIG{'ALRM'} = sub {
       say "ERROR: $PROGNAME timeout";
       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 39 
Line 66 
     Invalid     => 'CRITICAL',      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 @devices;
 my $opt_h;  my $opt_h;
 my $opt_V;  my $opt_V;
Line 58 
Line 83 
 }  }
   
 if ($opt_V) {  if ($opt_V) {
     print_revision( $PROGNAME, '$Revision$ ' );      print_revision();
     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;  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          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 ) {
       say 'No results found';
   }
   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{-|}, $SUDO, $BIOCTL, $d
                 = $VOLUMES{$device}{$volume_id};              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 ) {  sub parse_bioctl_line {
     foreach my $index ( sort keys %{ $VOLUMES{$device} } ) {      ($_) = @_;
         my $cur_state      chomp;
             = $Status_Map{ $VOLUMES{$device}{$index}{'status'} }  
             ? $Status_Map{ $VOLUMES{$device}{$index}{'status'} }  
             : 'UNKNOWN';  
   
         if ( $VOLUMES{$device}{$index}{'device'} =~ /^\d+:\d/ ) {      my @o = map { s/^\s+|\s+$//g; $_ } split;
             push @{ $states{$cur_state} },      return if $o[0] eq 'Volume';
                 sprintf(  
                 "%5s %-7s %-11s %s",      state $vid = '';
                 $VOLUMES{$device}{$index}{'volume'}{'controller'},      state $controller;
                 $VOLUMES{$device}{$index}{'device'},  
                 $VOLUMES{$device}{$index}{'status'},      my $index = "$vid.$o[0]";
                 $VOLUMES{$device}{$index}{'name'}      if ( $o[0] !~ /^\d+$/ ) {
                 );          $controller = shift @o;
         }          $vid        = $o[0];
         else {          $index      = $vid;
             push @{ $states{$cur_state} },  
                 sprintf( "%5s %-7s %s",  
                 $VOLUMES{$device}{$index}{'controller'},  
                 $VOLUMES{$device}{$index}{'device'},  
                 $VOLUMES{$device}{$index}{'status'} );  
         }  
     }      }
   
       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;  sub check_status {
 foreach my $error ( sort { $ERRORS{$b} <=> $ERRORS{$a} } keys %ERRORS ) {      my ($volumes) = @_;
     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 $d ( sort keys %{$volumes} ) {
             unless ( $error eq 'OK' ) {          foreach my $i ( sort { $a <=> $b } keys %{ $volumes->{$d} } ) {
                 print '<br>';              my $volume = $volumes->{$d}->{$i};
                 print map {" - $_<br>"} @{ $states{$error} };              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 {  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> [ -d <device2> [ -d ... ] ]      $PROGNAME -d <device> [ -d <device2> [ -d ... ] ]
   
Line 177 
Line 213 
   
 EOL  EOL
   
     print_revision( $PROGNAME, '$Revision$' );      print_revision();
   
       print $License;
   
       return 1;
 }  }
   
   sub print_revision {
       my $rev = '$Revision$';
       $rev =~ s/^\D+([\d\.]+)\D+$/v$1/xms;
   
       say "$PROGNAME $rev";
   
       return 1;
   }

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.20

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