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']