1
2
3
4
5
6
7
8
9
10
11 import Globals
12 from Products.ZenUtils.Utils import zenPath
13
14
15 from Products.ZenRRD.Thresholds import Thresholds
16
17 import rrdtool
18 import os
19 import time
20
21 import logging
22 log = logging.getLogger("zen.DaemonStats")
23
25 return zenPath('perf', partial + '.rrd')
26
28 "Utility for a daemon to write out internal performance statistics"
29
36
37
38 - def config(self, name, monitor, thresholds, rrdCreateCommand = None):
57
58
59 - def rrdFile(self, type, cycleTime, name, minVal = 'U', maxVal = 'U'):
60 """Create an RRD file if it does not exist.
61 Returns the basename of the rrdFile, suitable for checking thresholds.
62 """
63 if not self.name: return None
64 base = os.path.join('Daemons', self.name)
65 directory = zenPath('perf', base)
66 if not os.path.exists(directory):
67 os.makedirs(directory)
68 base = os.path.join(base, '%s_%s' % (self.monitor, name))
69 fileName = fullname(base)
70 if not os.path.exists(fileName):
71 rrdtool.create(fileName,
72 '--step', "%d" % cycleTime,
73 'DS:ds0:%s:%s:%s:%s' % (type,
74 cycleTime * 3,
75 minVal,
76 maxVal),
77 *self.createCommand)
78 return base
79
80
81 - def derive(self, name, cycleTime, value):
84
85 - def counter(self, name, cycleTime, value):
86 "Write a DERIVE(! NOT COUNTER!) value, return threshold events"
87 fileName = self.rrdFile('DERIVE', cycleTime, name, 0)
88 if fileName:
89 full = fullname(fileName)
90 try:
91 rrdtool.update(full, 'N:%s' % int(value))
92 startStop, names, values = \
93 rrdtool.fetch(full, 'AVERAGE',
94 '-s', 'now-%d' % (cycleTime*2),
95 '-e', 'now')
96 value = values[0][0]
97 if value is not None:
98 return self.thresholds.check(fileName, time.time(), value)
99 except rrdtool.error, err:
100 log.error('rrdtool reported error %s %s', err, full)
101 return []
102
103
104 - def gauge(self, name, cycleTime, value):
116