Home > Uncategorized > Send 3000 free SMS alerts from Nagios with Twilio

Send 3000 free SMS alerts from Nagios with Twilio

You may instead wish to use a push notification service, since SMS can’t guarantee delivery.


After implementing event log monitoring of hyper-critical services, it might be worth increasing priority of the alert received when there is a failure.

The most attention grabbing alert for me is SMS. Even personal Email I may push aside until the end of the day. But, since I’m not a 14 year old girl, the number of SMS I receive is quite low.

I was, in a most abstract way, referred to Twilio http://www.twilio.com/ a service that provides API access to “cloud based” services. Yes, it’s a cool startup that will probably bomb in 6-12 months (see: conference.io); but all I care about is that I don’t need to hack away an API for Google Voice, and I get 3000 free SMS messages. This is way more than the hopefully 0 count of highly critical alerts I will be receiving.

The service is extremely easy to configure so you can be ready to send SMS:

1) Go to twilio.com and sign up.

2) To verify the phone number you wish to send SMS messages to, go to the Phone number management page, and click “verify a number.”

3) Put in the number for the cell phone you will send alerts. Twilio’s system will then place a call the cell phone.  When prompted, enter the verification code presented by the web page.

4) Find and note the following info on the Twilio My Account page:
Account SID
Auth Token (hidden by default, click the lock)
Sandbox Phone Number

5) On your Nagios box, use curl to POST the following to the Twilio SMS API, replacing the noted info:

curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/[AccountSID]/SMS/Messages.xml' \
-d 'From=[Sandbox Phone Number]' \
-d 'To=[Verified Mobile Number]' \
-d 'Body=[Message]' \
-u [Account SID]:[AuthToken]

Note that there is a character limitation on the message body (as it’s SMS)  of 160 characters.  Twilio will not split the body if greater than 160 characters.  The POST will simply fail, likely with error.

Thanks to stackoverflow user Matt Williamson, the use of a single line has been worked out that will truncate and send an SMS.

Here is an example with a service related notification command:

/usr/bin/printf "%b" "$HOSTNAME$ $SERVICEDESC$ in $SERVICESTATE$: $SERVICEOUTPUT$" | awk '{print substr($0,0,160)'} | xargs -0 -I {} echo {} | sed 's/\\$/\\\\$/g' | xargs -0 -I {} curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/[AccountSID]/SMS/Messages.xml' -d 'From=[Sandbox Phone Number]' -d 'To=[Verified Mobile Number]' -d "Body={}" -u [Account SID]:[AuthToken]

Note the double-quotes around the Body POST variable.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: