1
2
3
4
5
6
7
8
9
10
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
40
41
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
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
100
101
104
105
107 return self.datapoints()
108
109
112
113
114 security.declareProtected(ZEN_MANAGE_DMD, 'manage_addRRDDataPoint')
129
130
131 security.declareProtected(ZEN_MANAGE_DMD, 'manage_deleteRRDDataPoints')
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')
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
190 """Return localized command target.
191 """
192
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
233
234
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
244
245
248
249
251 """
252 A SimpleRRDDataSource has a single datapoint that shares the name of the
253 data source.
254 """
255 security = ClassSecurityInfo()
256
257
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')
325
327 """
328 Return the datasource's only datapoint
329 """
330 dps = self.datapoints()
331 if dps:
332 return dps[0]
333
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')
351
352 security.declareProtected(ZEN_MANAGE_DMD, 'manage_addDataPointAlias')
361
362 security.declareProtected(ZEN_MANAGE_DMD, 'manage_removeDataPointAliases')
370