-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrrd-add-col
executable file
·210 lines (181 loc) · 4.85 KB
/
rrd-add-col
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/python
#
# 9/6/2013 - Initial version
#
# Insert/append a column to an rrd.
#
# To do:
# - Python 3
#
import sys
import os
import re
###############################################################################
def usage():
m = '''Usage: rrd-add-col <options>
-n no modification, dry run to stdout
-rrd <path> path to rrd for input and output
-xml <path> optional path for intermediate xml or - for stdout
-idx <num> position at which to add new column, zero based (end)
-name <str> name of new column to add
-type <str> type of new column to add (GAUGE)
-minhb <num> minimum heartbeat of new column (300)
-min <num> minimum value of new column (0)
-max <num> maximum value of new column (12000)'''
print m
class cvt_t:
dsre = re.compile(r'^[ \t]*<ds>[ \t]*$')
dsendre = re.compile(r'Round Robin Archives')
cdpendre = re.compile(r'^[ \t]*</cdp_prep>[ \t]*$')
rowre = re.compile(r'<row><v>(.*)</v></row>')
rraendre = re.compile(r'^[ \t]*</rra>[ \t]*$')
def __init__(self):
self.nop = False
self.rrdpath = None
self.xmlpath = '/tmp/arc' + str(os.getpid()) + '.xml'
self.idx = -1
self.name = None
self.type = 'GAUGE'
self.minhb = 300
self.min = 0
self.max = 12000
def run(self):
if self.xmlpath == '-':
self.nop = True
self.dumpxform()
if not self.nop:
self.restore()
def dumpxform(self):
if self.nop:
out = sys.stdout
else:
out = open(self.xmlpath, 'w', 65536)
cmd = 'rrdtool dump %s' % self.rrdpath
inp = os.popen(cmd)
state = 0
dscnt = 0
didds = False
didcdp = False
delim = '</v><v>'
nan = ['NaN']
idx = self.idx
rowre = self.rowre
rraendre = self.rraendre
for line in inp:
if state == 0:
if self.dsre.search(line):
if dscnt == idx:
self.insertds(out)
didds = True
dscnt += 1
elif self.dsendre.search(line):
if not didds:
self.insertds(out)
idx = dscnt
state = 1
dscnt = 0
elif state == 1:
if self.dsre.search(line):
if dscnt == idx:
self.insertcdp(out)
didcdp = True
dscnt += 1
elif self.cdpendre.search(line):
if not didcdp:
self.insertcdp(out)
state = 2
dscnt = 0
else:
mat = rowre.search(line)
if mat:
prefix = line[0 : mat.start()]
ary = mat.group(1).split(delim)
ary[idx : idx] = nan
s = delim.join(ary)
line = prefix + '<row><v>' + s + '</v></row>\n'
elif rraendre.search(line):
state = 1
dscnt = 0
out.write(line)
inp.close()
out.close()
def restore(self):
cmd = None
bak = self.rrdpath + '.bak'
try:
os.stat(bak)
except Exception:
cmd = 'cp -p %s %s' % (self.rrdpath, bak)
if cmd:
os.system(cmd)
os.unlink(self.rrdpath)
cmd = 'rrdtool restore %s %s' % (self.xmlpath, self.rrdpath)
os.system(cmd)
os.unlink(self.xmlpath)
def insertds(self, out):
s = '''<ds>
<name>%s</name>
<type>%s</type>
<minimal_heartbeat>%d</minimal_heartbeat>
<min>%g</min>
<max>%g</max>
<last_ds>NaN</last_ds>
<value>NaN</value>
<unknown_sec>0</unknown_sec>
</ds>
''' % (self.name, self.type, self.minhb, self.min, self.max)
out.write(s)
def insertcdp(self, out):
s = '''<ds>
<primary_value>NaN</primary_value>
<secondary_value>NaN</secondary_value>
<value>NaN</value>
<unknown_datapoints>0</unknown_datapoints>
</ds>
'''
out.write(s)
###############################################################################
def main(args = None):
if args is None:
args = sys.argv[1:]
cvt = cvt_t()
try:
while args and args[0].startswith('-'):
arg = args.pop(0)
if arg == '-n':
cvt.nop = True
elif arg == '-rrd':
cvt.rrdpath = args.pop(0)
elif arg == '-xml':
cvt.xmlpath = args.pop(0)
elif arg == '-idx':
cvt.idx = int(args.pop(0))
elif arg == '-name':
cvt.name = args.pop(0)
elif arg == '-type':
cvt.type = args.pop(0).upper()
elif arg == '-minhb':
cvt.minhb = int(args.pop(0))
elif arg == '-min':
cvt.min = float(args.pop(0))
elif arg == '-max':
cvt.max = float(args.pop(0))
else:
raise RuntimeError, 'unknown option ' + arg
if not cvt.rrdpath:
raise RuntimeError, 'no -rrd specified'
if not cvt.name:
raise RuntimeError, 'no -name specified'
except StandardError, se:
usage()
print se
return 1
cvt.run()
return 0
if __name__ == '__main__':
sys.exit(main())
###############################################################################
# Local Variables:
# mode: indented-text
# indent-tabs-mode: nil
# End: