CodeLab
19Aug/100

Writing a custom, non-SNMP, non-SSH, PythonPlugin-based zenoss modeler

If you have been customizing zenoss to suit your business needs you are likely to have developed several SNMP-based or SSH-based modelers in your zenpacks. But what if either protocol does not suit your needs? There is another, not so well documented plug-in class supported by zenoss: PythonPlugin.

When sub-classing PythonPlugin there are 2 mandatory methods that you must provide: collect and process. The collect method (surprisingly) implements whatever custom mechanism you need to fetch and return your data; which will then be processed by the process method (as in the standard SNMP/SSH-based modeler plugins). The collect method will be called with three arguments: self, device (well it really is a "device proxy") and log.

If you need to access custom zProperties in your modeler then you will need to override the copyDataToProxy method so you ensure that your custom zProperty gets added to the deviceproxy.

Finally, you can control whether the modeler plugin should be run or not for the current device at run time by overriding the condition method, and returning either True or False based on the condition of your choice.

Here is the complete example to demonstrate it:

__doc__="""
   Custom modeler example
"""
from Products.ZenModel.ZenPackPersistence import ZenPackPersistence
from Products.DataCollector.plugins.CollectorPlugin import PythonPlugin
import logging
log = logging.getLogger("zen.zenmodeler")

class CustomModelerMap(PythonPlugin):

    ZENPACKID = 'Zenpacks.community.CustomModelerExample'
    maptype   = 'CustomModelerMap'

    def condition(self, device, log):
       '''
          Override "condition" to control whether this modeler 
          should be applied or not to the current device
       '''
       if device.zUseMyModeler:
          return True
       else:
          return False

    def copyDataToProxy(self, device, proxy):
        '''
          Override "copyDataToProxy" to enable the access 
          to custom zProperties on the rest of your modeler
        '''
        result = PythonPlugin.copyDataToProxy(self, device, proxy)
        proxy.zUseMyModeler = device.zUseMyModeler

    
    def collect(self, device, log):
        '''
          Do here whatever you needed to fetch your data & prepare
          the results to me processed later
        '''
        return {'sysName' : 'Fake system set up by a custom modeler'}

    def process(self, device, results, log):
        '''
           Do your modeling maps as usual
        '''
        om = self.objectMap({ })
        if results.has_key('sysName'):
            om.setHWProductKey = results[ 'sysName' ]      
        return om


Filed under: Python, Zenoss Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

Trackbacks are disabled.