November 8, 2016 · ucsmsdk ucspython

watch for ucs server events - ucsmsdk

Starting 0.9.1.1 we have simplified the code required to wait for an event.

The below code creates a Service Profile Mo org-root/ls-eventhandle
We then create two threads each thread invokes a wait_for_event method to wait for a specific condition
Here, we wait for the descr property to change to specific values

import threading  
from ucsmsdk.ucshandle import UcsHandle  
from ucsmsdk.mometa.ls.LsServer import LsServer

handle = UcsHandle(<ucsm_ip>, <username>, <password>)  
handle.login()


# remove is already exists
sp = handle.query_dn("org-root/ls-eventhandle")  
if sp:  
    handle.remove_mo(sp)
    handle.commit()


# add
org = handle.query_dn("org-root")  
sp = LsServer(org, name="eventhandle", descr="")  
handle.add_mo(sp, True)  
handle.commit()


def cb(mce):  
    print mce.mo.descr


def one():  
    handle.wait_for_event(sp, "descr", "demo_one", cb)


def two():  
    handle.wait_for_event(sp, "descr", ["demo_two", "demo_three"], cb)

t1 = threading.Thread(name="thread1", target=one)  
t2 = threading.Thread(name="thread2", target=two)

t1.start()  
t2.start()

t1.join()  
t2.join()

handle.logout()  

The above code when executed will wait, until both the threads finish waiting.

The below script changes the descr property to match condition in thread1. Similarly, thread2's wait ends when either of the two values specified in thread2's wait_for_event are seen.

from ucsmsdk.ucshandle import UcsHandle

handle = UcsHandle(<ucsm_ip>, <username>, <password>)  
handle.login()

sp = handle.query_dn("org-root/ls-eventhandle")

# modify
sp.descr = "demo_one"  
handle.set_mo(sp)  
handle.commit()

handle.logout()  
  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket