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

Source Code for Module ZenModel.RRDDataSource

  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__="""RRDDataSource 
 15   
 16  Base class for DataSources 
 17  """ 
 18   
 19  import os 
 20   
 21  from DateTime import DateTime 
 22  from AccessControl import ClassSecurityInfo, Permissions 
 23  from Products.ZenModel.ZenossSecurity import ZEN_MANAGE_DMD 
 24   
 25  from Products.PageTemplates.Expressions import getEngine 
 26   
 27  from Products.ZenUtils.ZenTales import talesCompile 
 28  from Products.ZenRelations.RelSchema import * 
 29  from Products.ZenWidgets import messaging 
 30   
 31  from ZenModelRM import ZenModelRM 
 32  from ZenPackable import ZenPackable 
 33   
34 -def manage_addRRDDataSource(context, id, dsOption, REQUEST = None):
35 """make a RRDDataSource""" 36 ds = context.getDataSourceInstance(id, dsOption, REQUEST=None) 37 context._setObject(ds.id, ds) 38 if REQUEST is not None: 39 REQUEST['RESPONSE'].redirect(context.absolute_url()+'/manage_main')
40 41
42 -class RRDDataSource(ZenModelRM, ZenPackable):
43 44 meta_type = 'RRDDataSource' 45 46 paramtypes = ('integer', 'string', 'float') 47 sourcetypes = () 48 49 sourcetype = None 50 enabled = True 51 component = '' 52 eventClass = '' 53 eventKey = '' 54 severity = 3 55 commandTemplate = "" 56 cycletime = 300 57 58 _properties = ( 59 {'id':'sourcetype', 'type':'selection', 60 'select_variable' : 'sourcetypes', 'mode':'w'}, 61 {'id':'enabled', 'type':'boolean', 'mode':'w'}, 62 {'id':'component', 'type':'string', 'mode':'w'}, 63 {'id':'eventClass', 'type':'string', 'mode':'w'}, 64 {'id':'eventKey', 'type':'string', 'mode':'w'}, 65 {'id':'severity', 'type':'int', 'mode':'w'}, 66 {'id':'commandTemplate', 'type':'string', 'mode':'w'}, 67 {'id':'cycletime', 'type':'int', 'mode':'w'}, 68 ) 69 70 _relations = ZenPackable._relations + ( 71 ("rrdTemplate", ToOne(ToManyCont,"Products.ZenModel.RRDTemplate","datasources")), 72 ("datapoints", ToManyCont(ToOne,"Products.ZenModel.RRDDataPoint","datasource")), 73 ) 74 75 # Screen action bindings (and tab definitions) 76 factory_type_information = ( 77 { 78 'immediate_view' : 'editRRDDataSource', 79 'actions' : 80 ( 81 { 'id' : 'edit' 82 , 'name' : 'Data Source' 83 , 'action' : 'editRRDDataSource' 84 , 'permissions' : ( Permissions.view, ) 85 }, 86 ) 87 }, 88 ) 89 90 security = ClassSecurityInfo() 91 92
93 - def breadCrumbs(self, terminator='dmd'):
94 """Return the breadcrumb links for this object add ActionRules list. 95 [('url','id'), ...] 96 """ 97 from RRDTemplate import crumbspath 98 crumbs = super(RRDDataSource, self).breadCrumbs(terminator) 99 return crumbspath(self.rrdTemplate(), crumbs, -2)
100 101
102 - def getDescription(self):
103 return None
104 105
106 - def getRRDDataPoints(self):
107 return self.datapoints()
108 109
110 - def useZenCommand(self):
111 return False
112 113 114 security.declareProtected(ZEN_MANAGE_DMD, 'manage_addRRDDataPoint')
115 - def manage_addRRDDataPoint(self, id, REQUEST = None):
116 """make a RRDDataPoint""" 117 if not id: 118 return self.callZenScreen(REQUEST) 119 from Products.ZenModel.RRDDataPoint import RRDDataPoint 120 dp = RRDDataPoint(id) 121 self.datapoints._setObject(dp.id, dp) 122 dp = self.datapoints._getOb(dp.id) 123 if REQUEST: 124 if dp: 125 url = '%s/datapoints/%s' % (self.getPrimaryUrlPath(), dp.id) 126 REQUEST['RESPONSE'].redirect(url) 127 return self.callZenScreen(REQUEST) 128 return dp
129 130 131 security.declareProtected(ZEN_MANAGE_DMD, 'manage_deleteRRDDataPoints')
132 - def manage_deleteRRDDataPoints(self, ids=(), REQUEST=None):
133 """Delete RRDDataPoints from this RRDDataSource""" 134 135 def clean(rel, id): 136 for obj in rel(): 137 if id in obj.dsnames: 138 obj.dsnames.remove(id) 139 if not obj.dsnames: 140 rel._delObject(obj.id)
141 142 if not ids: return self.callZenScreen(REQUEST) 143 for id in ids: 144 dp = getattr(self.datapoints,id,False) 145 if dp: 146 if getattr(self, 'device', False): 147 perfConf = self.device().getPerformanceServer() 148 perfConf.deleteRRDFiles(device=self.device().id, datapoint=dp.name()) 149 else: 150 for d in self.deviceClass.obj.getSubDevicesGen(): 151 perfConf = d.getPerformanceServer() 152 perfConf.deleteRRDFiles(device=d.id, datapoint=dp.name()) 153 154 clean(self.graphs, dp.name()) 155 clean(self.thresholds, dp.name()) 156 self.datapoints._delObject(dp.id) 157 158 if REQUEST: 159 return self.callZenScreen(REQUEST)
160 161 162 security.declareProtected(ZEN_MANAGE_DMD, 'manage_addDataPointsToGraphs')
163 - def manage_addDataPointsToGraphs(self, ids=(), graphIds=(), REQUEST=None):
164 """ 165 Create GraphPoints for all datapoints given datapoints (ids) 166 in each of the graphDefs (graphIds.) 167 If a graphpoint already exists for a datapoint in a graphDef then 168 don't create a 2nd one. 169 """ 170 newGps = [] 171 for graphDefId in graphIds: 172 graphDef = self.rrdTemplate.graphDefs._getOb(graphDefId, None) 173 if graphDef: 174 for dpId in ids: 175 dp = self.datapoints._getOb(dpId, None) 176 if dp and not graphDef.isDataPointGraphed(dp.name()): 177 newGps += graphDef.manage_addDataPointGraphPoints( 178 [dp.name()]) 179 if REQUEST: 180 numNew = len(newGps) 181 messaging.IMessageSender(self).sendToBrowser( 182 'Graph Points Added', 183 '%s GraphPoint%s added' % (numNew, numNew != 1 and 's' or '') 184 ) 185 return self.callZenScreen(REQUEST) 186 return newGps
187 188
189 - def getCommand(self, context, cmd=None):
190 """Return localized command target. 191 """ 192 # Perform a TALES eval on the expression using self 193 if cmd is None: 194 cmd = self.commandTemplate 195 if not cmd.startswith('string:') and not cmd.startswith('python:'): 196 cmd = 'string:%s' % cmd 197 compiled = talesCompile(cmd) 198 d = context.device() 199 environ = {'dev' : d, 200 'device': d, 201 'devname': d.id, 202 'here' : context, 203 'zCommandPath' : context.zCommandPath, 204 'nothing' : None, 205 'now' : DateTime() } 206 res = compiled(getEngine().getContext(environ)) 207 if isinstance(res, Exception): 208 raise res 209 res = self.checkCommandPrefix(context, res) 210 return res
211 212
213 - def getComponent(self, context, component=None):
214 """Return localized component. 215 """ 216 if component is None: 217 component = self.component 218 if not component.startswith('string:') and \ 219 not component.startswith('python:'): 220 component = 'string:%s' % component 221 compiled = talesCompile(component) 222 d = context.device() 223 environ = {'dev' : d, 224 'device': d, 225 'devname': d.id, 226 'here' : context, 227 'nothing' : None, 228 'now' : DateTime() } 229 res = compiled(getEngine().getContext(environ)) 230 if isinstance(res, Exception): 231 raise res 232 return res
233 234
235 - def checkCommandPrefix(self, context, cmd):
236 if not cmd.startswith('/') and not cmd.startswith('$'): 237 if not cmd.startswith(context.zCommandPath): 238 cmd = os.path.join(context.zCommandPath, cmd) 239 return cmd
240 241
242 - def getSeverityString(self):
243 return self.ZenEventManager.getSeverityString(self.severity)
244 245
246 - def zmanage_editProperties(self, REQUEST=None, ignored=None):
247 return ZenModelRM.zmanage_editProperties(self, REQUEST)
248 249
250 -class SimpleRRDDataSource(RRDDataSource):
251 """ 252 A SimpleRRDDataSource has a single datapoint that shares the name of the 253 data source. 254 """ 255 security = ClassSecurityInfo() 256 257
258 - def addDataPoints(self):
259 """ 260 Make sure there is exactly one datapoint and that it has the same name 261 as the datasource. 262 """ 263 dpid = self.prepId(self.id) 264 remove = [d for d in self.datapoints() if d.id != dpid] 265 for dp in remove: 266 self.datapoints._delObject(dp.id) 267 if not self.datapoints._getOb(dpid, None): 268 self.manage_addRRDDataPoint(dpid)
269 270 security.declareProtected(ZEN_MANAGE_DMD, 'zmanage_editProperties')
271 - def zmanage_editProperties(self, REQUEST=None):
272 """ 273 Overrides the method defined in RRDDataSource. Called when user clicks 274 the Save button on the Data Source editor page. 275 """ 276 self.addDataPoints() 277 278 if REQUEST and self.datapoints(): 279 280 datapoint = self.soleDataPoint() 281 282 if REQUEST.has_key('rrdtype'): 283 if REQUEST['rrdtype'] in datapoint.rrdtypes: 284 datapoint.rrdtype = REQUEST['rrdtype'] 285 else: 286 messaging.IMessageSender(self).sendToBrowser( 287 'Error', 288 "%s is an invalid Type" % rrdtype, 289 priority=messaging.WARNING 290 ) 291 return self.callZenScreen(REQUEST) 292 293 if REQUEST.has_key('rrdmin'): 294 value = REQUEST['rrdmin'] 295 if value != '': 296 try: 297 value = long(value) 298 except ValueError: 299 messaging.IMessageSender(self).sendToBrowser( 300 'Error', 301 "%s is an invalid RRD Min" % value, 302 priority=messaging.WARNING 303 ) 304 return self.callZenScreen(REQUEST) 305 datapoint.rrdmin = value 306 307 if REQUEST.has_key('rrdmax'): 308 value = REQUEST['rrdmax'] 309 if value != '': 310 try: 311 value = long(value) 312 except ValueError: 313 messaging.IMessageSender(self).sendToBrowser( 314 'Error', 315 "%s is an invalid RRD Max" % value, 316 priority=messaging.WARNING 317 ) 318 return self.callZenScreen(REQUEST) 319 datapoint.rrdmax = value 320 321 if REQUEST.has_key('createCmd'): 322 datapoint.createCmd = REQUEST['createCmd'] 323 324 return RRDDataSource.zmanage_editProperties(self, REQUEST)
325
326 - def soleDataPoint(self):
327 """ 328 Return the datasource's only datapoint 329 """ 330 dps = self.datapoints() 331 if dps: 332 return dps[0]
333
334 - def aliases(self):
335 """ 336 Return the datapoint aliases that belong to the datasource's only 337 datapoint 338 """ 339 dp = self.soleDataPoint() 340 if dp: 341 return dp.aliases()
342 343 security.declareProtected(ZEN_MANAGE_DMD, 'manage_addDataPointsToGraphs')
344 - def manage_addDataPointsToGraphs(self, ids=(), graphIds=(), REQUEST=None):
345 """ 346 Override method in super class. ids will always be an empty tuple, so 347 call the super class's method with the single datapoint as the ids. 348 """ 349 return RRDDataSource.manage_addDataPointsToGraphs(self, 350 (self.soleDataPoint().id,), graphIds, REQUEST)
351 352 security.declareProtected(ZEN_MANAGE_DMD, 'manage_addDataPointAlias')
353 - def manage_addDataPointAlias(self, id, formula, REQUEST=None):
354 """ 355 Add a datapoint alias to the datasource's only datapoint 356 """ 357 alias = self.soleDataPoint().manage_addDataPointAlias( id, formula ) 358 if REQUEST: 359 return self.callZenScreen( REQUEST ) 360 return alias
361 362 security.declareProtected(ZEN_MANAGE_DMD, 'manage_removeDataPointAliases')
363 - def manage_removeDataPointAliases(self, ids=(), REQUEST=None):
364 """ 365 Remove the passed aliases from the datasource's only datapoint 366 """ 367 self.soleDataPoint().manage_removeDataPointAliases( ids ) 368 if REQUEST: 369 return self.callZenScreen(REQUEST)
370