Monday, September 16, 2013

Simple automated email alerts

You have a remote system which you want to know the state of. Or you want to monitor it. Or you want to be notified when something happens. But of course you don't want to constantly log into the system and check. There can be dozens parameters to check. You might forget one or two. And that is not a true zen if you have to do this manually.

So, here comes mailing notifications. That'd be awesome to get an email once a situation has occurred. But we don't want to spend time and money on a SMTP server. And we just want to monitor our home server. The solution is easy - use any of your GMail accounts (it can be any provider, but I'm here talking about GMail) or register a new one.

Then write a script. Copy paste from here and edit.
  1. #!/bin/sh
  2. source "${LOCAL_CONFIG_DIR}/"
  3. SUBJECT="Alert! Raspberry Pi Home. rootfs > ${2}%"
  4. TEXT="Alert on Raspberry Pi home. In the last ${1} seconds rootfs passed the threshold of ${2} and reached ${3}%."
  5. echo ${TEXT} | \
  6. env MAILRC=/dev/null from="${SENDER}" smtp-auth-user="${SENDER}" smtp-auth-password="${SENDER_PASSWORD}" \
  7. mailx -n \
  8.     -S smtp-use-starttls \
  9.     -S smtp-auth=login \
  10.     -S smtp="$SMTP_SERVER" \
  11.     -s "${SUBJECT}" "${RECEIVER}"

and here is a which is included at line #3
  3. SENDER_PASSWORD=" password"
  4. SMTP_SERVER=smtp://

I separated config from the alert script because you can have a number of alerts which you want to use the same config with.

This is the simplest automated notification functionality. It sends a notification in plain text. There are ways to attach files (read man mailx) and there are ways to include html as a body. For my purposes it was perfectly fine, so I didn't bother myself with decorations.

UPDATE [September 30, 2013]

Multiline emails can be sent using this technique:

  1. (
  2. cat << EOF
  3. Message from hostname '$HOSTNAME'.
  5. This system is reaching/exceeding the defined threshold value ($ALERT_THRESHOLD) during the last '$ALERT_TIMEINTVL' seconds.
  7. The current value is: $current_value
  9. Please take proper actions to correct this situation.
  10. EOF
  11. ) | \
  12. env MAILRC=/dev/null from="${SENDER}" smtp-auth-user="${SENDER}" smtp-auth-password="${SENDER_PASSWORD}" \
  13. mailx -n \
  14.     -S smtp-use-starttls \
  15.     -S smtp-auth=login \
  16.     -S smtp="$SMTP_SERVER" \
  17.     -s "${SUBJECT}" "${RECEIVER}"


  1. First of all, thank you for the excellent script that you have offered. I have used it for the past few weeks without any major issue. I am now interested in setting up email alerts.

    I created the file with my email information in it, but am unsure where to save it. It is referenced in the as located in {LOCAL_CONFIG_DIR}. Is that the notify folder or somewhere else? While error reporting may be nice my goal in setting up email reports is for a daily final log showing points earned in each account. Thank you.

  2. Hi Colin,
    talks about that variable a little bit. It is whatever directory you choose to hold your in. Just put this line in your .bashrc or configuration file of whatever shell you prefer

    export LOCAL_CONFIG_DIR=/directory/which/has/my/

    tell me if you have any other questions.

  3. hi, excellent job!
    I have a question about the sender email. Are both the sender and receiver email address my actual email accounts? I'm a bit confused with the password for sender email is the actual password for Basically this means I need to have two email accounts to set up the notification?

  4. You don't have to have two email addresses for notifications to work. You can send emails from the same address, mailx needs it to send a message. Yes, sender's email and password are your email and password.

  5. i am getting
    ./ 2: ./ source: not found

    1. may be because you don't have that file in the folder you're trying to access it from.

  6. Hi Sergery,

    I've been trying to setup email alerts for your bing script, and i keep running into this error:

    env: mailx: No such directory or file exist

    Any idea on how to solve? Thanks!

    1. sure, google for "install mailx {insert your OS here}"

    2. ohhh....i made a mistake of assuming mailx was installed in Raspian. Thanks!