Sending Nagios notifications with AWS SNS
The original mini-tutorial for this post is here. Please be sure to Brooks Garrett's website.
A short tutorial to setup SMS messaging for Nagios 4 notifications. This tutorial is for Nagios 4 on Ubuntu 18.04. This tutorial will assume you currently have an AWS account and have already setup SNS, working Nagios installation at `/usr/local/nagios/', Python 2.7, and root access to your server.
Download this oldish python script from Github ( https://github.com/sricola/pyAmazonSNS) and install it in `/usr/local/nagios/NagiosAWSSNS/send_sns.py`. Add your AWS IAM ID and Key in this line:
sns = boto.connect_sns('AWS_ACCESS_KEY_ID','AWS_SECRET_ACCESS_KEY', region=region)
Update the `--topic='TOPIC_NAME'` in the following template to match the Topic you created in AWS SNS, and add the template to the `/usr/local/nagios/etc/objects/commands.cfg` file:
# 'notify-host-by-sns' command definition define command{ command_name notify-host-by-sns command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /opt/pyAmazonSNS/send_sns.py --topic="TOPIC_NAME" --sub="$NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$" } # 'notify-service-by-sns' command definition define command{ command_name notify-service-by-sns command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /opt/pyAmazonSNS/send_sns.py --topic="TOPIC_NAME" --sub="$NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$" }
Lastly, update the '/usr/local/nagios/etc/objects/contacts.cfg` file to use the above commands. Under the contact definition you want to use for these SMS messages, add the following lines (see docs for more information):
define contact { contact_name nagiosadmin use generic-contact alias Nagios Admin host_notifications_enabled 1 service_notifications_enabled 1 service_notification_options w,u,c,r host_notification_options d,u,r service_notification_commands notify-service-by-email, notify-service-by-sns host_notification_commands notify-host-by-email, notify-host-by-sns email <your-email-address> can_submit_commands 1 }
Finally, restart Nagios and check there were no errors in syslog.
service nagios restart tail /usr/local/nagios/var/nagios.log