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

Diff for /nagios/check_hw_sensors/check_hw_sensors between version 1.35 and 1.42

version 1.35, 2009/11/10 23:15:15 version 1.42, 2009/11/24 17:54:54
Line 1 
Line 1 
 #!/usr/bin/perl -T  #!/usr/bin/perl -T
 # $RedRiver: check_hw_sensors,v 1.34 2009/11/10 23:12:49 andrew Exp $  # $RedRiver: check_hw_sensors,v 1.41 2009/11/12 18:53:52 andrew Exp $
 ########################################################################  ########################################################################
 # check_hw_sensors *** A nagios check for OpenBSD sysctl hw.sensors  # check_hw_sensors *** A nagios check for OpenBSD sysctl hw.sensors
 #  #
Line 10 
Line 10 
   
 local %ENV = ();  local %ENV = ();
   
   use POSIX;
   use Config;
   use Getopt::Long;
   use List::Util qw/ first /;
   
 my $NAGIOS_OUTPUT = 1;  my $NAGIOS_OUTPUT = 1;
   
   our $VERSION = q{$Revision$}; $VERSION =~ s/^\D+([\d\.]+)\D+$/v$1/xms;
   
 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 34 
 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 EOL  EOL
   
 use POSIX;  my $PROGNAME       = 'check_hw_sensors';
 use Config;  
 use lib '/usr/local/libexec/nagios';  
 use utils qw($TIMEOUT %ERRORS &support);  
   
 use Getopt::Long;  
 Getopt::Long::Configure('bundling');  
   
 my $PROGNAME = 'check_hw_sensors';  
   
 my $SYSCTL         = '/sbin/sysctl';  my $SYSCTL         = '/sbin/sysctl';
 my $GETCAP         = '/usr/bin/getcap';  my $GETCAP         = '/usr/bin/getcap';
 my $BASE           = 'hw.sensors';  my $BASE           = 'hw.sensors';
 my $DEFAULT_CONFIG = '/etc/sensorsd.conf';  my $DEFAULT_CONFIG = '/etc/sensorsd.conf';
 my $OSVer          = $Config{'osvers'} || 0;  
   
   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);
   
   $SIG{'ALRM'} = sub {
       print "ERROR: $PROGNAME timeout\n";
       exit $ERRORS{'UNKNOWN'};
   };
   alarm $TIMEOUT;
   
   Getopt::Long::Configure('bundling');
   
   my $OSVer = $Config{'osvers'} || 0;
   
 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 $opt_V;  my $opt_V;
 my $opt_h;  my $opt_h;
Line 69 
Line 86 
 }  }
   
 if ($opt_V) {  if ($opt_V) {
     print_revision( $PROGNAME, '$Revision$ ' );      print_revision( $PROGNAME, $VERSION );
     exit $ERRORS{'OK'};      exit $ERRORS{'OK'};
 }  }
   
Line 79 
Line 96 
 }  }
   
 # set the default this way so it only happens if someone typed -f or --filename  # set the default this way so it only happens if someone typed -f or --filename
 $FILENAME = $DEFAULT_CONFIG if ( defined $FILENAME && $FILENAME eq q{} );  if ( defined $FILENAME && $FILENAME eq q{} ) {
       $FILENAME = $DEFAULT_CONFIG;
   }
   
 # Stuff is output in this file by print_sensor()  # Stuff is output in this file by print_sensor()
 # http://www.openbsd.org/cgi-bin/cvsweb/src/sbin/sysctl/sysctl.c  # http://www.openbsd.org/cgi-bin/cvsweb/src/sbin/sysctl/sysctl.c
Line 130 
Line 149 
     }      }
     $CHECK_SENSOR = $SENSOR;      $CHECK_SENSOR = $SENSOR;
   
     $CHECKS{$SENSOR}{'warn'} = $WARNING;      if ($WARNING)  { $CHECKS{$SENSOR}{'warn'} = $WARNING; }
     $CHECKS{$SENSOR}{'crit'} = $CRITICAL;      if ($CRITICAL) { $CHECKS{$SENSOR}{'crit'} = $CRITICAL; }
   
 }  }
 elsif ( defined $FILENAME ) {  elsif ( defined $FILENAME ) {
     %CHECKS = read_file($FILENAME);      %CHECKS = read_file($FILENAME);
Line 144 
Line 162 
   
 my $have_results = 0;  my $have_results = 0;
 $state = 'OK';  $state = 'OK';
 foreach my $error ( sort { $ERRORS{$b} <=> $ERRORS{$a} } keys %ERRORS ) {  foreach
       my $error ( reverse sort { $ERRORS{$a} <=> $ERRORS{$b} } keys %ERRORS )
   {
     if ( exists $STATES{$error} ) {      if ( exists $STATES{$error} ) {
         $have_results++;          $have_results++;
         $state = $error if $ERRORS{$state} < $ERRORS{$error};          $state = $error if $ERRORS{$state} < $ERRORS{$error};
   
         if ($NAGIOS_OUTPUT) {          if ($NAGIOS_OUTPUT) {
             print "$error (" . scalar( @{ $STATES{$error} } ) . ")";              print $error . ' (' . scalar( @{ $STATES{$error} } ) . ')';
             if ( $error ne 'OK' ) {              if ( $error ne 'OK' ) {
                 s/\s+/ /gxms;  
                 print '<br>';                  print '<br>';
                 print map {" - $_<br>"} @{ $STATES{$error} };                  print map {" - $_<br>"} @{ $STATES{$error} };
             }              }
         }          }
         else {          else {
             print "$error (" . scalar( @{ $STATES{$error} } ) . "):\n";              print $error . ' (' . scalar( @{ $STATES{$error} } ) . "):\n";
             foreach ( @{ $STATES{$error} } ) {              foreach ( @{ $STATES{$error} } ) {
                 print "   $_\n";                  print "   $_\n";
             }              }
Line 173 
Line 192 
 sub read_sensors {  sub read_sensors {
     my ($sensor) = @_;      my ($sensor) = @_;
     my @S;      my @S;
     open my $sysctl, '-|', $SYSCTL, $sensor      open my $sysctl, q{-|}, $SYSCTL, $sensor
         or die "Couldn't open sysctl: $!\n";          or die "Couldn't open sysctl: $!\n";
     while (<$sysctl>) {      while (<$sysctl>) {
         chomp;          chomp;
Line 191 
Line 210 
 sub parse_sensor {  sub parse_sensor {
     my ($sensor) = @_;      my ($sensor) = @_;
   
       ## no critic 'literal'
     my ( $id, $output ) = split /=/xms, $sensor;      my ( $id, $output ) = split /=/xms, $sensor;
     my @s = split /\./xms,   $id;      my @s = split /\./xms,   $id;
     my @o = split /,\s*/xms, $output;      my @o = split /,\s*/xms, $output;
Line 241 
Line 261 
     my $filename = shift;      my $filename = shift;
     my %contents;      my %contents;
   
     die "file '$filename' does not exist.\n" unless -e $filename;      die "file '$filename' does not exist.\n" if !-e $filename;
   
     open my $fh, '-|', $GETCAP, '-a', '-f', $filename      open my $fh, q{-|}, $GETCAP, q{-a}, q{-f}, $filename
         or die "Couldn't open '$GETCAP -a -f $filename': $!\n";          or die "Couldn't open '$GETCAP -a -f $filename': $!\n";
     while (<$fh>) {      while (<$fh>) {
         chomp;          chomp;
Line 274 
Line 294 
 sub parse_check {  sub parse_check {
     my $check = shift;      my $check = shift;
   
     return unless $check;      return          if !$check;
     return 'STATUS' if $check->{'STATUS'};      return 'STATUS' if $check->{'STATUS'};
     return 'IGNORE' if $check->{'IGNORE'};      return 'IGNORE' if $check->{'IGNORE'};
   
Line 288 
Line 308 
         }          }
   
         foreach my $direction ( 'low', 'high' ) {          foreach my $direction ( 'low', 'high' ) {
             my $c = $code . '.' . $direction;              my $c = $code . q{.} . $direction;
             if ( defined $check->{$direction} ) {              if ( defined $check->{$direction} ) {
                 $check->{$c} ||= $check->{$direction};                  $check->{$c} ||= $check->{$direction};
             }              }
Line 322 
Line 342 
         my ( $r, $data );          my ( $r, $data );
         if ( exists $C->{ $sensor->{id} } ) {          if ( exists $C->{ $sensor->{id} } ) {
             $r = check_sensor( $sensor, $C->{ $sensor->{id} } );              $r = check_sensor( $sensor, $C->{ $sensor->{id} } );
             $data = $sensor->{id} . '=' . $sensor->{output};              $data = $sensor->{id} . q{=} . $sensor->{output};
         }          }
         elsif ( !$O->{ignore_status} && $sensor->{status} ) {          elsif ( $sensor->{status} && !$O->{IGNORE_STATUS} ) {
             $r = check_sensor( $sensor, { STATUS => 1 } );              $r = check_sensor( $sensor, { STATUS => 1 } );
             $data = $sensor->{id} . '=' . $sensor->{output};              $data = $sensor->{id} . q{=} . $sensor->{output};
         }          }
         else {          else {
   
Line 345 
Line 365 
     my ( $sensor, $check ) = @_;      my ( $sensor, $check ) = @_;
     my $result = 'UNKNOWN';      my $result = 'UNKNOWN';
   
     return $result unless ref $sensor eq 'HASH';      return $result if ref $sensor ne 'HASH';
     $check = parse_check($check) if $check;      $check = parse_check($check) if $check;
   
     if ( !$check ) { return $result; }      if ( !$check ) { return $result; }
Line 358 
Line 378 
     elsif ( $check eq 'IGNORE' ) { return; }      elsif ( $check eq 'IGNORE' ) { return; }
   
     my $type = $sensor->{'type'};      my $type = $sensor->{'type'};
     if (grep { $type eq $_ }      if (first { $type eq $_ }
         qw(          qw(
         fan fanrpm          fan fanrpm
         volt volts_dc          volt volts_dc
Line 370 
Line 390 
     {      {
         $result = check_sensor_numeric( $sensor->{'data'}, $check );          $result = check_sensor_numeric( $sensor->{'data'}, $check );
     }      }
     elsif ( grep { $type eq $_ } qw( drive indicator ) ) {      elsif ( first { $type eq $_ } qw( drive indicator ) ) {
         my $data = $sensor->{'data'};          my $data = $sensor->{'data'};
         $data =~ s/^drive\s+//xms;          $data =~ s/^drive\s+//xms;
         $result = check_sensor_list( $data, $check );          $result = check_sensor_list( $data, $check );
     }      }
     else {      else {
         warn 'Unknown Sensor Type: ', $sensor->{'id'}, '=', $type, "\n";          warn "Unknown Sensor Type: $sensor->{id} = $type\n";
     }      }
   
     return $result;      return $result;
Line 398 
Line 418 
     }      }
   
     foreach my $code ( 'warn', 'crit' ) {      foreach my $code ( 'warn', 'crit' ) {
         if (   defined $check->{ $code . ".low" }          if (   defined $check->{ $code . '.low' }
             || defined $check->{ $code . ".high" } )              || defined $check->{ $code . '.high' } )
         {          {
             if ((   defined $check->{ $code . ".low" }              if ((   defined $check->{ $code . '.low' }
                     && $check->{ $code . ".low" } >= $data                      && $check->{ $code . '.low' } >= $data
                 )                  )
                 || ( defined $check->{ $code . ".high" }                  || ( defined $check->{ $code . '.high' }
                     && $check->{ $code . ".high" } <= $data )                      && $check->{ $code . '.high' } <= $data )
                 )                  )
             {              {
                 $result = $errors{$code};                  $result = $errors{$code};
Line 418 
Line 438 
                 my $c = $_;                  my $c = $_;
                 $c =~ s/[^\d\.]//gxms;                  $c =~ s/[^\d\.]//gxms;
                 if ( !length $c ) {                  if ( !length $c ) {
                     warn "INVALID CHECK (" . $_ . ") for '$code'\n";                      warn "INVALID CHECK ($_) for '$code'\n";
                     next;                      next;
                 }                  }
   
Line 529 
Line 549 
   
 EOL  EOL
   
     print_revision( $PROGNAME, '$Revision$' );      print_revision( $PROGNAME, $VERSION );
   
     print $LICENSE;      print $LICENSE;
   
Line 538 
Line 558 
   
 sub print_revision {  sub print_revision {
     my ( $prog, $rev ) = @_;      my ( $prog, $rev ) = @_;
     $rev =~ s/^\D+([\d\.]+)\D+$/v$1/xms;  
   
     print "$prog $rev\n";      print "$prog $rev\n";
   

Legend:
Removed from v.1.35  
changed lines
  Added in v.1.42

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