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
Programming languages, Coding, Executables, Package Creation, and Scripting.
gaijin
Posts: 16 Joined: 2016-11-22 12:08
#1
Post
by gaijin » 2022-06-29 12:12
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 ###
### ======================================================================= ###
gaijin
Posts: 16 Joined: 2016-11-22 12:08
#3
Post
by gaijin » 2022-06-30 07:33
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
}
Head_on_a_Stick
Posts: 14114 Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 133 times
#4
Post
by Head_on_a_Stick » 2022-06-30 14:56
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:
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
#5
Post
by gaijin » 2022-07-01 07:45
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
Posts: 2678 Joined: 2018-06-20 15:16
Location: Colorado
Has thanked: 41 times
Been thanked: 196 times
#6
Post
by CwF » 2022-07-01 13:33
It would be simpler to cat /sys/class/hwmon/sometin' directly instead of calling sensors.
gaijin
Posts: 16 Joined: 2016-11-22 12:08
#7
Post
by gaijin » 2022-07-19 12:22
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.