Temperature monitoring script with email alerts

We have quite a bit of expensive equipment in our server room and we’ve had the A/C fail a couple times. As a result, I’ve installed a raspberry pi zero with a DS18B20 temperature sensor connected to it to monitor the temperature of the room.  If it goes above a set threshold, it will send an email to the engineers so we can log in and shut stuff down until the problem is fixed.

 

This project branches off from the one I did earlier on monitoring temperature with a raspberry pi and MRTG.  This too uses MRTG but I won’t get into the details of that- you can see how I set that up here.

 

The big piece here is the alerting logic.  You’d be surprised how fast the temp can go up in a small room with lots of gear putting out a lot of heat.  For that reason, I monitor the temperature every minute.  If the current temperature exceeds the threshold I set in the script, it fires an email and sets an alert flag to true.  The reason I did this is so we don’t get an email every minute while the temperature is above threshold.  How irritating would that be?  So another piece of logic in the script checks to see if the alert flag has been tripped.  If it has, no email is sent until the temperature comes down below the threshold.  Then an all clear email is sent and the cycle repeats itself.

 

I used the instructions here to set up ssmtp on the pi.  In my case, I used our comcast email relay since we have comcast so the instructions for that are a little different.  You can also use your company’s own mail relay if you have one internally that can be used to send email to external addresses.  As has been my practice lately, I’ve uploaded the code to GitHub here for you to do with as you please.

 

As always, if you have any constructive criticism or comments, feel free to leave them below and I’ll get back to you ASAP.

16 thoughts on “Temperature monitoring script with email alerts

    • Yes not a stupid question at all. You can download the noobs image and install raspbian or other distributions. It walks you through setting everything up pretty well. Feel free to reply if you’re still having problems and I’ll see what I can do to help.

      Liked by 1 person

      • Thanks I think I got everything almost right. I think I’ve placed the scripts in the wrong directory. I put them in the /usr/bin just not sure if that’s the right location. Thanks in advance.

        Like

      • The get_temp.py is placed in /usr/local/bin/. Also had to modify the end of get_temp seems print”0″ has to have parenthesis. Print has become a function from what I’ve found print(“0”)

        Liked by 1 person

      • This maybe the last question the script isn’t running at startup and also it looks like once it meets one of the conditions it just stops instead of looping/resetting itself.

        Like

  1. Dinryu,

    I checked the get_temp.py script on my Pi, the format that’s in GitHub and the code as it is works there. I’m running Raspbian Jessie and python version 2.7.9 so maybe you have newer/different version of Python?

    root@oftemp:/etc# cat /etc/*release*
    PRETTY_NAME=”Raspbian GNU/Linux 8 (jessie)”
    NAME=”Raspbian GNU/Linux”
    VERSION_ID=”8″
    VERSION=”8 (jessie)”
    ID=raspbian
    ID_LIKE=debian
    HOME_URL=”http://www.raspbian.org/”
    SUPPORT_URL=”http://www.raspbian.org/RaspbianForums”
    BUG_REPORT_URL=”http://www.raspbian.org/RaspbianBugs”

    root@oftemp:/etc# python –version
    Python 2.7.9

    The 0 at the end of the script is there to satisfy MRTG’s need to have 2 values when collecting data. If you don’t care about that, you could just comment out the print statement in the loop section of the script. It looks like at first glance you wouldn’t even have to modify the check_temp.sh script because I’m discarding anything after the first line anyway.

    Also, rather than relying on a service or even in /etc/rc.local, I have the check_temp.sh script in root’s crontab to run every minute of every day:

    root@oftemp:/etc# crontab -l
    # Edit this file to introduce tasks to be run by cron.
    #
    # Each task to run has to be defined through a single line
    # indicating with different fields when the task will be run
    # and what command to run for the task
    #
    # To define the time you can provide concrete values for
    # minute (m), hour (h), day of month (dom), month (mon),
    # and day of week (dow) or use ‘*’ in these fields (for ‘any’).#
    # Notice that tasks will be started based on the cron’s system
    # daemon’s notion of time and timezones.
    #
    # Output of the crontab jobs (including errors) is sent through
    # email to the user the crontab file belongs to (unless redirected).
    #
    # For example, you can run a backup of all your user accounts
    # at 5 a.m every week with:
    # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
    #
    # For more information see the manual pages of crontab(5) and cron(8)
    #
    # m h dom mon dow command
    * * * * * /usr/local/bin/check_temp.sh

    I hope this helps answer some of your questions. Having a Linux/Unix background is a HUGE help when working with Raspbian. It is a Debian derivative just like CentOS is a Redhat derivative so the file locations and some commands are a bit different. If you’ve played with Ubuntu, then you’ve played with Debian as Ubuntu is also a Debian derivative.

    Shoot me a comment if you have any more questions!

    Liked by 1 person

  2. It should not be sending an email every minute- instrument some debugging in the script to see why it thinks it should be sending every time it runs. Don’t forget to check what’s going on in the alert_file and watch the alert_status variable as you’re testing. It should be pretty obvious- I started debugging by echoing out the current value of the temperature and what the threshold variable is set to. Good luck!

    Liked by 1 person

    • Whoops I should be clearer it’s sending an email to itself but as root. Keep getting “sh: 1: modprobe: not found” in these emails every minute. So it’s using up its daily sending quota a little before mid-day.

      Like

      • Figured it out you have to put the full path /sbin/modprobe w1-gpio when cron is being used. The emails aren’t sending itself an error log everything is finally running without errors. Thanks a lot Eric.

        Like

  3. hmm.. you don’t have to modprobe the driver every time the script runs- only at boot time. /etc/modprobe takes care of that during the boot process.

    OS requirements:
    add to end of /etc/modprobe:
    w1-gpio
    w1-therm

    add to /boot/config.txt:
    # Add 1wire support for DS18B20 temperature sensor
    dtoverlay=w1-gpio

    reboot

    Sorry it seems like I missed that somewhere- I updated the readme.md on github.

    Like

  4. Tried to run this script as well. The Python script reads the temperature just right, but the sh script runs into errors in line 57 (and 38, if i lower the threshold lower than the current temp):

    “-eq Ganzzahliger Ausdruck erwartet.

    “eq single-digit (unary) operator expected”

    Any ideas what causes this errors?

    Like

    • Python version 2.7.13-2
      debian:
      cat /etc/*release*
      PRETTY_NAME=”Raspbian GNU/Linux 9 (stretch)”
      NAME=”Raspbian GNU/Linux”
      VERSION_ID=”9″
      VERSION=”9 (stretch)”
      ID=raspbian
      ID_LIKE=debian
      HOME_URL=”http://www.raspbian.org/”

      Like

Leave a reply to Eric Cancel reply