rrdtool delete


遇到一些问题,已经记录数据的rrd文件,需要删除一段历史数据,但rrdtool是不支持对历史数据的update的,因此只能自己想办法

Google了一下:Remove data from RRDTool ,得到这个url

http://stackoverflow.com/questions/10298484/remove-data-from-rrdtool


那么思路就确定了:

1.Use RRDTool Dump to export RRD files to XML.
2.Open the XML file, find and edit the bad data.
3.Restore the RRD file using RRDTool Restore .


这个思路其实已经很明确了,多说无益,直接写个简陋的脚本吧


#!/bin/sh

if [ "$#" -ne 3  ];then
	echo "usage: $0 file.rrd starttime endtime"
	echo "exmple: $0 /home//rrd/snmp/40/device.rrd 1357745100 1357752600  "
	exit 1
fi

rrdfile=$1
starttime=$2
endtime=$3

IFS='
'

/bin/rm -f ${rrdfile}.xml ${rrdfile}.new.xml

/usr/local/rrdtool/bin/rrdtool last ${rrdfile} > /dev/null
/usr/local/rrdtool/bin/rrdtool dump ${rrdfile} > ${rrdfile}.xml

>${rrdfile}.new.xml

while read -r LINE
do
	CST=`echo "$LINE" | awk '{print $4}'`
	if [ "$CST" = "CST" ]
	then
		TIME=`echo "$LINE" | awk '{print $6}'`
		if [ $TIME -ge $starttime ] && [ $TIME -le $endtime ]
		then
			#echo "$LINE"
			NEWLINE=`echo "${LINE}" | sed 's#<v>................</v>#<v>NaN</v>#g'`;
			echo "$NEWLINE" >> ${rrdfile}.new.xml
			continue
		fi
	fi
	echo "$LINE" >> ${rrdfile}.new.xml	
done < ${rrdfile}.xml

mv ${rrdfile} ${rrdfile}.bak
/usr/local/rrdtool/bin/rrdtool restore ${rrdfile}.new.xml ${rrdfile}
/bin/rm -f ${rrdfile}.xml ${rrdfile}.new.xml

临时用的,其实可改进的地方还很多,各位自己发挥吧,希望能共享下。


你可能感兴趣的:(rrdtool delete)