1
2
3
4
5
6
7
8
9
10
11
12
13
14 __doc__='''zenstep
15
16 Re-write an RRD files to have a different (larger) step.
17
18 $Id$
19 '''
20
21 import Globals
22 from Products.ZenModel.PerformanceConf import performancePath
23 from Products.ZenUtils.ZCmdBase import ZCmdBase
24
25 import os
26 import rrdtool
27 import logging
28 log = logging.getLogger('zen.updateStep')
29
31 """Run over the rrd files in a directory and write the data
32 into updated config files"""
33
35 "process one archive in the file"
36 step = info['step']
37 end = info['last_update']
38 pdp_per_row = rra['pdp_per_row']
39 rows = rra['rows']
40 resolution = step*pdp_per_row
41 start = end - resolution*rows
42 start = start/resolution*resolution
43 end = end/resolution*resolution
44 data = rrdtool.fetch(fullpath,
45 'AVERAGE',
46 '--resolution', str(resolution),
47 '--start', str(start),
48 '--end', str(end))
49 (start, end, resolution), ds0, data = data
50 rnd = lambda x: x
51 if info['ds']['ds0']['type'] == 'COUNTER':
52 rnd = lambda x: int(x+0.5)
53 result = []
54 for i, (v,) in enumerate(data):
55 if v != None:
56 dump[start + i*resolution] = rnd(v)
57 return result
58
59
61 "convert a single file"
62 log.debug("processing %s" % fullpath)
63 newpath = os.path.join(os.path.dirname(fullpath),
64 '.' + os.path.basename(fullpath))
65
66 info = rrdtool.info(fullpath)
67 dataType = info['ds']['ds0']['type']
68 rra = info['rra']
69 try:
70 os.unlink(newpath)
71 except OSError:
72 pass
73
74
75 earliest = start = info['last_update']
76
77 biggest = 0
78 for rra in info['rra']:
79 step = info['step']
80 pdp_per_row = rra['pdp_per_row']
81 rows = rra['rows']
82 earliest = min(earliest, start - pdp_per_row*step*rows)
83 biggest = max(biggest, pdp_per_row*step)
84
85 rrdtool.create(
86 newpath,
87 'DS:ds0:%s:%d:U:U' % (dataType, biggest * 2),
88 '--start', str(earliest),
89 '--step', str(self.options.step),
90 *self.defaultRRDCommand.split())
91
92 updates = {}
93 for rra in info['rra']:
94 self.processRRA(fullpath, rra, info, updates)
95
96 updates = updates.items()
97 updates.sort()
98 rrdtool.update(newpath, *[('%d@%s' % (t, v)) for t, v in updates])
99
100 rrdtool.tune(newpath, '-h','ds0:%d' % (self.options.step*3))
101 if self.options.commit:
102 os.rename(newpath, fullpath)
103
104
119
120
122 ZCmdBase.buildOptions(self)
123 self.parser.add_option('-s', '--step', dest='step', type='int',
124 default=300, help='Default step size in seconds')
125 self.parser.add_option('-r', '--root', dest='root',
126 default=performancePath(''),
127 help='Root tree to convert')
128 self.parser.add_option('-f', '--file', dest='filename',
129 help='Root tree to convert')
130 self.parser.add_option('--commit', dest='commit', action='store_true',
131 default=False,
132 help='Really put the converted files in place.')
133 self.parser.add_option('--monitor', dest='monitor',
134 default='localhost',
135 help='Name of this collection host.')
136
137 if __name__ == '__main__':
138 us = UpdateStep()
139 us.run()
140