Perl Twitter Proxy
Problem this snippet solves:
This example is a HTTP daemon that implements the EventNotification interface to receive iControl status messages. It will then proxy that message and post it to a specified Twitter account.
Who says the BIG-IP can't be social! This perl program implements a HTTP daemon running on port 8088 and listens for the iControl Management::EventNotification::changes_occurred method. There are a few settings you'll want to configure before running this program. Namely the BIGIP variables containing the BIG-IP you are connecting to, the LISTENER variables that specify where this perl module is running, and the TWITTER credentials for your Twitter account.-->
To make it plain dead simple, you can just plop this directly on your BIG-IP and run it as-is with only setting the Twitter credentials. Keep in mind that installing anything on your BIG-IP could effect performance so do so at your own risk.
Once you have everything setup, just run the app and you should be good to go.
The subscription is only set to monitor pool members, but you could easily modify the event_type_list in the setupSubscription subroutine by adding more event types you would like to monitor.
Happy Twittering!
Code :
#!/usr/bin/perl #---------------------------------------------------------------------------- # The contents of this file are subject to the "END USER LICENSE AGREEMENT FOR F5 # Software Development Kit for iControl"; you may not use this file except in # compliance with the License. The License is included in the iControl # Software Development Kit. # # Software distributed under the License is distributed on an "AS IS" # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See # the License for the specific language governing rights and limitations # under the License. # # The Original Code is iControl Code and related documentation # distributed by F5. # # The Initial Developer of the Original Code is F5 Networks, # Inc. Seattle, WA, USA. Portions created by F5 are Copyright (C) 1996-2007 F5 Networks, # Inc. All Rights Reserved. iControl (TM) is a registered trademark of F5 Networks, Inc. # # Alternatively, the contents of this file may be used under the terms # of the GNU General Public License (the "GPL"), in which case the # provisions of GPL are applicable instead of those above. If you wish # to allow use of your version of this file only under the terms of the # GPL and not to allow others to use your version of this file under the # License, indicate your decision by deleting the provisions above and # replace them with the notice and other provisions required by the GPL. # If you do not delete the provisions above, a recipient may use your # version of this file under either the License or the GPL. #---------------------------------------------------------------------------- use SOAP::Transport::HTTP; my $INFO = 1; my $DEBUG = 1; my $BIGIP = "localhost"; my $BIGIP_PORT = "80"; my $BIGIP_UID = ""; my $BIGIP_PWD = ""; my $LISTENER_NAME = "iControlTwitterProxy"; my $LISTENER_HOST = "localhost"; my $LISTENER_PORT = 8088; my $LISTENER_UID = ""; my $LISTENER_PWD = ""; my $TWITTER_UID = "TWITTER_ACCOUNT_GOES_HERE"; my $TWITTER_PWD = "TWITTER_PASSWORD_GOES_HERE"; # don't want to die on 'Broken pipe' or Ctrl-c $SIG{PIPE} = 'IGNORE'; ### ### API for the SOAP server for cmd_dispatch ### package Management::EventNotification; BEGIN { push (@INC, "."); } use iControlTypeCast; #------------------------------------------------------------------------------------------ # sub events_occurred #------------------------------------------------------------------------------------------ sub events_occurred { my ($self,$event_source,$subscription_id,$event_data_list,$time_stamp) = @_; if ( $DEBUG ) { print "events_occurred() was just invoked!\n"; } # $event_source $system_id = $event_source->{"system_id"}; $url = $event_source->{"url"}; $year = $time_stamp->{"year"}; $month = $time_stamp->{"month"}; $day = $time_stamp->{"day"}; $hour = $time_stamp->{"hour"}; $minute = $time_stamp->{"minute"}; $second = $time_stamp->{"second"}; if ( $INFO ) { print "Event Occurred\n"; } if ( $INFO ) { print " System Id : $system_id\n"; } if ( $INFO ) { print " Url : $url\n"; } if ( $INFO ) { print "subscription id: $subscription_id\n"; } if ( $INFO ) { print " Time Stamp : $year:$month:$day/$hour:$minute:$second\n"; } if ( $DEBUG ) { print " Event Data List\n"; } foreach $event_data (@{$event_data_list}) { processEvent($event_data); } } #------------------------------------------------------------------------------------------ # sub processEvent #------------------------------------------------------------------------------------------ sub processEvent() { my ($event_data) = (@_); $username = $event_data->{"username"}; $sequence_number = $event_data->{"sequence_number"}; $event_type = $event_data->{"event_type"}; $object_type = $event_data->{"object_type"}; @event_data_item_list = @{$event_data->{"event_data_item_list"}}; if ( $DEBUG ) { print "---------------\n"; } if ( $DEBUG ) { print " seq # : $sequence_number\n"; } if ( $DEBUG ) { print " Event Type : $event_type\n"; } if ( $DEBUG ) { print "Object Type : $object_type\n"; } if ( $DEBUG ) { print "Event List\n"; } if ( $DEBUG ) { print " ----------------\n"; } if ( "OBJECTTYPE_RULE" eq $object_type ) { &processRule($event_data); } elsif ( "OBJECTTYPE_INTERFACE" eq $object_type ) { &processInterface($event_data); } elsif ( "OBJECTTYPE_MONITOR_INSTANCE" eq $object_type ) { if ( $DEBUG ) { print "Ignoring object type '$object_type'\n"; } } elsif ( "OBJECTTYPE_POOL" eq $object_type ) { if ( $DEBUG ) { print "Ignoring object type '$object_type'\n"; } } elsif ( "OBJECTTYPE_POOL_MEMBER" eq $object_type ) { if ( $DEBUG ) { &processPoolMember($event_data); } } elsif ( "OBJECTTYPE_VIRTUAL_SERVER" eq $object_type ) { if ( $DEBUG ) { print "Ignoring object type '$object_type'\n"; } } else { if ( $DEBUG ) { print "Ignoring object type '$object_type'\n"; } } } #------------------------------------------------------------------------------------------ # sub processRule #------------------------------------------------------------------------------------------ sub processRule() { my ($event_data) = (@_); @event_data_item_list = @{$event_data->{"event_data_item_list"}}; $event_type = $event_data->{"event_type"}; my $EVENTTYPE_MAP = { "EVENTTYPE_MODIFY" => "MODIFIED", "EVENTTYPE_CREATE" => "CREATED", "EVENTTYPE_DELETE" => "DELETED", }; foreach $event_data_item (@event_data_item_list) { $event_data_name = $event_data_item->{"event_data_name"}; $event_data_type = $event_data_item->{"event_data_type"}; $event_data_value = $event_data_item->{"event_data_value"}; if ( $DEBUG ) { print " Item Data : $event_data_name = $event_data_value ($event_data_type)\n"; } if ( $event_data_name eq "rule_name") { $rule_name = $event_data_value; } } if ( "" ne $rule_name ) { $Action = $EVENTTYPE_MAP->{$event_type}; $tweet = "Rule '$rule_name' has been $Action."; &processTweet($tweet); } } #------------------------------------------------------------------------------------------ # sub processInterface #------------------------------------------------------------------------------------------ sub processInterface() { my ($event_data) = (@_); @event_data_item_list = @{$event_data->{"event_data_item_list"}}; $event_type = $event_data->{"event_type"}; my $MEDIA_STATUS_MAP = { "0" => "UP", "1" => "DOWN", "2" => "DISABLED", "3" => "UNINITIALIZED", "4" => "LOOPBACK", "5" => "UNPOPULATED", }; $interface_name = ""; $interface_status = ""; foreach $event_data_item (@event_data_item_list) { $event_data_name = $event_data_item->{"event_data_name"}; $event_data_type = $event_data_item->{"event_data_type"}; $event_data_value = $event_data_item->{"event_data_value"}; if ( $DEBUG ) { print " Item Data : $event_data_name = $event_data_value ($event_data_type)\n"; } if ( $event_data_name eq "interface_name") { $interface_name = $event_data_value; } elsif ( $event_data_name eq "interface_status") { $interface_status = $event_data_value; } } if ( ("" ne $interface_name) && ("" ne $interface_status) ) { $sStatus = $MEDIA_STATUS_MAP->{$interface_status}; $tweet = "Interface '$interface_name' status changed to $sStatus."; &processTweet($tweet); } } #------------------------------------------------------------------------------------------ # sub processPoolMember #------------------------------------------------------------------------------------------ sub processPoolMember() { my ($event_data) = (@_); @event_data_item_list = @{$event_data->{"event_data_item_list"}}; $pool_name = ""; $member_ip = ""; $member_port = ""; $new_sessions = ""; $availability = ""; $enabled = ""; $monitor_status = ""; $session_status = ""; foreach $event_data_item (@event_data_item_list) { $event_data_name = $event_data_item->{"event_data_name"}; $event_data_type = $event_data_item->{"event_data_type"}; $event_data_value = $event_data_item->{"event_data_value"}; if ( $DEBUG ) { print " Item Data : $event_data_name = $event_data_value ($event_data_type)\n"; } if ( $event_data_name eq "pool_member_pool_name") { $pool_name = $event_data_value; } elsif ( $event_data_name eq "pool_member_addr") { $member_ip = $event_data_value; } elsif ( $event_data_name eq "pool_member_port") { $member_port = $event_data_value; } elsif ( $event_data_name eq "pool_member_new_session_enable") { $new_sessions = $event_data_value; } elsif ( $event_data_name eq "generic_status_availability_state") { $availability = $event_data_value; } elsif ( $event_data_name eq "generic_status_enabled_state") { $enabled = $event_data_value; } elsif ( $event_data_name eq "generic_status_enabled_state") { $enabled = $event_data_value; } elsif ( $event_data_name eq "pool_member_monitor_status") { $monitor_status = $event_data_value; } elsif ( $event_data_name eq "pool_member_session_status") { $session_status = $event_data_value; } } if ( ("" ne $pool_name) and ("" ne $member_ip) and ("" ne $member_port) and ("" ne $new_sessions) and ("" ne $availability) and ("" ne $enabled) ) { &processMemberChange($pool_name, $member_ip, $member_port, $new_sessions, $availability, $enabled); } elsif ( ("" ne $pool_name) and ("" ne $member_ip) and ("" ne $member_port) and ("" ne $monitor_status) ) { &processMonitorChange($pool_name, $member_ip, $member_port, $monitor_status, $session_status); } else { if ( $DEBUG ) { print "Unqualifying event notification\n"; } } } #------------------------------------------------------------------------------------------ # sub processMemberChange #------------------------------------------------------------------------------------------ sub processMemberChange() { my ($pool_name, $member_ip, $member_port, $new_sessions, $availability, $enabled) = (@_); if ( ("" ne $pool_name) and ("" ne $member_ip) and ("" ne $member_port) and ("" ne $new_sessions) and ("" ne $availability) and ("" ne $enabled) ) { if ( $DEBUG ) { print "Processing Member Change Tweet:\n"; } if ( $DEBUG ) { print " Pool Name: $pool_name\n"; } if ( $DEBUG ) { print " Member : $member_addr:$member_port\n"; } if ( $DEBUG ) { print " New Sess : $new_sessions\n"; } if ( $DEBUG ) { print " Avail : $availability\n"; } if ( $DEBUG ) { print " Enabled : $enabled\n"; } $sEnabled = "UNKNOWN"; $sAvailable = "ERROR"; $ENABLED_MAP = { "1" => "UP", "2" => "DOWN" }; $sEnabled = $ENABLED_MAP->{$enabled}; if ( "" eq $sEnabled ) { $sEnabled = "UNKNOWN"; } # if ( $enabled eq "1") { $sEnabled = "UP"; } # elsif ( $enabled eq "2") { $sEnabled = "DOWN"; } # else { $sEnabled = "UNKNOWN"; } $AVAILABILITY_MAP = { "0" => "ERROR", "1" => "ACTIVE", "2" => "INACTIVE", "3" => "INACTIVE (Needs Intervention)", "4" => "INACTIVE (Unknown)", "5" => "INACTIVE (Unlicensed)", }; $sAvailable = $AVAILABILITY_MAP->{$availability}; if ( "" eq $sAvailable ) { $sAvailable = "ERROR"; } # if ( $availability eq "0") { $sAvailable = "ERROR"; } # elsif ( $availability eq "1") { $sAvailable = "ACTIVE"; } # elsif ( $availability eq "2") { $sAvailable = "INACTIVE"; } # elsif ( $availability eq "3") { $sAvailable = "INACTIVE (Needs Intervention)"; } # elsif ( $availability eq "4") { $sAvailable = "INACTIVE (Unknown)"; } # elsif ( $availability eq "5") { $sAvailable = "INACTIVE (Unlicensed)"; } # else { $sAvailable = "ERROR"; } if ( ("UP" eq $sEnabled) and ("ACTIVE" eq $sAvailable) ) { $tweet = "Member $member_ip:$member_port in pool $pool_name seems to have recovered and is ready to rock!"; } else { $tweet = "Member $member_ip:$member_port in pool $pool_name seems to be having problems. It is marked '$sEnabled' with a '$sAvailable' state." } &processTweet($tweet); } } #------------------------------------------------------------------------------------------ # sub processMonitorChange #------------------------------------------------------------------------------------------ sub processMonitorChange() { my ($pool_name, $member_ip, $member_port, $monitor_state, $session_status) = (@_); if ( ("" ne $pool_name) and ("" ne $member_ip) and ("" ne $member_port) and ("" ne $monitor_state) ) { if ( $DEBUG ) { print "Processing Monitor Change Tweet:\n"; } if ( $DEBUG ) { print " Pool Name: $pool_name\n"; } if ( $DEBUG ) { print " Member : $member_addr:$member_port\n"; } if ( $DEBUG ) { print " Mon State: $monitor_state\n"; } $MONITOR_STATE_MAP = { "0" => "UNCHECKED", "1" => "CHECKING", "2" => "UP", "3" => "DOWN", "4" => "FORCED DOWN", "5" => "MAINT", "6" => "ADDRESS DOWN", "7" => "DOWN BY IRULE", "8" => "MARKED DOWN", }; $SESSION_STATUS_MAP = { "0" => "ENABLED", "1" => "DISABLED", "2" => "FORCED DISABLED", "3" => "ADDRESS DISABLED", }; $sState = $MONITOR_STATE_MAP->{$monitor_state}; $sSessionStatus = $SESSION_STATUS_MAP->{$session_status}; if ( ("UP" eq $sState) and ("ENABLED" eq $sSessionStatus) ) { $tweet = "Member $member_ip:$member_port in pool $pool_name seems to have recovered and is ready to rock!"; } else { $tweet = "Member $member_ip:$member_port in pool $pool_name is down. Monitor is reporting a monitor/session state = '$sState/$sSessionStatus'"; } &processTweet($tweet); } } #------------------------------------------------------------------------------------------ # sub processTweet #------------------------------------------------------------------------------------------ sub processTweet() { my ($tweet) = (@_); # call curl to submit POST to twitter $cmd = "curl --data-ascii \"status=$tweet\" --user $TWITTER_UID:$TWITTER_PWD http://twitter.com:80/statuses/update.xml"; if ( $DEBUG ) { print "$cmd\n"; } system($cmd); } #------------------------------------------------------------------------------------------ # sub SOAP::Transport::HTTP::Client::get_basic_credentials #------------------------------------------------------------------------------------------ sub SOAP::Transport::HTTP::Client::get_basic_credentials { return "$BIGIP_UID" => "$BIGIP_PWD"; } #------------------------------------------------------------------------------------------ # sub setupSubscription #------------------------------------------------------------------------------------------ sub setupSubscription() { my $exists = 0; my $sProtocol = "http"; if ( $BIGIP_PORT eq 443 ) { $sProtocol = "https"; } $EventSubscription = SOAP::Lite -> uri('urn:iControl:Management/EventSubscription') -> proxy("$sProtocol://$BIGIP:$BIGIP_PORT/iControl/iControlPortal.cgi"); eval { $EventSubscription->transport->http_request->header ( 'Authorization' => 'Basic ' . MIME::Base64::encode("$BIGIP_UID:$BIGIP_PWD", '') ); }; if ( $DEBUG ) { print "Requesting Subscription List...\n"; } # Get a list of the subscriptions and look for a match. $soapResponse = $EventSubscription->get_list(); if ( !$soapResponse->fault ) { @sub_list = @{$soapResponse->result}; if ( $DEBUG ) { print "Requesting Subscritpion Details...\n"; } $soapResponse = $EventSubscription->query ( SOAP::Data->name(id_list => [@sub_list]) ); if ( !$soapResponse->fault ) { @SubscriptionStatusList = @{$soapResponse->result}; @params = $soapResponse->paramsout; @SubscriptionDefinitionList = @{@params[0]}; foreach $SubscriptionDefinition (@SubscriptionDefinitionList) { $id = $SubscriptionDefinition->{"id"}; $details = $SubscriptionDefinition->{"details"}; $name = $details->{"name"}; @event_type_list = @{$details->{"event_type_list"}}; $url = $details->{"url"}; $url_credentials = $details->{"url_credentials"}; $auth_mode = $url_credentials->{"auth_mode"}; $username = $url_credentials->{"username"}; $password = $url_credentials->{"password"}; $ttl = $details->{"ttl"}; $min_events_per_timeslice = $details->{"min_events_per_timeslice"}; $max_timeslice = $details->{"max_timeslice"}; $enabled_state = $details->{"enabled_state"}; if ( $DEBUG ) { print "Comparing subscription ($name, $url)...\n"; } if ( ($name eq $LISTENER_NAME) && ($url eq "http://$LISTENER_HOST:$LISTENER_PORT") ) { if ( $DEBUG ) { print "Found a match!!!\n"; } $exists = 1; break; } } } } if ( !$exists ) { if ( $DEBUG ) { print "Subscription doesn't exist, creating one...\n"; } # No subscription exists, need to create one... $name = $LISTENER_NAME; @event_type_list = ( "EVENTTYPE_CREATE", "EVENTTYPE_MODIFY", "EVENTTYPE_DELETE", "EVENTTYPE_POOL_MEMBER" ); $url = "http://$LISTENER_HOST:$LISTENER_PORT"; $url_credentials = { auth_mode => "AUTHMODE_BASIC", username => $LISTENER_UID, password => $LISTENER_PWD, }; $ttl = -1; $min_events_per_timeslice = 5; $max_timeslice = 10; $enabled_state = "STATE_ENABLED"; $sub_detail = { name => $name, event_type_list => [@event_type_list], url => $url, url_credentials => $url_credentials, ttl => $ttl, min_events_per_timeslice => $min_events_per_timeslice, max_timeslice => $max_timeslice, enabled_state => $enabled_state }; $soapResponse = $EventSubscription->create ( SOAP::Data->name(sub_detail_list => [$sub_detail]) ); if ( !$soapResponse->fault ) { @SubscriptionStatusList = @{$soapResponse->result}; $i = 0; foreach $SubscriptionStatus (@SubscriptionStatusList) { $code = $SubscriptionStatus->{"code"}; $data = $SubscriptionStatus->{"data"}; if ( $DEBUG ) { print "Creation of Subscription $i\n"; } if ( $DEBUG ) { print " code: $code\n"; } if ( $DEBUG ) { print " data: $data\n"; } $i++; } $exists = 1; } } return $exists; } #------------------------------------------------------------------------------------------ # Main program logic #------------------------------------------------------------------------------------------ if ( &setupSubscription() ) { my $daemon = SOAP::Transport::HTTP::Daemon -> new (LocalAddr => $LISTENER_HOST, LocalPort => $LISTENER_PORT) -> on_action(sub {}) -> dispatch_with ( {'urn:iControl:Management/EventNotification' => 'Management::EventNotification'} ); print "Starting HTTP daemon listening on ", $daemon->url, "\n"; $daemon->handle; } 1;