views:

110

answers:

2

I have written the below Powershell function to call an F5 unit. It loops round for each server and gets each individual stat and then executes a SQL SMO call.

The code is running really slowly and i think i have discounted the SQL call as the cause.

How can the powershell be improved?

function Print-VServerStats()
{

param($virtual_server);


$VirtualServerStatistics = (Get-F5.iControl).LocalLBVirtualServer.get_statistics( (, $virtual_server) );
$VirtualServerStatisticEntry = $VirtualServerStatistics.statistics[0];
$Statistics = $VirtualServerStatisticEntry.statistics | ? {$_.type -eq "STATISTIC_CLIENT_SIDE_CURRENT_CONNECTIONS" -or
                                                           $_.type -eq "STATISTIC_CLIENT_SIDE_MAXIMUM_CONNECTIONS" -or
                                                           $_.type -eq "STATISTIC_CLIENT_SIDE_TOTAL_CONNECTIONS"};

foreach ($Statistic in $Statistics)
{
  $val = Convert-To64Bit $Statistic.value.high $Statistic.value.low;

  switch ($Statistic.type) {
    "STATISTIC_CLIENT_SIDE_CURRENT_CONNECTIONS" {
     $label = "Current Connections";
    }
     "STATISTIC_CLIENT_SIDE_MAXIMUM_CONNECTIONS" {
     $label = "Max Connections";
    }
      "STATISTIC_CLIENT_SIDE_TOTAL_CONNECTIONS" {
     $label = "Total Connections";
    }
  }
    $profcmd.Parameters["@property"].Value = $SrceName
    $profcmd.Parameters["@propertyDesc"].Value = $label
    $profcmd.Parameters["@ValDim1"].Value = $virtual_server
    $profcmd.Parameters["@value"].Value = $val 
    $profcmd.Parameters["@Timestamp"].Value = $t
    [void]$profcmd.ExecuteNonQuery()
  }
}
A: 

We have an F5 BigIP and we've noticed that the UI on the device is really really slow. We haven't traced the cause, but it is very likely your delay is sourcing on the F5 device itself. Measure-Command on

$VirtualServerStatistics = (Get-F5.iControl).LocalLBVirtualServer.get_statistics( (, $virtual_server) );

Should show it if that's the case.

sysadmin1138
A: 

it's hard to test without having F5 but maybe you can shorten/improve the code by replacing:

$Statistics = $VirtualServerStatisticEntry.statistics | ? {$_.type -eq "STATISTIC_CLIENT_SIDE_CURRENT_CONNECTIONS" -or $_.type -eq "STATISTIC_CLIENT_SIDE_MAXIMUM_CONNECTIONS" -or $_.type -eq "STATISTIC_CLIENT_SIDE_TOTAL_CONNECTIONS"};

With

$Statistics = $VirtualServerStatisticEntry.statistics | ? {$_.type -match '^STATISTIC_CLIENT_SIDE_(CURRENT|MAXIMUM|TOTAL)_CONNECTIONS?' }

Shay Levy
I think this could save several miliseconds, but Andrew would like to save some seconds (at least).
stej