Pool Toggle

Problem this snippet solves:

The script takes command line arguments to log into a version 9 BigIP and either show all the pools and members, or enable|disable all of a specified pool's members.

Code :

use SOAP::Lite;
#----------------------------------------------------------------------------
# Validate Arguments
#----------------------------------------------------------------------------
my $sHost = $ARGV[0];
my $sPort = $ARGV[1];
my $sUID = $ARGV[2];
my $sPWD = $ARGV[3];
my $sPool = $ARGV[4];
my $desired_pool_state = $ARGV[5];
my $sProtocol = "https";

sub usage()
{
die ("Usage: PoolToggle.pl host port uid pwd ([pool] AND [enable|disable])\n");
}

if ( ($sHost eq "") or ($sPort eq "") or ($sUID eq "") or ($sPWD eq "") or (($sPool ne "") and ($desired_pool_state eq "")) )
{
usage();
}

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

$Pool = SOAP::Lite
-> uri('urn:iControl:LocalLB/Pool')
-> readable(1)
-> proxy("$sProtocol://$sHost:$sPort/iControl/iControlPortal.cgi");
$PoolMember = SOAP::Lite
-> uri('urn:iControl:LocalLB/PoolMember')
-> readable(1)
-> proxy("$sProtocol://$sHost:$sPort/iControl/iControlPortal.cgi");

#----------------------------------------------------------------------------
# Attempt to add auth headers to avoid dual-round trip
#----------------------------------------------------------------------------
eval { $Pool->transport->http_request->header
(
'Authorization' =>
'Basic ' . MIME::Base64::encode("$sUID:$sPWD", '')
); };
eval { $Pool->transport->http_request->header
(
'Authorization' =>
'Basic ' . MIME::Base64::encode("$sUID:$sPWD", '')
); };

#----------------------------------------------------------------------------
# support for custom enum types
#----------------------------------------------------------------------------
sub SOAP::Deserializer::typecast
{
my ($self, $value, $name, $attrs, $children, $type) = @_;
my $retval = undef;
if ( "{urn:iControl}Common.EnabledState" == $type )
{
$retval = $value;
}
return $retval;
}

#----------------------------------------------------------------------------
# Main logic
#----------------------------------------------------------------------------
if ( "" eq $sPool )
{
#------------------------------------------------------------------------
# No pool supplied.  Query pool list and display members for given pool
#------------------------------------------------------------------------
$soapResponse = $Pool->get_list();
&checkResponse($soapResponse);
@pool_list = @{$soapResponse->result};
&showPoolMembers(@pool_list);
}
elsif ( "" ne $sPool )
{
#------------------------------------------------------------------------
# Pool supplied, but no member so disable all pools members
#------------------------------------------------------------------------
if ( $desired_pool_state eq "enable" )
{
&enablePoolMembers($sPool);
}
elsif ( $desired_pool_state eq "disable" )
{
&disablePoolMembers($sPool);
}
else 
{
die ("If pool supplied, you must state enable or disable\n");
}
}
else
{
print "No conditions defined yet\n";
}
#----------------------------------------------------------------------------
# Show list of pools and members
#----------------------------------------------------------------------------
sub showPoolMembers()
{
my (@pool_list) = @_;
my @member_state_lists = &getPoolMemberStates(@pool_list);

print "Pool Members Enabled State\n";
print "==========================\n";
$i = 0;
foreach $pool (@pool_list)
{
print "pool $pool\n{\n";
@member_state_list = @{@member_state_lists[$i]};
foreach $member_state (@member_state_list)
{
$member = $member_state->{"member"};
$addr = $member->{"address"};
$port = $member->{"port"};
$session_state = $member_state->{"session_state"};
print "    $addr:$port ($session_state)\n";
}
print "}\n";
$i++;
}
}

#----------------------------------------------------------------------------
# returns the status structures for the members of the specified pools
#----------------------------------------------------------------------------
sub getPoolMemberStates()
{
my (@pool_list) = @_;

$soapResponse = $PoolMember->get_session_enabled_state
(
SOAP::Data->name(pool_names => [@pool_list])
);
&checkResponse($soapResponse);
@member_state_lists = @{$soapResponse->result};

return @member_state_lists;
}

sub enablePoolMembers()
{
  my ($pool) = (@_);
  &setPoolMemberStates($pool, "STATE_ENABLED");
}

sub disablePoolMembers()
{
  my ($pool) = (@_);
  &setPoolMemberStates($pool, "STATE_DISABLED");
}

sub setPoolMemberStates()
{
  my ($pool, $state) = (@_);

  # Get a list of pool members
  $soapResponse = $Pool->get_member
  (
    SOAP::Data->name(pool_names => [$pool])
  );
  &checkResponse($soapResponse);
  @member_lists = @{$soapResponse->result};

  # Extract the 1st list for the single pool passed in.
  @member_list = @{@member_lists[0]};

  # build parameters for set_session_enabled_state();
  foreach $member_def (@member_list)
  {
    $address = $member_def->{"address"};
    $port = $member_def->{"port"};
    $member = { address => $address, port => $port };
    $MemberSessionState =
    {
      member => $member,
      session_state => $state
    };
    push @MemberSessionStateList, $MemberSessionState;
  }
  push @MemberSessionStateLists, [@MemberSessionStateList];

  # Set the session enabled state
  $soapResponse = $PoolMember->set_session_enabled_state
  (
    SOAP::Data->name(pool_names => [$pool]),
    SOAP::Data->name(session_states => [@MemberSessionStateLists])
  );
  &checkResponse($soapResponse);

  # build parameters for set_monitor_state();
  foreach $member (@member_list)
  {
    $MemberMonitorState =
    {
      member => $member,
      monitor_state => $state
    };
    push @MemberMonitorStateList, $MemberMonitorState;
  }
  push @MemberMonitorStateLists, [@MemberMonitorStateList];

  # set the monitor state
  $soapResponse = $PoolMember->set_monitor_state
  (
    SOAP::Data->name(pool_names => [$pool]),
    SOAP::Data->name(monitor_states => [@MemberMonitorStateLists])
  );
  &checkResponse($soapResponse);
}

#----------------------------------------------------------------------------
# checkResponse makes sure the error isn't a SOAP error
#----------------------------------------------------------------------------
sub checkResponse()
{
my ($soapResponse) = (@_);
if ( $soapResponse->fault )
{
print $soapResponse->faultcode, " ", $soapResponse->faultstring, "\n";
exit();
}
}
Published Mar 09, 2015
Version 1.0
No CommentsBe the first to comment