Skip to content

Instantly share code, notes, and snippets.

@yalinhuang
Forked from arantius/simple-rrd-merge.py
Last active February 9, 2018 17:34
Show Gist options
  • Save yalinhuang/9a8d35421c1feb148bd0 to your computer and use it in GitHub Desktop.
Save yalinhuang/9a8d35421c1feb148bd0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""Simple script to merge multiple RRD files together.
Accepts any number of RRD file names as arguments.
Produces an "rrdtool dump" style file on stdout.
The last RRD file should have a slot for every possible record in the
resulting merged RRD.
Run something like:
$ python simple-merge-rrd.py filea.rrd fileb.rrd filec.rrd | \
rrdtool restore /dev/stdin merged.rrd
"""
import re
import subprocess
import sys
def main():
rrd_data = {}
rrds = sys.argv[1:]
last_rrd = len(rrds) - 1
for i, rrdname in enumerate(rrds):
p = subprocess.Popen(
('rrdtool', 'dump', rrdname), stdout=subprocess.PIPE)
for j, line in enumerate(p.stdout):
m = re.search(r'<cf>(.*)</cf>', line)
if m:
cf = m.group(1)
m = re.search(r'<pdp_per_row>(.*)</pdp_per_row>', line)
if m:
pdp = m.group(1)
m = re.search(r' / (\d+) --> (.*)', line)
if m:
k = cf + pdp
rrd_data.setdefault(k, {})
if (m.group(1) not in rrd_data[k]) or (
'NaN' in rrd_data[k]): # Fill in only when there is no exsiting data
rrd_data[k][m.group(1)] = line
line = rrd_data[k][m.group(1)]
if i == last_rrd:
print line.rstrip()
if __name__ == '__main__':
main()
@yalinhuang
Copy link
Author

The rrd merge script managed by arantius is useful for most cases where the values read from later RRD files overwrite previous ones. Also, the final RRD file preserves the "timeline" in the last RRD file; see line 44.

I am forking this to change the overwriting behavior so that later values do NOT overwrite previous ones. That is, given RRD files are fed to the program in the order of "A.rrd B.rrd C.rrd", then the values in A.rrd are the most respectful ones and stay in the final RRD file. B.rrd only contributes those that A.rrd does not have; similarly, C.rrd delivers those not in A.rrd nor in B.rrd. Regardless, the timeilne of the final RRD file would be that of C.rrd.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment