Package Products :: Package ZenRRD :: Module utils |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenRRD.utils

      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__="""utils
     15
     16  RRD utility functions
     17  """
     18
     19  from sets import Set
     20  from Acquisition import aq_chain
     21
     22  from Exceptions import RRDObjectNotFound, TooManyArgs
     23
     24 -def loadargs(obj, args):
     25      """
     26      Load data into a RRD Object
     27
     28      @param obj: RRD object
     29      @type obj: RRD object
     30      @param args: arguments
     31      @type args: list of strings
     32      """
     33      import string
     34      arglen = len(args)
     35      if arglen > len(obj._properties):
     36          raise TooManyArgs( "Too many args" )
     37      i = 0
     38      for arg in args:
     39          if i > arglen: break
     40          arg = arg.strip()
     41          att = obj._properties[i]['id']
     42          try:
     43              if arg != '':
     44                  if obj._properties[i]['type'] == 'lines':
     45                      value = map(string.strip, arg.split(','))
     46                      att = '_' + att
     47                  elif obj._properties[i]['type'] == 'int':
     48                      value = int(arg)
     49                  elif obj._properties[i]['type'] == 'long':
     50                      value = long(arg)
     51                  elif obj._properties[i]['type'] == 'float':
     52                      value = float(arg)
     53                  elif obj._properties[i]['type'] == 'boolean':
     54                      value = eval(arg)
     55                  else:
     56                      value = arg
     57                  if value is not None: setattr(obj,att,value)
     58          except:
     59              print "att = %s value = %s" % (att, arg)
     60              raise
     61          i += 1
     62
     63
     64 -def prefixid(idprefix, id):
     65      """
     66      See if prefix needs to be added to id
     67
     68      @param idprefix: prefix
     69      @type idprefix: string
     70      @param id: identifier
     71      @type id: string
     72      @return: add the prefix with a '-' in between
     73      @rtype: string
     74      """
     75      if id.find(idprefix) != 0:
     76          id = idprefix + '-' + id
     77      return id
     78
     79
     80 -def rootid(idprefix, id):
     81      """
     82      See if prefix needs to be removed from id
     83
     84      @param idprefix: prefix
     85      @type idprefix: string
     86      @param id: identifier
     87      @type id: string
     88      @return: remove the prefix with a '-' in between or return None
     89      @rtype: string or None
     90      """
     91      if idprefix[-1] != '-': idprefix += '-'
     92      if id.find(idprefix) == 0:
     93          return id[len(idprefix):]
     94
     95
     96 -def walkupconfig(context, name):
     97      """
     98      Given a Zope context, try to find the rrdconfig object
     99      for the name.
    100      Raises RRDObjectNotFound if not found.
    101
    102      @param context: Zope context
    103      @type context: Zope context object
    104      @param name: RRDView name
    105      @type name: string
    106      @return: rrdconfig object or None
    107      @rtype: rrdconfig object
    108      """
    109      if not name: return
    110      while 1:
    111          if hasattr(context, 'rrdconfig') and hasattr(context.rrdconfig, name):
    112              return getattr(context.rrdconfig, name)
    113          context = context.aq_parent
    114          if context.id == 'dmd':
    115              raise RRDObjectNotFound( "Object %s not found in context %s" % \
    116                                      (name, context.getPrimaryUrlPath()))
    117
    118
    119 -def templateNames(context):
    120      """
    121      Return template names in the given context
    122
    123      @param context: Zope context
    124      @type context: Zope context object
    125      @return: names of the templates
    126      @rtype: set of strings
    127      """
    128      names = Set()
    129      for obj in aq_chain(context):
    130          rrdconfig = getattr(obj, 'rrdconfig', None)
    131          if rrdconfig:
    132              names = names.union(rrdconfig.objectIds(spec='RRDTargetType'))
    133      return names
    134
    135
    136
    137 -def getRRDView(context, name):
    138      """
    139      Lookup an RRDView based on its name
    140
    141      @param context: Zope context
    142      @type context: Zope context object
    143      @param name: RRDView name
    144      @type name: string
    145      @return: rrdconfig object or None
    146      @rtype: rrdconfig object
    147      """
    148      return walkupconfig(context, 'RRDView-'+name)
    149
    150
    151 -def getRRDTargetType(context, name):
    152      """
    153      Lookup an rrdtargettype based on its name
    154
    155      @param context: Zope context
    156      @type context: Zope context object
    157      @param name: RRDView name
    158      @type name: string
    159      @return: rrdconfig object or None
    160      @rtype: rrdconfig object
    161      """
    162      return walkupconfig(context, 'RRDTargetType-'+name)
    163
    164
    165 -def getRRDDataSource(context, name):
    166      """
    167      Lookup an rrddatasource based on its name
    168
    169      @param context: Zope context
    170      @type context: Zope context object
    171      @param name: RRDView name
    172      @type name: string
    173      @return: rrdconfig object or None
    174      @rtype: rrdconfig object
    175      """
    176      return walkupconfig(context, 'RRDDataSource-'+name)
    177
    178
    179 -def rpneval(value, rpn):
    180      """
    181      Totally bogus RPN evaluation only works with one-level stack
    182
    183      @param value: something that can be used as a number
    184      @type value: string
    185      @param rpn: Reverse Polish Notatio (RPN) expression
    186      @type rpn: string
    187      @todo: make unbogus
    188      """
    189      if type(value) == type(''): return value
    190      operators = ('+','-','*','/')
    191      rpn = rpn.split(',')
    192      operator = ''
    193      for i in range(len(rpn)):
    194          symbol = rpn.pop()
    195          symbol = symbol.strip()
    196          if symbol in operators:
    197              operator = symbol
    198          else:
    199              expr = str(value) + operator + symbol
    200              value = eval(expr)
    201      return value
    202