Down A Member If A Monitor Failed

Problem this snippet solves:

This script will set a pool members state to FORCED DOWN if it determines that a health monitor failed (MONITOR_STATUS_DOWN). The reasoning behind this is that it is sometimes necessary to perform maintenance on a node if a health monitor fails. This will disable future client connections until the maintenance can be performed.

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 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 $sPool = $ARGV[4];
my $sPoolMember = $ARGV[5];
my $sProtocol = "https";

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

if ( ($sHost eq "") or ($sPort eq "") or ($sUID eq "") or ($sPWD eq "") )
{
die ("Usage: checkPoolMember.pl host port uid pwd pool_name memberip_port\n");
}

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

sub SOAP::Deserializer::typecast-->
{
my ($self, $value, $name, $attrs, $children, $type) = @_;
my $retval = undef;
if ( $type eq "{urn:iControl}LocalLB.MonitorStatus" )
{
$retval = $value;
}
return $retval;
}

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

if ( ($sPool eq "") or ($sPoolMember eq "") )
{
usage();
}

&checkPoolMember($sPool, $sPoolMember);

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

#----------------------------------------------------------------------------
# checkPoolMember
#----------------------------------------------------------------------------
sub checkPoolMember()
{
($sPool, $sPoolMember) = (@_);

($sMemberAddr, $sMemberPort) = split(/:/, $sPoolMember, 2);

print "checking status of pool member $sPool/$sMemberAddr:$sMemberPort\n";

$soapResponse = $PoolMember->get_monitor_status
(
SOAP::Data->name(pool_names => [$sPool])
);
&checkResponse($soapResponse);
@MonitorStatusAofA = @{$soapResponse->result};
@MonitorStatusList = @{$MonitorStatusAofA[0]};

$bFoundMember = 0;
# Loop over monitor statuses until we found a match on the
# specified pool member
foreach $MonitorStatus (@MonitorStatusList)
{
$member = $MonitorStatus->{"member"};
$addr = $member->{"address"};
$port = $member->{"port"};
$monitor_status = $MonitorStatus->{"monitor_status"};

if ( ($sMemberAddr eq $addr) and ($sMemberPort eq $port) )
{
print "Monitor Status => $monitor_status\n";
$bFoundMember = 1;
if ( $monitor_status eq "MONITOR_STATUS_DOWN" )
{
# Monitor has been marked down, do disable it.
&disablePoolMember($sPool, $sMemberAddr, $sMemberPort);
}
}
}

if ( 0 == $bFoundMember )
{
print "Member $sPoolMember not found in pool $sPool\n";
}
}


#----------------------------------------------------------------------------
# disablePoolMember()
#----------------------------------------------------------------------------
sub disablePoolMember()
{
($sPool, $sMemberAddr, $sMemberPort) = (@_);
&setPoolMemberState($sPool, $sMemberAddr, $sMemberPort, "STATE_DISABLED");
}

#----------------------------------------------------------------------------
# enablePoolMember()
#----------------------------------------------------------------------------
sub enablePoolMember()
{
($sPool, $sMemberAddr, $sMemberPort) = (@_);
&setPoolMemberState($sPool, $sMemberAddr, $sMemberPort, "STATE_ENABLED");
}

#----------------------------------------------------------------------------
# setPoolMemberState()
#----------------------------------------------------------------------------
sub setPoolMemberState()
{
($sPool, $sMemberAddr, $sMemberPort, $sMonitorState) = (@_);

$member = 
{
address => $sMemberAddr,
port => $sMemberPort
};
$monitor_state = $sMonitorState;

$MonitorState = 
{
member => $member,
monitor_state => $monitor_state
};

@MonitorStateList = 
(
[$MonitorState]
);

print "disabling pool member...\n";
$soapResponse = $PoolMember->set_monitor_state
(
SOAP::Data->name(pool_names => [$sPool]),
SOAP::Data->name(monitor_states => [@MonitorStateList])
);
&checkResponse($soapResponse);

print "Monitor status => MONITOR_STATUS_FORCED_DOWN\n";
}
Published Mar 07, 2015
Version 1.0
No CommentsBe the first to comment