1
2
3
4
5
6
7
8
9
10
11
12
13
14 import time
15
16 import logging
17 log = logging.getLogger("zen.RRDView")
18
19 from Acquisition import aq_chain
20
21 from Products.ZenRRD.RRDUtil import convertToRRDTime
22 from Products.ZenUtils import Map
23 from Products.ZenWidgets import messaging
24
25 from Products.ZenModel.ConfigurationError import ConfigurationError
26
27 CACHE_TIME = 60.
28
29 _cache = Map.Locked(Map.Timed({}, CACHE_TIME))
30
37
38
40
41
43 """
44 Mixin to provide hooks to RRD management functions
45 """
46
66
85
86
87 - def getRRDValue(self, dsname, start=None, end=None, function="LAST",
88 format="%.2lf", extraRpn=""):
89 """Return a single rrd value from its file using function.
90 """
91 dsnames = (dsname,)
92 results = self.getRRDValues(
93 dsnames, start, end, function, format, extraRpn)
94 if results and dsname in results:
95 return results[dsname]
96
97
103
104
113
114
121
122
128
129
130 - def getRRDValues(self, dsnames, start=None, end=None, function="LAST",
131 format="%.2lf", extraRpn=""):
132 """
133 Return a dict of key value pairs where dsnames are the keys.
134 """
135 try:
136 if not start:
137 start = time.time() - self.defaultDateRange
138 gopts = []
139 names = list(dsnames[:])
140 for dsname in dsnames:
141 for dp in self.getRRDDataPoints():
142 if dp.name().find(dsname) > -1:
143 break
144 else:
145 names.remove(dsname)
146 continue
147 filename = self.getRRDFileName(dp.name())
148 rpn = str(dp.rpn)
149 if rpn:
150 rpn = "," + rpn
151 if extraRpn:
152 rpn = rpn + "," + extraRpn
153
154 gopts.append("DEF:%s_r=%s:ds0:AVERAGE" % (dsname,filename))
155 gopts.append("CDEF:%s_c=%s_r%s" % (dsname,dsname,rpn))
156 gopts.append("VDEF:%s=%s_c,%s" % (dsname,dsname,function))
157 gopts.append("PRINT:%s:%s" % (dsname, format))
158 gopts.append("--start=%s" % convertToRRDTime(start))
159 if end:
160 gopts.append("--end=%s" % convertToRRDTime(end))
161 if not names:
162 return {}
163 perfServer = self.device().getPerformanceServer()
164 vals = []
165 if perfServer:
166 vals = perfServer.performanceCustomSummary(gopts)
167 if vals is None:
168 vals = [None] * len(dsnames)
169 def cvt(val):
170 if val is None or val.lower() == "nan":
171 return None
172 return float(val)
173 return dict(zip(names, map(cvt, vals)))
174 except Exception, ex:
175 log.exception(ex)
176
177
178
179 - def getRRDSum(self, points, start=None, end=None, function="LAST"):
214
215
229
230
232 ''' Fetch a graph by id. if not found return None
233 '''
234 for t in self.getRRDTemplates():
235 for g in t.getGraphDefs():
236 if g.id == graphId:
237 return g
238 return None
239
240
242 """Return the target type name of this component. By default meta_type.
243 Override to create custom type selection.
244 """
245 return self.meta_type
246
247
249 """Look up an rrd file based on its data point name"""
250 names = [p.name() for p in self.getRRDDataPoints()
251 if p.name().endswith(dsname)]
252 if names:
253 return '%s/%s.rrd' % (self.rrdPath(), names[0])
254 else:
255 return '%s/%s.rrd' % (self.rrdPath(), dsname)
256
257
260
263
265 """Should this component be monitored for performance using snmp.
266 """
267 return False
268
274
276 try:
277 return self.getRRDTemplates()[0]
278 except IndexError:
279 return None
280
282 "Return the template of the given name."
283 try:
284 return self._getOb(name)
285 except AttributeError:
286 pass
287 for obj in aq_chain(self):
288 try:
289 return obj.rrdTemplates._getOb(name)
290 except AttributeError:
291 pass
292 return None
293
294
296 """Return a dictionary where keys are dsnames and values are thresholds.
297 """
298 result = {}
299 for thresh in templ.thresholds():
300 if not thresh.enabled: continue
301 for dsname in thresh.dsnames:
302 threshdef = result.setdefault(dsname, [])
303 threshdef.append(thresh.getConfig(self))
304 return result
305
306
309
310
314
315 - def getRRDContextData(self, context):
317
319 from Products.ZenEvents.Exceptions import pythonThresholdException
320 result = []
321 for template in self.getRRDTemplates():
322
323
324 names = []
325 for ds in template.getRRDDataSources(dsType):
326 for dp in ds.datapoints():
327 names.append(dp.name())
328 for threshold in template.thresholds():
329 if not threshold.enabled: continue
330 for ds in threshold.dsnames:
331 if ds in names:
332 try:
333 thresh = threshold.createThresholdInstance(self)
334 result.append(thresh)
335 except pythonThresholdException, ex:
336 log.warn(ex)
337 zem = self.primaryAq().getEventManager()
338 import socket
339 device = socket.gethostname()
340 path = template.absolute_url_path()
341 msg = \
342 "The threshold %s in template %s has caused an exception." % (threshold.id, path)
343 evt = dict(summary=str(ex), severity=3,
344 component='zenhub', message=msg,
345 dedupid='zenhub|' + str(ex),
346 template=path,
347 threshold=threshold.id,
348 device=device, eventClass="/Status/Update",)
349 zem.sendEvent(evt)
350 break
351 return result
352
353
368
369
382
383
386