You are here

Twilio as SMS monitoring for Opsview

4 posts / 0 new
Last post
darrell.breeden_85546
darrell.breeden_85546's picture
Twilio as SMS monitoring for Opsview

Hello everyone, 

I've been using this for a while, but was kind of surprised when I wrote it how little I saw people trying to use Twilio to send SMS alerts. In order to use it you need the following:

  • A Twilio Account
  • A valid Twilio Number (Or a number verified with twilio)
  • cURL on the Opsview server

To use it, do the following:

  1. Add the following code to a script (notify_by_twilio) owned by nagios and executable in /usr/local/nagios/libexec/notifications

 

#!/bin/bash

###########################################
#
#    Twilio notification method
#    Written by Darrell James Breeden 
#    for Cumulose Code on December 10th
#                      2011
#
#
#
#    Changelog:
#
#    1.0    12-2011    Initial Release
#    1.1    08-14    Sanity Checks
#
#
#    Requirements
#    
#    * cURL : Requires curl to make API
#         calls to Twilio
#
###########################################

### Provide the ACCOUNTSID and AUTHTOKEN for your
### Twilio account
ACCOUNTSID=
AUTHTOKEN=

### CALLERID will be the phone number you are using in Twilio. If you are not using
### A twilio number, it must at least be verified by Twilio first
### or it will not be an authorized API Request
CALLERID=

### The "MOBILENUMBER" contact variable has to be created
### for Twilio to work. This is done in Opsview. If the method
### is called without this environment variable, it will break

CURL=`which curl`

if [ -z $CURL ] 
then
    echo "cURL is required for this method to work. Please install via apt or yum"
    exit 1
fi

if [ -z "$NAGIOS__CONTACTMOBILENUMBER" ]
    then
          echo "This script requires a contact mobile number "
          exit 1
     else
          PHONE=$NAGIOS__CONTACTMOBILENUMBER
fi

MSG=` echo -n "NOTIFICATION TYPE $NAGIOS_NOTIFICATIONTYPE "`
MSG=` echo -n "$MSG HOSTNAME $NAGIOS_HOSTNAME "`
MSG=` echo -n "$MSG HOST ADDRESS $NAGIOS_HOSTADDRESS "`

if [ -z "$NAGIOS_SERVICEDESC" ]
     then
          MSG=`echo -n "$MSG HOST STATE $NAGIOS_HOSTSTATE "`
else

          MSG=`echo -n "$MSG SERVICE $NAGIOS_SERVICEDESC "`
          MSG=`echo -n "$MSG SERVICE STATE $NAGIOS_SERVICESTATE "` 
          MSG=`echo -n "$MSG SERVICE DETAILS $NAGIOS_SERVICEOUTPUT "`

fi

RESPONSE=`curl -fSs -u "$ACCOUNTSID:$AUTHTOKEN" -d "From=$CALLERID" -d "To=$PHONE" -d "Body=$MSG" "https://api.twilio.com/2010-04-01/Accounts/$ACCOUNTSID/SMS/Messages" 2>&1`
        if [ $? -gt 0 ]; 
    then 
        echo "Failed to send SMS to $PHONE: $RESPONSE"
        else 
        echo "done"
        fi

 

 

2. Fill in the details on the script for AUTHTOKEN, ACCOUNTSID, and CALLERID

3. Create the notification method in Opsview

4. Required item for it will be MOBILENUMBER

5. Make sure your contacts have a MOBILENUMBER setup and that their notification methods use the twilio script and you're good to go!

darrell.breeden_85546
darrell.breeden_85546's picture
Voice Plugin

I'm also working on a service that will receive opsview alerts by API call, have them call the contact, and offer them the option to acknowledge it via phone utilizing the Opsview API.

jvullo
jvullo's picture
Hi

Hi Darrell,

Very nice, I was thinking about doing this.

Just curious - Where did you get the script for this?

Joe

 

darrell.breeden_85546
darrell.breeden_85546's picture
"Where did you find this?"

I wrote it