e-Zest members share technology ideas to foster digital transformation.

How to enable SMS alerts in Nagios using AWS SNS

Written by Anagha Thorat | Feb 6, 2019 6:14:00 AM

Nagios is a powerful management and monitoring system. It helps in identifying the IT infrastructure problems. It also helps in resolving the problems before they affect the infrastructure and create critical issues.

One of the key components of Nagios is to alert before something breaks. Nagios sends e-mail alerts to recipients. But we cannot access the e-mails 24/7, we need some option where we have access to the device 24/7. The most convenient option would be SMS notifications. We will be integrating AWS SNS (Amazon Simple Notification Service) with Nagios to send SMS notifications for critical alerts. We will be using AWS CLI (AWS Command Line Interface) for enabling the SMS Alerts.

Prerequisites:
OS: Amazon Linux
AWS CLI Installed
RAM: 3GB

Check AWS CLI is installed

To check if AWS CLI is installed hit the following command
aws --version
If it throws an error like command not found install AWS CLI then you can use the below command
pip install AWS CLI--upgrade --user

Create SNS topic on AWS console

Follow the following steps:
  1. Log in to the AWS SNS Console
  2. Click on ‘Create New Topic’
  3. Enter the topic name and display name and click on ‘Create New Topic’
  4. After creation of topic click on the topic
  5. Click on ‘Create Subscription’
  6. While creating the subscription select the protocol as ‘SMS’
  7. Add mobile number as an endpoint
  8. Click on ‘Create Subscription’

Script to enable AWS SMS Alerts
Login to the Nagios Server

Create a file to write a script
vim /opt/smsalerts.sh

Copy the following code in the file
#!/bin/bash
export AWS_DEFAULT_REGION=us-east-1
/usr/bin/aws sns publish --topic-arn arn:aws:sns:us-east-1:XXXXXXXXX:Nagios_Notification --message "$*"
#EOF

Note: edit the topic-arn in the above script
Save the file.

Define command to enable SMS alerts in Nagios

Now edit the commands.cfg file of Nagios
vim /etc/Nagios/objects/commands.cfg

Add the following lines to the end of file
define command{
command_name notify-service-by-sns
command_line /usr/bin/printf "%b" "***** Nagios *****\nNotification Type: $NOTIFICATIONTYPE$\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\nDate/Time: $LONGDATETIME$\nAdditional Info:\n$SERVICEOUTPUT$\n" | xargs /opt/smsalerts.sh $1
}


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" | xargs /opt/smsalerts.sh $1
}

Define contact to enable SMS Alerts

Edit the templates.cfg
Find the code where contact is defined. Add the command name for service_notification_commands and host_notification_commands. Refer below code for reference


Restart the Nagios Service

Hit the below command to restart the Nagios
'service Nagios restart'