Ps Icontrol Dashboard

Problem this snippet solves:

This Powershell application will use iControl and the Google Chart API to build a monitoring dashboard.

PowerShell is a very extensible scripting language and the fact that it integrates so nicely with iControl means you can do all sorts of fun things with it. In this tech tip, I'll illustrate how to use just a couple of iControl method calls (3 to be exact) to create a load distribution dashboard for you desktop (with a little help from the Google Chart API).

How to use this snippet:

ChangeLog

  • 6/11/2010 Changed parameter names to (BIGIP, User, and Pass)
  • 6/11/2010 Added additional Pool parameter to allow for filtering of pool's to monitor

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 (
  $BIGIP = $null,
  $User = $null,
  $Pass = $null,
  $Pool = ""
);

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

$g_title = "iControl PowerShell Dashboard";
$g_graphsize = "300x150";
$g_charttype = "p";
$g_interval = 5;
$g_browser = New-Object -com InternetExplorer.Application;
$g_browser.Navigate2("About:blank");
$g_browser.Visible = $true;
$g_browser.TheaterMode = $true;

#-------------------------------------------------------------------------
# function Run-Dashboard
#-------------------------------------------------------------------------
function Run-Dashboard()
{
  while($true)
  {
    Write-Host "Requesting data..."
    $file_data = Get-Data;
    Refresh-Browser $file_data;
    Start-Sleep $g_interval;
  }
}

#-------------------------------------------------------------------------
# function Convert-To64Bit
#-------------------------------------------------------------------------
function Convert-To64Bit()
{
  param($high, $low);
  return ($high*[Math]::Pow(2,32))+$low;
}

#-------------------------------------------------------------------------
# function Extract-Statistic
#-------------------------------------------------------------------------
function Extract-Statistic()
{
  param($StatisticA, $type);
  $value = -1;
  
  foreach($Statistic in $StatisticA)
  { 
    if ( $Statistic.type -eq $type )
    {
      $value = Convert-To64Bit $Statistic.value.high $Statistic.value.low;
      break;
    }
  }
  return $value;
}

#-------------------------------------------------------------------------
# function Extract-Status
#-------------------------------------------------------------------------
function Extract-Status()
{
  param($MemberObjectStatusA, $IPPortDefinition);
  $color = "#FF0000";
  
  foreach($MemberObjectStatus in $MemberObjectStatusA)
  {
    if ( ($MemberObjectStatus.member.address -eq $IPPortDefinition.address) -and
         ($MemberObjectStatus.member.port -eq $IPPortDefinition.port) )
    {
      $availability_status = $MemberObjectStatus.object_status.availability_status;
      $enabled_status = $MemberObjectStatus.object_status.enabled_status;
      
      if ( ($availability_status -eq "AVAILABILITY_STATUS_GREEN") -and
           ($enabled_status -eq "ENABLED_STATUS_ENABLED" ) )
      {
        $color = "#00FF00";
      }
    }
  }
  return $color;
}

#-------------------------------------------------------------------------
# function Get-Data
#-------------------------------------------------------------------------
function Get-Data()
{
  # TODO - get connection statistics
  $now = [DateTime]::Now;
  #
  
  $file_data = "
  
  $g_title
  
  
  

iControl Dashboard

$now

"; $file_data += " "; $charts_total = ""; $charts_current = ""; $charts_bytes = ""; $PoolList = (Get-F5.iControl).LocalLBPool.get_list() | Where-Object { $_.ToLower().IndexOf($Pool.ToLower()) -ne -1 } | Sort-Object; $MemberStatisticsA = (Get-F5.iControl).LocalLBPoolMember.get_all_statistics($PoolList) $MemberObjectStatusAofA = (Get-F5.iControl).LocalLBPoolMember.get_object_status($PoolList); # loop over each pool $i = 0; foreach($MemberStatistics in $MemberStatisticsA) { $hash_total = @{}; $hash_current = @{}; $hash_bytes = @{}; $hash_status = @{}; $sum_total = 0; $sum_current = 0; $sum_bytes = 0; $PoolName = $PoolList[$i]; # loop over each member $MemberStatisticEntryA = $MemberStatistics.statistics; foreach($MemberStatisticEntry in $MemberStatisticEntryA) { $member = $MemberStatisticEntry.member; $addr = $member.address; $port = $member.port; $addrport = "${addr}:${port}"; $StatisticA = $MemberStatisticEntry.statistics; $total = Extract-Statistic $StatisticA "STATISTIC_SERVER_SIDE_TOTAL_CONNECTIONS" $sum_total += $total; $hash_total.Add($addrport, $total); $current = Extract-Statistic $StatisticA "STATISTIC_SERVER_SIDE_CURRENT_CONNECTIONS" $sum_current += $current; $hash_current.Add($addrport, $current); $bytes = Extract-Statistic $StatisticA "STATISTIC_SERVER_SIDE_BYTES_IN" $sum_bytes += $bytes; $hash_bytes.Add($addrport, $bytes); $color = Extract-Status $MemberObjectStatusAofA[$i] $member; $hash_status.Add($addrport, $color); } $chd_t = ""; $chd_c = ""; $chd_b = ""; $chl_t = ""; $chl_c = ""; $chl_b = ""; $chdl_t = ""; $chdl_c = ""; $chdl_b = ""; $tbl_t = ""; $tbl_c = ""; $tbl_b = ""; # enumerate the total connections foreach($k in $hash_total.Keys) { $member = $k; $v_t = $hash_total[$k]; $v_c = $hash_current[$k]; $v_b = $hash_bytes[$k]; $color = $hash_status[$k]; $div = $sum_total; if ($div -eq 0 ) { $div = 1; } $p_t = ($v_t/$div)*100; $div = $sum_current; if ($div -eq 0 ) { $div = 1; } $p_c = ($v_c/$div)*100; $div = $sum_bytes; if ($div -eq 0 ) { $div = 1; } $p_b = ($v_b/$div)*100; if ( $chd_t.Length -gt 0 ) { $chd_t += ","; $chd_c += ","; $chd_b += ","; } $chd_t += $p_t; $chd_c += $p_c; $chd_b += $p_b; if ( $chl_t.Length -gt 0 ) { $chl_t += "|"; $chl_c += "|"; $chl_b += "|"; $chdl_t += "|"; $chdl_c += "|"; $chdl_b += "|"; } $chl_t += "$member"; $chl_c += "$member"; $chl_b += "$member"; $chdl_t += "$member - $v_t"; $chdl_c += "$member - $v_c"; $chdl_b += "$member - $v_b"; #$alt_t += "($member,$v_t)"; #$alt_c += "($member,$v_c)"; #$alt_b += "($member,$v_b)"; $tbl_t += ""; $tbl_c += ""; $tbl_b += ""; } if ( $sum_total -gt 0 ) { $chart_total = "Total Connections for pool $PoolName"; } else { $chart_total = ""; } if ( $sum_current -gt 0 ) { $chart_current = "Current Connections for pool $PoolName"; } else { $chart_current = ""; } if ( $sum_bytes -gt 0 ) { $chart_bytes = "Incoming Bytes for pool $PoolName"; } else { $chart_current = ""; } if ( $i -gt 0 ) { $file_data += ""; } $file_data += " "; $i++; } $file_data += "
PoolTotal ConnectionsCurrent ConnectionsBytes In
$member$v_t
$member$v_c
$member$v_b

$PoolName $chart_total
$tbl_t
MemberValue
$chart_current
$tbl_c
MemberValue
$chart_bytes
$tbl_b
MemberValue
"; return $file_data; } #------------------------------------------------------------------------- # function Refresh-Browser #------------------------------------------------------------------------- function Refresh-Browser() { param($file_data); if ( $null -eq $g_browser ) { Write-Host "Creating new Browser" $g_browser = New-Object -com InternetExplorer.Application; $g_browser.Navigate2("About:blank"); $g_browser.Visible = $true; $g_browser.TheaterMode = $true; } $docBody = $g_browser.Document.DocumentElement.lastChild; $docBody.InnerHTML = $file_data; } #------------------------------------------------------------------------- # function Kill-Browser #------------------------------------------------------------------------- function Kill-Browser() { if ( $null -ne $g_browser ) { $g_browser.TheaterMode = $false; $g_browser.Quit(); $g_browser = $null; } } #------------------------------------------------------------------------- # Exception handling #------------------------------------------------------------------------- Trap [Exception] { Write-Host $("TRAPPED: " + $_.Exception.GetType().FullName); Write-Host $("TRAPPED: " + $_.Exception.Message); Kill-Browser Exit; } #------------------------------------------------------------------------- # Do-Initialize #------------------------------------------------------------------------- function Do-Initialize() { if ( (Get-PSSnapin | Where-Object { $_.Name -eq "iControlSnapIn"}) -eq $null ) { Add-PSSnapIn iControlSnapIn } $success = Initialize-F5.iControl -HostName $BIGIP -Username $User -Password $Pass; return $success; } #------------------------------------------------------------------------- # Main Application Logic #------------------------------------------------------------------------- if ( ($BIGIP -eq $null) -or ($User -eq $null) -or ($Pass -eq $null) ) { Write-Usage; } if ( Do-Initialize ) { Run-Dashboard } else { Write-Error "ERROR: iControl subsystem not initialized" Kill-Browser }
Published Mar 09, 2015
Version 1.0

Was this article helpful?

No CommentsBe the first to comment