rrdtool 绘图

一些基础知识就不说了,直接上脚本吧

负载脚本:

 1 #!/bin/sh
  2 RRD_DIR="/var/www/html/graph"
  3 RRD_FILE="load.rrd"
  4 RRD_LOG="load.log"
  5 NOW_STRING=`date +%s`
  6 NOW=`date "+%Y/%m/%d %H:%M:%S"`
  7 LOAD_5=`uptime | awk '{print $10}' | cut -c 0-4`
  8 LOAD_10=`uptime | awk '{print $11}' | cut -c 0-4`
  9 LOAD_15=`uptime | awk '{print $12}' | cut -c 0-4`
 10 ################################
 11 ##### Create rrdtool file ######
 12 create_rrdfile(){
 13     rrdtool create $RRD_DIR/$RRD_FILE \
 14     --step 300 \
 15     DS:load_5:GAUGE:600:U:U \
 16     DS:load_10:GAUGE:600:U:U \
 17     DS:load_15:GAUGE:600:U:U \
 18     RRA:AVERAGE:0.5:1:600 \
 19     RRA:AVERAGE:0.5:6:700 \
 20     RRA:AVERAGE:0.5:24:775 \
 21     RRA:AVERAGE:0.5:288:797
 22 }
 23 
 24 ###############################
 25 ##### Update rrdtool file #####
 26 if [ -f $RRD_DIR/$RRD_FILE ];then
 27     echo "rrdtool update mem.rrd $NOW:$NOW_STRING:$LOAD_5:$LOAD_10:$LOAD_15" >> $RRD_DIR/$RRD_LOG
 28     rrdtool update $RRD_DIR/$RRD_FILE $NOW_STRING:$LOAD_5:$LOAD_10:$LOAD_15
 29 else
 30     create_rrdfile
 31 fi
 32 
 33 ###############################
 34 ##### Graph rrdtool file #####
 35 time="hour day week"
 36 for t in $time
 37 do
 38     rrdtool graph $RRD_DIR/load-$t.png \
 39     --title "localhost memcache" \
 40     -s `date -d "-1 $t" +%s` \
 41     DEF:t5=$RRD_DIR/$RRD_FILE:load_5:AVERAGE \
 42     DEF:t10=$RRD_DIR/$RRD_FILE:load_10:AVERAGE \
 43     DEF:t15=$RRD_DIR/$RRD_FILE:load_15:AVERAGE \
 44     AREA:t5#ff0000:"load_5" \
 45     AREA:t10#339966:"load_10" \
 46     AREA:t15#0000ff:"load_15" \
 47     COMMENT:"\n" \
 48     -X 0 -v "load per second"
 49 done

每小时:

rrdtool 绘图_第1张图片


每天:

rrdtool 绘图_第2张图片


每周:


rrdtool 绘图_第3张图片

内存脚本:

#!/bin/sh
RRD_DIR="/var/www/html/graph"
RRD_FILE="mem.rrd"
RRD_LOG="mem.log"
NOW_STRING=`date +%s`
NOW=`date "+%Y/%m/%d %H:%M:%S"`
RRD_DATA=`free -m| grep Mem | awk '{print $3}'`

################################
##### Create rrdtool file ######
create_rrdfile(){
    rrdtool create $RRD_DIR/$RRD_FILE \
    --step 300 \
    DS:mem:GAUGE:600:U:U \
    RRA:AVERAGE:0.5:1:600 \
    RRA:AVERAGE:0.5:6:700 \
    RRA:AVERAGE:0.5:24:775 \
    RRA:AVERAGE:0.5:288:797 \
    RRA:LAST:0.5:1:600 \
    RRA:LAST:0.5:6:700 \
    RRA:LAST:0.5:24:775 \
    RRA:LAST:0.5:288:797 \
    RRA:MAX:0.5:1:600 \
    RRA:MAX:0.5:6:700 \
    RRA:MAX:0.5:24:775 \
    RRA:MAX:0.5:288:797 \
    RRA:MIN:0.5:1:600 \
    RRA:MIN:0.5:6:700 \
    RRA:MIN:0.5:24:775 \
    RRA:MIN:0.5:288:797
}

###############################
##### Update rrdtool file #####
if [ -f $RRD_DIR/$RRD_FILE ];then
    echo "rrdtool update mem.rrd $NOW:$NOW_STRING:$RRD_DATA" >> $RRD_DIR/$RRD_LOG
    rrdtool update $RRD_DIR/$RRD_FILE $NOW_STRING:$RRD_DATA
else
    create_rrdfile
fi

###############################
##### Graph rrdtool file #####
time="hour day week"
for t in $time
do
    rrdtool graph $RRD_DIR/mem-$t.png \
    --title "localhost memcache" \
    -s `date -d "-1 $t" +%s` \
    DEF:t1=$RRD_DIR/$RRD_FILE:mem:LAST \
    DEF:t2=$RRD_DIR/$RRD_FILE:mem:AVERAGE \
    DEF:t3=$RRD_DIR/$RRD_FILE:mem:MAX \
    DEF:t4=$RRD_DIR/$RRD_FILE:mem:MIN \
    -X 0 \
    HRULE:7800#ff0000 \
    -v "mem per second" \
    COMMENT:"              NOW ------ AVERAGE ------ MAX ------ MIN" \
    COMMENT:"\n" \
    AREA:t1#ff0000:"MEM" \
    GPRINT:t1:LAST:'%0.0lf' \
    GPRINT:t2:AVERAGE:'%0.0lf' \
    GPRINT:t3:MAX:'%0.0lf' \
    GPRINT:t4:MIN:'%0.0lf' 
done

每小时:

rrdtool 绘图_第4张图片


每天:

rrdtool 绘图_第5张图片


每周:


rrdtool 绘图_第6张图片


你可能感兴趣的:(rrdtool)