Forum Discussion
F5 Python SDK: How to get virtual server availability?
Great to see more people opting to use the SDK!
You can work with the output normally, as with any dict-type, using
dict.get('something')
. Add in a set of square-brackets, if the extracted value is yet another key-value pair, and if you're looking for a very specific match: dict.get('something')['something']
.
Note: In the for-loop, I'm using
virtual.name
- this extraction works without use of .get('name')
because 'name' is a 1st-level extraction - no nesting is involved. For the same reason, I can do state = stats.entries...
in my function, instead of state = stats.get('entries')...
f5-sdk v2.2.0 ; py3.4.3 ; 11.5.4
from f5.bigip import ManagementRoot
def get_vs_state(vs_name):
virtual_s = API_ROOT.tm.ltm.virtuals.virtual.load(name=vs_name)
stats = virtual_s.stats.load()
state = stats.entries.get('status.availabilityState')['description']
return state
API_ROOT = ManagementRoot("bip-01", "admin", "admin")
VIRTUALS = API_ROOT.tm.ltm.virtuals.get_collection()
EXAMPLE OF USE:
Print AvailabilityState of all Virtual Servers
for virtual in VIRTUALS:
print("Name: '%s' AvailabilityState: '%s'" % (virtual.name, get_vs_state(virtual.name)))
Thank you for your help and patience! Much appreciated...
Not sure if there is a difference in the data structures between our versions: F5 SDK v2.0.2 BIGIP 12.1.1 Python 3.3.2
When I run the stats.entries.get method you outlined above, I get a return of 'None'. However there is a value for status.availibilityState buried in nested dicts for stats.entries.
Here's a test I ran in interactive shell (I have a module that obtains a token and base object of 'mgmt'):
>>> virtuals = mgmt.tm.ltm.virtuals.virtual.load(name='my_virtual_name')
>>> stats = virtuals.stats.load()
>>> state = stats.entries.get('status.availabilityState')['description']
Traceback (most recent call last):
File "", line 1, in
TypeError: 'NoneType' object is not subscriptable
>>> stats.entries
{'https://localhost/mgmt/tm/ltm/virtual/my_virtual_name/~Common~my_virtual_name/stats': {'nestedStats': {'selfLink': 'https://localhost/mgmt/tm/ltm/virtual/my_virtual_name/~Common~my_virtual_name/stats?ver=12.1.1', 'kind': 'tm:ltm:virtual:virtualstats', 'entries': {'totRequests': {'value': 0}, 'status.statusReason': {'description': 'The virtual server is available'}, 'csMinConnDur': {'value': 0}, 'fiveMinAvgUsageRatio': {'value': 0}, 'syncookie.syncookies': {'value': 0}, 'csMeanConnDur': {'value': 0}, 'tmName': {'description': '/Common/my_virtual_name'}, 'clientside.slowKilled': {'value': 0}, 'syncookie.hwSyncookies': {'value': 0}, 'ephemeral.bitsOut': {'value': 0}, 'syncookie.rejects': {'value': 0}, 'ephemeral.curConns': {'value': 0}, 'clientside.pktsOut': {'value': 0}, 'syncookie.syncacheCurr': {'value': 0}, 'clientside.bitsIn': {'value': 0}, 'ephemeral.pktsOut': {'value': 0}, 'destination': {'description': '192.168.1.1:443'}, 'syncookieStatus': {'description': 'not-activated'}, 'syncookie.hwsyncookieInstance': {'value': 0}, 'syncookie.swsyncookieInstance': {'value': 0}, 'clientside.evictedConns': {'value': 0}, 'clientside.curConns': {'value': 0}, 'clientside.totConns': {'value': 0}, 'clientside.bitsOut': {'value': 0}, 'oneMinAvgUsageRatio': {'value': 0}, 'ephemeral.totConns': {'value': 0}, 'syncookie.accepts': {'value': 0}, 'status.availabilityState': {'description': 'available'}, 'syncookie.syncacheOver': {'value': 0}, 'syncookie.hwAccepts': {'value': 0}, 'ephemeral.bitsIn': {'value': 0}, 'status.enabledState': {'description': 'enabled'}, 'ephemeral.evictedConns': {'value': 0}, 'fiveSecAvgUsageRatio': {'value': 0}, 'clientside.maxConns': {'value': 0}, 'csMaxConnDur': {'value': 0}, 'ephemeral.pktsIn': {'value': 0}, 'ephemeral.slowKilled': {'value': 0}, 'clientside.pktsIn': {'value': 0}, 'ephemeral.maxConns': {'value': 0}, 'cmpEnabled': {'description': 'enabled'}, 'cmpEnableMode': {'description': 'all-cpus'}}}}}
I'm not sure if your version is the same, but it appears the stats are nested three levels deep, first key being a link, second 'nestedStats', then 'entries'. If I follow the entire path I do get the value:
>>> print(stats.entries.get('https://localhost/mgmt/tm/ltm/virtual/my_virtual_name/~Common~my_virtual_name/stats')['nestedStats']['entries']['status.availabilityState']['description'])
available
Am I running something incorrectly or are the returned structures different between our versions? I can run through the nest like you mentioned and get the value, though the first key (formatted as a URL) throws me off as I'm not sure if there is an elegant way to reference it. I could get it by listing the keys for that level of dict but that seems kludge-y.
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com