#!/usr/bin/env python import os import subprocess def prep_list(rra_path, ext='rrd'): file_list = [] dir_list = [] for root, dirs, files in os.walk(rra_path): for dir in dirs: dir_list.append("%s/%s" % (root, dir)) for file in files: if file.split('.')[-1] == ext: file_list.append("%s/%s" % (root, file)) return dir_list, file_list def mk_xml_struct(prefix, old_prefix, dirs): os.mkdir(prefix) for dir in dirs: os.mkdir("%s%s" % (prefix, dir.replace(old_prefix, ''))) def convert(prefix, old_prefix, files, replacer='.rrd', replacee='.xml', rrd_action='dump'): for file in files: new_file = file.replace(replacer, replacee).replace(old_prefix, prefix) subprocess.Popen(['/usr/bin/rrdtool', rrd_action, file, new_file]) def encode(): dirs, files = prep_list('rra') mk_xml_struct('xml', 'rra', dirs) convert('xml', 'rra', files) def decode(): dirs, files = prep_list('xml', 'xml') mk_xml_struct('rra', 'xml', dirs) convert('rra', 'xml', files, '.xml', '.rrd', 'restore') if __name__ == '__main__': decode()