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

Source Code for Module Products.ZenHub.ServiceTester

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