Package Products :: Package ZenHub :: Module ServiceTester |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenHub.ServiceTester

     1  ###########################################################################
     2  #
     3  # This program is part of Zenoss Core, an open source monitoring platform.
     4  # Copyright (C) 2011 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
    14  __doc__ = """ServiceTester
    15
    16  Simple utility class for testing out zenhub services.
    17  Sample usage (at the bottom of a service):
    18
    19  if __name__ == '__main__':
    20      from Products.ZenHub.ServiceTester import ServiceTester
    21      tester = ServiceTester(PingPerformanceConfig)
    22      def printer(config):
    23          for ip in config.monitoredIps:
    24              print '\t', ip
    25      tester.printDeviceProxy = printer
    26      tester.showDeviceInfo()
    27
    28  Note that the service instance can be found as an attribute
    29  on the tester object.
    30
    31  ie tester.service == PingPerformanceConfig(dmd, 'localhost')
    32  """
    33
    34  from pprint import pprint
    35  import logging
    36  log = logging.getLogger('zen.ServiceTester')
    37
    38  import Globals
    39
    40  from Products.ZenUtils.ZCmdBase import ZCmdBase
    41
    42
    43 -class ServiceTester(ZCmdBase):
    44      doesLogging = False
    45
    46 -    def __init__(self, Klass):
    47          ZCmdBase.__init__(self, False, False, False)
    48          # It's super annoying to try to figure out how to get the
    49          # zenhub service to drop into debug mode.  Use the following.
    50          self.setLogLevel(10)
    51          logging.basicConfig()
    52          self.service = Klass(self.dmd, self.options.monitor)
    53
    54 -    def buildOptions(self):
    55          ZCmdBase.buildOptions(self)
    56          self.parser.add_option('--monitor', dest='monitor', default='localhost',
    57                                 help="Specify the collector to collect against.")
    58          self.parser.add_option('-d', '--device', dest='device',
    59                                 help="Show the configs for a single device")
    60
    61 -    def setLogLevel(self, level=10):
    62          """
    63          Change the logging level to allow for more insight into the
    64          in-flight mechanics of Zenoss.
    65
    66          @parameter level: logging level at which messages display (eg logging.INFO)
    67          @type level: integer
    68          """
    69          rootlog = logging.getLogger()
    70          rootlog.setLevel(level)
    71          for handler in rootlog.handlers:
    72              if isinstance(handler, logging.StreamHandler):
    73                  handler.setLevel(level)
    74
    75 -    def pprint(self, arg):
    76          pprint(arg)
    77
    78 -    def showDeviceInfo(self):
    79          if self.options.device:
    80              name = self.options.device
    81              config = self.service.remote_getDeviceConfigs([name])
    82              if config:
    83                  print "Config for %s =" % name
    84                  self.printDeviceProxy(config[0])
    85              else:
    86                  log.warn("No configs found for %s", name)
    87          else:
    88              devices = sorted(x.id for x in self.service.remote_getDeviceConfigs())
    89              print "Device list = %s" % devices
    90
    91 -    def printDeviceProxy(self, proxy):
    92          """
    93          Device proxies don't report their interal state very well. This
    94          should be overwritten by the zenhub service writer.
    95          """
    96          pprint(proxy)
    97