vADC Docs

Traffic Managers can Tweet Too

by on ‎03-15-2013 10:21 AM - edited on ‎06-02-2015 12:04 PM by PaulWallace (1,627 Views)

If tweeting is good enough for the Pope, then Stingray can certainly get in on the game!  If you use tools to monitor Twitter accounts, this article will explain how you can direct Stingray's event log messages to a nominated twitter account so that you can be alerted immediately a key event occurs:

 

twitter.png

 

You might also like to check out the companion article - TrafficScript can Tweet Too.

 

Creating the Twitter Application

 

Posting to Twitter is now a little involved.  You need to create an application with the correct access (Read + Write), and then create the appropriate oauth parameters so that clients can authenticate themselves to this application.

 

To start off, log on to https://dev.twitter.com/apps and create an application as follows:

 

twitterapp.png

 

Follow the steps in https://dev.twitter.com/docs/auth/tokens-devtwittercom to create the access tokens so that you can post to your application from your own twitter account.

 

The OAuth authentication for your application requires two public/secret key pairs that you can generate from the management website:

 

twitteroauth.png

 

Configuring Stingray

 

Create the event action

 

Download and edit the following event handling script, updating the oauth tokens to match your own application and access tokens:

 

#!/usr/bin/python  
  
import sys  
import socket  
import oauth2 as oauth  
  
def oauth_req(url, http_method="GET", post_body=None,  
        http_headers=None):  
    consumer = oauth.Consumer(key="Pplp3z4ogRW4wKP3YOlAA", secret="jbWlWYOSgzC9XXXXXXXXXXXXXXXX")  
    token = oauth.Token(key="1267105740-xhMWKdsNqoKAof7wptTZ5PmNrodBJcQm1tQ5ssR", secret="p8GCJUZLXk1AeXXXXXXXXXXXX")  
    client = oauth.Client(consumer, token)  
  
    resp, content = client.request(  
        url,  
        method=http_method,  
        body=post_body,  
        headers=http_headers  
    )  
    return content  
  
msg = sys.argv[-1]  
words = msg.split( '\t' )  
  
host = socket.gethostname().split( '.' )[0]  
  
update = oauth_req(  
  "https://api.twitter.com/1.1/statuses/update.json",  
  "POST",  
  "status=" + words[0] + ": " + words[-1] + " (" + words[-2] + " " + words[1].split('/')[-1] + ") #" + host  
)  

 

From your Stingray Admin Server, create a new External Program Action named 'Tweet', and upload the edited event handling script.

 

Note that the script won't work on the Stingray Virtual Appliance (because it does not have a python interpreter), and on a Linux or Solaris host, you will need to install the python oauth2 module (pip install oauth2).

 

Test the event out with the: 'Update and Test' button in the edit page for the Tweet action, and review the event log to confirm that there are no errors:

 

eventupdateandtest.png

 

You should see the test tweet in your twitter feed:

 

tweettest.png

 

Wire the action to the appropriate events

 

Wire up an event handler to trigger the Tweet action when the appropriate events are raised:

 

alerts.png

 

... and then site back and follow your twitter stream to see what's up with your Stingrays.

Contributors