Hacky Alerting using Amazon Web Services SNS Weewx

160 views
Skip to first unread message

Josh Cagle

unread,
Apr 27, 2016, 1:23:00 AM4/27/16
to weewx-user
I thought I would share this with you all. Just in case anyone ever wanted to do something like this. Anyways, I setup a very primitive alert using a bash script that calls the AWS SNS. I borrowed quite a bit of my script from the link below to do this.


I modified my script to monitor the dewpoint on our farm. This alerts us to start our wind machine when it hits 36 degrees F.

You must have an Amazon Web Service account to use the SNS service. You can use the free tier and send 1000 emails and 100 SMS per month for free. 

I created an new user in my existing AWS account within IAM and assigned it the SNS full control role. 

I then installed the awscli.

# apt-get install awscli

I then configured aws for this system with the new user with the SNS full contorl role. 

 # aws configure

You have to enter the AWS key and AWS Secret key that were generated when you made the new user. You should also use an east coast default region if you want to use SMS. The us-west-2 region does not support SMS. 

I used the following command to create the topic.

# aws sns create-topic --name dewpoint-alert

I used the command below to subscribe to the topic.

# aws sns subscribe --topic arn:aws:sns:us-west-2:110057505160:dewpoint-alert --protocol email --notification-endpoint <email address>

If you look at the script you can see how I call the topic and alert with a message. 

You can look up the topics and subscriptions on your AWS account with the following commands. 

# aws sns list-topics
 
# aws sns list-subscriptions

Note: Substitute the email address with the email address you would like to use to get alerts. 

I assume that you substitute --protocol email with --protocol sms if you have the default region to an east cost default region.  

The script is called by a cron job that runs every 15 minutes.

# crontab -e

1,16,31,46 * * * * /root/scripts/weewxAlert.sh

Contents of weewxAlert.sh

#!/bin/bash


# This script will alert the subscribers every three minutes for a total of 5 iterations.  If dewpoint is below 36 degrees F. The alert will come from the AWS SNS topic.

logDir
="/var/log/weatherAlert/weatherAlert.log"


notify
() {
 stateFile
="/tmp/weatherAlertAlarm"
 
if test `find $stateFile 2>/dev/null`
 
then
 echo
"Five alerts already sent!" >> $logDir
 date
>>$logDir
 
else
 
for i in `seq 0 4`;
 
do
 aws sns publish
\
         
\--topic-arn arn:aws:sns:us-west-2:110057505160:dewpoint-alert\
         
--message "$alert" 2>/dev/null
 touch
/tmp/weatherAlertAlarm
 echo
"Message sent" date >> $logDir
 date
>> $logDir
 sleep
180
 
done
 
fi
}


# Gets dewpoint from /var/www/weewx/RSS/weewx_rss.xml
dewpoint
=`/bin/cat /var/www/weewx/RSS/weewx_rss.xml\
 | grep Dewpoint: | awk '{print $2}' | cut -f1 -d"&"`



if (($(echo "$dewpoint < 36" | bc -l) ));
then
 echo $dewpoint
 alert
="Weather Alert! DewPoint is $dewpoint. Start up the wind machine."
 notify $alert
else
       
# Script ends if the dewpoint is not below 36 degrees.
 echo
"Dewpoint is not below 36 degrees F" >> $logDir
 date
>> $logDir
fi

In order to acknowledge the alert. You have to kill the cronjob. If you want to start it up again and alert you have to remove the /tmp/weatherAlertAlarm

If any of you need any help alerting. Please let me know. I hope this was helpful. 

Josh Cagle

unread,
Apr 27, 2016, 1:26:43 AM4/27/16
to weewx-user
I just realized this might not be the best place for this. Could any of you point me in the right direction for this post? If any? Thanks. 

Thomas Keffer

unread,
Apr 27, 2016, 8:47:15 AM4/27/16
to weewx-user
Very slick!

Best place to put this is the Wiki. Otherwise, it will get lost in the thousands of weewx-user threads.

By any chance, is there a Python API for the AWS SNS service? Then it could be packaged as a weewx service, making it super easy for a user to install.

-tk



--
You received this message because you are subscribed to the Google Groups "weewx-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to weewx-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted
Message has been deleted

Chris Swanda

unread,
Apr 27, 2016, 8:55:34 AM4/27/16
to weewx-user
Very nice!  I DevOps a very large AWS presence for my day job (and nights and weekends, ha) and really need to take advantage of the free accounts for personal things.  

Thanks for sharing this.

vince

unread,
Apr 27, 2016, 9:45:35 AM4/27/16
to weewx-user
On Wednesday, April 27, 2016 at 5:47:15 AM UTC-7, Tom Keffer wrote:
By any chance, is there a Python API for the AWS SNS service? Then it could be packaged as a weewx service, making it super easy for a user to install.
 
There's a python api for AWS everything.

Try a google search for "aws sns python" for some good hits:
I wrote a little python utility to add route53 TXT records this way for $work.  Pretty straightforward.

Just a heads-up, it can be difficult finding 'complete' examples that do typical things admins normally want to work.  You generally have to cobble together snippets of examples and stackoverflow questions to figure it out.  Their examples suite is pretty skeletal.

Message has been deleted

Josh Cagle

unread,
Apr 27, 2016, 9:52:41 AM4/27/16
to weewx-user
Maybe have an acknowledge button on the web server to stop the alerts. Kind of like Zabbix or Nagios.
Reply all
Reply to author
Forward
0 new messages