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