Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Plain temperature monitoring in Ubuntu 18.04
{
echo To: YOUR_EMAIL@gmail.com
echo From: YOUR_EMAIL@gmail.com
echo Subject: Temperature warning! $TIMESTAMP
echo Current CPU temperature is $TEMP
} | ssmtp YOUR_EMAIL@gmail.com
# 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
root=YOUR_EMAIL@gmail.com
# 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
AuthUser=YOUR_EMAIL@gmail.com
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 destination-email-address@some-domain.com
#/bin/bash
#Purpose = Send email if system temperature is higher than normal, log CPU temperatures
#Created on 17-02-2019
#Author = aveysov@gmail.com
#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: aveysov@gmail.com
echo From: aveysov@gmail.com
echo Subject: Temperature warning! $TIMESTAMP
echo Current CPU temperature is $TEMP
} | ssmtp aveysov@gmail.com
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
You can’t perform that action at this time.