1
2
3
4
5
6
7
8
9
10
11
12
13
14 import logging
15 log = logging.getLogger("zen.RRDUtil")
16
17 from Products.ZenModel.PerformanceConf import performancePath
18
20 if x is None or x == '' or x == -1 or x == '-1':
21 return 'U'
22 return x
23
25 - def __init__(self, defaultRrdCreateCommand, defaultCycleTime):
26 self.defaultRrdCreateCommand = defaultRrdCreateCommand
27 self.defaultCycleTime = defaultCycleTime
28
29 - def save(self, path, value, rrdType, rrdCommand=None, cycleTime=None,
30 min='U', max='U'):
31 import rrdtool, os
32
33 if value is None: return None
34
35 if cycleTime is None:
36 cycleTime = self.defaultCycleTime
37
38 filename = performancePath(path) + '.rrd'
39 if not rrdCommand:
40 rrdCommand = self.defaultRrdCreateCommand
41 if not os.path.exists(filename):
42 log.debug("create new rrd %s", filename)
43 dirname = os.path.dirname(filename)
44 if not os.path.exists(dirname):
45 os.makedirs(dirname, 0750)
46
47 min, max = map(_checkUndefined, (min, max))
48 dataSource = 'DS:%s:%s:%d:%s:%s' % ('ds0', rrdType,
49 3*cycleTime, min, max)
50 rrdtool.create(filename,
51 "--step", str(cycleTime),
52 str(dataSource), *rrdCommand.split())
53
54 if rrdType in ('COUNTER', 'DERIVE'):
55 try:
56 value = long(value)
57 except (TypeError, ValueError):
58 return None
59 try:
60 rrdtool.update(filename, 'N:%s' % value)
61 log.debug('%s: %r', filename, value)
62 except rrdtool.error, err:
63
64 log.error('rrd error %s %s', err, path)
65
66 if rrdType in ('COUNTER', 'DERIVE'):
67 startStop, names, values = \
68 rrdtool.fetch(filename, 'AVERAGE',
69 '-s', 'now-%d' % (cycleTime*2),
70 '-e', 'now')
71 value = values[0][0]
72 return value
73