Skip to content

Instantly share code, notes, and snippets.

@snakers4
Last active February 24, 2019 07:24
Show Gist options
  • Save snakers4/cf0ffd57c3ef7f4e2e25f6b3347dcdec to your computer and use it in GitHub Desktop.
Save snakers4/cf0ffd57c3ef7f4e2e25f6b3347dcdec to your computer and use it in GitHub Desktop.
Plain temperature monitoring in Ubuntu 18.04
{
echo To: [email protected]
echo From: [email protected]
echo Subject: Temperature warning! $TIMESTAMP
echo Current CPU temperature is $TEMP
} | ssmtp [email protected]
# some rubbish email
#
# START CONFIG
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
# root=postmaster
[email protected]
# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
# mailhub=mail
mailhub=smtp.gmail.com:587
[email protected]
AuthPass=YOUR_PASS
UseTLS=YES
UseSTARTTLS=YES
# Where will the mail seem to come from?
rewriteDomain=gmail.com
# The full hostname
# hostname=snakers41-ubuntu
hostname=YOUR_HOST_NAME
# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES
# END CONFIG
# IMPORTANT - turn on less secure apps in google account settings
# https://support.google.com/accounts/answer/6010255
# test email
# echo "Test message from Linux server using ssmtp" | sudo ssmtp -vvv [email protected]
#/bin/bash
#Purpose = Send email if system temperature is higher than normal, log CPU temperatures
#Created on 17-02-2019
#Author = [email protected]
#Version 1.1
#START
TIMESTAMP=`date "+%Y-%m-%d %H-%M-%S"`
MAX_TEMP=75.0
LAST_DETECTION_TIME_FILE=~/sh_scripts/last_temp.log
ROLLING_TEMP_LOG=~/sh_scripts/temp.log
# this is very CPU specific
# change this to work with your CPU
TEMP="$(sensors | grep -A 2 k10temp | tail -n 1 | grep -o -P '(?<=temp1).*(?=high)' | grep -o -P '(?<=\+).*(?=°C)')"
if [ $(echo "$TEMP > $MAX_TEMP" | bc -l) -eq 1 ]
then
# if last detection is empty
if [ -s $LAST_DETECTION_TIME_FILE ]
then
# otherwise copy the temperature to the rolling log
cat "$LAST_DETECTION_TIME_FILE" >> "$ROLLING_TEMP_LOG"
# log the temperature
echo $TIMESTAMP $TEMP > $LAST_DETECTION_TIME_FILE
# truncate the log file
echo "$(tail -100000 "$ROLLING_TEMP_LOG")" > "$ROLLING_TEMP_LOG"
else
# log the temperature
echo $TIMESTAMP $TEMP > $LAST_DETECTION_TIME_FILE
# send an email
{
echo To: [email protected]
echo From: [email protected]
echo Subject: Temperature warning! $TIMESTAMP
echo Current CPU temperature is $TEMP
} | ssmtp [email protected]
fi
else
# truncate the current temperature log
> $LAST_DETECTION_TIME_FILE
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment