rrdtool通过流量数据绘图

线上机器的流量图都采用MRTG来画的,有些机器部署了同一业务,需要知道这些业务跑了多少带宽,由于MRTG的图有每个时间点的流量数据log,所以就在这个log的基础上取数据用rrdtool合并起来,就可以知道这些机器总共跑了多少带宽了。

   原理:定期(这里是5分钟)从mrtg服务器上同步log过来,然后截取最近的流量数据,如果通过过来的log时间不是最新的,说明这台机器可能出故障或者mrtg流量有问题,就忽略它;将取出来的数据用rrdtool更新,画图即可;

先说下MRTG的log格式吧:


--------------------------------------------------------------------
MRTG日志例样
--------------------------------------------------------------------
mrtg上次运行 输入总流量 输出总流量
1233651303 86083097205191 1077254914026673

时间戳 当前输入 当前输出 平均最大输入 平均最大输出
1233651303 1502168 36756425 1502168 36756425
1233651242 1637256 37749359 1840688 39682490
1233651000 1593342 35050409 1808632 37572979
1233650700 1711678 38139787 1966100 42713270


--------------------------------------------------------------------
MRTG日志详解
--------------------------------------------------------------------
MRTG日志分为两部分:

+ 第一部分

日志的第一行是第一部分。有三列,分别代表MRTG上次的运行时间戳,输入
总流量和输出总流量。

+ 第二部分

除去第一行剩下的部分为第二部分,总共5列,分别代表:
1. A(第一列) 这一列相关数据的时间戳,需要注意的是开始时每行之间的时间间
隔为5分钟,最后为一天.
perl -e 'print scalar localtime(x),"\n"'

2. B(第二列) 每秒的平均输入(average incoming)流量,以字节为单位。

3. C(第三列) 每秒的平均输出(average outgoing)流量,以字节为单位。

4. D(第四列) 当前间隔内每秒的最大输入流量(maximum incoming),以字节为单
位。这是从当前间隔内所有的更新中计算出来的。假设当前时隔为1小时,每5分
钟更新一次,那么这个值就是所有12次数据中最大的那个。

5. E(第五列) 当前间隔内每秒的最大输出流量(maximum outgoing),以字节为单
位。计算方法同上。


代码如下:


# cat rsync_log.sh
#!/bin/bash
dir=$(dirname $0)
cd $dir
H=host
src="ccrsync@xxxxx::MRTGHOME"
dst="log"
res=result.log
> $res
while read host eth        #host文件中按格式记录要同步的机器名和网卡名:hostname ethx
do
    f="$host.xxxx.net_$eth.log"
    echo -n "$host "
    echo -n "$host " >> $res
    /usr/bin/rsync -az --timeout=120  $src/$host/$f $dst
    if [ ! -f log/${f} ];then
        echo -n "needs check"
        echo "0 0" >> $res        #如果要同步的网卡流量数据没有,则设为0
    elif [ `expr $(date +%s) - $(ls -l  --time-style=+%s log/$f |awk '{print $6}')` -gt 1200 ];then
        echo -n "is older"
        echo "0 0" >> $res        #若mtrg未更新了,则设为0
    else
        head -2 log/$f |tail -1 |cut -d' ' -f2,3 >> $res
    fi
    echo
done < $H
#cat $res,查看结果,若流量数据为0的就可以去检查mrtg是否正常了
$dir/rrd.sh            #更新rrd数据
$dir/graph.sh h    #画最近多少小时的图
$dir/graph.sh w    #最近一星期
$dir/graph.sh m
$dir/graph.sh y
# cat rrd.sh
#!/bin/bash
dir=$(dirname $0)
db="$dir/rrd/all.rrd"
file="$dir/cdn/all.png"
log=$dir/result.log
declare -a in_out="($(cat $log  |awk '{a+=$2;b+=$3}END{print a,b}'))"
echo ${in_out[0]}
echo ${in_out[1]}
if [ ! -f $db ];then
    /usr/bin/rrdtool create $db --step 300 \
        DS:inner:GAUGE:600:0:U \
        DS:outer:GAUGE:600:0:U \
        RRA:AVERAGE:0.5:1:4800 \
        RRA:AVERAGE:0.5:6:2400 \
        RRA:AVERAGE:0.5:24:1200 \
        RRA:AVERAGE:0.5:288:600
fi
now=$(date +%s)
now1=$(date +"%F %T")
/usr/bin/rrdtool update $db $now:${in_out[0]}:${in_out[1]}



# cat graph.sh
#!/bin/bash
usage()
{
    echo "Usage:$0 <h>|<w>|<m>|<y>"
}
if [ $# -ne 1 ];then
   usage
   exit 1
fi
dir=$(dirname $0)
db="$dir/rrd/all.rrd"
str=""
dt=$(date +"%F %T")
case $1 in
    "h")
        str=${str}" --end now --start end-108000 --title \"last 30 hours(update time:$dt)\" --x-grid=MINUTE:30:HOUR:1:HOUR:1:0:'%H'"
        file="$dir/cdn/hour.png"
        ;;
    "w")
        str=${str}" -s -8day -e now-1min -t \"week(update time:$dt)\" --x-grid=HOUR:2:HOUR:6:HOUR:24:0:'%a'"
        file="$dir/cdn/week.png"
        ;;
    "m")
        str=${str}" -s -40day -e now-1min -t \"last month(update time:$dt)\" --x-grid=HOUR:12:HOUR:24:HOUR:24:0:'%d'"
        file="$dir/cdn/month.png"
        ;;
    "y")
        str=${str}" -s -20month -e now-1min -t \"last year(update time:$dt)\" --x-grid=MONTH:1:MONTH:1:MONTH:1:0:'%b'"
        file="$dir/cdn/year.png"
        ;;
    "*")
        usage
        exit 1
        ;;
esac
str=${str}' -v "Bits per Second" -w 700 -h 240 '
str=${str}"
    DEF:in0=$db:inner:AVERAGE
    DEF:out0=$db:outer:AVERAGE "
#由于mrtg的流量数据单位为byte,咱们绘图的为bit,则需*8
str=${str}'
    CDEF:in1=in0,8,*   
    CDEF:out1=out0,8,*
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
    LINE1:out1#0000ff:"out\t"
    GPRINT:out1:LAST:"last %6.2lf%s\t"
    GPRINT:out1:MAX:"max %6.2lf%s\t"
    GPRINT:out1:MIN:"min %6.2lf%s\t"
    GPRINT:out1:AVERAGE:"average %6.2lf%s\n"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
    AREA:in1#22DD22:"in\t"
    GPRINT:in1:LAST:"last %6.2lf%s\t"
    GPRINT:in1:MAX:"max %6.2lf%s\t"
    GPRINT:in1:MIN:"min %6.2lf%s\t"
    GPRINT:in1:AVERAGE:"average %6.2lf%s\n"
'
cmd=`echo /usr/bin/rrdtool graph $file ${str}`
#/usr/bin/rrdtool graph $file ${str}
eval $cmd


rsync_log.sh这脚本放到crontab里面5分钟跑一次就可以持续生成图了

效果如下:

wKioL1N3D-Tx9un3AAeVIMGxoIE116.jpg


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章