Package ZenModel :: Module GraphPoint
[hide private]
[frames] | no frames]

Source Code for Module ZenModel.GraphPoint

  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__="""GraphPoint 
 15   
 16  Defines attributes for how a data source will be graphed 
 17  and builds the nessesary rrd commands. 
 18  """ 
 19   
 20  import os 
 21   
 22  from Globals import InitializeClass 
 23  from AccessControl import ClassSecurityInfo, Permissions 
 24  from Products.ZenRelations.RelSchema import * 
 25  from ZenModelRM import ZenModelRM 
 26  from ZenPackable import ZenPackable 
 27   
 28                                        
29 -def manage_addGraphPoint(context, id, REQUEST = None):
30 ''' This is here so than zope will let us copy/paste/rename 31 graphpoints. 32 ''' 33 gp = GraphPoint(id) 34 context._setObject(gp.id, gp) 35 if REQUEST: 36 return context.callZenScreen(REQUEST)
37 38
39 -class GraphPoint(ZenModelRM, ZenPackable):
40 ''' 41 ''' 42 43 isThreshold = False 44 45 DEFAULT_FORMAT = '%5.2lf%s' 46 DEFAULT_LEGEND = '${graphPoint/id}' 47 DEFAULT_MULTIGRAPH_LEGEND = '${here/name | here/id} ${graphPoint/id}' 48 49 sequence = 0 50 _properties = ( 51 {'id':'sequence', 'type':'long', 'mode':'w'}, 52 ) 53 54 _relations = ZenPackable._relations + ( 55 ("graphDef", ToOne(ToManyCont,"Products.ZenModel.GraphDefinition","graphPoints")), 56 ) 57 58 factory_type_information = ( 59 { 60 'immediate_view' : 'editGraphPoint', 61 'actions' : 62 ( 63 { 'id' : 'edit' 64 , 'name' : 'Graph Point' 65 , 'action' : 'editGraphPoint' 66 , 'permissions' : ( Permissions.view, ) 67 }, 68 ) 69 }, 70 ) 71 72 colors = ( 73 '#00cc00', '#0000ff', '#00ffff', '#ff0000', 74 '#ffff00', '#cc0000', '#0000cc', '#0080c0', 75 '#8080c0', '#ff0080', '#800080', '#0000a0', 76 '#408080', '#808000', '#000000', '#00ff00', 77 '#fb31fb', '#0080ff', '#ff8000', '#800000', 78 ) 79 80 81 ## Interface 82 83
84 - def breadCrumbs(self, terminator='dmd'):
85 """Return the breadcrumb links for this object add ActionRules list. 86 [('url','id'), ...] 87 """ 88 if self.graphDef.rrdTemplate(): 89 from RRDTemplate import crumbspath 90 crumbs = super(GraphPoint, self).breadCrumbs(terminator) 91 return crumbspath(self.graphDef(), crumbs, -3) 92 return ZenModelRM.breadCrumbs(self, terminator)
93 94
95 - def manage_editProperties(self, REQUEST):
96 ''' 97 ''' 98 if REQUEST.get('color', ''): 99 REQUEST.color = '#%s' % REQUEST.color.lstrip('#') 100 return self.zmanage_editProperties(REQUEST)
101 102
103 - def getDescription(self):
104 ''' Return a description 105 ''' 106 return self.id
107 108
109 - def getTalesContext(self, thing=None, **kw):
110 ''' 111 Standard stuff to add to context for tales expressions 112 ''' 113 context = { 114 'graphDef': self.graphDef(), 115 'graphPoint': self, 116 } 117 if thing: 118 if thing.meta_type == 'Device': 119 context['dev'] = thing 120 context['devId'] = thing.id 121 else: 122 context['comp'] = thing 123 context['compId'] = thing.id 124 context['compName'] = thing.name() 125 context['dev'] = thing.device() 126 context['devId'] = thing.device().id 127 for key, value in kw.items(): 128 context[key] = value 129 return context
130 131
132 - def talesEval(self, str, context, **kw):
133 ''' 134 return a tales evaluation of str 135 ''' 136 from Products.ZenUtils.ZenTales import talesEvalStr 137 extraContext = self.getTalesContext(thing=context, **kw) 138 try: 139 result = talesEvalStr(str, context, extraContext) 140 except Exception, e: 141 result = '(Tales expression error)' 142 return result
143 144 145 ## Graphing Support 146
147 - def getColor(self, index):
148 index %= len(self.colors) 149 color = self.color or self.colors[index] 150 color = '#%s' % color.lstrip('#') 151 return color
152 153
154 - def getThresholdColor(self, index):
155 index %= len(self.colors) 156 color = self.color or self.colors[-1 * (index+1)] 157 color = '#%s' % color.lstrip('#') 158 return color
159 160
161 - def getGraphCmds(self, cmds, context, rrdDir, addSummary, idx, 162 multiid=-1, prefix=''):
163 ''' Build the graphing commands for this graphpoint 164 ''' 165 return cmds
166 167
168 - def getDsName(self, base, multiid=-1, prefix=''):
169 name = self.addPrefix(prefix, base) 170 if multiid > -1: 171 name = '%s_%s' % (name, multiid) 172 return name
173 174
175 - def addPrefix(self, prefix, base):
176 ''' If not base then return '' 177 elif prefix then return prefix_base 178 else return base 179 The result is rrd scrubbed 180 ''' 181 s = base or '' 182 if s and prefix: 183 s = '_'.join((prefix, base)) 184 s = self.scrubForRRD(s) 185 return s
186 187
188 - def scrubForRRD(self, value, namespace=None):
189 ''' scrub value so it is a valid rrd variable name. If namespace 190 is provided then massage value as needed to avoid name conflicts 191 with items in namespace. 192 ''' 193 import string 194 import itertools 195 def Scrub(c): 196 if c not in string.ascii_letters + string.digits + '_-': 197 c = '_' 198 return c
199 value = ''.join([Scrub(c) for c in value]) 200 if namespace: 201 postfixIter = itertools.count(2) 202 candidate = value 203 while candidate in namespace: 204 candidate = value + str(postfixIter.next()) 205 value = candidate 206 return value
207 208
209 - def escapeForRRD(self, value):
210 ''' 211 Escapes characters like colon ':' for use by RRDTool which would 212 ''' 213 value = value.replace(":", "\:") 214 return value
215 216 217 InitializeClass(GraphPoint) 218