Forum Discussion

mcalnd_327206's avatar
mcalnd_327206
Icon for Nimbostratus rankNimbostratus
Jun 18, 2018

iControlRest - Using Powershell's Invoke-RestMethod to obtain connections stats from pool members?

Hi, I'm hoping someone can help here, I'm 2 days and counting and have hit a brick-wall on it. I personally have doubts if this can work, although I'd like to think it is possible.

I'm writing a script in Powershell to monitor the serverside.curConns value and return it using Powershell. The plan is to incorporate this into a bigger script to allow a server reboot once the value reaches 0

At the moment, we have 8 (and growing) servers in this pool, and the idea is to run this on each server when it goes to do a system restart during patching.

I only want to change the values at the top of my script, so this script will remain unchanged across the 8 servers when copied locally.

For example, lets say the 8 servers are named... PROD_SERVER01 to PROD_SERVER08

My script is below.

 ======= CHANGE PER ENVIRONMENT ==========
$f5partition = "Production"
$f5poolname = "Frontend_App_pool"
$f5port = 8080
$user = "myf5userid"
$pass = "myf5password"
$f5server = "myf5server.mydomain.local"
 ======= NO CHANGES BELOW THIS LINE! =======
$servername = $env:COMPUTERNAME
 ====== AUTHORISATION SECTION ==========
$pair = "$($user):$($pass)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$Headers = @{
    Authorization = $basicAuthValue
}
=== CONNECT TO F5 API =========
$reply = Invoke-RestMethod -Uri https://${f5server}/mgmt/tm/ltm/pool/~${f5partition}~${f5poolname}/members/~${f5partition}~${servername}:${f5port}/stats/?$select=serverside.curConns -Headers $Headers

My JSON results will look like this...Just like the ones detailed in the IControlRest pdf manual...results below truncated for readability...

{
  "generation": 263285,
  "kind": "tm:ltm:pool:members:membersstats",
  "selfLink": "https://localhost/mgmt/tm/ltm/pool/~Production~Frontend_App_pool/members/~Production~PROD_SERVER01:8080/stats?ver=12.1.3",
  "entries": {
    "https://localhost/mgmt/tm/ltm/pool/~Production~Frontend_App_pool/members/~Production~PROD_SERVER01:8080/~Production~PROD_SERVER01:8080/stats": {
      "nestedStats": {
        "kind": "tm:ltm:pool:members:membersstats",
        "selfLink": "https://localhost/mgmt/tm/ltm/pool/~Production~Frontend_App_pool/members/~Production~PROD_SERVER01:8080/~Production~PROD_SERVER01:8080/stats?ver=12.1.3",
        "entries": {
          "serverside.curConns": {
            "value": 0
          }
      }
    }
  }
}
}

If I run the script above once, I then have the JSON loaded into the $reply variable.

Using Powershell ISE, if I then construct a WRITE-HOST statement and start typing, the remaining will fill in automatically. For example, I started with $reply. then the first entry is $reply.entries and so on, until I can find what I want...

Write-Host $reply.entries.'https://localhost/mgmt/tm/ltm/pool/~Production~Frontend-App_pool/members/~Production~PROD_SERVER01:8080/~Production~PROD_SERVER01:8080/stats'.nestedStats.entries.'serverside.curConns'.value

This will display the value to the screen

My problem is that I am having trouble writing the script to run on PROD_SERVERS02-08, because that above WRITE-HOST statement uses fixed properties.

I've tried lots of variable substitutions on this line to no avail. I'm also struggling to do anything constructive with enumerating through the multi-tiered JSON in a loop to extract the value.

Any help greatly received! Thanks in advance. I'm trying to write something that doesn't have any baggage with it, so not downloading curl .exes or pre-built functions to cart around server-to-server.

Neil

No RepliesBe the first to reply