1
2
3
4
5
6
7
8
9
10
11
12
13
14 import sys
15 from Globals import DTMLFile
16 from Globals import InitializeClass
17 from AccessControl import ClassSecurityInfo, Permissions
18 from Products.ZenModel.ZenossSecurity import *
19 from zope.interface import implements
20
21 from ZenModelRM import ZenModelRM
22 from Products.ZenModel.interfaces import IIndexed
23
24 from Products.ZenRelations.RelSchema import *
25 from Products.ZenModel.RRDDataSource import SimpleRRDDataSource
26 from Products.ZenModel.BasicDataSource import BasicDataSource
27 from Products.ZenModel.BuiltInDS import BuiltInDS
28 from Products.ZenModel.ConfigurationError import ConfigurationError
29 from Products.ZenUtils.Utils import importClass
30 from Products.ZenWidgets import messaging
31 from RRDDataPoint import SEPARATOR
32 from ZenPackable import ZenPackable
33
34 import logging
35 log = logging.getLogger('zen.RRDTemplate')
36
37 RRDTEMPLATE_CATALOG = 'searchRRDTemplates'
38
39
59
60
62 """
63 Yield all templates in the searchRRDTemplates catalog which fall under
64 the given root and match the given criteria. To get all RRDTemplates
65 pass dmd in as root. If criteria contains a
66 value for getPhysicalRoot then the root parameter will be ignored.
67
68 If the searchRRDTemplates catalog is not present then fall back to using
69 DeviceClass.getAllRRDTemplatesPainfully(). In this case root must
70 be a DeviceClass and criteria is ignored. (This is compatible with
71 previous DeviceClass.getAllRRDTemplates usage.)
72
73 The searchRRDTemplates catalog was added in 2.2
74 """
75 zcat = getattr(root, RRDTEMPLATE_CATALOG, None)
76 if zcat is not None:
77 criteria = criteria or {}
78 criteria.setdefault('getPhysicalPath', root.getPrimaryId())
79 brains = zcat(criteria)
80 for result in brains:
81 yield result.getObject()
82 else:
83 for t in root.getAllRRDTemplatesPainfully():
84 yield t
85
86
93
94
95 addRRDTemplate = DTMLFile('dtml/addRRDTemplate',globals())
96
97
99 """Create the crumbs path for sub objects of an RRDTemplate.
100 """
101 dc = templ.deviceClass()
102 if dc:
103 url = '%s/perfConfig' % dc.getPrimaryUrlPath()
104 else:
105 url = '%s/objTemplates' % templ.getPrimaryParent().getPrimaryUrlPath()
106 crumbs.insert(idx,(url,'Templates'))
107 return crumbs
108
109
111
112 implements(IIndexed)
113 meta_type = 'RRDTemplate'
114
115 default_catalog = RRDTEMPLATE_CATALOG
116
117 security = ClassSecurityInfo()
118
119 description = ""
120 targetPythonClass = "Products.ZenModel.Device"
121
122 _properties = (
123 {'id':'description', 'type':'text', 'mode':'w'},
124 {'id':'targetPythonClass', 'type':'string', 'mode':'w'},
125 )
126
127
128
129
130 _relations = ZenPackable._relations + (
131 ("deviceClass", ToOne(
132 ToManyCont,"Products.ZenModel.TemplateContainer", "rrdTemplates")),
133 ("datasources", ToManyCont(
134 ToOne,"Products.ZenModel.RRDDataSource", "rrdTemplate")),
135 ("graphs", ToManyCont(
136 ToOne,"Products.ZenModel.RRDGraph", "rrdTemplate")),
137 ("thresholds", ToManyCont(
138 ToOne,"Products.ZenModel.ThresholdClass", "rrdTemplate")),
139 ("graphDefs", ToManyCont(
140 ToOne,"Products.ZenModel.GraphDefinition", "rrdTemplate")),
141 )
142
143
144
145 factory_type_information = (
146 {
147 'immediate_view' : 'viewRRDTemplate',
148 'actions' :
149 (
150 { 'id' : 'overview'
151 , 'name' : 'Performance Template'
152 , 'action' : 'viewRRDTemplate'
153 , 'permissions' : ( Permissions.view, )
154 },
155 )
156 },
157 )
158
160 """Return the breadcrumb links for this object add ActionRules list.
161 [('url','id'), ...]
162 """
163 crumbs = super(RRDTemplate, self).breadCrumbs(terminator)
164 return crumbspath(self, crumbs)
165
166
172
173
175 ''' Return an ordered list of the graph definitions
176 '''
177 def cmpGraphDefs(a, b):
178 try: a = int(a.sequence)
179 except ValueError: a = sys.maxint
180 try: b = int(b.sequence)
181 except ValueError: b = sys.maxint
182 return cmp(a, b)
183 graphDefs = [g for g in self.graphDefs()]
184 graphDefs.sort(cmpGraphDefs)
185 return graphDefs
186
187
192
193
195 ''' Return a list of names of graphable thresholds
196 '''
197 return [t for t in self.thresholds()]
198
199
201 """Return the list of all datapoint names.
202 """
203
204
205
206 datasources = [ds for ds in self.datasources()
207 if hasattr(ds, 'datapoints')]
208 return [dp.name() for ds in datasources for dp in ds.datapoints()]
209
210
212 """Return a list of all datapoints on this template.
213 """
214 if dsType is None: return self.datasources()
215 return [ds for ds in self.datasources()
216 if ds.sourcetype == dsType
217 or (dsType=='COMMAND' and ds.useZenCommand())]
218
219
221 """Return a list of all datapoints on this template.
222 """
223 result = []
224 for s in self.datasources():
225 result.extend(s.datapoints())
226 return result
227
228
230 """Return a datapoint based on its name.
231 """
232 source = name
233 point = name
234 if name.find(SEPARATOR) >= 0:
235 source, point = name.split(SEPARATOR, 1)
236 ds = self.datasources._getOb(source, None)
237 if ds is None:
238 results = []
239 for ds in self.datasources():
240 for dp in ds.datapoints():
241 if dp.name() == name:
242 results.append(dp)
243 if len(results) == 1:
244 return results[0]
245 else:
246 return ds.datapoints._getOb(point)
247 raise ConfigurationError('Unknown data point "%s"' % name)
248
249
250 security.declareProtected('Add DMD Objects', 'manage_addRRDDataSource')
271
272
274 """
275 Returns the python class object that this template can be bound to.
276 """
277 from Products.ZenModel.Device import Device
278 cname = getattr(self, "targetPythonClass", None)
279 if cname:
280 try:
281 return importClass(cname)
282 except ImportError:
283 log.exception("Unable to import class " + cname)
284 return Device
285
286
287 security.declareProtected(ZEN_MANAGE_DMD, 'manage_deleteRRDDataSources')
297
298 if not ids: return self.callZenScreen(REQUEST)
299 for id in ids:
300 self._p_changed = True
301 if getattr(self.datasources,id,False):
302 if getattr(self, 'device', False):
303 perfConf = self.device().getPerformanceServer()
304 if perfConf:
305 perfConf.deleteRRDFiles(device=self.device().id,
306 datasource=id)
307 else:
308 for d in self.deviceClass.obj.getSubDevicesGen():
309 perfConf = d.getPerformanceServer()
310 if perfConf:
311 perfConf.deleteRRDFiles(device=d, datasource=id)
312
313 self.datasources._delObject(id)
314 clean(self.graphs, id)
315 clean(self.thresholds, id)
316
317 if REQUEST:
318 messaging.IMessageSender(self).sendToBrowser(
319 'Datasources Deleted',
320 'Datasource%s %s deleted.' % (len(ids)==1 and '' or 's',
321 ', '.join(ids))
322 )
323 return self.callZenScreen(REQUEST)
324
325
326 security.declareProtected('Add DMD Objects', 'manage_addRRDThreshold')
345
346
347 security.declareProtected(ZEN_MANAGE_DMD, 'manage_deleteRRDThresholds')
362
363
364 security.declareProtected(ZEN_MANAGE_DMD, 'manage_addGraphDefinition')
383
384
385 security.declareProtected(ZEN_MANAGE_DMD, 'manage_deleteGraphDefinitions')
399
400
401 security.declareProtected(ZEN_MANAGE_DMD, 'manage_resequenceGraphDefs')
408
409
410 security.declareProtected(ZEN_MANAGE_DMD, 'manage_addDataSourcesToGraphs')
412 """
413 Create GraphPoints for all datapoints in the given datasources (ids)
414 in each of the graphDefs (graphIds.)
415 If a graphpoint already exists for a datapoint in a graphDef then
416 don't create a 2nd one.
417 """
418 newGraphPoints = []
419 for dsId in ids:
420 ds = self.datasources._getOb(dsId, None)
421 if ds:
422 newGraphPoints += ds.manage_addDataPointsToGraphs(
423 [dp.id for dp in ds.datapoints()],
424 graphIds)
425 numAdded = len(newGraphPoints)
426 if REQUEST:
427 messaging.IMessageSender(self).sendToBrowser(
428 'Graph Points Added',
429 'Added %s GraphPoint%s' % (numAdded, numAdded != 1 and 's' or '')
430 )
431 return self.callZenScreen(REQUEST)
432 return newGraphPoints
433
434
435 security.declareProtected(ZEN_MANAGE_DMD, 'manage_addDataSourcesToGraphs')
458
459
465
466
476
477
479 ''' Given one of the dsOptions returned by getDataSourceOptions)
480 return an instance of the that RRDDataSource subclass.
481 '''
482 dsClassName, dsType = dsOption.split('.')
483 for c in self.getDataSourceClasses():
484 if dsClassName == c.__name__:
485 ds = c(id)
486 ds.sourcetype = dsType
487 break
488 else:
489 raise ConfigurationError('Cannot find datasource class'
490 ' for %s' % dsOption)
491 return ds
492
493
500
501
503 ''' Given one of the dsOptions returned by getDataSourceOptions)
504 return an instance of the that RRDDataSource subclass.
505 '''
506 for c, name in self.getThresholdClasses():
507 if thresholdClassName == c.__name__:
508 return c(id)
509 raise ConfigurationError('Cannot find threshold class %s' %
510 thresholdClassName)
511
512
514 """
515 Get a list of all event class names within the permission scope.
516 """
517 return self.primaryAq().Events.getOrganizerNames()
518
519
520 InitializeClass(RRDTemplate)
521