Ps Local Traffic Summary

Problem this snippet solves:

This application will replicate the network status functionality found in the BIG-IP management GUI with a PowerShell console implementation.

The BIG-IP Management GUI has a feature called the Network Map that includes a summary of the objects on the system and their current statuses. This tech tip will take that GUI component and break it down into the underlying iControl method calls and present an alternative implementation from within Windows PowerShell.

Code :

#----------------------------------------------------------------------------
# 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-2008 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.
#----------------------------------------------------------------------------
param (
  $g_bigip = $null,
  $g_uid = $null,
  $g_pwd = $null
)

Set-PSDebug -strict;

$g_vs_list = $null;
$g_vs_pool_list = $null;
$g_vs_rule_list = $null;

#-------------------------------------------------------------------------
# function Show-Usage
#-------------------------------------------------------------------------
function Show-Usage()
{
  Write-Host "Usage: LocalTrafficSummary.ps1 host uid pwd";
  exit;
}

#-------------------------------------------------------------------------
#
#-------------------------------------------------------------------------
function New-StatusObject()
{
  param(
    $name = $null
  );
  $obj = New-Object -TypeName System.Object;
  $obj | Add-Member -Type noteProperty -Name "ObjectType" $name;
  $obj | Add-Member -Type noteProperty -Name "Total" 0;
  $obj | Add-Member -Type noteProperty -Name "Available" 0;
  $obj | Add-Member -Type noteProperty -Name "Unavailable" 0;
  $obj | Add-Member -Type noteProperty -Name "Offline" 0;
  $obj | Add-Member -Type noteProperty -Name "Unknown" 0;
  
  return $obj;
}

#-------------------------------------------------------------------------
#
#-------------------------------------------------------------------------
function Update-ObjectStatus()
{
  param (
    $obj,
    $object_status
  );
  $obj.Total++;
  
  $avail = $object_status.availability_status;
  $enabled = $object_status.enabled_status;
  switch("$avail,$enabled")
  {
        # Available in some capacity
        "AVAILABILITY_STATUS_GREEN,ENABLED_STATUS_ENABLED" { $obj.Available++; }
        "AVAILABILITY_STATUS_GREEN,ENABLED_STATUS_DISABLED" { $obj.Unavailable++; }
        "AVAILABILITY_STATUS_GREEN,ENABLED_STATUS_DISABLED_BY_PARENT" { $obj.Unavailable++; }

        # Object Not Available at the current moment
        "AVAILABILITY_STATUS_YELLOW,ENABLED_STATUS_ENABLED" { $obj.Unavailable++; }
        "AVAILABILITY_STATUS_YELLOW,ENABLED_STATUS_DISABLED" { $obj.Unavailable++; }
        "AVAILABILITY_STATUS_YELLOW,ENABLED_STATUS_DISABLED_BY_PARENT" { $obj.Unavailable++; }

        # Object is not available
        "AVAILABILITY_STATUS_RED,ENABLED_STATUS_ENABLED" { $obj.Offline++; }
        "AVAILABILITY_STATUS_RED,ENABLED_STATUS_DISABLED" { $obj.Unavailable++; }
        "AVAILABILITY_STATUS_RED,ENABLED_STATUS_DISABLED_BY_PARENT" { $obj.Unavailable++; }

        # Object's availability status is unknown
        "AVAILABILITY_STATUS_BLUE,ENABLED_STATUS_ENABLED" { $obj.Unknown++; }
        "AVAILABILITY_STATUS_BLUE,ENABLED_STATUS_DISABLED" { $obj.Unavailable++; }
        "AVAILABILITY_STATUS_BLUE,ENABLED_STATUS_DISABLED_BY_PARENT" { $obj.Unavailable++; }
        
        # Object is unlicensed
        "AVAILABILITY_STATUS_GRAY,ENABLED_STATUS_ENABLED" { $obj.Unavailable++; }
        "AVAILABILITY_STATUS_GRAY,ENABLED_STATUS_DISABLED" { $obj.Unavailable++; }
        "AVAILABILITY_STATUS_GRAY,ENABLED_STATUS_DISABLED_BY_PARENT" { $obj.Unavailable++; }
        
        default { Write-Host "$_";}
  }
}


#-------------------------------------------------------------------------
#
#-------------------------------------------------------------------------
function Sanitize-Array()
{
  param (
    [string[]]$in
  );
  
  $out = $null;
  foreach ($item in $in)
  {
    if ( $item.Length -gt 0 )
    {
      if ( $null -eq $out ) { $out = (, $item) }
      else { $out += $item }
    }
  }
  return $out;
}

#-------------------------------------------------------------------------
#
#-------------------------------------------------------------------------
function Is-InArray()
{
  param(
    [string[]]$in,
    [string]$val
  );
  $bFound = $false;
  
  foreach($v in $in)
  { 
    if ( $v -eq $val )
    {
      $bFound = $true;
    }
  }
  
  return $bFound;
}

#-------------------------------------------------------------------------
#
#-------------------------------------------------------------------------
function Get-VSList()
{
  if ( $null -eq $g_vs_list )
  {
    $g_vs_list = (Get-F5.iControl).LocalLBVirtualServer.get_list();
  }
  return $g_vs_list;
}

#-------------------------------------------------------------------------
#
#-------------------------------------------------------------------------
function Get-VSPoolList()
{
  if ( $null -eq $g_vs_pool_list )
  {
    $g_vs_pool_list = 
      Sanitize-Array (Get-F5.iControl).LocalLBVirtualServer.get_default_pool_name( (Get-VSList) );
  }
  return $g_vs_pool_list;
}

#-------------------------------------------------------------------------
#
#-------------------------------------------------------------------------
function Get-VSRuleList()
{
  if ( $null -eq $g_vs_rule_list )
  {
    $g_vs_rule_list = 
      (Get-F5.iControl).LocalLBVirtualServer.get_rule( (Get-VSList) );
  }
  return $g_vs_rule_list;
}

#-------------------------------------------------------------------------
#
#-------------------------------------------------------------------------
function Get-VSStatus()
{
  $obj = New-StatusObject -name "Virtual Servers";

  $vs_statuses = 
    (Get-F5.iControl).LocalLBVirtualServer.get_object_status( (Get-VSList) );
  foreach ($status in $vs_statuses)
  {
    Update-ObjectStatus $obj $status;
  }
  return $obj;
}

#-------------------------------------------------------------------------
#
#-------------------------------------------------------------------------
function Get-PoolStatus()
{
  $obj = New-StatusObject -name "Pools";

  $pool_statuses = 
    (Get-F5.iControl).LocalLBPool.get_object_status( (Get-VSPoolList) );
  foreach ($status in $pool_statuses)
  {
    Update-ObjectStatus $obj $status;
  }
  
  return $obj;
}

#-------------------------------------------------------------------------
#
#-------------------------------------------------------------------------
function Get-PoolMemberStatus()
{
  $obj = New-StatusObject -name "Pool Members";

  $MemberStatAofA = 
    (Get-F5.iControl).LocalLBPoolMember.get_object_status( (Get-VSPoolList) );
  foreach ($MemberStatA in $MemberStatAofA)
  {
    foreach ($MemberStat in $MemberStatA)
    {
      Update-ObjectStatus $obj $MemberStat.object_status;
    }
  }
  
  return $obj;
}

#-------------------------------------------------------------------------
#
#-------------------------------------------------------------------------
function Get-NodeStatus()
{
  $obj = New-StatusObject -name "Nodes";
  
  $IPPortDefAofA = (Get-F5.iControl).LocalLBPool.get_member( (Get-VSPoolList) );
  $node_list = $null;
  
  foreach($IPPortDefA in $IPPortDefAofA)
  {
    foreach($IPPortDef in $IPPortDefA)
    {
      if ( !(Is-InArray $node_list $IPPortDef.address) )
      {
        if ( $null -eq $node_list ) { $node_list = (, $IPPortDef.address) }
        else { $node_list += $IPPortDef.address; }
      }
    }
  }
  
  if ( $node_list.Length -gt 0 )
  {
    $NodeStatusA = (Get-F5.iControl).LocalLBNodeAddress.get_object_status($node_list);
    foreach ($status in $NodeStatusA)
    {
      Update-ObjectStatus $obj $status;
    }
  }

  return $obj;
}

#-------------------------------------------------------------------------
#
#-------------------------------------------------------------------------
function Get-iRuleStatus()
{
  $obj = New-StatusObject -name "iRules";
  
  $vs_list = Get-VSList;
  $VSRuleAofA = Get-VSRuleList;
  
  foreach ($VSRuleA in $VSRuleAofA)
  {
    $obj.Total += $VSRuleA.Length;
  }
  
  $obj.Available = "-";
  $obj.Unavailable = "-";
  $obj.Offline = "-";
  $obj.Unknown = "-";
  
  return $obj;
}

#-------------------------------------------------------------------------
#
#-------------------------------------------------------------------------
function Print-LocalTrafficSummary()
{
  ( (Get-VSStatus), 
    (Get-PoolStatus), 
    (Get-PoolMemberStatus), 
    (Get-NodeStatus), 
    (Get-iRuleStatus) ) | Format-Table
}

#-------------------------------------------------------------------------
#
#-------------------------------------------------------------------------
function Do-Initialize()
{
  $snapin = Get-PSSnapin | Where-Object { $_.Name -eq "iControlSnapIn" }
  if ( $null -eq $snapin )
  {
    Add-PSSnapIn iControlSnapIn
  }
  $success = Initialize-F5.iControl -HostName $g_bigip -Username $g_uid -Password $g_pwd;
  
  return $success;
}

#-------------------------------------------------------------------------
# Main Application Logic
#-------------------------------------------------------------------------
if ( ($g_bigip -eq $null) -or ($g_uid -eq $null) -or ($g_pwd -eq $null) )
{
  Show-Usage;
}

if ( Do-Initialize )
{
  Print-LocalTrafficSummary
}
else
{
  Write-Error "ERROR: iControl subsystem not initialized"
}
Published Mar 09, 2015
Version 1.0
No CommentsBe the first to comment