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

# Source Code for Module Products.ZenUtils.CyclingDaemon

     1  ###########################################################################
     2  #
     3  # This program is part of Zenoss Core, an open source monitoring platform.
     4  # Copyright (C) 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 or (at your
     8  # option) any later version as published by the Free Software Foundation.
     9  #
    10  # For complete information please visit: http://www.zenoss.com/oss/
    11  #
    12  ###########################################################################
    13  from Globals import *
    14  import socket
    15  from Products.ZenUtils.ZCmdBase import ZCmdBase
    16  from Products.ZenUtils.Utils import getDefaultZopeUrl
    17  from Products.ZenEvents import Event
    18  from twisted.internet import reactor, defer
    19
    20  DEFAULT_MONITOR = "localhost"
    21
    22 -class CyclingDaemon(ZCmdBase):
    23
    24 -    def main_loop(self):
    25          raise NotImplementedError("Your daemon must define this method.")
    26
    27 -    def run(self):
    28          reactor.callLater(0, self.runCycle)
    29          reactor.run()
    30
    31 -    def finish(self, results=None):
    32          reactor.stop()
    33
    34 -    def sendEvent(self, evt):
    35          """Send event to the system.
    36          """
    37          self.dmd.ZenEventManager.sendEvent(evt)
    38
    39 -    def sendHeartbeat(self):
    40          """Send a heartbeat event for this monitor.
    41          """
    42          timeout = self.options.cycletime*3
    43          evt = Event.EventHeartbeat(self.options.monitor, self.name, timeout)
    44          self.sendEvent(evt)
    45          self.niceDoggie(self.options.cycletime)
    46
    47      @defer.inlineCallbacks
    48 -    def runCycle(self):
    49          try:
    50              self.syncdb()
    51              yield self.main_loop()
    52              self.sendHeartbeat()
    53          except Exception, e:
    54              self.log.exception("Unexpected exception while running jobs")
    55          if not self.options.cycle:
    56              self.finish()
    57          reactor.callLater(self.options.cycletime, self.runCycle)
    58
    59 -    def sigTerm(self, signum=None, frame=None):
    60          """
    61          Controlled shutdown of main loop on interrupt.
    62          """
    63          try:
    64              ZCmdBase.sigTerm(self, signum, frame)
    65          except SystemExit:
    66              self.finish()
    67
    68 -    def buildOptions(self):
    69          ZCmdBase.buildOptions(self)
    70          self.parser.add_option('--cycletime',
    71              dest='cycletime', default=60, type="int",
    72              help="check events every cycletime seconds")
    73          self.parser.add_option(
    74              '--zopeurl', dest='zopeurl',
    75              default=getDefaultZopeUrl(),
    76              help="http path to the root of the zope server")
    77          self.parser.add_option("--monitor", dest="monitor",
    78              default=DEFAULT_MONITOR,
    79              help="Name of monitor instance to use for heartbeat "
    80                  " events. Default is %s." % DEFAULT_MONITOR)
    81