| Trees | Indices | Help |
|
|---|
|
|
1 ##############################################################################
2 #
3 # Copyright (C) Zenoss, Inc. 2007, all rights reserved.
4 #
5 # This content is made available according to terms specified in
6 # License.zenoss under the directory where your Zenoss product is installed.
7 #
8 ##############################################################################
9
10
11 import Globals
12 from Products.ZenUtils.Utils import zenPath
13
14 # FIXME dependency from Utils to Thresholds
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
31 self.name = ""
32 self.monitor = ""
33 self.rrdCreateCommand = ""
34
35 self.thresholds = Thresholds()
36
37
39 """Initialize the object. We could do this in __init__, but
40 that would delay creation to after configuration time, which
41 may run asynchronously with collection or heartbeats. By
42 deferring initialization, this object implements the Null
43 Object pattern until the application is ready to start writing
44 real statistics.
45 """
46 self.name = name
47 self.monitor = monitor
48 if not rrdCreateCommand:
49 from Products.ZenModel.PerformanceConf import PerformanceConf
50 rrdCreateCommand = PerformanceConf.defaultRRDCreateCommand
51 if not isinstance(rrdCreateCommand, basestring):
52 self.createCommand = rrdCreateCommand
53 else:
54 self.createCommand = rrdCreateCommand.split('\n')
55 self.thresholds = Thresholds()
56 self.thresholds.updateList(thresholds)
57
58
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
84
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
105 "Write a gauge value, return threshold events"
106 fileName = self.rrdFile('GAUGE', cycleTime, name)
107 if fileName:
108 full = fullname(fileName)
109 try:
110 rrdtool.update(full, 'N:%s' % value)
111 except rrdtool.error, err:
112 log.error('rrdtool reported error %s %s', err, full)
113 if value is not None:
114 return self.thresholds.check(fileName, time.time(), value)
115 return []
116
| Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0.1.1812 on Mon Jul 30 17:11:24 2012 | http://epydoc.sourceforge.net |