Page 1 of 1

SMS Notifications in Nagios XI

Posted: Tue Apr 14, 2015 3:30 am
by M4nd4R
I am using Nagios XI in order to monitor many servers across our infrastructure. I have been getting email notifications successfully, but wish to configure SMS notifications in Nagios XI.

The main problem is, SMS gateway is on another server and in order to send any SMS from Nagios server, I need to use ssh as follows:

Code: Select all

ssh -i /path/to/ssh-key user@SMS-GATEWAY '/path/to/send_sms_script.sh /path/to/contact_list.txt /path/to/message.txt'
where, send_sms_script.sh is the script that takes to parameters - contact_list.txt, list of contact numbers to which SMS is to be sent and message.txt is the file that bears the actual message.

In order to send any Nagios notification to a user, the message.txt has to be edited accordingly with the service/host details. For that, I chose to edit the file message.txt before sending any SMS notification.

With these considerations, I created a command notify_service_by_sms.sh on Nagios server as follows:

Code: Select all

#!/bin/bash

while read line
do
        DATA="$line"
done
SDESC=`echo $DATA | awk -F"," '{print $1}'`
HALIAS=`echo $DATA | awk -F"," '{print $2}'`
SSTATE=`echo $DATA | awk -F"," '{print $3}'`
SDATE=`echo $DATA | awk -F"," '{print $4}'`
ADDINFO=`echo $DATA | awk -F"," '{print $5}'`

SMSG="/path/to/message.txt"
SCONT="/path/to/contact_list.txt"

MSG="**Nagios Alert**\nService:$SDESC\nHost:$HALIAS\nState:$SSTATE\nTime:$SDATE\nAdditional Info:$ADDINFO"

/usr/bin/ssh -i /path/to/ssh-key user@SMS-GATEWAY "echo \"$MSG\" > /path/to/message.txt && /path/to/send_sms_script.sh /path/to/contact_list.txt /path/to/message.txt"
I tested the above script with following command to verify that it works well, and it worked well too:

Code: Select all

printf "%b" "Load Average,Test Host,Warning,14-Apr-2015 14:00,Test Info" | /usr/local/nagios/libexex/notify_service_by_sms.sh
Then I created a command notify-service-by-sms in Nagios XI (CCM > Commands) as follows:

Code: Select all

/usr/bin/printf "%b" "$SERVICEDESC$,$HOSTALIAS$,$SERVICESTATE$,$LONGDATETIME$,$SERVICEOUTPUT$\n" | /usr/local/nagios/libexec/notify_service_by_sms.sh
Then, in CCM > Contacts, I selected a user and in "Alert Settings" tab, I selected 'notify-service-by-email' and 'notify-service-by-sms' as the "Service Notification Commands".
I also made sure that, this particular user is allocated a particular service that is always in critical condition, so that I get notified periodically (5 minutes).

After saving the configurations, I could find that, I am getting email notifications, but SMS notifications are not being received.

Can you please advice me in this concern?

Re: SMS Notifications in Nagios XI

Posted: Tue Apr 14, 2015 9:32 am
by tmcdonald
When you test from the command line, are you doing so as root or as nagios? The nagios user will need to be able to run the script.

Re: SMS Notifications in Nagios XI

Posted: Tue Apr 14, 2015 10:23 pm
by M4nd4R
@tmcdonald- That's really a very nice catch. I tested the script as a root user and over that the ssh-key had 600 permissions.
I created a new ssh-key for nagios user and made necessary changes in the script.
Now, it's working absolutely fine.

Thanks tmcdonald. :)

Re: SMS Notifications in Nagios XI

Posted: Wed Apr 15, 2015 9:14 am
by tmcdonald
I'll be closing this thread now, but feel free to open another if you need anything in the future!