Beispiel #1
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)
Beispiel #2
0
 def getCommand(self, context, cmd=None, device=None):
     """Return localized command target.
     """
     # Perform a TALES eval on the expression using self
     if cmd is None:
         cmd = self.commandTemplate
     if not cmd.startswith('string:') and not cmd.startswith('python:'):
         cmd = 'string:%s' % cmd
     compiled = talesCompile(cmd)
     d = device if device is not None else context.device()
     environ = {'dev' : d,
                'device': d,
                'devname': d.id,
                'ds': self,
                'datasource': self,
                'here' : context,
                'context': context,
                'zCommandPath' : context.zCommandPath,
                'nothing' : None,
                'now' : DateTime() }
     res = compiled(getEngine().getContext(environ))
     if isinstance(res, Exception):
         raise res
     res = self.checkCommandPrefix(context, res)
     return res
 def getCommand(ob, context, cmd=None):
     '''
         generate the plugin command
     '''
     if ob.cmdFile is not None: # this uses an external script
         cmd = binPath(ob.cmdFile)
         if ob.check_file(cmd) is False:  cmd = ob.cmdFile
         props = getattr(context,'_properties')
         data = ob.getData(props)
         parts = [cmd] + ob.evalArgs(context, data) + ob.addArgs(context, data)
         cmd = ' '.join(parts)
         cmd = RRDDataSource.getCommand(ob, context, cmd)
         return cmd
     else:
         if cmd is None:
             cmd = ob.commandTemplate
         if len(cmd) == 0:
             cmd = ob.command
         if not cmd.startswith('string:') and not cmd.startswith('python:'):
             cmd = 'string:%s' % cmd
         compiled = talesCompile(cmd)
         d = context.device()
         environ = {'dev' : d,
                    'device': d,
                    'devname': d.id,
                    'ds': ob,
                    'datasource': ob,
                    'here' : context,
                    'zCommandPath' : context.zCommandPath,
                    'nothing' : None,
                    'now' : DateTime() }
         res = compiled(getEngine().getContext(environ))
         if isinstance(res, Exception):
             raise res
         return res
 def getCommand(self, context, cmd=None):
     """Return localized command target.
     """
     # Perform a TALES eval on the expression using self
     if cmd is None:
         cmd = self.commandTemplate
     if not cmd.startswith('string:') and not cmd.startswith('python:'):
         cmd = 'string:%s' % cmd
     compiled = talesCompile(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
     res = self.checkCommandPrefix(context, res)
     return res
 def getCommand(self, context, cmd=None):
     # No getCommand in BasicDataSource  - inherits from 
     #   SimpleRRDDataSource inherits from RRDDataSource
     # Duplicate getCommand from RRDDataSource and add  stringToFind
     # Perform a TALES eval on the expression using self
     if cmd is None:
         cmd = self.commandTemplate
     log.debug(' Raw cmd is %s ' % (cmd))
     if self.stringToFind:
         #Need to ensure any white space is wrapped in quotes
         cmd = cmd + ' "' + self.stringToFind + '"'
     log.debug(' cmd with stringToFind is %s ' % (cmd))
     if not cmd.startswith('string:') and not cmd.startswith('python:'):
         cmd = 'string:%s' % cmd
     log.debug(' cmd after string check is %s ' % (cmd))
     compiled = talesCompile(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() }
     log.debug(' environ  is %s ' % (environ))
     res = compiled(getEngine().getContext(environ))
     log.debug(' res compiled engine is %s ' % (res))
     if isinstance(res, Exception):
         raise res
     res = self.checkCommandPrefix(context, res)
     log.debug(' res to return is %s ' % (res))
     return 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
Beispiel #7
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
Beispiel #9
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
Beispiel #11
0
 def compile(self, cmd, target):
     ''' Evaluate command as a tales expression
     '''
     command = cmd.command
     # make sure we are targeting the right collector
     if not command.startswith("dcsh") and hasattr(target, "getPerformanceServerName"):
         collector = target.getPerformanceServer()
         # if there isn't a collector just run it locally
         if collector and hasattr(collector, 'isLocalHost') and not collector.isLocalHost():
             command = 'dcsh --collector=${device/getPerformanceServerName} -n "%s"' % (command.replace('\n', ' '))
     exp = "string:"+ command
     compiled = talesCompile(exp)
     environ = target.getUserCommandEnvironment()
     res = compiled(getEngine().getContext(environ))
     if isinstance(res, Exception):
         raise res
     return res
 def compile(self, cmd, target):
     ''' Evaluate command as a tales expression
     '''
     command = cmd.command
     # make sure we are targeting the right collector
     if not command.startswith("dcsh") and hasattr(target, "getPerformanceServerName"):
         collector = target.getPerformanceServer()
         # if there isn't a collector just run it locally
         if collector and hasattr(collector, 'isLocalHost') and not collector.isLocalHost():
             command = 'dcsh --collector=${device/getPerformanceServerName} -n "%s"' % (command)
     exp = "string:"+ command
     compiled = talesCompile(exp)
     environ = target.getUserCommandEnvironment()
     res = compiled(getEngine().getContext(environ))
     if isinstance(res, Exception):
         raise res
     return res
Beispiel #13
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(context, componentId, componentField=None):
    """Return localized component.
    """
    if componentField is None:
        return componentId
    if not componentField.startswith('string:') and \
            not componentField.startswith('python:'):
        componentField = 'string:%s' % componentField
    compiled = talesCompile(componentField)
    d = context
    environ = {'dev' : d,
               'device': d,
               'here' : context,
               'nothing' : None,
               'now' : DateTime() }
    res = compiled(getEngine().getContext(environ))
    if isinstance(res, Exception):
        raise res
    return res
 def compile(self, cmd, target):
     ''' Evaluate command as a tales expression
     '''
     command = cmd.command
     # make sure we are targeting the right collector
     if not command.startswith("dcsh") and hasattr(
             target, "getPerformanceServerName"):
         collector = target.getPerformanceServer()
         if collector:
             command = 'zminion --minion-name zminion_%s run -- "%s"' % (
                 target.getPerformanceServerName(),
                 command.replace('\n', ' '))
     exp = "string:" + command
     compiled = talesCompile(exp)
     environ = target.getUserCommandEnvironment()
     res = compiled(getEngine().getContext(environ))
     if isinstance(res, Exception):
         raise res
     return res
Beispiel #16
0
 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
     return res
Beispiel #17
0
 def getComponent(self, context, component=None, device=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 = device if device is not None else 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
     return res
Beispiel #18
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)
Beispiel #19
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)
Beispiel #20
0
def retrieveDataSourceConfigs(entity):
    """
    Represents an AppEngine datasource configuration on a device.
    """
    for template in entity.getRRDTemplates():
        for ds in template.getRRDDataSources():
            datapoints = ds.datapoints()
            if ds.sourcetype == 'AppEngine' and len(datapoints) > 0:
                rrdConfig = {}
                for dp in ds.datapoints():
                    rrdConfig[dp.id] = RRDConfig(dp)
                thresholds = []
                for thresh in template.thresholds():
                    thresholds.append(thresh.createThresholdInstance(entity))
                instance = ds.instance
                if not instance.startswith('string:') and \
                   not instance.startswith('python:'):
                    instance = 'string:%s' % instance
                compiled = talesCompile(instance)
                d = entity.device()
                environ = {
                    'dev': d,
                    'device': d,
                    'devname': d.id,
                    'here': entity,
                    'nothing': None,
                    'now': DateTime()
                }
                instance = compiled(getEngine().getContext(environ))
                counterId = ds.id
                if counterId == None: continue
                metricId = MetricId(counterId, instance)
                rrdPath = '%s/%s' % (entity.rrdPath(), rrdConfig[dp.id].dpName)
                eventKey = ds.eventKey

                yield AppEngineDataSourceConfig(entity.id, ds.id, metricId,
                                                rrdPath, rrdConfig, thresholds,
                                                eventKey)
def retrieveDataSourceConfigs( entity ):
    """
    Represents an AppEngine datasource configuration on a device.
    """
    for template in entity.getRRDTemplates():
        for ds in template.getRRDDataSources():
            datapoints=ds.datapoints()
            if ds.sourcetype == 'AppEngine' and len(datapoints) > 0:
                rrdConfig = {}
                for dp in ds.datapoints():
                    rrdConfig[dp.id] = RRDConfig(dp)
                thresholds = []
                for thresh in template.thresholds():
                    thresholds.append(thresh.createThresholdInstance(entity))
                instance = ds.instance
                if not instance.startswith('string:') and \
                   not instance.startswith('python:'):
                    instance = 'string:%s' % instance
                compiled = talesCompile(instance)
                d = entity.device()
                environ = {'dev' : d,
                           'device': d,
                           'devname': d.id,
                           'here' : entity,
                           'nothing' : None,
                           'now' : DateTime() }
                instance = compiled(getEngine().getContext(environ))
                counterId = ds.id
                if counterId==None: continue
                metricId=MetricId(counterId, instance)
                rrdPath='%s/%s' % (entity.rrdPath(), rrdConfig[dp.id].dpName)
                eventKey = ds.eventKey

                yield AppEngineDataSourceConfig(
                                entity.id, ds.id,
                                metricId, rrdPath,
                                rrdConfig, thresholds, eventKey )
Beispiel #22
0
 def writeExportRows(self, fieldsAndLabels, objects, out=None):
     '''Write out csv rows with the given objects and fields.
     If out is not None then call out.write() with the result and return None
     otherwise return the result.
     Each item in fieldsAndLabels is either a string representing a
      field/key/index (see getDataField) or it is a tuple of (field, label)
      where label is the string to be used in the first row as label
      for that column.
     Objects can be either dicts, lists/tuples or other objects. Field
      is interpreted as a key, index or attribute depending on what
      object is.
     Method names can be passed instead of attribute/key/indices as field.
      In this case the method is called and the return value is used in
      the export.
     '''
     import csv
     import StringIO
     if out:
         buffer = out
     else:
         buffer = StringIO.StringIO()
     fields = []
     labels = []
     if not fieldsAndLabels:
         fieldsAndLabels = []
     if not objects:
         objects = []
     for p in fieldsAndLabels:
         if isinstance(p, tuple):
             fields.append(p[0])
             labels.append(p[1])
         else:
             fields.append(p)
             labels.append(p)
     writer = csv.writer(buffer)
     writer.writerow(labels)
     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)
     for i, field in enumerate(fields):
         testTales = field.split(':',1)[0].strip().lower()
         if testTales=='python':
             fields[i] = talesCompile(field)
     for o in objects:
         writer.writerow([getDataField(o,f) for f in fields])
     if out:
         result = None
     else:
         result = buffer.getvalue()
     # aborting the long running export transaction so it is not retried
     import transaction
     transaction.abort()
     return result
Beispiel #23
0
    def writeExportRows(self, fieldsAndLabels, objects, out=None):
        '''Write out csv rows with the given objects and fields.
        If out is not None then call out.write() with the result and return None
        otherwise return the result.
        Each item in fieldsAndLabels is either a string representing a
         field/key/index (see getDataField) or it is a tuple of (field, label)
         where label is the string to be used in the first row as label
         for that column.
        Objects can be either dicts, lists/tuples or other objects. Field
         is interpreted as a key, index or attribute depending on what
         object is.
        Method names can be passed instead of attribute/key/indices as field.
         In this case the method is called and the return value is used in
         the export.
        '''
        import csv
        import StringIO
        if out:
            buffer = out
        else:
            buffer = StringIO.StringIO()
        fields = []
        labels = []
        if not fieldsAndLabels:
            fieldsAndLabels = []
        if not objects:
            objects = []
        for p in fieldsAndLabels:
            if isinstance(p, tuple):
                fields.append(p[0])
                labels.append(p[1])
            else:
                fields.append(p)
                labels.append(p)
        writer = csv.writer(buffer)
        writer.writerow(labels)

        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)

        for i, field in enumerate(fields):
            testTales = field.split(':', 1)[0].strip().lower()
            if testTales == 'python':
                fields[i] = talesCompile(field)
        for o in objects:
            writer.writerow([getDataField(o, f) for f in fields])
        if out:
            result = None
        else:
            result = buffer.getvalue()
        # aborting the long running export transaction so it is not retried
        import transaction
        transaction.abort()
        return result
 def getTALES(self, ob):
     '''return compiled TALES expression'''
     # make sure it is prepended with :string or :python for TALES
     if not ob.startswith('string:') and not ob.startswith('python:'):
         ob = 'string:%s' % ob
     return talesCompile(ob)