Skip to content

Instantly share code, notes, and snippets.

@QNimbus
Last active January 23, 2020 10:54
Show Gist options
  • Save QNimbus/529ab50d7293a42f7bb2924c27b4b951 to your computer and use it in GitHub Desktop.
Save QNimbus/529ab50d7293a42f7bb2924c27b4b951 to your computer and use it in GitHub Desktop.
Script for use with M/Monit to monitor CPU core temperature
#!/bin/sh -e
##
## Title: CPUTemp.sh
## Description: Script for use with M/Monit to monitor CPU core temperature
## Author: B. van wetten
## Created date: 21-01-2020
## Updated date: 23-01-2020
## Version: 0.3
## GitHub Gist: https://gist.github.com/QNimbus/529ab50d7293a42f7bb2924c27b4b951
##
## Usage: CPUTemp.sh [max_temp]
## Sample M/Monit condition: status != 0 for 3 times within 5 cycles
## Notes: Exit code 0 when no CPU core temperature is greater than 'max_temp'
## otherwise exitcode is equal to max core temperature.
## If no command line parameter is supplied 'max_temp' equals 60 degrees
MaxCPUTemp=${1:-60}
NumCPUs=`sysctl -n kern.smp.cpus`
CurrentCPU=0
ExitCode=0
while [ $CurrentCPU -lt $NumCPUs ]
do
CPUTemp=`sysctl dev.cpu.$CurrentCPU.temperature | awk '{print $2}' | awk -F. '{print $1}'`
echo "CPU core ${CurrentCPU} temperature: ${CPUTemp}"
if [ $CPUTemp -gt $MaxCPUTemp ] && [ $CPUTemp -gt $ExitCode ]
then
ExitCode=$CPUTemp
fi
CurrentCPU=$((CurrentCPU+1))
done
echo "Maximum CPU core temperature allowed: ${MaxCPUTemp}"
exit $ExitCode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment