Forum Discussion
Ed_Summers
Jan 19, 2017Nimbostratus
F5 Python SDK: How to get virtual server availability?
Due to my limited Python and SDK experience I am finding it difficult to easily obtain the availability state of a virtual server. Given a 'virtual' object, what is the best/easiest way to return the...
Hannes_Rapp
Nimbostratus
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)))
Hannes_Rapp
Jan 19, 2017Nimbostratus
You're right. The response structure of virtual stats is different in 12.1.1, and it's absolutely dreadful. Best I could offer now is a hack so that this insignificant part at the beginning would be cut out
{'https://localhost/mgmt/tm/ltm/virtual/my_virtual_name/~Common~my_virtual_name/stats': {'nestedStats': {
, then everything will work exactly the same as in my 11.5.4 sample, but it's kind of a dirty approach.
Will look further into cleaner solutions tomorrow.
Recent Discussions
Related Content
DevCentral Quicklinks
* 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
Discover DevCentral Connects