自定义增量数据在MRTG中未达预期结果的问题排查
MRTG自定义每分钟增量数据显示异常问题解决
问题描述
我正尝试在MRTG中使用自定义每分钟增量数据,但显示结果不符合预期:期望每分钟显示500,日志里却显示8(500/60≈8.333取整);用100时显示2(100/60≈1.666取整)。我的数据源是每分钟增量,而非MRTG默认的每秒增量。
当前mrtg.cfg配置
Title[trans]: Database - Tester MaxBytes[trans]: 25 AbsMax[trans]: 200 Options[trans]: nopercent noinfo Target[trans]: `/tools/database/test` PageTop[trans]: <h1> Database - Tester </h1> ShortLegend[trans]: tx Ylegend[trans]: Transactions LegendI[trans]: Transactions LegendO[trans]: Legend1[trans]: Transactions Legend2[trans]: Legend3[trans]: Transactions Legend4[trans]: XSize[trans]: 350 YSize[trans]: 150 Directory[trans]: db_test WithPeak[trans]: ymwd Unscaled[trans]: ymwd
测试脚本
#!/bin/bash if [ -f test.ctr ]; then oldnum=`cat test.ctr` else echo "0" > test.ctr fi increm='500' newnum=$(($oldnum + $increm)) echo $newnum echo $newnum echo echo "tester ctr" echo "$newnum" > test.ctr
异常日志
1723253042 49337 49337 1723253042 8 8 8 8 1723252982 8 8 8 8 1723252800 8 8 8 8 1723252500 8 8 8 8 1723252200 8 8 8 8
解决方案
MRTG默认会把获取到的增量值除以采集间隔(默认60秒),转换成每秒速率,所以才会出现你看到的取整结果。要显示原始每分钟增量,需要修改配置并调整脚本输出:
1. 修改mrtg.cfg配置项
- 添加
Rate[trans]: no:禁用MRTG自动计算每秒速率的逻辑,直接使用脚本输出的原始增量值。 - 调整
MaxBytes[trans]和AbsMax[trans]:原配置的25、200远小于实际的500增量,会导致图表显示异常,改成符合实际最大值的数值,比如:MaxBytes[trans]: 1000 AbsMax[trans]: 2000 - 保留其他现有配置,确保
Options里的nopercent noinfo不变,避免百分比显示干扰。
修改后的核心配置片段:
MaxBytes[trans]: 1000 AbsMax[trans]: 2000 Options[trans]: nopercent noinfo Rate[trans]: no Target[trans]: `/tools/database/test`
2. 调整脚本输出逻辑
当前脚本输出的是累计值,MRTG需要的是两次采集之间的增量值,而非累计值。修改脚本,直接输出当前周期的增量(不需要维护累计文件):
#!/bin/bash # 直接输出每分钟的增量值 increm='500' echo $increm echo $increm echo echo "tester ctr"
如果后续需要真实业务增量(比如从数据库统计最近一分钟的交易数),只需把increm='500'替换成实际统计命令即可。
原理说明
当Rate[trans]: no生效后,MRTG会直接使用脚本输出的数值作为当前周期的数值,不再进行除以60的速率转换,这样就能准确显示每分钟的500增量。
内容的提问来源于stack exchange,提问作者Sophia
相关产品推荐
相关产品推荐

