Package Products :: Package ZenWin :: Module Watcher |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenWin.Watcher

     1  ###########################################################################
     2  #
     3  # This program is part of Zenoss Core, an open source monitoring platform.
     4  # Copyright (C) 2007-2009 Zenoss Inc.
     5  #
     6  # This program is free software; you can redistribute it and/or modify it
     7  # under the terms of the GNU General Public License version 2 as published by
     8  # the Free Software Foundation.
     9  #
    10  # For complete information please visit: http://www.zenoss.com/oss/
    11  #
    12  ###########################################################################
    13
    14  from pysamba.twisted.reactor import eventContext
    15  from pysamba.wbem.Query import Query
    16  from Products.ZenUtils.Driver import drive
    17
    18  import logging
    19  log = logging.getLogger("zen.Watcher")
    20
    21 -class Watcher:
    22
    23 -    def __init__(self, device, query):
    24          self.wmi = Query()
    25          self.device = device
    26          self.queryString = query
    27          self.enum = None
    28          self.busy = False
    29          self.closeRequested = False
    30          log.debug("Starting watcher on %s", device.id)
    31
    32 -    def connect(self):
    33          self.busy = True
    34
    35          def finished(result):
    36              self.busy = False
    37              if self.closeRequested:
    38                  self.close()
    39              return result
    40
    41          def inner(driver):
    42              log.debug("connecting to %s", self.device.id)
    43              d = self.device
    44
    45              yield self.wmi.connect(eventContext,
    46                                     d.id,
    47                                     d.manageIp,
    48                                     "%s%%%s" % (d.zWinUser, d.zWinPassword))
    49              driver.next()
    50
    51              log.debug("connected to %s sending query", self.device.id)
    52              log.debug("%s" % self.queryString)
    53              yield self.wmi.notificationQuery(self.queryString)
    54
    55              self.enum = driver.next()
    56              log.debug("got query response from %s", self.device.id)
    57
    58          return drive(inner).addBoth(finished)
    59
    60 -    def getEvents(self, timeout=0, chunkSize=10):
    61          assert self.enum
    62          self.busy = True
    63          log.debug("Fetching events for %s", self.device.id)
    64
    65          def fetched(result):
    66              log.debug("Events fetched for %s", self.device.id)
    67              return result
    68
    69          def finished(result):
    70              self.busy = False
    71              if self.closeRequested:
    72                  self.close()
    73              return result
    74
    75          result = self.enum.fetchSome(timeoutMs=timeout, chunkSize=chunkSize)
    76          result.addBoth(finished)
    77          result.addCallback(fetched)
    78          return result
    79
    80 -    def close(self):
    81          if self.busy:
    82              log.debug("close requested on busy WMI Query for %s; deferring",
    83                             self.device.id)
    84              self.closeRequested = True
    85          elif self.wmi:
    86              log.debug("closing WMI Query for %s", self.device.id)
    87              self.wmi.close()
    88              self.wmi = None
    89
    90 -    def __del__(self):
    91          log.debug("Watcher.__del__ called for %s, busy=%r closeRequested=%r",
    92                    self.device.id, self.busy, self.closeRequested)
    93          self.close()
    94