【记录】如何删除cacti图形中某个时间段的数据

第一至第六步骤

第一:确认图像是那个rrd
    #在cacti里找到图像打开调试,就能看到

第二:确认该rrd内有几列数据
    #先确定rrdtool命令在哪儿
    find / -name rrdtool
    rrdtool fetch test.rrd AVERAGE

第三:去cacti里面看,该接口图,从何时开有有数据的
    #年月日,时分

第四:明确监控粒度(5分钟300秒?)

第五:确定要删除的数据时间段
    起时间 2018/05/18 10:00 
    终时间 2018/05/18 10:40

第六:将起,止时间转换为unix时间
    起   date -d "2018/05/18 10:00" +%s      >>  1526608800
    终   date -d "2018/05/18 10:40" +%s      >>  1526611200

    #拓展知识: Unix时间转换成 标准时间的命令
    # date  -d  @1526611200      得到  "Fri May 18 10:40:00 CST 2018"
    # date -d @1526611200 "+%Y/%m/%d %H:%M"    得到  "2018/05/18 10:40"
    #       或者下面提供了一个 在线 的 Unix时间 转 标准时间的网页工具
    #       https://tool.chinaz.com/tools/unixtime.aspx

[root@NC-CMNET-smokeping ~]# date -d "2018/05/18 13:00" +%s
1526619600
[root@NC-CMNET-smokeping ~]# date -d "2018/05/18 15:00" +%s
1526626800

第七:查看起止的前后的rrd记录(先查看记录值,后期复查)

#注意:这里起止时间各向前向后延2此采集点(即:600秒)
    rrdtool fetch test.rrd AVERAGE --start 1526608200 --end 1526611800

第八:编写脚本,赋予执行权限

    touch ceshi.sh && chmod +x ceshi.sh

cat > ceshi.sh <<EOF
#注意有可能 cacti主机上的时间格式不是用的CST ,有可能是 EST 或者UTC等,这个在实际中需要注意验证
#这个在 rrdtool dump ${rrdfile} > ${rrdfile}.xml 这一步导出的 xml文件可以进行查询到底是CST 还是 EST.
#!/bin/sh
#version: 1.2
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

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

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

>${rrdfile}.new.xml

while read -r LINE
do
    #CST=`echo "$LINE" | awk '{print $4}'`
    #if [ "$CST" = "CST" ]
    #这是改进后的脚本,不区分时区格式不管是 CST EST EDT
    CST=`echo "$LINE" | awk '{print $8}' | awk -F'<v>' '{print $1}'`
    if [ "$CST" = '<row>' ]
    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
rrdtool restore ${rrdfile}.new.xml ${rrdfile}
/bin/rm -f ${rrdfile}.xml ${rrdfile}.new.xml
EOF

第九:确认转换时间点,在5分钟的后一分钟,争取在3分钟内完成(基本完不成,实验4M的rrd,需要14分钟)

    ./ceshi.sh /root/test.rrd 1526608800 1526611200

前后对比


声明:本文为原创,作者为 辣条①号,转载时请保留本声明及附带文章链接:https://boke.wsfnk.com/archives/336.html
谢谢你请我吃辣条谢谢你请我吃辣条

如果文章对你有帮助,欢迎点击上方按钮打赏作者

最后编辑于:2023/8/1作者: 辣条①号

现在在做什么? 接下来打算做什么? 你的目标什么? 期限还有多少? 进度如何? 不负遇见,不谈亏欠!

暂无评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

arrow grin ! ? cool roll eek evil razz mrgreen smile oops lol mad twisted wink idea cry shock neutral sad ???

文章目录