def getCommand(self, context, cmd=None):
     ''' generate the plugin command '''
     if self.cmdFile is not None: # this uses an external script
         cmd = binPath(self.cmdFile)
         if self.check_file(cmd) is False:  cmd = self.cmdFile
         props = getattr(context,'_properties')
         data = self.getData(props)
         parts = [cmd] + self.evalArgs(context, data) + self.addArgs(context, data)
         cmd = ' '.join(parts)
         cmd = RRDDataSource.getCommand(self, context, cmd)
         return cmd
     else:
         if cmd is None: cmd = self.commandTemplate
         if len(cmd) == 0: cmd = self.command
         compiled = self.getTALES(cmd)
         d = context.device()
         environ = {'dev' : d,
                    'device': d,
                    'devname': d.id,
                    'ds': self,
                    'datasource': self,
                    'here' : context,
                    'zCommandPath' : context.zCommandPath,
                    'nothing' : None,
                    'now' : DateTime() }
         res = compiled(getEngine().getContext(environ))
         if isinstance(res, Exception): raise res
         return res
示例#2
0
 def talesEval(self, text):
     dev = self.getDevice()
     if not dev:
         return 'Device %s could not be found' % self.deviceId
     comp = self.getComponent()
     if not comp:
         return 'Component %s could not be found for %s' % (
             self.componentPath, self.deviceId)
     graph = self.getGraphDef()
     if not graph:
         return 'Graph %s could not be found for %s' % (self.graphId,
                                                        self.deviceId)
     compiled = talesCompile('string:' + text)
     e = {
         'dev': dev,
         'device': dev,
         'comp': comp,
         'component': comp,
         'graph': graph
     }
     try:
         result = compiled(getEngine().getContext(e))
         if isinstance(result, Exception):
             result = 'Error: %s' % str(result)
     except Exception, e:
         result = 'Error: %s' % str(e)
 def getEventClass(self, environ, eventClass=None):
     '''Return localized (TALES-interpreted) eventClass.'''
     # choose default if not provided
     if eventClass is None: eventClass = self.eventClass
     compiled = self.getTALES(eventClass)
     self.eventClass = compiled
     res = compiled(getEngine().getContext(environ))
     if isinstance(res, Exception): raise res
     self.eventClass = res
 def getComments(self):
     """ Returns tales-evaluated comments
     """
     compiled = talesCompile("string:" + self.comments)
     e = {"rpt": self, "report": self, "now": DateTime()}
     result = compiled(getEngine().getContext(e))
     if isinstance(result, Exception):
         result = "Error: %s" % str(result)
     return result
示例#5
0
 def getComments(self):
     """
     Returns tales-evaluated comments
     """
     compiled = talesCompile('string:' + self.comments)
     e = {'rpt': self, 'report': self, 'now': DateTime()}
     result = compiled(getEngine().getContext(e))
     if isinstance(result, Exception):
         result = 'Error: %s' % str(result)
     return result
 def getComments(self):
     """
     Returns tales-evaluated comments
     """
     compiled = talesCompile('string:' + self.comments)
     e = {'rpt': self, 'report': self, 'now':DateTime()}
     result = compiled(getEngine().getContext(e))
     if isinstance(result, Exception):
         result = 'Error: %s' % str(result)
     return result
示例#7
0
 def collect(self, device, log):
     nmapoptions = getattr(device, 'zNmapPortscanOptions', NMAPDEFAULTS) 
     #compile Tales expressions
     tales = readyopts = None
     try:
         tales = talesCompile('string:' + nmapoptions)
         readyopts = tales(getEngine().getContext({'here':device, 'device':device, 'dev':device}))
         readyopts = readyopts + " " + device.manageIp
     #if there was an error make a best effort
     except Exception, e:
         log.error("zNmapPortscanOptions contain illegal Tales expression, please review: %s" % e)
         readyopts = NMAPDEFAULTS + " " + device.manageIp
 def getEventClass(self, environ, eventClass=None):
     """Return localized eventClass.
     """
     if eventClass is None:
         eventClass = self.eventClass
     if not eventClass.startswith('string:') and \
             not eventClass.startswith('python:'):
         eventClass = 'string:%s' % eventClass
     compiled = talesCompile(eventClass)
     self.eventClass = compiled
     res = compiled(getEngine().getContext(environ))
     if isinstance(res, Exception):
         raise res
     self.eventClass = res
示例#9
0
 def getDataField(thing, field):
     if not isinstance(field, basestring):
         value = field(getEngine().getContext({'here':thing, 'device':thing, 'dev':thing})  )
     else:
         if isinstance(thing, dict):
             value = thing.get(field, '')
         elif isinstance(thing, list) or isinstance(thing, tuple):
             value = thing[int(field)]
         else:
             value = getattr(thing, field, '')
     if isinstance(value, ZenModelBase):
         value = value.id
     elif callable(value):
         value = value()
     if value == None:
         value = ''
     return str(value)
示例#10
0
 def getDataField(thing, field):
     if not isinstance(field, basestring):
         value = field(getEngine().getContext({'here':thing, 'device':thing, 'dev':thing})  )
     else:
         if isinstance(thing, dict):
             value = thing.get(field, '')
         elif isinstance(thing, list) or isinstance(thing, tuple):
             value = thing[int(field)]
         else:
             value = getattr(thing, field, '')
     if isinstance(value, ZenModelBase):
         value = value.id
     elif callable(value):
         value = value()
     if value is None:
         value = ''
     return str(value)
示例#11
0
 def collect(self, device, log):
     nmapoptions = getattr(device, 'zNmapPortscanOptions', NMAPDEFAULTS)
     #compile Tales expressions
     tales = readyopts = None
     try:
         tales = talesCompile('string:' + nmapoptions)
         readyopts = tales(getEngine().getContext({
             'here': device,
             'device': device,
             'dev': device
         }))
         readyopts = readyopts + " " + device.manageIp
     #if there was an error make a best effort
     except Exception, e:
         log.error(
             "zNmapPortscanOptions contain illegal Tales expression, please review: %s"
             % e)
         readyopts = NMAPDEFAULTS + " " + device.manageIp
 def getComponent(self, context, component=None):
     ''' Return localized component. '''
     # choose default if not provided
     if component is None: component = self.component
     compiled = self.getTALES(component)
     d = context.device()
     environ = {
                'dev' : d,
                'device': d,
                'devname': d.id,
                'here' : context,
                'nothing' : None,
                'now' : DateTime() 
                }
     res = compiled(getEngine().getContext(environ))
     if isinstance(res, Exception): raise res
     self.getEventClass(environ, context.eventClass)
     return res
 def getComponent(self, context, component=None):
     """Return localized component.
     """
     if component is None:
         component = self.component
     if not component.startswith('string:') and \
             not component.startswith('python:'):
         component = 'string:%s' % component
     compiled = talesCompile(component)
     d = context.device()
     environ = {'dev' : d,
                'device': d,
                'devname': d.id,
                'here' : context,
                'nothing' : None,
                'now' : DateTime() }
     res = compiled(getEngine().getContext(environ))
     if isinstance(res, Exception):
         raise res
     self.getEventClass(environ, context.eventClass)
     return res
示例#14
0
 def talesEval(self, text):
     dev = self.getDevice()
     if not dev:
         return 'Device %s could not be found' % self.deviceId
     comp = self.getComponent()
     if not comp:
         return 'Component %s could not be found for %s' % (
                     self.componentPath, self.deviceId)
     graph = self.getGraphDef()
     if not graph:
         return 'Graph %s could not be found for %s' % (
                     self.graphId, self.deviceId)
     compiled = talesCompile('string:' + text)
     e = {'dev':dev, 'device': dev,
             'comp': comp, 'component':comp,
             'graph': graph}
     try:
         result = compiled(getEngine().getContext(e))
         if isinstance(result, Exception):
             result = 'Error: %s' % str(result)
     except Exception, e:
         result = 'Error: %s' %  str(e)
示例#15
0
 def collect(self, device, log):
     nmapoptions = getattr(device, 'zNmapPortscanOptions', NMAPDEFAULTS)
     #compile Tales expressions
     tales = readyopts = None
     try:
         tales = talesCompile('string:' + nmapoptions)
         readyopts = tales(getEngine().getContext({
             'here': device,
             'device': device,
             'dev': device
         }))
         readyopts = readyopts + " " + device.manageIp
     #if there was an error make a best effort
     except Exception as e:
         log.error(
             "zNmapPortscanOptions contain illegal Tales expression, please review: %s"
             % e)
         readyopts = NMAPDEFAULTS + " " + device.manageIp
     nmapoptions = readyopts.split(" ")
     log.info("running the following nmap command: %s %s" % \
               ("/usr/bin/nmap", " ".join(nmapoptions)))
     return getProcessOutput("/usr/bin/nmap", nmapoptions)