Pool Connections Powershell Probe

AP.F5.LTM.Probe.PoolConnections.Powershell (ProbeActionModuleType)

Gets all Pool Connections as Property Bags

Element properties:

TypeProbeActionModuleType
IsolationAny
AccessibilityPublic
RunAsDefault
OutputTypeSystem.PropertyBagData

Member Modules:

ID Module Type TypeId RunAs 
PSScript ProbeAction Microsoft.Windows.PowerShellPropertyBagTriggerOnlyProbe Default

Overrideable Parameters:

IDParameterTypeSelectorDisplay NameDescription
Debugbool$Config/Debug$DebugDebug Flag, if true then additional info is posted to event log.

Source Code:

<ProbeActionModuleType ID="AP.F5.LTM.Probe.PoolConnections.Powershell" Accessibility="Public" Batching="false">
<Configuration>
<IncludeSchemaTypes>
<SchemaType>Windows!Microsoft.Windows.PowerShellSchema</SchemaType>
</IncludeSchemaTypes>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="Debug" type="xsd:boolean"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="DeviceAddresses" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="DevicePorts" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="DeviceCommunities" type="xsd:string"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="Debug" Selector="$Config/Debug$" ParameterType="bool"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<ProbeAction ID="PSScript" TypeID="Windows!Microsoft.Windows.PowerShellPropertyBagTriggerOnlyProbe">
<ScriptName>Get-PoolConnections.ps1</ScriptName>
<ScriptBody><Script>#==================================================================================
# Script: Get-PoolConnections.ps1
# Date: 01/08/18
# Author: Andi Patrick
# Purpose: Gets Current Pool Connections via SNMP returns all as Property Bag
#==================================================================================

# Get the named parameters
param($Debug,$DeviceAddresses,$DevicePorts,$DeviceCommunities,$SharpSnmpLocation)

# Get Start Time For Script
$StartTime = (GET-DATE)

#Constants used for event logging
$SCRIPT_NAME = 'Get-PoolConnections.ps1'
$EVENT_LEVEL_ERROR = 1
$EVENT_LEVEL_WARNING = 2
$EVENT_LEVEL_INFO = 4

$SCRIPT_STARTED = 4601
$SCRIPT_PROPERTYBAG_CREATED = 4602
$SCRIPT_EVENT = 4603
$SCRIPT_ENDED = 4604
$SCRIPT_ERROR = 4605

#==================================================================================
# Sub: LogDebugEvent
# Purpose: Logs an informational event to the Operations Manager event log
# only if Debug argument is true
#==================================================================================
function Log-DebugEvent
{
param($eventNo,$message)

$message = "`n" + $message
if ($Debug -eq $true)
{
$api.LogScriptEvent($SCRIPT_NAME,$eventNo,$EVENT_LEVEL_INFO,$message)
}
}

#Start by setting up API object.
$api = New-Object -comObject 'MOM.ScriptAPI'

# Log Startup Message
Log-DebugEvent $SCRIPT_STARTED " Script Started."

# Get Working All Device Addresses, Ports and Communities
[System.Collections.ArrayList]$DeviceAddressList = $DeviceAddresses.Split(",")
[System.Collections.ArrayList]$DevicePortList = $DevicePorts.Split(",")
[System.Collections.ArrayList]$DeviceCommunityList = $DeviceCommunities.Split(",")

# Load SharpSNMPLib
[reflection.assembly]::LoadFrom( (Resolve-Path $SharpSnmpLocation) )
# Use SNMP v2
$ver = [Lextm.SharpSnmpLib.VersionCode]::V2
# Set Walk Mode to WithinSubtree
$walkMode = [Lextm.SharpSnmpLib.Messaging.WalkMode]::WithinSubtree

# OIDs used
# bigipTrafficMgmt.bigipLocalTM.ltmPools.ltmPoolStat.ltmPoolStatTable.ltmPoolStatEntry.ltmPoolStatName
[string]$ltmPoolStatName = ".1.3.6.1.4.1.3375.2.2.5.2.3.1.1"
# bigipTrafficMgmt.bigipLocalTM.ltmPools.ltmPoolStat.ltmPoolStatTable.ltmPoolStatEntry.ltmPoolStatServerCurConns
[string]$ltmPoolStatServerCurConns = ".1.3.6.1.4.1.3375.2.2.5.2.3.1.8"

# Initialize Connections Array
$ConnectionsArray = @()

For ($i=0;$i -lt $DeviceAddressList.Count;$i++) {

Try
{
# Create endpoint for SNMP server
$ip = [System.Net.IPAddress]::Parse($DeviceAddressList[$i])
$svr = New-Object System.Net.IpEndPoint ($ip, $DevicePortList[$i])

# Get Pool Names from SNMP
$PoolNameList = New-Object 'System.Collections.Generic.List[Lextm.SharpSnmpLib.Variable]'
[Lextm.SharpSnmpLib.Messaging.Messenger]::Walk($ver, $svr, $DeviceCommunityList[$i], $ltmPoolStatName , $PoolNameList, 3000, $walkMode) &gt; $null

# Get Current Connections from SNMP
$ConnectionsList = New-Object 'System.Collections.Generic.List[Lextm.SharpSnmpLib.Variable]'
[Lextm.SharpSnmpLib.Messaging.Messenger]::Walk($ver, $svr, $DeviceCommunityList[$i], $ltmPoolStatServerCurConns , $ConnectionsList, 3000, $walkMode) &gt; $null

# Create Objects On First Device
If($ConnectionsArray.Count -eq 0) {
For ($j=0; $j -lt $PoolNameList.Count; $j++) {
[string]$PoolName = $PoolNameList[$j].Data.ToString()
[int]$Connections = [int]$ConnectionsList[$j].Data.ToString()

# Create Object &amp; Properties
$NewConnectionObject = New-Object System.Object
$NewConnectionObject | Add-Member -type NoteProperty -name PoolName -value $PoolName
$NewConnectionObject | Add-Member -type NoteProperty -name Connections -value $Connections
# Add to Array
$ConnectionsArray += $NewConnectionObject
}
} else {
# Loop Through All SNMP Results
For ($j=0; $j -lt $PoolNameList.Count; $j++) {
[string]$PoolName = $PoolNameList[$j].Data.ToString()
[int]$Connections = [int]$ConnectionsList[$j].Data.ToString()

# Find matching Entry in Array
For($k=0;$k -lt $ConnectionsArray.Count; $k++){
If ($PoolName -eq $ConnectionsArray[$k].PoolName) {
# Check if Connections is Greater
If ($Connections -gt $ConnectionsArray[$k].Connections) {
Write-Host "Found Bigger"
$ConnectionsArray[$k].Connections = $Connections
}
break;
}
}
}
}

}
Catch
{
$message = "SNMP Server : " + $DeviceAddressList[$i] + "`r`nSNMP Port : " + $DevicePortList[$i] + "`r`nSNMP Community : " + $DeviceCommunityList[$i] + "`r`nSharpSnmp Location : " + $SharpSnmpLocation + "`r`nError : $_"
Log-DebugEvent $SCRIPT_ERROR $message
}
}

# Create Property bags From the Array
For($i=0;$i -lt $ConnectionsArray.Count; $i++){
# Get Conenctions
$Connections = [int]$ConnectionsArray[$i].Connections
# Get Pool Name
$Name = $ConnectionsArray[$i].PoolName
#Create a property bag.
[string] $message = "Created Pool Property bag for $Name`r`n" + "Connections : " + $Connections
Log-DebugEvent $SCRIPT_PROPERTYBAG_CREATED $message
$bag = $api.CreatePropertyBag()
$bag.AddValue("PoolName", $Name)
$bag.AddValue("Connections", $Connections)
#$api.Return($bag)
$bag

}
# Get End Time For Script
$EndTime = (GET-DATE)
$TimeTaken = NEW-TIMESPAN -Start $StartTime -End $EndTime
$Seconds = [math]::Round($TimeTaken.TotalSeconds, 2)
# Log Finished Message
Log-DebugEvent $SCRIPT_ENDED "Script Finished. Took $Seconds Seconds to Complete!"</Script></ScriptBody>
<SnapIns/>
<Parameters>
<Parameter>
<Name>DeviceAddresses</Name>
<Value>$Config/DeviceAddresses$</Value>
</Parameter>
<Parameter>
<Name>DevicePorts</Name>
<Value>$Config/DevicePorts$</Value>
</Parameter>
<Parameter>
<Name>DeviceCommunities</Name>
<Value>$Config/DeviceCommunities$</Value>
</Parameter>
<Parameter>
<Name>debug</Name>
<Value>$Config/Debug$</Value>
</Parameter>
<Parameter>
<Name>SharpSnmpLocation</Name>
<Value>$FileResource[Name="AP.F5.LTM.SharpSnmpLib"]/Path$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>60</TimeoutSeconds>
</ProbeAction>
</MemberModules>
<Composition>
<Node ID="PSScript"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.PropertyBagData</OutputType>
<TriggerOnly>true</TriggerOnly>
</ProbeActionModuleType>