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

Source Code for Module 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  utility functions for RRDProduct 
 17   
 18  $Id: utils.py,v 1.9 2003/05/12 16:13:28 edahl Exp $""" 
 19   
 20  __version__ = "$Revision: 1.9 $"[11:-2] 
 21   
 22  from sets import Set 
 23  from Acquisition import aq_chain 
 24   
 25  from Exceptions import RRDObjectNotFound, TooManyArgs 
 26   
27 -def loadargs(obj, args):
28 """Load data into a RRD Object""" 29 import string 30 arglen = len(args) 31 if arglen > len(obj._properties): raise TooManyArgs, "Too many args" 32 i = 0 33 for arg in args: 34 if i > arglen: break 35 arg = arg.strip() 36 att = obj._properties[i]['id'] 37 try: 38 if arg != '': 39 if obj._properties[i]['type'] == 'lines': 40 value = map(string.strip, arg.split(',')) 41 att = '_' + att 42 elif obj._properties[i]['type'] == 'int': 43 value = int(arg) 44 elif obj._properties[i]['type'] == 'long': 45 value = long(arg) 46 elif obj._properties[i]['type'] == 'float': 47 value = float(arg) 48 elif obj._properties[i]['type'] == 'boolean': 49 value = eval(arg) 50 else: 51 value = arg 52 if value is not None: setattr(obj,att,value) 53 except: 54 print "att = %s value = %s" % (att, arg) 55 raise 56 i += 1
57 58
59 -def prefixid(idprefix, id):
60 """see if prefix needs to be added to id""" 61 if id.find(idprefix) != 0: 62 id = idprefix + '-' + id 63 return id
64 65
66 -def rootid(idprefix, id):
67 if idprefix[-1] != '-': idprefix += '-' 68 if id.find(idprefix) == 0: 69 return id[len(idprefix):]
70 71
72 -def walkupconfig(context, name):
73 if not name: return 74 while 1: 75 if hasattr(context, 'rrdconfig') and hasattr(context.rrdconfig, name): 76 return getattr(context.rrdconfig, name) 77 context = context.aq_parent 78 if context.id == 'dmd': 79 raise RRDObjectNotFound,"Object %s not found in context %s" % \ 80 (name, context.getPrimaryUrlPath())
81 82
83 -def templateNames(context):
84 names = Set() 85 for obj in aq_chain(context): 86 rrdconfig = getattr(obj, 'rrdconfig', None) 87 if rrdconfig: 88 names = names.union(rrdconfig.objectIds(spec='RRDTargetType')) 89 return names
90 91 92
93 -def getRRDView(context, name):
94 """lookup an rrdview based on its name""" 95 return walkupconfig(context, 'RRDView-'+name)
96 97
98 -def getRRDTargetType(context, name):
99 """lookup an rrdtargettype based on its name""" 100 return walkupconfig(context, 'RRDTargetType-'+name)
101 102
103 -def getRRDDataSource(context, name):
104 """lookup an rrddatasource based on its name""" 105 return walkupconfig(context, 'RRDDataSource-'+name)
106 107
108 -def rpneval(value, rpn):
109 """totally bogus rpn valuation only works with one level stack""" 110 if type(value) == type(''): return value 111 operators = ('+','-','*','/') 112 rpn = rpn.split(',') 113 operator = '' 114 for i in range(len(rpn)): 115 symbol = rpn.pop() 116 symbol = symbol.strip() 117 if symbol in operators: 118 operator = symbol 119 else: 120 expr = str(value) + operator + symbol 121 value = eval(expr) 122 return value
123