Jump to content

SMS Integration on NAGIOS for sms19.info with HTTP API

From TetraWiki
Revision as of 08:15, 17 December 2013 by Biswajit (talk | contribs)


OBJECTIVE

SMS Integration on NAGIOS for sms19.info with HTTP API

Details Required

Basic SMS Sending Script for SMS19.INFO

/usr/local/nagios/libexec/sms.php

<html>
<body>
<?php
$user = 'Username';
$pass = 'Password';
$num = $argv[1];
$message = $argv[2];
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.sms19.info/ComposeSMS.aspx?username=".urlencode($user)."&password=".urlencode($pass)."&sender=MPEDIA&to=".urlencode($num)."&message=".urlencode($message)."&priority=1&dnd=1&unicode=0");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
?>
</body>
</html>

Testing of basic Script

On Linux Command prompt and in the /usr/local/nagios/libexec/ ( directory )

php sms.php 9810030018 "Test from Nagios"

Where sms.php is the script , 9810030018 is the mobile to which sms needs to be send and then "Test from Nagios" is the message

This test will send the SMS on the Mobile

Nagios Integration

Create /usr/local/nagios/libexec/sms.sh


#!/bin/sh
cd /usr/local/nagios/libexec/
php sms.php  9810030018 "$1"
php sms.php  9810077506 "$1"

Then

chmod 755 /usr/local/nagios/libexec/sms.sh
chown nagios.nagios /usr/local/nagios/libexec/sms.sh

Add the following in command.cfg

# 'notify-service-by-sms' command definition
define command{
command_name    notify-service-by-sms
command_line      /usr/local/nagios/libexec/sms.sh "--Nagios Service Notification-- Host: $HOSTNAME$, State: $HOSTSTATE$ Service $SERVICEDESC$ Description: $SERVICESTATE$ Time: $LONGDATETIME$ Service_output: $SERVICEOUTPUT$"
} 
# 'notify-host-by-sms' command definition
define command{
command_name    notify-host-by-sms
command_line      /usr/local/nagios/libexec/sms.sh "--Nagios Host Notification-- Host: $HOSTNAME$, State: $HOSTSTATE$, Time: $LONGDATETIME$"
}

Add the SMS contact in contact.cfg

define contact{
       contact_name                    biswajit-mm-sms               ; Short name of user
       use                             generic-contact         ; Inherit default values from generic-contact template (defined above)
       alias                           SMS-to-BB-MM      ; Full name of user
       service_notification_commands    notify-service-by-sms
       host_notification_commands       notify-host-by-sms
       }

Make it part of some contact Group to get the SMS ( contact.cfg)

define contactgroup{
       contactgroup_name       escalation
       alias                   Escalation Contact Group
       members                 biswajitmail, mailamit,biswajit-mm-sms
       }

Check the Nagios configuration via

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

Reload the Nagios to have the changes reflected

/etc/init.d/nagios reload


Once Any alert is generated the SMS will be in the following sample text

From: LM-MPEDIA
Received: Dec 17, 2013 11:24
--Nagios Service Notification-- Host: arihant.arihanteletricals.com, State: UP Service Cpu Utilization Description: CRITICAL Time: Tue Dec 17 11:23:55 I ST 2013 Service_output: CRITICAL - user: 21.08, nice: 0.50, sys: 5.40, iowait: 73.04, irq: 0.50, softirq: 2.46 idle: 0.50