Commit 5c775491 authored by julric's avatar julric

added python-script to add a new row to an existing rrd-file

parent 42cc8304
#!/usr/bin/python
#########################################################
# Python-Script to add a new column to an existing rrd #
#########################################################
# #
# important: you need to have the rrdtools package #
# installed, e.g. for debian linux type: #
# apt-get install rrdtools #
# #
#########################################################
import sys
import os
from subprocess import call
# check if 3 arguments are given
if len(sys.argv) < 4:
print '\nUsage: rrdAddDS.py <inputfile.rrd> <outputfile.rrd> <newDSname>\n'
print 'Example: rrdAddDS.py rrd_data.rrd rrd_data_new.rrd temperature\n'
sys.exit(2)
tempfile = sys.argv[1][:-3] + "xml"
tempfile_new = sys.argv[2][:-3] + "xml"
# create xml-file and convert rrd to xml
f = open(tempfile, "w")
print('\nconverting ' + sys.argv[1] + ' to ' + tempfile + ' ...')
call(["rrdtool", "dump", sys.argv[1]], stdout=f)
print('\t\t\t\t\t\t\t ... done')
# insert new row on 3 places in the xml-file
print('\napplying changes, add row "' + sys.argv[3] + '" to rrd ...')
with open(tempfile) as fin, open(tempfile_new, 'w') as fout:
for line in fin:
if line.strip() == '<!-- Round Robin Archives -->':
fout.write(' <ds>\n <name> ' + sys.argv[3] + ' </name>\n <type> GAUGE </type>\n <minimal_heartbeat>300</minimal_heartbeat>\n <min>0.0000000000e+00</min>\n <max>NaN</max>\n\n <!-- PDP Status -->\n <last_ds>UNKN</last_ds>\n <value>1.8530980000e+01</value>\n <unknown_sec> 0 </unknown_sec>\n </ds>\n\n')
if line.strip() == '</cdp_prep>':
fout.write(' <ds>\n <primary_value>NaN</primary_value>\n <secondary_value>NaN</secondary_value>\n <value>NaN</value>\n <unknown_datapoints>0</unknown_datapoints>\n </ds>\n')
if '</row>' in line:
fout.write(line[:-7] + '<v>NaN</v></row>\n')
else:
fout.write(line)
print('\t\t\t\t\t\t\t ... done')
# convert new xml to new rrd
print('\nconverting ' + tempfile_new + ' to ' + sys.argv[2] + ' ...')
try:
os.remove(sys.argv[2])
except OSError:
pass
call(["rrdtool", "restore", tempfile_new, sys.argv[2]])
print('\t\t\t\t\t\t\t ... done')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment