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.
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:
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.
alert.sh
- #!/bin/sh
- source "${LOCAL_CONFIG_DIR}/mailx.config.sh"
- SUBJECT="Alert! Raspberry Pi Home. rootfs > ${2}%"
- TEXT="Alert on Raspberry Pi home. In the last ${1} seconds rootfs passed the threshold of ${2} and reached ${3}%."
- echo ${TEXT} | \
- env MAILRC=/dev/null from="${SENDER}" smtp-auth-user="${SENDER}" smtp-auth-password="${SENDER_PASSWORD}" \
- mailx -n \
- -S smtp-use-starttls \
- -S smtp-auth=login \
- -S smtp="$SMTP_SERVER" \
- -s "${SUBJECT}" "${RECEIVER}"
and here is a mailx.config.sh which is included at line #3
- RECEIVER=yourself@gmail.com
- SENDER=alert...@gmail.com
- SENDER_PASSWORD="alert...@gmail.com password"
- SMTP_SERVER=smtp://smtp.gmail.com:587
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:
- (
- cat << EOF
- Message from hostname '$HOSTNAME'.
- This system is reaching/exceeding the defined threshold value ($ALERT_THRESHOLD) during the last '$ALERT_TIMEINTVL' seconds.
- The current value is: $current_value
- Please take proper actions to correct this situation.
- EOF
- ) | \
- env MAILRC=/dev/null from="${SENDER}" smtp-auth-user="${SENDER}" smtp-auth-password="${SENDER_PASSWORD}" \
- mailx -n \
- -S smtp-use-starttls \
- -S smtp-auth=login \
- -S smtp="$SMTP_SERVER" \
- -s "${SUBJECT}" "${RECEIVER}"
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.
ReplyDeleteI created the mailx.config.sh file with my email information in it, but am unsure where to save it. It is referenced in the onScriptComplete.sh 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.
Hi Colin,
ReplyDeletehttp://sealemar.blogspot.com/2013/10/bing-rewards-automation-script-unix-cron.html
talks about that variable a little bit. It is whatever directory you choose to hold your mailx.config.sh in. Just put this line in your .bashrc or configuration file of whatever shell you prefer
export LOCAL_CONFIG_DIR=/directory/which/has/my/mailx.config.sh/in/it
tell me if you have any other questions.
hi, excellent job!
ReplyDeleteI 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 alert...@gmail.com.. the password for sender email is the actual password for alert...@gmail.com? Basically this means I need to have two email accounts to set up the notification?
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.
ReplyDeletei am getting
ReplyDelete./alert.sh: 2: ./alert.sh: source: not found
may be because you don't have that file in the folder you're trying to access it from.
DeleteHi Sergery,
ReplyDeleteI'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!
sure, google for "install mailx {insert your OS here}"
Deleteohhh....i made a mistake of assuming mailx was installed in Raspian. Thanks!
Delete