Package Products :: Package DataCollector :: Module PythonClient
[hide private]
[frames] | no frames]

Source Code for Module Products.DataCollector.PythonClient

  1  ############################################################################## 
  2  #  
  3  # Copyright (C) Zenoss, Inc. 2007, 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__ = """PythonClient 
 12  Python performance data collector client 
 13  """ 
 14   
 15  import logging 
 16  log = logging.getLogger("zen.PythonClient") 
 17   
 18  import Globals 
 19   
 20  from BaseClient import BaseClient 
 21  from twisted.internet.defer import Deferred, DeferredList 
 22  from twisted.python.failure import Failure 
 23   
24 -class PythonClient(BaseClient):
25 """ 26 Implement the DataCollector Client interface for Python 27 """ 28 29
30 - def __init__(self, device=None, datacollector=None, plugins=[]):
31 """ 32 Initializer 33 34 @param device: remote device to use the datacollector 35 @type device: device object 36 @param datacollector: performance data collector object 37 @type datacollector: datacollector object 38 @param plugins: Python-based performance data collector plugin 39 @type plugins: list of plugin objects 40 """ 41 BaseClient.__init__(self, device, datacollector) 42 self.hostname = device.id 43 self.plugins = plugins 44 self.results = []
45 46
47 - def run(self):
48 """ 49 Start Python collection. 50 """ 51 deferreds = [] 52 for plugin in self.plugins: 53 log.debug("Running collection for plugin %s", plugin.name()) 54 r = plugin.collect(self.device, log) 55 if isinstance(r, Deferred): 56 deferreds.append(r) 57 r.addBoth(self.collectComplete, plugin) 58 else: 59 log.debug("Results for %s: %s", plugin.name(), str(r)) 60 self.results.append((plugin, r)) 61 62 dl = DeferredList(deferreds) 63 dl.addCallback(self.collectComplete, None)
64 65
66 - def collectComplete(self, r, plugin):
67 """ 68 Twisted deferred error callback used to store the 69 results of the collection run 70 71 @param r: result from the collection run 72 @type r: result or Exception 73 @param plugin: Python-based performance data collector plugin 74 @type plugin: plugin object 75 """ 76 if plugin is None: 77 self.clientFinished() 78 return 79 80 if isinstance(r, Failure): 81 log.warn("Error in %s: %s", plugin.name(), r.getErrorMessage()) 82 else: 83 log.debug("Results for %s: %s", plugin.name(), str(r)) 84 self.results.append((plugin, r))
85 86
87 - def clientFinished(self):
88 """ 89 Stop the collection of performance data 90 """ 91 log.info("Python client finished collection for %s" % self.device.id) 92 if self.datacollector: 93 self.datacollector.clientFinished(self)
94 95
96 - def getResults(self):
97 """ 98 Return the results of the data collection. 99 To be implemented by child classes 100 101 @return: list of results 102 @rtype: list of results 103 """ 104 return self.results
105