HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

REST API calls to BIG-IP LTM to get the status of pool members

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
callsrestltmthestatusbiggetmemberspoolapi

Problem

This code calls the iControl REST API provided by BIG-IP LTM. Trying to get the list of the pools, it's current status and the pool members associated with the pool.

My code works like this, based on three calls:

  • Get the pool names (/mgmt/tm/ltm/pool)



  • Based on the pool names (path), get its status (mgmt/tm/ltm/pool/~pool~name/stats)



  • Based on the pool names (path), get its pool members (/mgmt/tm/ltm/pool/~pool~names/members)



If I do only the first call, I get the output within milliseconds. After adding the second call, it becomes worse. With a third call, it takes good amount of time (more than 6 minutes) to get all the output for about 400 pools.

How can I optimize this code?

import requests
from requests.auth import HTTPBasicAuth

BASE_URL = "https://localhost/mgmt/tm"
username = "admin"
password = "admin"

def makeRequest(username, password, url):
    response_data = requests.get(url, auth = HTTPBasicAuth(username, password), verify = False)

    return response_data.json()

pool_data = makeRequest(username, password, BASE_URL + "/ltm/pool")

for pools in pool_data['items']:
    print pools['name']
    tildPath = pools['fullPath'].replace('/','~')

    #GET the Pool stats
    pool_stats = makeRequest(username, password, BASE_URL + "/ltm/pool/" + tildPath + "/stats")
    print pool_stats['entries']['status.availabilityState']['description']

    #GET the Pool Members
    pool_members = makeRequest(username, password, BASE_URL + "/ltm/pool/" + tildPath + "/members")

    for members in pool_members['items']:
        print members['name'] +" " + members['address'] + " " + members['state']

Solution

Regarding your performance problem there's nothing obviously wrong with the code. I'd suggest you make sure that the server isn't throttling the responses in subsequent runs.

If that's not the case the next step would be to determine which of the calls is slow and pinpoint if it's an issue e.g. with JSON parsing or accumulating some garbage data that's not necessary.

In particular a profiler would come in handy for this, even something like cProfile would probably be enough to check for obvious problems.

  • The code should follow PEP8 naming conventions everywhere, i.e. make_request.



  • Also compatibility with Python 3 would be nice, so use print(...).

Context

StackExchange Code Review Q#123571, answer score: 2

Revisions (0)

No revisions yet.