This repository was archived by the owner on Jun 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrrdinfo-parser.py
More file actions
executable file
·182 lines (143 loc) · 5.77 KB
/
rrdinfo-parser.py
File metadata and controls
executable file
·182 lines (143 loc) · 5.77 KB
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
#!/usr/bin/env python3
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 textwidth=79 autoindent
from __future__ import print_function
"""
Conversion to Python2/3: Francois GUILLIER (2015) <dev @ guillier . org>
Original source from http://andorian.blogspot.com/2011/02/rrdinfo-parser.html
Original author: Laban Mwangi
Dumps the schema of existing rrd files using rrdtool info,
Parses this dump, and generates an rrd graph definition
"""
import optparse
import subprocess
import re
import sys
class RRDParser(object):
"""RRDParser - Given an rrd file, return it's graph definition
Typically what you'd feed to rrdtool create to get the same graph
Keyword
Params - Dictionary
rrdfile - String - File to read
debug - Int - Debug level >=0
"""
def __init__(self, params):
super(RRDParser, self).__init__()
self.params = params
self.info = ""
self.schema = {}
def _rrdinfodump(self):
"""rrdinfodump - Acquires the rrdinfo dump"""
if self.params['debug'] >= 2:
print("Getting dump for: %s" % (self.params['file']))
cmd = ["rrdtool", "info", self.params['file']]
proc = subprocess.Popen(cmd, stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE, close_fds=True,
env={'LC_NUMERIC': 'C'})
str_stdout = proc.stdout.read().decode()
str_stderr = proc.stdout.read().decode()
ret_code = proc.poll()
if self.params['debug'] >= 2:
print("command : %s" % cmd)
print("Return Code : %s" % ret_code)
print("Stdout : %s" % str_stdout)
print("Stderr : %s" % str_stderr)
if str_stdout.find('header_size'):
self.info = str_stdout
return True
else:
return False
def _parse_hdr(self):
"""_parse_hdr analyzes the info variable and updates hdr info."""
self.schema["hdr"] = {}
for line in self.info.split("\n"):
for hdr in ["filename", "step", "last_update"]:
if line.startswith(hdr):
self.schema["hdr"][hdr] = line.split(" = ")[-1].strip('"')
def _parse_ds(self):
"""_parse_ds analyzes the info variable and updates the schema dict"""
ds_re = r"ds\[(?P<datasource>.*?)\]\.(?P<field>.*?) "
ds_re += r"= (?P<arg>.*)"
ds_dict = {}
for line in self.info.split("\n"):
if line.startswith("ds"):
m = re.search(ds_re, line)
if m:
data_source, field, value = m.groups(0)
if not data_source in ds_dict:
ds_dict[data_source] = {}
ds_dict[data_source][field] = value.strip('"').strip()
if not len(ds_dict):
print("DS is a required field")
sys.exit(1)
self.schema["ds"] = ds_dict
def _parse_rra(self):
"""_parse_rra analyzes the info variable and updates the schema dict"""
rra_re = r"rra\[(?P<datasource>\d\+?)\]\.(?P<field>[a-z_]+?) "
rra_re += r"= (?P<arg>.*)"
rra_dict = {}
for line in self.info.split("\n"):
if line.startswith("rra"):
m = re.search(rra_re, line)
if m:
rra, field, value = m.groups(0)
if not rra in rra_dict:
rra_dict[rra] = {}
rra_dict[rra][field] = value.strip('"').strip()
if not len(rra_dict):
print("rra is a required field")
sys.exit(1)
self.schema["rra"] = rra_dict
def parse(self):
"""Parse - Generates the new graph definition"""
self._rrdinfodump()
self._parse_hdr()
self._parse_ds()
self._parse_rra()
hdr = self.schema["hdr"]
ds = self.schema["ds"]
rra = self.schema["rra"]
print('rrdtool create %(filename)s ' % hdr, end='')
print('--start %(last_update)s' % hdr, end=' ')
print('--step %(step)s \\' % hdr)
for key, value in ds.items():
for item in ['max', 'min']:
if value[item] == 'NaN':
value[item] = 'U'
else:
value[item] = float(value[item])
ds_str = ' DS:%s:' % key
ds_str += '%s:%s:%s:%s \\' % (
value['type'], value['minimal_heartbeat'],
value['min'], value['max'])
print(ds_str)
rrakeys = list(rra.keys())
rrakeys.sort()
rra_strings = []
for key in rrakeys:
value = rra[key]
for item in ['xff']:
value[item] = float(value[item])
rra_str = ' RRA:%s:%.1f:' % (value['cf'],
value['xff'])
rra_str += '%s:%s' % (value['pdp_per_row'], value['rows'])
rra_strings.append(rra_str)
print(' \\\n'.join(rra_strings))
def main():
"""Main function. Called when this file is a shell script"""
usage = "usage: %prog [options]"
parser = optparse.OptionParser(usage)
parser.add_option("-v", "--verbose", dest="debug",
default="0", type="int",
help="Debug. Higher integers increases verbosity")
parser.add_option("-f", "--file", dest="file",
default="test.rrd", type="string",
help="Existing rrdfile to parse")
(options, args) = parser.parse_args()
params = {}
params['debug'] = options.debug
params['file'] = options.file
rrd_parser = RRDParser(params)
rrd_parser.parse()
if __name__ == '__main__':
main()