查看设备版本号及主备状态

  1. from f5.bigip import ManagementRoot
  2. mgmt = ManagementRoot(ip, 'X', 'X')
  3. fail = mgmt.tm.sys.failover.load()
  4. failOverStat = fail.apiRawValues['apiAnonymous'].rstrip()
  5. print("%s: Ver %s, %s" % (ip, mgmt.tmos_version, failOverStat))

可以这么理解:CLI:tmsh show sys failover —- https://host/mgmt/tm/sys/failover —- mgmt.tm.sys.failover。

调试API调用

  1. from f5.bigip import ManagementRoot
  2. mgmt = ManagementRoot(ip, 'X', 'X') # set on instantiation
  3. mgmt.debug = True # turn on after instantiation
  4. print(x) # x为列表,当前为空[]
  5. mgmt.tm.ltm.pools.pool.exists(name='testpool')
  6. for x in mgmt.debug_output:
  7. print(x) # 根据执行的情况,每条语句或者api对应一条curl语句,可以直接复制单独测试
  8. mgmt.debug = False # turn off

注意F5 SDK调用API不是使用的curl,只是为了方便排查,才将debug结果格式化成curl命令格式。具体的排查结果可以查看Debugging API calls with the python sdk

查看VS或者Pool状态

  1. import datetime
  2. from f5.bigip import ManagementRoot
  3. from f5.utils.responses.handlers import Stats
  4. mgmt = ManagementRoot(IP, X, X)
  5. vs_collection = mgmt.tm.ltm.virtuals.get_collection()
  6. print("---vs---")
  7. for vs in vs_collection:
  8. starttime = datetime.datetime.now()
  9. vs_stats = Stats(vs.stats.load())
  10. endtime = datetime.datetime.now()
  11. print("VS: %s, availabilityState: %s, enabledState: %s, statusReason: %s, loadTime: %s" % (vs.name, vs_stats.stat.status_availabilityState['description'], vs_stats.stat.status_enabledState['description'], vs_stats.stat.status_statusReason['description'], (endtime - starttime).seconds))
  12. pool_collection = mgmt.tm.ltm.pools.get_collection()
  13. print("---pool---")
  14. for pool in pool_collection:
  15. starttime = datetime.datetime.now()
  16. pool_stats = Stats(pool.stats.load())
  17. endtime = datetime.datetime.now()
  18. print("Pool: %s, availabilityState: %s, enabledState: %s, statusReason: %s, loadTime: %s" % (pool.name, pool_stats.stat.status_availabilityState['description'], pool_stats.stat.status_enabledState['description'], pool_stats.stat.status_statusReason['description'], (endtime - starttime).seconds))