Simple Linux server thermal warning using bash, lm-sensors and Gotify
During the summer months heat becomes a problem for everybody, even home-lab servers. Monitoring the temperature of your server in order to react in case you feel uncomfortable with the degrees it has reached doesn't have to be complicated.
You will need the lm-sensors
package installed on your Linux server, a Gotify server for cellphone push notifications and crontab for scheduling.
After installing lm-sensors with sudo apt install lm-sensors
, on Debian based systems, you create the following script in you /home/{{user}}/bin folder
#!/bin/bash
server_temp=$(sensors coretemp-isa-0000 | grep 'Package id 0:' | cut -d' ' -f 5 | cut -c 2,3)
if [ $server_temp -gt 79 ]
then
TITLE="Thermal Warning"
MESSAGE="Server is $server_temp C hot!"
PRIORITY=5
URL="https://yourgotifyserver.com/message?token=YourTokenHere"
curl -s -S --data '{"message": "'"${MESSAGE}"'", "title": "'"${TITLE}"'", "priority":'"${PRIORITY}"', "extras": {"client::display": {"contentType": "text/markdown"}}}' -H 'Content-Type: application/json' "$URL"
fi
echo "`date`, $server_temp" >> /temp/thermal.csv
What you should change in this script is the name of your CPU that lm-sensors uses, in my case it's the coretemp-isa-0000
, you can find yours by running sensors
command on your server. Secondly change the URL to point to your Gotify server along with the Token you've created there for the messages.
Finally, use crontab for easy scheduling of the script by entering crontab -e
at the command line. I use the following schedule for execution every 30 minutes:
*/30 * * * * /home/user/bin/thermal_warning > /dev/null 2>&1
The final line of the script exports the temperature to a CSV file for logging purposes that you can later import in a spreadsheet app and look for trends during the day.