For more information regarding the security incident at F5, the actions we are taking to address it, and our ongoing efforts to protect our customers, click here.

Event Subscription Client

Problem this snippet solves:

This application illustrates how to use the Management::EventSubscription interface to create and manage subscriptions for system event notifications.

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-2005 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::Lite + trace => qw(method debug);
use SOAP::Lite;
use Time::gmtime;
use MIME::Base64;

BEGIN { push (@INC, ".."); }
use iControlTypeCast;

#----------------------------------------------------------------------------
# Validate Arguments
#----------------------------------------------------------------------------
my $sHost = $ARGV[0];
my $sPort = $ARGV[1];
my $sUID = $ARGV[2];
my $sPWD = $ARGV[3];
my $sSubscription = $ARGV[4];
my $sArg1 = $ARGV[5];

my $sProtocol = "https";

# Script specific variables

if ( ("80" eq $sPort) or ("8080" eq $sPort) )
{
$sProtocol = "http";
}

if ( ($sHost eq "") or ($sPort eq "") or ($sUID eq "") or ($sPWD eq "") )
{
die ("Usage: EventSubscription.pl host port uid pwd [list|create|remove]\n");
}

#----------------------------------------------------------------------------
# Transport Information
#----------------------------------------------------------------------------
sub SOAP::Transport::HTTP::Client::get_basic_credentials
{
return "$sUID" => "$sPWD";
}

$EventSubscription = SOAP::Lite
-> uri('urn:iControl:Management/EventSubscription')
-> proxy("$sProtocol://$sHost:$sPort/iControl/iControlPortal.cgi");
eval { $EventSubscription->transport->http_request->header
(
'Authorization' => 
'Basic ' . MIME::Base64::encode("$sUID:$sPWD", '')
); };


$EventNotification = SOAP::Lite
-> uri('urn:iControl:Management/EventNotification')
-> proxy("$sProtocol://$sHost:$sPort/iControl/iControlPortal.cgi");
eval { $EventNotification->transport->http_request->header
(
'Authorization' => 
'Basic ' . MIME::Base64::encode("$sUID:$sPWD", '')
); };

if ( ($sSubscription eq "") or ($sSubscription eq "list") )
{
&getSubscriptionList();
}
elsif ( $sSubscription eq "create" )
{
  &createSubscription();
}
elsif ( $sSubscription eq "remove" )
{
  &removeSubscription($sArg1);
}
else
{
&getSubscription($sSubscription);
}

#----------------------------------------------------------------------------
# checkResponse
#----------------------------------------------------------------------------
sub checkResponse()
{
my ($soapResponse) = (@_);
if ( $soapResponse->fault )
{
print $soapResponse->faultcode, " ", $soapResponse->faultstring, "\n";
exit();
}
}

#----------------------------------------------------------------------------
# getSubscriptionList
#----------------------------------------------------------------------------
sub getSubscriptionList()
{
$soapResponse = $EventSubscription->get_list();
&checkResponse($soapResponse);
my @subscription_list = @{$soapResponse->result};

  &getSubscription(@subscription_list);
}

#----------------------------------------------------------------------------
# getSubscription
#----------------------------------------------------------------------------
sub getSubscription()
{
my @subscription_id = @_;

$soapResponse = $EventSubscription->query
(
SOAP::Data->name(id_list => [@subscription_id])
);
&checkResponse($soapResponse);

@SubscriptionStatusList = @{$soapResponse->result};
@params = $soapResponse->paramsout;
@SubscriptionDefinitionList = @{@params[0]};

for $i ( 0 .. $#SubscriptionDefinitionList)
{
$SubscriptionDefinition = $SubscriptionDefinitionList[$i];
$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"};

print "Details for Subscription '$id'\n";
print "                     Name : $name\n";
print "                      url : $url\n";
print "   credentials ($username:$password) type $auth_mode\n";
print "                      ttl : $ttl\n";
print " min_events_per_timeslice : $min_events_per_timeslice\n";
print "            max_timeslice : $max_timeslice\n";
print "            enabled_state : $enabled_state\n";  
print "                   EVENTS\n";
for $j (0 .. $#event_type_list )
{
print "                            $event_type_list[$j]\n";
}

}
}

#----------------------------------------------------------------------------
# createSubscription
#----------------------------------------------------------------------------
sub createSubscription()
{
  $name = "my subscription";

  @event_type_list =
  (
    "EVENTTYPE_CREATE",
    "EVENTTYPE_MODIFY",
    "EVENTTYPE_DELETE",
    "EVENTTYPE_DB_VARIABLE"
  );

  $url = "http://192.168.11.26";
  $url_credentials =
  {
    auth_mode => "AUTHMODE_BASIC",
    username => "admin",
    password => "admin"
  };
  $ttl = -1;
  $min_events_per_timeslice = 30;
  $max_timeslice = 60;
  $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])
  );
&checkResponse($soapResponse);

  @SubscriptionStatusList = @{$soapResponse->result};
  $i = 0;
  foreach $SubscriptionStatus (@SubscriptionStatusList)
  {
    $code = $SubscriptionStatus->{"code"};
    $data = $SubscriptionStatus->{"data"};

    print "Creation of Subscription $i\n";
    print "   code: $code\n";
    print "   data: $data\n";
    $i++;
  }

}

#----------------------------------------------------------------------------
# removeSubscription
#----------------------------------------------------------------------------
sub removeSubscription()
{
my @subscription_id = @_;
$soapResponse = $EventSubscription->remove
(
SOAP::Data->name(id_list => [@subscription_id])
);
&checkResponse($soapResponse);

  @SubscriptionStatusList = @{$soapResponse->result};
  $i = 0;
  foreach $SubscriptionStatus (@SubscriptionStatusList)
  {
    $id = @subscription_id[$i];
    $code = $SubscriptionStatus->{"code"};
    $data = $SubscriptionStatus->{"data"};

    print "Removal of Subscription '$id'\n";
    print "   code: $code\n";
    print "   data: $data\n";
    $i++;
  }
}
Published Mar 07, 2015
Version 1.0
No CommentsBe the first to comment