Skip to content

Instantly share code, notes, and snippets.

@beargolightly
Last active February 13, 2019 04:45
Show Gist options
  • Save beargolightly/0ee479dc481b9214491f958d30cd0de8 to your computer and use it in GitHub Desktop.
Save beargolightly/0ee479dc481b9214491f958d30cd0de8 to your computer and use it in GitHub Desktop.
F5 LTM pool member control for Powershell.ps1
[CmdletBinding()]
param(
[string]$LTMName,
[string]$Username,
[string]$Password,
[string]$ApplicationName,
[string]$MemberIP,
[int]$MaxDisableWaitTime,
[int]$MaxOfflineWaitTime,
[int]$MaxHealthMonitorWaitTime,
[int]$ConnectionCountDisabled,
[int]$ConnectionCountOffline,
[ValidateSet('Enable','Disable')]
[string]$EnableOrDisable
)
if ($OctopusParameters) {
if ($OctopusParameters['LTMName']) {[string]$LTMName = $OctopusParameters['LTMName']}
if ($OctopusParameters['Username']) { [string]$Username = $OctopusParameters['Username']}
if ($OctopusParameters['Password']) { [string]$Password = $OctopusParameters['Password']}
if ($OctopusParameters['ApplicatioName']) { [string]$ApplicationName = $OctopusParameters['ApplicationName']}
if ($OctopusParameters['MemberIP']) { [string]$MemberIP = $OctopusParameters['MemberIP']}
if ($OctopusParameters['MaxDisableWaitTime']) { [int]$MaxDisableWaitTime = $OctopusParameters['MaxDisableWaitTime']}
if ($OctopusParameters['MaxOfflineWaitTime']) { [int]$MaxOfflineWaitTime = $OctopusParameters['MaxOfflineWaitTime']}
if ($OctopusParameters['MaxHealthMonitorWaitTime']) { [string]$MaxHealthMonitorWaitTime = $OctopusParameters['MaxHealthMonitorWaitTime']}
if ($OctopusParameters['ConnectionCountDisabled']) { [int]$ConnectionCountDisabled = $OctopusParameters['ConnectionCountDisabled']}
if ($OctopusParameters['ConnectionCountOffline']) { [int]$ConnectionCountOffline = $OctopusParameters['ConnectionCountOffline']}
if ($OctopusParameters['EnableOrDisable']) { [string]$EnableOrDisable = $OctopusParameters['EnableOrDisable']}
}
function Wait-LTMPoolMemberConnectionCount()
{
param(
[int]$MaxWaitTime = 300, #defaults to 5 minutes
[int]$ConnectionCount = 0
)
[int]$serverside_curConns = 100;
$elapsed = [System.Diagnostics.Stopwatch]::StartNew();
while ( $serverside_curConns -gt $ConnectionCount -and $elapsed.ElapsedMilliseconds -lt ($MaxDisableWaitTime * 1000))
{
$serverside_curConns = ($poolMember|Get-PoolMemberStats|Select-Object -ExpandProperty serverside.curConns).value
$remainingTimer = $MaxOfflineWaitTime - [int]($elapsed.ElapsedMilliseconds / 1000)
Write-Output "Waiting for connections to drain to $ConnectionCount (currently $serverside_curConns) - $remainingTimer seconds remain"
Start-Sleep -s 5
}
}
function Set-LTMPoolMemberOffline()
{
Write-Output "Disabling pool member $($poolMember.address)..."
if (Disable-PoolMember -InputObject $poolMember) { Write-Output "Pool member $($poolMember.address) disabled!" "";
Wait-LTMPoolMemberConnectionCount -MaxWaitTime $MaxDisableWaitTime -ConnectionCount $ConnectionCountDisabled }
Write-Output "Offlining pool member $($poolMember.address)..."
if (Disable-PoolMember -InputObject $poolMember -Force) { Write-Output "Pool member $($poolMember.address) offline!" "";
Wait-LTMPoolMemberConnectionCount -MaxWaitTime $MaxOfflineWaitTime -ConnectionCount $ConnectionCountOffline
} else {
Write-Output "Error while disabling/offlining pool member $($poolMember.address)"; exit 1 }
}
function Wait-LTMPoolMemberHealthy()
{
param(
[int]$MaxHealthMonitorWaitTime
)
$elapsed = [System.Diagnostics.Stopwatch]::StartNew();
while (($availabilityState -ne "available"))
{
$remainingTimer = $MaxHealthMonitorWaitTime - [int]($elapsed.ElapsedMilliseconds / 1000)
if ($remainingTimer -lt 1) { Write-Output "Timed out waiting for site startup!"; exit 1}
$availabilityState = ($poolMember|Get-PoolMemberStats|Select-Object -ExpandProperty status.availabilityState).description
if ($availabilityState -eq "available") { Write-Output "Site appears up!"; exit 0 } else { Write-Output "Waiting up to $remainingTimer more seconds..."; Start-Sleep -s 3 }
}
}
$LTMpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$LTMcreds = New-Object System.Management.Automation.PSCredential ($Username, $LTMpasswd)
New-F5Session -LTMName $LTMName -LTMCredentials $LTMcreds -Default
Write-Output "App: $ApplicationName MemberIP: $MemberIP"
$poolMember = Get-PoolMember -Application $ApplicationName -Address $MemberIP
if ($poolMember.address -eq $null) { Write-Output "Error retrieving pool member $MemberIP in iApp $ApplicationName!"; exit 1 }
If ($EnableOrDisable -eq "Disable")
{
Set-LTMPoolMemberOffline
}
elseif ($EnableOrDisable -eq "Enable") {
if (Enable-PoolMember -InputObject $poolMember)
{ Write-Output "Pool member $MemberIP enabled, waiting for online status..."
Wait-LTMPoolMemberHealthy -MaxHealthMonitorWaitTime $MaxHealthMonitorWaitTime
} else { Write-Output "Enabling pool member $MemberIP failed!"; exit 1 }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment