1
2
3
4
5
6
7
8
9
10
11
12
13 from Products.ZenUtils.Utils import binPath
14
15 __doc__="""RRDDataSource
16
17 Base class for DataSources
18 """
19
20 import os
21
22 from DateTime import DateTime
23 from AccessControl import ClassSecurityInfo, Permissions
24 from Products.ZenModel.ZenossSecurity import ZEN_MANAGE_DMD
25
26 from Products.PageTemplates.Expressions import getEngine
27
28 from Products.ZenUtils.ZenTales import talesCompile
29 from Products.ZenRelations.RelSchema import *
30 from Products.ZenWidgets import messaging
31
32 from ZenModelRM import ZenModelRM
33 from ZenPackable import ZenPackable
34
41
42
44
45 meta_type = 'RRDDataSource'
46
47 paramtypes = ('integer', 'string', 'float')
48 sourcetypes = ()
49
50 sourcetype = None
51 enabled = True
52 component = ''
53 eventClass = ''
54 eventKey = ''
55 severity = 3
56 commandTemplate = ""
57 cycletime = 300
58
59 _properties = (
60 {'id':'sourcetype', 'type':'selection',
61 'select_variable' : 'sourcetypes', 'mode':'w'},
62 {'id':'enabled', 'type':'boolean', 'mode':'w'},
63 {'id':'component', 'type':'string', 'mode':'w'},
64 {'id':'eventClass', 'type':'string', 'mode':'w'},
65 {'id':'eventKey', 'type':'string', 'mode':'w'},
66 {'id':'severity', 'type':'int', 'mode':'w'},
67 {'id':'commandTemplate', 'type':'string', 'mode':'w'},
68 {'id':'cycletime', 'type':'int', 'mode':'w'},
69 )
70
71 _relations = ZenPackable._relations + (
72 ("rrdTemplate", ToOne(ToManyCont,"Products.ZenModel.RRDTemplate","datasources")),
73 ("datapoints", ToManyCont(ToOne,"Products.ZenModel.RRDDataPoint","datasource")),
74 )
75
76
77 factory_type_information = (
78 {
79 'immediate_view' : 'editRRDDataSource',
80 'actions' :
81 (
82 { 'id' : 'edit'
83 , 'name' : 'Data Source'
84 , 'action' : 'editRRDDataSource'
85 , 'permissions' : ( Permissions.view, )
86 },
87 )
88 },
89 )
90
91 security = ClassSecurityInfo()
92
93
101
102
105
106
108 return self.datapoints()
109
110
113
114
115 security.declareProtected(ZEN_MANAGE_DMD, 'manage_addRRDDataPoint')
130
131
132 security.declareProtected(ZEN_MANAGE_DMD, 'manage_deleteRRDDataPoints')
142
143 if not ids: return self.callZenScreen(REQUEST)
144 for id in ids:
145 dp = getattr(self.datapoints,id,False)
146 if dp:
147 if getattr(self, 'device', False):
148 perfConf = self.device().getPerformanceServer()
149 perfConf.deleteRRDFiles(device=self.device().id, datapoint=dp.name())
150 else:
151 for d in self.deviceClass.obj.getSubDevicesGen():
152 perfConf = d.getPerformanceServer()
153 perfConf.deleteRRDFiles(device=d.id, datapoint=dp.name())
154
155 clean(self.graphs, dp.name())
156 clean(self.thresholds, dp.name())
157 self.datapoints._delObject(dp.id)
158
159 if REQUEST:
160 return self.callZenScreen(REQUEST)
161
162
163 security.declareProtected(ZEN_MANAGE_DMD, 'manage_addDataPointsToGraphs')
165 """
166 Create GraphPoints for all datapoints given datapoints (ids)
167 in each of the graphDefs (graphIds.)
168 If a graphpoint already exists for a datapoint in a graphDef then
169 don't create a 2nd one.
170 """
171 newGps = []
172 for graphDefId in graphIds:
173 graphDef = self.rrdTemplate.graphDefs._getOb(graphDefId, None)
174 if graphDef:
175 for dpId in ids:
176 dp = self.datapoints._getOb(dpId, None)
177 if dp and not graphDef.isDataPointGraphed(dp.name()):
178 newGps += graphDef.manage_addDataPointGraphPoints(
179 [dp.name()])
180 if REQUEST:
181 numNew = len(newGps)
182 messaging.IMessageSender(self).sendToBrowser(
183 'Graph Points Added',
184 '%s GraphPoint%s added' % (numNew, numNew != 1 and 's' or '')
185 )
186 return self.callZenScreen(REQUEST)
187 return newGps
188
189
191 """Return localized command target.
192 """
193
194 if cmd is None:
195 cmd = self.commandTemplate
196 if not cmd.startswith('string:') and not cmd.startswith('python:'):
197 cmd = 'string:%s' % cmd
198 compiled = talesCompile(cmd)
199 d = context.device()
200 environ = {'dev' : d,
201 'device': d,
202 'devname': d.id,
203 'here' : context,
204 'zCommandPath' : context.zCommandPath,
205 'nothing' : None,
206 'now' : DateTime() }
207 res = compiled(getEngine().getContext(environ))
208 if isinstance(res, Exception):
209 raise res
210 res = self.checkCommandPrefix(context, res)
211 return res
212
213
234
236 if not cmd.startswith('/') and not cmd.startswith('$'):
237 if context.zCommandPath and not cmd.startswith(context.zCommandPath):
238 cmd = os.path.join(context.zCommandPath, cmd)
239 elif binPath(cmd.split(" ",1)[0]):
240
241
242
243 cmdList = cmd.split(" ",1)
244 cmd = binPath(cmdList[0])
245 if len(cmdList) > 1:
246 cmd = "%s %s" % (cmd, cmdList[1])
247
248 return cmd
249
252
253
256
257
259 """
260 A SimpleRRDDataSource has a single datapoint that shares the name of the
261 data source.
262 """
263 security = ClassSecurityInfo()
264
265
267 """
268 Make sure there is exactly one datapoint and that it has the same name
269 as the datasource.
270 """
271 dpid = self.prepId(self.id)
272 remove = [d for d in self.datapoints() if d.id != dpid]
273 for dp in remove:
274 self.datapoints._delObject(dp.id)
275 if not self.datapoints._getOb(dpid, None):
276 self.manage_addRRDDataPoint(dpid)
277
278 security.declareProtected(ZEN_MANAGE_DMD, 'zmanage_editProperties')
333
335 """
336 Return the datasource's only datapoint
337 """
338 dps = self.datapoints()
339 if dps:
340 return dps[0]
341
343 """
344 Return the datapoint aliases that belong to the datasource's only
345 datapoint
346 """
347 dp = self.soleDataPoint()
348 if dp:
349 return dp.aliases()
350
351 security.declareProtected(ZEN_MANAGE_DMD, 'manage_addDataPointsToGraphs')
359
360 security.declareProtected(ZEN_MANAGE_DMD, 'manage_addDataPointAlias')
369
370 security.declareProtected(ZEN_MANAGE_DMD, 'manage_removeDataPointAliases')
378