示例#1
0
 def inner(driver):
     try:
         q = Query()
         yield q.connect(eventContext, hostname, creds)
         driver.next()
         yield q.notificationQuery(wql)
         result = driver.next()
         log.info("Query sent")
         while 1:
             printSize()
             try:
                 class_name = ''
                 while 1:
                     yield result.fetchSome(500)
                     if not driver.next(): break
                     log.info("Got %d items", len(driver.next()))
                     for obj in driver.next():
                         obj = obj.targetinstance
                         props = [p for p in obj.__dict__.keys()
                                  if not p.startswith('_')]
                         if obj._class_name != class_name:
                             class_name = obj._class_name
                             print obj._class_name
                             print repr(props)
                         print repr([getattr(obj, p) for p in props])
             except WError, ex:
                 log.exception(ex)
                 yield async_sleep(1)
                 driver.next()
         q.close()
示例#2
0
文件: watcher.py 项目: y11en/WMI_cmd
 def inner(driver):
     try:
         q = Query()
         yield q.connect(eventContext, hostname, creds)
         driver.next()
         yield q.notificationQuery(wql)
         result = driver.next()
         log.info("Query sent")
         while 1:
             printSize()
             try:
                 class_name = ''
                 while 1:
                     yield result.fetchSome(500)
                     if not driver.next(): break
                     log.info("Got %d items", len(driver.next()))
                     for obj in driver.next():
                         obj = obj.targetinstance
                         props = [
                             p for p in obj.__dict__.keys()
                             if not p.startswith('_')
                         ]
                         if obj._class_name != class_name:
                             class_name = obj._class_name
                             print obj._class_name
                             print repr(props)
                         print repr([getattr(obj, p) for p in props])
             except WError, ex:
                 log.exception(ex)
                 yield async_sleep(1)
                 driver.next()
         q.close()
示例#3
0
class Watcher(object):

    def __init__(self, device, query):
        self.wmi = Query()
        self.device = device
        self.queryString = query
        self.enum = None
        self.busy = False
        self.closeRequested = False
        log.debug("Starting watcher on %s", device.id)

    @defer.inlineCallbacks
    def connect(self):
        self.busy = True
        try:
            log.debug("connecting to %s", self.device.id)
            d = self.device

            yield self.wmi.connect(eventContext,
                                   d.id,
                                   d.manageIp,
                                   "%s%%%s" % (d.zWinUser, d.zWinPassword))

            log.debug("connected to %s sending query %s", self.device.id, self.queryString)
            self.enum = yield self.wmi.notificationQuery(self.queryString)
            log.debug("got query response from %s", self.device.id)
        finally:
            self.busy = False
            if self.closeRequested:
                self.close()

    @defer.inlineCallbacks
    def getEvents(self, timeout=0, chunkSize=10):
        assert self.enum
        self.busy = True
        log.debug("Fetching events for %s", self.device.id)
        result = []

        try:
            result = yield self.enum.fetchSome(timeoutMs=timeout, chunkSize=chunkSize)
        finally:
            self.busy = False
            if self.closeRequested:
                self.close()

        log.debug("Events fetched for %s", self.device.id)
        defer.returnValue(result)
    
    def close(self):
        if self.busy:
            log.debug("close requested on busy WMI Query for %s; deferring",
                           self.device.id)
            self.closeRequested = True
        elif self.wmi:
            log.debug("closing WMI Query for %s", self.device.id)
            self.wmi.close()
            self.wmi = None

    def __del__(self):
        log.debug("Watcher.__del__ called for %s, busy=%r closeRequested=%r",
                  self.device.id, self.busy, self.closeRequested)
        self.close()