Skip to content

Instantly share code, notes, and snippets.

@dgrant
Last active December 14, 2015 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgrant/5156440 to your computer and use it in GitHub Desktop.
Save dgrant/5156440 to your computer and use it in GitHub Desktop.
I run this file in a cron job every 5 minutes. It checks the CPU temperature using various sensors (using the 3rd party package lm-sensors) and sends en e-mail to a configurable e-mail address if any one of the temperature exceeds are configurable threshold.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
A simple program that sends warnings when disk space on one or more hard
drives has reached a critical level
"""
import re
from subprocess import Popen, PIPE
##############################################################
#CHANGE SETTINGS BELOW
##############################################################
EMAIL_ADDRESS = 'davidgrant@gmail.com'
THRESHOLD = 60
#if SEND_EMAIL=True, email will be sent, else, just stdout
SEND_EMAIL = True
REGEXES = (
('CPU temp', "CPU Temperature: *\+([\d\.]+)°C.*",),
# ('CPU temp', "",),
)
##############################################################
#DON'T TOUCH ANYTHING BELOW UNLESS YOU KNOW WHAT YOU ARE DOING
##############################################################
RE_OBJS = [(x, re.compile(y)) for x,y in REGEXES]
def main():
""" Main method """
msg = []
output = Popen(['/usr/bin/sensors'], stdout=PIPE).communicate()[0].decode('utf-8')
lines = output.split('\n')[1:-1]
errors = []
for name, regex in RE_OBJS:
for line in lines:
match_obj = regex.search(line)
if match_obj:
temp = match_obj.group(1)
if float(temp) > THRESHOLD:
errors.append((name, float(temp)))
break
else:
print('Did not find match anywhere')
if len(errors) == 0:
return
# Make an email message
for name, temp in errors:
msg.append(name + ", measured= " + str(temp) + ". Exceeds threshold=" + str(THRESHOLD))
if len(msg) > 0 :
msg = "\n".join(msg)
if SEND_EMAIL:
mail_command = 'echo "%s" |mail -s "[cpu temp high]" %s'
Popen(mail_command % (msg, EMAIL_ADDRESS), shell=True)
else:
print(msg)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment