Package Products :: Package ZenUtils :: Module CyclingDaemon |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenUtils.CyclingDaemon

     1  ##############################################################################
     2  #
     3  # Copyright (C) Zenoss, Inc. 2009, all rights reserved.
     4  #
     5  # This content is made available according to terms specified in
     6  # License.zenoss under the directory where your Zenoss product is installed.
     7  #
     8  ##############################################################################
     9
    10
    11  from Globals import *
    12  import socket
    13  from Products.ZenUtils.ZCmdBase import ZCmdBase
    14  from Products.ZenUtils.Utils import getDefaultZopeUrl
    15  from Products.ZenEvents import Event
    16  from twisted.internet import reactor, defer
    17
    18  DEFAULT_MONITOR = "localhost"
    19
    20 -class CyclingDaemon(ZCmdBase):
    21
    22 -    def main_loop(self):
    23          raise NotImplementedError("Your daemon must define this method.")
    24
    25 -    def run(self):
    26          reactor.callLater(0, self.runCycle)
    27          reactor.run()
    28
    29 -    def finish(self, results=None):
    30          reactor.stop()
    31
    32 -    def sendEvent(self, evt):
    33          """Send event to the system.
    34          """
    35          self.dmd.ZenEventManager.sendEvent(evt)
    36
    37 -    def sendHeartbeat(self):
    38          """Send a heartbeat event for this monitor.
    39          """
    40          timeout = self.options.cycletime*3
    41          evt = Event.EventHeartbeat(self.options.monitor, self.name, timeout)
    42          self.sendEvent(evt)
    43          self.niceDoggie(self.options.cycletime)
    44
    45      @defer.inlineCallbacks
    46 -    def runCycle(self):
    47          try:
    48              self.syncdb()
    49              yield self.main_loop()
    50              self.sendHeartbeat()
    51          except Exception, e:
    52              self.log.exception("Unexpected exception while running jobs")
    53          if not self.options.cycle:
    54              self.finish()
    55          reactor.callLater(self.options.cycletime, self.runCycle)
    56
    57 -    def sigTerm(self, signum=None, frame=None):
    58          """
    59          Controlled shutdown of main loop on interrupt.
    60          """
    61          try:
    62              ZCmdBase.sigTerm(self, signum, frame)
    63          except SystemExit:
    64              self.finish()
    65
    66 -    def buildOptions(self):
    67          ZCmdBase.buildOptions(self)
    68          self.parser.add_option('--cycletime',
    69              dest='cycletime', default=60, type="int",
    70              help="check events every cycletime seconds")
    71          self.parser.add_option(
    72              '--zopeurl', dest='zopeurl',
    73              default=getDefaultZopeUrl(),
    74              help="http path to the root of the zope server")
    75          self.parser.add_option("--monitor", dest="monitor",
    76              default=DEFAULT_MONITOR,
    77              help="Name of monitor instance to use for heartbeat "
    78                  " events. Default is %s." % DEFAULT_MONITOR)
    79