Last active
April 12, 2016 15:18
-
-
Save nanderson94/ed69bdb2dbacb12661a7890ee7c8d469 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Set up with a cron job to regularly check the CPU temperatures | |
# */5 * * * * /path/to/script.sh | |
# Alternatively, systemd timer | |
# [Unit] | |
# Description=Temperature monitor timer | |
# | |
# [Timer] | |
# OnUnitActiveSec=5m | |
# Unit=temperature-monitor.service | |
# OnBootSec=5m | |
# | |
# [Install] | |
# WantedBy=timers.target | |
# And service | |
# [Unit] | |
# Description=Temperature monitor | |
# After=network.target | |
# | |
# [Service] | |
# Type=oneshot | |
# ExecStart=/bin/bash /usr/local/bin/temp-mon.sh | |
# | |
# [Install] | |
# WantedBy=multi-user-wants.target | |
# Maximum temperature in degrees celcsius (as int) | |
MAX=40 | |
# Time backoff (in seconds) | |
INTERVAL=3599 | |
SENSORS=$(sensors); | |
# You may need to change the grep/awk depending on your setup | |
MATCH=$(echo "$SENSORS" | grep "Physical id 0" | awk '{print $4}') | |
TEMPC=${MATCH:1:2} | |
# Temperature is within range, all good. | |
if [ $TEMPC -lt $MAX ]; then | |
exit 0 | |
fi | |
if [ -e /tmp/temp-mon ]; then | |
LASTSEND=$(cat /tmp/temp-mon); | |
else | |
LASTSEND=0 | |
fi | |
LASTSENDNICE=$(date --utc --date=@$LASTSEND) | |
PAYLOAD=`cat <<EOF | |
WARNING: $HOSTNAME is reporting a temperature of $TEMPC°C. | |
The configured threshold of this system is $MAX. | |
The last error was sent on $LASTSENDNICE | |
The output of the last sensors command was: | |
$SENSORS | |
EOF | |
` | |
CURTIME=$(date --utc +%s); | |
DIFFTIME=$(expr $CURTIME - $LASTSEND); | |
if [ $DIFFTIME -gt $INTERVAL ]; then | |
echo $CURTIME > /tmp/temp-mon; | |
echo "$PAYLOAD" | mail -s "CRITICAL: Temperature warning for $HOSTNAME" nanderson@nanderson.me | |
echo "Mail sent!" | |
# Send the message! | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment