lighttpd配置之流量监控(mod_rrdtool)

Posted by Elton's Blog on July 13, 2009

lighttpd中带了一个监控模块,可以监控lighttpd服务器的流量。

  1. 确保安装lighttpd的时候,带了rrdtool模块,如果没有,将rrdtool加到lighttpd的USE中,重新编译
  2. 在/etc/lighttpd/lighttpd.conf中,激活mod_rrdtool模块,并添加以下代码:
 rrdtool.binary  = "/usr/bin/rrdtool"
 rrdtool.db-name = var.statedir + "/lighttpd.rrd"

rrdtool.binary是rrdtool的安装目录 rrdtool.db-name是rrdtool需要用到的数据库,在第一次启动前,确保没有那个rrd文件。

  1. 重启lighttpd,这样lighttpd的流量数据就可以实时的记录到rrd文件中了。
  2. 图形化报表 4a. 定义脚本
     #!/bin/sh
    
    

RRDTOOL=/usr/bin/rrdtool OUTDIR=/data/www/yourdomain.com/rrd/ #你的图片输出目录 INFILE=/var/lib/lighttpd/lighttpd.rrd #你的rrd数据库文件位置 OUTPRE=lighttpd-traffic #图片文件的前缀 WIDTH=600 HEIGHT=350

DISP=”-v bytes –title TrafficWebserver DEF:binraw=$INFILE:InOctets:AVERAGE DEF:binmaxraw=$INFILE:InOctets:MAX DEF:binminraw=$INFILE:InOctets:MIN DEF:bout=$INFILE:OutOctets:AVERAGE DEF:boutmax=$INFILE:OutOctets:MAX DEF:boutmin=$INFILE:OutOctets:MIN CDEF:bin=binraw,-1,* CDEF:binmax=binmaxraw,-1,* CDEF:binmin=binminraw,-1,* CDEF:binminmax=binmaxraw,binminraw,- CDEF:boutminmax=boutmax,boutmin,- AREA:binmin#ffffff: STACK:binmax#f00000: LINE1:binmin#a0a0a0: LINE1:binmax#a0a0a0: LINE2:bin#efb71d:incoming GPRINT:bin:MIN:%.2lf GPRINT:bin:AVERAGE:%.2lf GPRINT:bin:MAX:%.2lf AREA:boutmin#ffffff: STACK:boutminmax#00f000: LINE1:boutmin#a0a0a0: LINE1:boutmax#a0a0a0: LINE2:bout#a0a735:outgoing GPRINT:bout:MIN:%.2lf GPRINT:bout:AVERAGE:%.2lf GPRINT:bout:MAX:%.2lf “ $RRDTOOL graph $OUTDIR/$OUTPRE-hour.png -a PNG –start -14400 $DISP -w $WIDTH -h $HEIGHT $RRDTOOL graph $OUTDIR/$OUTPRE-day.png -a PNG –start -86400 $DISP -w $WIDTH -h $HEIGHT $RRDTOOL graph $OUTDIR/$OUTPRE-month.png -a PNG –start -2592000 $DISP -w $WIDTH -h $HEIGHT

OUTPRE=lighttpd-requests

DISP=”-v req –title RequestsperSecond -u 1 DEF:req=$INFILE:Requests:AVERAGE DEF:reqmax=$INFILE:Requests:MAX DEF:reqmin=$INFILE:Requests:MIN CDEF:reqminmax=reqmax,reqmin,- AREA:reqmin#ffffff: STACK:reqminmax#00f000: LINE1:reqmin#a0a0a0: LINE1:reqmax#a0a0a0: LINE2:req#00a735:requests”

$RRDTOOL graph $OUTDIR/$OUTPRE-hour.png -a PNG –start -14400 $DISP -w $WIDTH -h $HEIGHT $RRDTOOL graph $OUTDIR/$OUTPRE-day.png -a PNG –start -86400 $DISP -w $WIDTH -h $HEIGHT $RRDTOOL graph $OUTDIR/$OUTPRE-month.png -a PNG –start -2592000 $DISP -w $WIDTH -h $HEIGHT

</pre>
4b. 定义虚拟主机
修改/etc/lighttpd/lighttpd.conf
<pre class="prettyprint linenums">
$HTTP["host"] == "www.yourdomain.com" {
    server.document-root =var.basedir +  "/yourdomain.com/rrd/" }
</pre>
4c. 定义html文件 index.htm
<pre class="prettyprint linenums">
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
Lighttpd traffic & requests
## Lighttpd Traffic graph1
graph2
graph3
## Lighttpd Requests graph4
graph5
graph6
</pre>
4d. 访问www.yourdomain.com/index.htm就可以看到图片了 5. 加入到cron定时任务中
# crontab -e
输入:
0,20,40 * * * * nice -n 10 /etc/lighttpd/rrdtool.sh >& /dev/null

这样每隔20分钟就会自动执行一下生成图片的脚本,来显示lighttpd的流量情况