#!/usr/bin/perl

# title:   nagios_sensors.pl
# author:  John Stile <jstile@ilm.com>
# date:    Thu Sep 30 00:47:02 PDT 2004
# version: 0.01
#
# desc:   use to make lm_sensors metrics available to nagios
#

# debug messages if $debug=1
my $debug="0";

########################################## 
#            Set executable paths
##########################################

# Sensors binary
$sensors="/usr/bin/sensors";

# This is the only sensor chip we  care about
$Sensor_string="w83627hf-i2c-0-2d";

##########################################
#            Error Checking
##########################################

# - Check process table for this program. 
#   Expect to see one. 
#   Die if more than 1.

$self_count="0"; $total=0;
open ( PROCESSES, "ps -elf |")  || die "Cannot run ps:\t$!\n";
while (<PROCESSES>){
  if ( $_ =~ m/iinagios_sensors\.pl/ ){  ++$self_count ; }
}
if  ( "$self_count" > 1 ) { 
  die "Already running nagios_sensors.pl:\t$!\n"; 
}

# Declair and set default data.
$Start="0"; 

# Process output of `sensors` one line at a time.
open ( SENSORS, "$sensors |") || die "Cannot run sensors\n";
while (<SENSORS>){

  ########################################
  #  Screen for relevent sensor data
  ########################################  
  # Start collecting data when this line is reached
  if ( $_ =~ m/$Sensor_string/ ){
      $Start="1";
  # Stop Collecting data when this line is reached
  } elsif ( $_ =~ m/^$/ ) {
      $Start="0";
      done;
  }
  ###############################################
  # If Start is 1, we are working with good data
  if ( $Start eq "1" ){
    $bla=$_;  
    #
    # Temp1 = CPU1 Temp
    #   
    if ( $bla =~ m{
                      ^temp1:       # Starts with temp1:
                      \W+           # NonWord characters 
                      (\+)?         # Plus sign
		      ((\d+\.)?\d+) # Copy temp decimal value to $1
                      .*            # anything up to 
		      \n            # new line 
                  }xig              # allow comments, case insensetive, global	
     ) {    
	 $temp1="$2"; 
	if ( $debug == "1"){ print "Temp1:\t$2\n"; }
    }
    #
    # Temp2 = CPU2 Temp
    #   
    if ( $bla =~ m{
                      ^temp2:       # Starts with temp1:
                      \W+           # NonWord characters 
                      (\+)?         # Plus sign
		      ((\d+\.)?\d+) # Copy temp decimal value to $1
                      .*            # anything up to 
		      \n            # new line 
                  }xig              # allow comments, case insensetive, global	
     ) {    
	 $temp2="$2"; 
	 if ( $debug == "1"){ print "Temp2:\t$2\n"; }
    }
    #
    # Temp3 = CoreTemp
    #   
    if ( $bla =~ m{
                      ^temp3:       # Starts with temp1:
                      \W+           # NonWord characters 
                      (\+)?         # Plus sign
		      ((\d+\.)?\d+) # Copy temp decimal value to $1
                      .*            # anything up to 
		      \n            # new line 
                  }xig              # allow comments, case insensetive, global	
     ) {    
	 $temp3="$2"; 
	 if ( $debug == "1"){ print "Temp3:\t$2\n"; }
    }
    #
    # Fan1 = CPU1 Fan
    #   
    if ( $bla =~ m{
                     ^fan1:     # Starts with fan1:
		     \W*        # One or more NonWord characters
		     (\d+)      # Copy speed into $1
		     \W+        # One or more NonWord characters
		     RPM        # RPM
		     .*         # Anything up to 
		     \n         # a new line
                  }xig          # allow comments, case insensetive, global	
     ) {
	$fan1="$1";
	if ( $debug == "1"){ print "Fan1:\t$1\n"; }
    }
    #
    # Fan2 = CPU2 Fan
    #      
    if ( $bla =~ m{
                     ^fan2:     # Starts with fan2:
		     \W*        # One or more NonWord characters
		     (\d+)      # Copy speed into $1
		     \W+        # One or more NonWord characters 
		     RPM        # RPM
		     .*         # Anything up to
		     \n         # a new line
                  }xig          # allow comments, case insensetive, global	
     ) {
	$fan2="$1";
	if ( $debug == "1"){ print "Fan2:\t$1\n"; }
    }
    #
    # Fan3 = System fan ? have yet to determin.
    #   
    if ( $bla =~ m{
                     ^fan3:     # Starts with fan3:
		     \W*        # One or more NonWord characters
		     (\d+)      # Copy speed into $1
		     \W+        # One or more NonWord characters
		     RPM        # RPM
		     .*         # Anything up to
		     \n         # a new line 
                  }xig          # allow comments, case insensetive, global	
     ) {
	$fan3="$1";
	if ( $debug == "1"){ print "Fan3:\t$1\n"; }
     }
  }
   		   
}
# END of SENSORS data
close ( SENSORS );

$sensors_metrics="temp1=$temp1 C,temp2=$temp2 C,temp3=$temp3 C\tfan1=$fan1,fan2=$fan2,fan3=$fan3";
if ( $debug == "1"){ print "$sensors_metrics\n";}

################################################################
# ALERT AREA 
#     - if fan 1 or 2 is dead, register an alert
#     - if temp1,2, or 3 are above 60, register an alert
################################################################

# Set default alarm stat to OK.
my $Alarm="sensor ok $sensors_metrics" ;
for $temp ( $temp1, $temp2, $temp3 ){
	if ( $temp gt 60 ){
	      $Alarm="SENSOR CRITICAL - TEMP: $sensors_metrics";
	}
}
for $fan ( $fan1, $fan2 ){
	if ( $fan lt  300 ){
	      $Alarm="SENSOR CRITICAL - FAN: $sensors_metrics";
	}
}
print "$Alarm\n";

exit ;
