, RRDtool, script ( - , , )
"""
Modify XML data generated by `rrdtool dump` such that the last update was at
the unixtime specified (decimal). Data newer than this is simply omitted.
Sample usage::
rrdtool dump foo.rrd \
| python remove_samples_newer_than.py 1414782122 \
| rrdtool restore - foo_trimmed.rrd
"""
import sys
assert sys.argv[1:], "Must specify maximum Unix timestamp in decimal"
iMaxUpdate = sys.argv[1]
for rLine in iter(sys.stdin.readline, ''):
if "<lastupdate>" in rLine:
_, _, rData = rLine.partition("<lastupdate>")
rData, _, _ = rData.partition("</lastupdate")
iLastUpdate = int(rData)
assert iLastUpdate < iMaxUpdate, "Last update in RRD older than " \
"the time you provided, nothing to do"
print "<lastupdate>{0}</lastupdate>".format(iMaxUpdate)
elif "<row>" in rLine:
rData, _, _ = rLine.partition("<row>")
_, _, rData = rData.partition("/")
rData, _, _ = rData.partition("--")
rData = rData.strip()
iUpdate = int(rData)
if iUpdate < iMaxUpdate:
print rLine,
else:
print rLine,
. , - .