lxyfirst

C++博客 首页 新随笔 联系 聚合 管理
  33 Posts :: 3 Stories :: 27 Comments :: 0 Trackbacks
rrdtools其实是一个基于文件的数据库,用于数据积累,统计,生成图片。
1.创建数据库
rrdtool create usage.rrd DS:cpu:GAUGE:300:0:100 RRA:LAST
DS指定数据源,后面以':'为分隔符接着定义DS的属性,格式为DS:ds-name:GAUGE | COUNTER | DERIVE | ABSOLUTE:heartbeat:min:max
ds-name:数据源的名字,a-zA-Z0-9 。
统计方式:
GAUGE:一般用于普通的统计,如不同时间点的人数。
COUNTER:一般用于持续增加的计数器,计算增加速度。
COMPUTE:一般用于需要对数据做特殊运算处理。
RRA指定数据统计方式,主要有MIN(最小值) , MAX(最大值) , AVERAGE(平均值) , LAST(单点值)。

2.添加数据
rrdtool update usage.rrd [time:value]

3.查看数据
rrdtool fetch
rrdtool dump

4.生成图片
rrdtool graph


监控cpu和网卡的脚本实例
rrd_init.sh
#!/bin/bash
cd $(dirname $0)

#network
awk -F':' '{if($1~/eth[0123456]$/ && $2 > 0 ) print $1;}' /proc/net/dev | while read if_name
do
    if_name=${if_name// }
    echo "creating ${if_name}.rrd  ..."
    /usr/local/rrdtool/bin/rrdtool create ${if_name}.rrd -s 60 DS:rx:COUNTER:120:U:U DS:tx:COUNTER:120:U:U RRA:AVERAGE:0.5:1:1440
done

#disk io
awk '{if($3~/[sh]d[abcdefgh]$/ && $4 > 0 ) print $3;}' /proc/diskstats | while read disk_name
do
    disk_name=${disk_name// }
    echo "creating ${disk_name}.rrd  ..."
    /usr/local/rrdtool/bin/rrdtool create ${disk_name}.rrd -s 60 DS:rio:COUNTER:120:U:U DS:wio:COUNTER:120:U:U RRA:AVERAGE:0.5:1:1440
done


#cpu
awk '{if($1~/^cpu/) print $1;}' /proc/stat | while  read cpu_name
do
    cpu_name=${cpu_name// }
    echo "creating ${cpu_name}.rrd  ..."
    /usr/local/rrdtool/bin/rrdtool create ${cpu_name}.rrd -s 60 DS:idle:COUNTER:120:U:U DS:user:COUNTER:120:U:U DS:iowait:COUNTER:120:U:U DS:i
ntr:COUNTER:120:U:U DS:total:COUNTER:120:U:U RRA:AVERAGE:0.5:1:1440

done

#memory

/usr/local/rrdtool/bin/rrdtool create memory.rrd -s 60 DS:TotalMemory:GAUGE:120:U:U DS:FreeMemory:GAUGE:120:U:U DS:TotalSwap:GAUGE:120:U:U D
S:FreeSwap:GAUGE:120:U:U RRA:AVERAGE:0.5:1:1440


rrd_stat.sh
* * * * * /usr/local/rrdtool/scripts/cpu_rrd_stat.sh > /dev/null 2>&1
#!/bin/bash
cd $(dirname $0)


#network
awk -F':' '{if($1~/eth[0123456]$/ && $2 > 0  ) printf("%s %s\n",$1,$2);}' /proc/net/dev | awk '{printf("%s %d %d\n",$1,$2,$10);}' | while read
 if_name if_rx if_tx
do
        if_name=${if_name// }
        /usr/local/rrdtool/bin/rrdtool update ${if_name}.rrd N:$if_rx:$if_tx
done

#disk io
awk '{if($3~/[sh]d[abcdefgh]$/ && $4 > 0 ) printf("%s %d %d\n",$3,$6,$10); }' /proc/diskstats | while  read disk_name disk_rio disk_wio
do
        disk_name=${disk_name// }
        /usr/local/rrdtool/bin/rrdtool update ${disk_name}.rrd N:$disk_rio:$disk_wio

done

#cpu
awk '{if($1~/^cpu/) {user=$2+$3 ;intr=$7+$8; printf("%s %d %d %d %d %d\n",$1,$5,user,$6,intr,user+$4+$5+$6+intr);} }' /proc/stat | while  read
 cpu_name cpu_idle cpu_user cpu_iowait cpu_intr cpu_total
do
        cpu_name=${cpu_name// }
        /usr/local/rrdtool/bin/rrdtool update ${cpu_name}.rrd N:$cpu_idle:$cpu_user:$cpu_iowait:$cpu_intr:$cpu_total

done


awk '{if($1~/^MemTotal/) mem_total=$2;else if($1~/^MemFree/) mem_free=$2;else if($1~/^SwapTotal/) swap_total=$2;else if($1~/^SwapFree/) swap_f
ree=$2;} END{printf("%d %d %d %d\n",mem_total,mem_free,swap_total,swap_free);}' /proc/meminfo | while  read mem_total mem_free swap_total swap
_free
do
    /usr/local/rrdtool/bin/rrdtool update memory.rrd N:$mem_total:$mem_free:$swap_total:$swap_free
done


rrd_img.sh
*/5 * * * * /usr/local/rrdtool/scripts/cpu_rrd_img.sh > /dev/null 2>&1
#!/bin/bash
cd $(dirname $0)
awk -F':' '{if($1~/eth[0123456]$/ && $2 > 0 ) print $1;}' /proc/net/dev | while read if_name
do
#        -y 10:10 -u 100 -l 0                                             \
    if_name=${if_name// }
    echo "creating [${if_name}.png]  ..."
    /usr/local/rrdtool/bin/rrdtool graph "${if_name}.png"    \
        -s -86400                                              \
        -w 500 -h 100                                          \
        -x MINUTE:30:HOUR:2:HOUR:2:0:%H:%M                \
        -Y  -t "${if_name} traffic"                      \
        DEF:rx=${if_name}.rrd:rx:AVERAGE                       \
        DEF:tx=${if_name}.rrd:tx:AVERAGE                       \
        CDEF:rx_mbps=rx,1,\/                              \
        CDEF:tx_mbps=tx,1,\/                              \
        LINE1:rx_mbps#FF0000:"rx(mbps)"                        \
        LINE1:tx_mbps#00FF00:"tx(mbps)"


done

awk '{if($3~/[sh]d[abcdefgh]$/ && $4 > 0 ) print $3;}' /proc/diskstats | while read disk_name
do
    disk_name=${disk_name// }
    echo "creating [${disk_name}.png]  ..."
    /usr/local/rrdtool/bin/rrdtool graph "${disk_name}.png"    \
        -s -86400                                              \
        -w 500 -h 100                                          \
        -x MINUTE:30:HOUR:2:HOUR:2:0:%H:%M                \
        -Y -t "${disk_name} io"                         \
        DEF:rio=${disk_name}.rrd:rio:AVERAGE                       \
        DEF:wio=${disk_name}.rrd:wio:AVERAGE                       \
        CDEF:rio_sector=rio,1,\/                              \
        CDEF:wio_sector=wio,1,\/                              \
        LINE1:rio_sector#00FF00:"rio(sector)"                        \
        LINE1:wio_sector#FF0000:"wio(sector)"


done

awk '{if($1~/^cpu/) print $1;}' /proc/stat | while  read cpu_name
do
    cpu_name=${cpu_name// }
    echo "creating [${cpu_name}.png]  ..."
    /usr/local/rrdtool/bin/rrdtool graph "${cpu_name}.png"      \
        -s -86400                                               \
        -w 500 -h 100                                           \
        -x MINUTE:30:HOUR:2:HOUR:2:0:%H:%M                \
        -Y  -t "${cpu_name} usage"                          \
        DEF:idle=${cpu_name}.rrd:idle:AVERAGE                   \
        DEF:user=${cpu_name}.rrd:user:AVERAGE                   \
        DEF:iowait=${cpu_name}.rrd:iowait:AVERAGE                   \
        DEF:intr=${cpu_name}.rrd:intr:AVERAGE                   \
        DEF:total=${cpu_name}.rrd:total:AVERAGE                 \
        CDEF:idle_persent=idle,total,\/,100,\*                         \
        CDEF:user_persent=user,total,\/,100,\*                         \
        CDEF:iowait_persent=iowait,total,\/,100,\*                         \
        CDEF:intr_persent=intr,total,\/,100,\*                         \
        LINE1:idle_persent#00FF00:"idle(%)"                        \
        LINE1:user_persent#0000FF:"user(%)"                        \
        LINE1:iowait_persent#FF0000:"iowait(%)"                    \
        LINE1:intr_persent#FF00FF:"intr(%)"

done

   rrd_name="memory"
    echo "creating [${rrd_name}.png]  ..."
    /usr/local/rrdtool/bin/rrdtool graph "${rrd_name}.png"      \
        -s -86400                                               \
        -w 500 -h 100                                           \
        -x MINUTE:30:HOUR:2:HOUR:2:0:%H:%M                \
        -Y  -t "${rrd_name} usage"                          \
        DEF:TotalMemory=${rrd_name}.rrd:TotalMemory:AVERAGE                   \
        DEF:FreeMemory=${rrd_name}.rrd:FreeMemory:AVERAGE                   \
        DEF:TotalSwap=${rrd_name}.rrd:TotalSwap:AVERAGE                   \
        DEF:FreeSwap=${rrd_name}.rrd:FreeSwap:AVERAGE                   \
        CDEF:TotalMemoryM=TotalMemory,1024,\/                         \
        CDEF:FreeMemoryM=FreeMemory,1024,\/                         \
        CDEF:TotalSwapM=TotalSwap,1024,\/                         \
        CDEF:FreeSwapM=FreeSwap,1024,\/                         \
        LINE1:TotalMemoryM#00FF00:"TotalMemory(M)"                        \
        LINE1:FreeMemoryM#0000FF:"FreeMemory(M)"                        \
        LINE1:TotalSwapM#FF0000:"TotalSwap(M)"                    \
        LINE1:FreeSwapM#FF00FF:"FreeSwap(M)"


cp 
*.png ${imgpath}/

posted on 2009-01-15 12:22 star 阅读(3338) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理