Collect all partition pool member stats with tmsh

Code is community submitted, community supported, and recognized as ‘Use At Your Own Risk’.

Short Description

This solution resulted from a question on how to get all the stats from pool members of all partitions in one iControl REST method call. I couldn't figure out a solution to that just with iControl REST, so I created a tmsh script to collect the stats and a python script to run that script and download the stats.

Problem solved by this Code Snippet

Eliminates the need to make a lot of REST calls to get the pool member data

How to use this Code Snippet

python poolstats.py admin <username>

Code Snippet Meta Information

  1. Version: tested with tmos 15.1.8.1 and python 3.11
  2. Coding Language: tmsh / python

Full Code Snippet

TMSH script (latest here)

proc script::run {} {

    # where to store the data - /shared/images
    set fp [open "/shared/images/poolstats.csv" w+]

    tmsh::cd /

    # CSV HEADER
    puts $fp "pool,member,ss.bits-in,ss.bits-out,ss.pkts-in,ss.pkts-out"

    # iterate through pools
    foreach pool [tmsh::get_config /ltm pool recursive] {
        set pl [tmsh::get_name $pool]
        foreach obj [tmsh::get_status /ltm pool $pl detail] {
            foreach member [tmsh::get_field_value $obj members] {
                set mbr [tmsh::get_name $member]
                set ss_bit_in [tmsh::get_field_value $member serverside.bits-in]
                set ss_bit_out [tmsh::get_field_value $member serverside.bits-out]
                set ss_pkts_in [tmsh::get_field_value $member serverside.pkts-in]
                set ss_pkts_out [tmsh::get_field_value $member serverside.pkts-out]
                puts $fp "$pl,$mbr,$ss_bit_in,$ss_bit_out,$ss_pkts_in,$ss_pkts_out"
            }
        }
    }
    # close file
    close $fp
}

Python script (latest here)

from bigrest.bigip import BIGIP
from time import sleep
import argparse
import getpass
import sys


def build_parser():
    parser = argparse.ArgumentParser()
    parser.add_argument("host", help="BIG-IP IP/FQDN")
    parser.add_argument("user", help="BIG-IP Username")

    return parser.parse_args()


def instantiate_bigip(host, user):
    pw = getpass.getpass(prompt=f"\n\tWell hello {user}, please enter your password: ")
    try:
        obj = BIGIP(host, user, pw, session_verify=False)
    except Exception as e:
        print(f"Failed to connect to {args.host} due to {type(e).__name__}:\n")
        print(f"{e}")
        sys.exit()
    return obj


def deploy_script():
    # slurp the file
    with open('poolstats.tcl') as f:
        tmsh_script = f.read()
    try:
        cli_script = {'name': 'poolstats.tcl', 'apiAnonymous': tmsh_script}
        b.create('/mgmt/tm/cli/script', cli_script)
    except Exception as e:
        print(f'{e}')
        sys.exit()


def run_poolstats(b):
    try:
        data = {'command': 'run', 'name': '/Common/poolstats.tcl', 'utilCmdArgs': ''}
        b.command('/mgmt/tm/cli/script', data)
    except Exception as e:
        print(f'{e}')


def download_poolstats_data(b):
    try:
        b.download('/mgmt/cm/autodeploy/software-image-downloads', 'poolstats.csv')
    except Exception as e:
        print(f'{e}')


if __name__ == "__main__":
    args = build_parser()
    b = instantiate_bigip(args.host, args.user)

    if not b.exist('/mgmt/tm/cli/script/poolstats.tcl'):
        deploy_script()

    run_poolstats(b)

    # might need to add a delay here if the file write takes a long time with big configs
    # sleep(30)
    download_poolstats_data(b)

 

Published May 26, 2023
Version 1.0
No CommentsBe the first to comment