Jun 152015
 

I recently found out that Telstra has a SMS API, and for now, it’s free to use for up to 1000 SMSes a month for Australians (sorry rest of the world).

As I’ve been building up Zabbix for our work environment where SMSes are used regularly to alert techs to critical alerts, this piqued my interest as this means that for now, I could have SMS alerts for my homelab, and potentially implement the same solution at work.

To use the Telstra SMS API, you will first need to sign up for API keys to use it. Once you have the API keys, simple HTTP requests are used to send and check on SMSes.
The Quick start documentation contains all the scripts you need to get started, so I’ll skip over all of the basic stuff and move straight onto the Zabbix integration.

Building the scripts

Since the Telstra API uses OAuth2 Authentication tokens, We’ll need to take this into account when trying to send SMSes.
I’ve decided to write this script in python like the rest of my Zabbix scripts, and I’m also using flat files for key storage for this initial test implementation.

The full script is on GitHub, but I’ll do a quick explanation here.

First thing the script needs is to check for a token, and if one doesn’t exist or is expired, retrieve the an authentication token.
I’ve stored the authentication token in /tmp/telstraauth for now.

f = open('/tmp/telstraauth','r+')
filetoken = f.read()
f.close()
dicttoken = json.loads(filetoken)
authtoken = dicttoken["access_token"]
dtobj = datetime.datetime.strptime(dicttoken["datetime"], '%Y-%m-%d %H:%M:%S.%f')
expires = dtobj + datetime.timedelta(0,int(dicttoken["expires_in"]))
if expires < now: print "expired" response = urllib2.urlopen(url) newtokendict = json.loads(response.read()) newtokendict["datetime"] = str(now) authtoken = newtokendict["access_token"] newjsontoken = json.JSONEncoder().encode(newtokendict) f = open('/tmp/telstraauth','w') print "new token : "+newjsontoken f.write(newjsontoken) f.close() else: print "not expired"

Once there is a token, the script will then send the message.

smsdata = { 'to':smsrcpt, 'body':subject+" "+body }
headers = { 'Content-type':'application/json','Authorization':'Bearer '+str(authtoken) }
url = "https://api.telstra.com/v1/sms/messages"

req = urllib2.Request(url,headers=headers,data=json.dumps(smsdata))
msg = urllib2.urlopen(req)

Configuring Zabbix

Creating a new Media Type

Like the other notification methods, this requires a new Media Type. Under Administration -> Media Types, create a new media type and give it a name. I've used Telstra SMS. Then choose the type Script and enter in the script name.
I've named the script telstrasms.py

Configuring User Media Type

Once the Media type is created, you'll need to define the media type on the user to set a number to send messages to. Under the User -> Media tab, create a new media type, and define a number to send to.

Configuring Alert

Once the user has been configured, you'll need to set up the actions to use the new SMS media type.
Under the Operations tab of the Action, make sure the Send Only to is set to Telstra SMS, and the user that is set up there has the correct media type set up.

Once that is done, you should get alerts via SMS from Telstra.

Share

  One Response to “Using The Telstra SMS API With Zabbix”

  1. […] Using The Telstra SMS API With Zabbix […]

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)