Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Bash script check fan

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
gaijin
Posts: 16
Joined: 2016-11-22 12:08

Bash script check fan

#1 Post by gaijin »

Hi
I want to have a script which will test if a fan in a pc works. I plug it into nagios scripts.
I do not know why, when the fan is off, and "sensors" shows, 0 the script says "OK - 0".
What is wrong here?
Thank you.

sensors
Fan_tylny: 0 RPM

./check_fan Fan_tylny
OK - 0

Code: Select all

#!/bin/bash
 
### ======================================================================= ###
###     A nagios plugin to check disk uses for given disk or mountpoint     ###
###     Uses: ./check_fan Fan_tylny                                         ###

### ======================================================================= ###
 
### ======================================================================= ###
###                         FUNCTIONS                                       ###
### ======================================================================= ###
 
calculate_fan_speed(){
 FAN_SPEED=`sensors |grep ${FAN} | awk '{print $2}' `

case ${FAN_SPEED} in

[1000-5890]*)
 echo "OK - ${FAN_SPEED}"
 exit 0
 ;;
 
[100-900]*)
 echo "WARNING - ${FAN_SPEED}"
 exit 1
 ;;

[0]*)
 echo "CRITICAL - ${FAN_SPEED}"
 exit 2
 ;;

 *)
 echo "UNKNOWN - ${FAN_SPEED}"
 exit 3
 ;;
 esac
}
 
### ======================================================================= ###
###                         SCRIPT EXECUTION START HERE                     ###
### ======================================================================= ###
 
if [[ -z "$1" ]] 
then
        echo "Missing parameters! Syntax: ./`basename $0` fan"
        exit 3
else
        FAN=$1
fi
 
calculate_fan_speed
 
### ======================================================================= ###
###                         END OF SCRIPT                                   ###
### ======================================================================= ###

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 132 times

Re: Bash script check fan

#2 Post by Head_on_a_Stick »

[EDITed for a better explanation]

The matching numbers in the case statement apply on a per-character basis so this line:

Code: Select all

[1000-5890]*)
Will match with 1, 0 (several times), any number between 0 and 5, 8 and 9. Followed by any other character(s) (or not).

See also https://stackoverflow.com/questions/254 ... mber-range
deadbang

gaijin
Posts: 16
Joined: 2016-11-22 12:08

Re: Bash script check fan

#3 Post by gaijin »

Hi thank you
The same "problem". It's example plugin to nagios.
https://stackoverflow.com/questions/300 ... -statement

I changed like this, because I want to know, when the fan is broken (rotate 0).

Code: Select all

calculate_fan_speed(){
 FAN_SPEED=`sensors |grep ${FAN} | awk '{print $2}' `

case ${FAN_SPEED} in

[1-9]*)
 echo "OK - ${FAN_SPEED}"
 exit 0
 ;;

[0]*)
 echo "CRITICAL - ${FAN_SPEED}"
 exit 2
 ;;

 *)
 echo "UNKNOWN - ${FAN_SPEED}"
 exit 3
 ;;
 esac
}

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 132 times

Re: Bash script check fan

#4 Post by Head_on_a_Stick »

Note that backticks are discouraged and also that awk can pattern match so

Code: Select all

FAN_SPEED=$(sensors | awk -v fan="$FAN" '$0~fan{print $2}')
The bash shebang can be changed to /bin/sh if the [[ test is replaced by a POSIX sh version:

Code: Select all

if [ -z "$1" ]
And curly brackets are only needed if the variable is followed by an underscore or alphanumeric character.
deadbang

gaijin
Posts: 16
Joined: 2016-11-22 12:08

Re: Bash script check fan

#5 Post by gaijin »

Thank you
My fans rotate >1000
This is my plugin:

Code: Select all

#!/bin/bash
 
### ======================================================================= ###
###     A nagios plugin to check sensors fan rotate           				         ###
###     Uses: ./check_fan FAN_CPU FAN_REAR                             			         ###
###     1000-9999 OK, 1-999 warning, 0 critical                           				         ###
### ======================================================================= ###
 
### ======================================================================= ###
###                         FUNCTIONS                                       				        ###
### ======================================================================= ###
 
calculate_fan_speed(){
 FAN_SPEED=`sensors | awk -v fan="$FAN" '$0~fan{print $2}' `

case ${FAN_SPEED} in

[1-9][0-9][0-9][0-9]*)
 echo "OK - ${FAN_SPEED}"
 exit 0
 ;;
 
[1-9]|[1-9][0-9]|[1-9][0-9][0-9]*)
 echo "WARNING - ${FAN_SPEED}"
 exit 1
 ;;

[0]*)
 echo "CRITICAL - ${FAN_SPEED}"
 exit 2
 ;;

 *)
 echo "UNKNOWN - ${FAN_SPEED}"
 exit 3
 ;;
 esac
}
 
### ======================================================================= ###
###                         SCRIPT EXECUTION START HERE                     			        ###
### ======================================================================= ###
 
if [ -z "$1" ]
then
        echo "Missing parameters! Syntax: ./`basename $0` fan"
        exit 3
else
        FAN=$1
fi
 
calculate_fan_speed
 
### ======================================================================= ###
###                         END OF SCRIPT                                  				        ###
### ======================================================================= ###

CwF
Global Moderator
Global Moderator
Posts: 2636
Joined: 2018-06-20 15:16
Location: Colorado
Has thanked: 41 times
Been thanked: 192 times

Re: Bash script check fan

#6 Post by CwF »

It would be simpler to cat /sys/class/hwmon/sometin' directly instead of calling sensors.

gaijin
Posts: 16
Joined: 2016-11-22 12:08

Re: Bash script check fan

#7 Post by gaijin »

CwF wrote: 2022-07-01 13:33 It would be simpler to cat /sys/class/hwmon/sometin' directly instead of calling sensors.
Thank you. It is an alternative.

Post Reply