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

Source Code for Module DataCollector.PythonClient

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