Learn F5 Technologies, Get Answers & Share Community Solutions Join DevCentral

Filter by:
  • Solution
  • Technology
Answers

Using the Python SDK for monitors

I'm starting to use the python SDK and I can't figure out how to access the monitor objects on the BIGIP.

Here's my code for displaying the nodes

from f5.bigip import ManagementRoot
mgmt = ManagementRoot('IP Address', 'user', 'password)
nodes = mgmt.tm.ltm.nodes
for node in nodes.get_colletion():
    print(node.name)

Here's my code for displaying the monitors

from f5.bigip import ManagementRoot
mgmt = ManagementRoot('ip address', 'user', 'password')
monitors = mgmt.tm.ltm.monitors
for monitor in monitors.get_collection()
    print(monitor.name)

I read the documentation located http://f5-sdk.readthedocs.io/en/latest/index.html

The difference between monitors and node in the documentation is the (star) * is separated by colon in the node portion. Don't know if that makes a difference. REST Kind -> tm:ltm:monitors* REST Kind -> tm:ltm:node:*

Any thoughts would help

0
Rate this Question

Answers to this Question

placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

(YMMV depending on version. I'm using version 12.1.2.)

'monitor' is another collection with the various monitor types underneath it. If you just get the collection from 'monitor' you'll have a list of reference links to the sub-collections underneath it. One way of getting an actual list of monitors is to get the collection of the specific monitor type:

>>> https_monitors = mgmt.tm.ltm.monitor.https_s.get_collection()
>>> for mon in https_monitors:
...     print(mon.name)
...

Reminder that since the monitor objects are collections, they'll end in 's', or '_s' if the resource already ends in 's'. Example: to get https monitors, use 'https_s'. To get http monitors, use 'https'.

Apologies if I've butchered SDK terminology on collections/sub-collections. I'm still getting used to the environment myself.

0
Comments on this Answer
Comment made 25-May-2017 by Joel Breton 213

Thanks for the quick reply!

Works perfectly

0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

Hi guys,

how can I create a new monitor using the Python SDK?

Unfortunately

monitor = mgmt.tm.ltm.monitors.monitor.create(partition='Common', name='MyMonitor01')

is not working.

AttributeError: '<class 'f5.bigip.tm.ltm.Ltm'>' object has no attribute 'monitors'

regards

Daniel

0
Comments on this Answer
Comment made 02-Jun-2017 by Joel Breton 213

Hi Daniel,

When creating a monitor you need to specify the type before the create method.

For example if you want to create an http monitor this would be the syntax

monitor = mgmt.tm.ltm.monitor.https.http.create(partition='Common', name='MyMonitor01')

Joel

0
Comment made 09-Jun-2017 by Joel Breton 213

Found a different strategy to create objects on the BIG-IP using the F5-SDK. This uses a technique in Python call unpacking, basically you can create the object in python with all the options you need and then pass the object with the create method.

Create the object as a dictionary in Python - HTTP Monitor

http_monitor = { 
    'name': "http_monitor",
    'partition': "Common",
    'interval' : '10',
    'send' : 'GET /mysite\r\n',
    'timeUntilUp' : '5',
    'timeout' : '31',
}

Create object on the BIG-IP

If you try to pass the object directly you will receive an error

mgmt.tm.ltm.monitor.https.http.create(http_monitor)

TypeError: create() takes 1 positional argument but 2 were given

Add ** in front of the monitor dictionary to upack it

mgmt.tm.ltm.monitor.https.http.create(**http_monitor)

The monitor gets created with all the arguments.

1
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

I'm trying to get a list of all available monitors. Is there a way to loop through the monitor types to then do a get_collection():

for monitor in mgmt.tm.ltm.monitor.get_collection():
        for item in monitor.get_collection():
                pprint(item)

This clearly doesn't work

0
Comments on this Answer
Comment made 24-Apr-2018 by Rob 180

It seems I'm not allowed to edit an answer...

What I'm trying to achieve is a select field in a web page that mirrors the behavior of the Health Monitors section of a pool properties page in the BigIP gui, so that I can add/remove monitors from a pool through the rest api.

0