基于Ubuntu6.06的MRTG网络流量分析统计
2007.01.04 12:00 kenthy#qingdaonews.com
系统环境: ubuntu 6.06 [ kernel 2.6.15-26-686 ]
软件环境:
gcc 4.0.3-1ubuntu5
perl 5.8.7-10ubuntu
apache 2.2.0
相关软件:
libpng12-0_1.2.8rel-5ubuntu0.1_i386.deb
gdb_6.4-1ubuntu5.1_i386.deb
zlib1g_1.2.3-6ubuntu4.i386.deb
snmpd_5.2.1.2-4ubuntu2_i386.deb
mrtg_2.12.2-1_i386.deb
sysstat_6.0.2-1_i386.deb
配置过程:
1. 配置Apache网站服务器 [ DocumentRoot /opt/apache2/htdocs ]
#mkdir /opt/apache2/htdocs/mrtg
#mkdir /opt/mrtg
#mkdir /var/lock/mrtg
#/usr/sbin/apachectl start
2. 配置snmpd
1> 编辑/etc/snmp/snmpd.conf ,添加相应行或打开/关闭注释
#com2sec paranoid default public
com2sec readonly default public
#com2sec readwrite default private
view mib2 included .iso.org.dod.internet.mgmt.mib-2.system fc
access notConfigGroup "" any noauth exact mib2 none none
2> 启动snmpd服务
#ln -s /etc/init.d/snmpd /etc/rc2.d/S20snmpd
#/etc/init.d/snmpd start
3. 配置mrtg
1> 使用/usr/bin/cfgmaker工具创建/etc/mrtg.cfg初始化配置文件
#cfgmaker --global 'WorkDir:/opt/apache2/htdocs/mrtg' --global 'Options[_]:growright,bits' --global 'Language:Chinese' --ifref=ip > /etc/mrtg.cfg
2> 增加CPU负载监控
a> 建立脚本
#cat > /opt/mrtg/mrtg.cpu << "EOF"
#!/bin/bash
cpuusr=`/usr/bin/sar -u 1 3 | grep Average | awk '{print $3}'`
cpusys=`/usr/bin/sar -u 1 3 | grep Average | awk '{print $5}'`
UPtime=`/usr/bin/uptime | awk '{print $3""$4""$5}'`
echo $cpuusr
echo $cpusys
echo $UPtime
hostname
EOF
b> 增加执行权限
#chmod 755 /opt/mrtg/mrtg.cpu
c> 追加配置信息到/etc/mrtg.cfg
#cat >> /etc/mrtg.cfg << "EOF"
Target[cpu]: `/opt/mrtg/mrtg.cpu`
MaxBytes[cpu]: 100
Options[cpu]: gauge, nopercent, growright
YLegend[cpu]: CPU loading (%)
ShortLegend[cpu]: %
LegendO[cpu]: CPU us;
LegendI[cpu]: CPU sy;
Title[cpu]: CPU Loading
PageTop[cpu]:
CPU Loading Statistic
EOF
3> 增加内存使用状况监控
a> 建立脚本
#cat > /opt/mrtg/mrtg.ram << "EOF"
#!/bin/bash
# run this script to check the mem usage.
totalmem=`/usr/bin/free |grep Mem |awk '{print $2}'`
usedmem=`/usr/bin/free |grep Mem |awk '{print $3}'`
UPtime=`/usr/bin/uptime | awk '{print $3""$4""$5}'`
echo $totalmem
echo $usedmem
echo $UPtime
hostname
EOF
b> 增加执行权限
#chmod 755 /opt/mrtg/mrtg.www
c> 追加配置信息到/etc/mrtg.cfg
#cat >> /etc/mrtg.cfg << "EOF"
Target[ram]: `/opt/mrtg/mrtg.ram`
#Unscaled[ram]: dwym
MaxBytes[ram]: 2048000
Title[ram]:Memory
ShortLegend[ram]: &
kmg[ram]:kB,MB
kilo[ram]:1024
YLegend[ram]: Memory Usage :
Legend1[ram]: Total Memory :
Legend2[ram]: Used Memory :
LegendI[ram]: Total Memory :
LegendO[ram]: Used Memory :
Options[ram]: growright,gauge,nopercent
PageTop[ram]:Memory Useage Statistic
EOF
4> 增加网站连接负载监控
a> 建立脚本
#cat > /opt/mrtg/mrtg.www << "EOF"
#!/bin/bash
all=`netstat -a | grep www|awk '{print $5}'|sort | wc -l|awk '{print$1 - 1}'`
user=`netstat -a | grep www|awk '{print $5}'|cut -d":" -f1|sort| uniq |wc -l | awk '{print $1 - 1}'`
if [ "$all" = "-1" ]; then
echo 0
else
echo $all
fi
if [ "$user" = "-1" ]; then
echo 0
else
echo $user
fi
UPtime=`/usr/bin/uptime | awk '{print $3 " " $4 " " $5}'`
echo $UPtime
hostname
EOF
b> 增加执行权限
#chmod 755 /opt/mrtg/mrtg.www
c> 追加配置信息到/etc/mrtg.cfg
#cat >> /etc/mrtg.cfg << "EOF"
Target[www]: `/opt/mrtg/mrtg.www`
MaxBytes[www]: 500
Options[www]: nopercent, growright
YLegend[www]: Online Users
ShortLegend[www]: %
LegendI[www]: Connect :
LegendO[www]: Online :
Title[www]: WWW Connect
PageTop[www]: WWW Connect Statistic
EOF
4. 运行mrtg建立网络流量/CPU负载/内存使用状况/网站连接负载等统计信息页面
1> 配置计划任务,每5分钟作一次数据分析
#crontab -e
*/5 * * * * env LANG=C /usr/bin/mrtg /etc/mrtg.cfg
#ln -s /etc/init.d/cron /etc/rc2.d/S89cron
2> 立即执行数据分析
#env LANG=C /usr/bin/mrtg /etc/mrtg.cfg
3> 创建网页索引文件
#indexmaker /etc/mrtg.cfg > /opt/apache2/htdocs/mrtg/index.html
4> 访问 即可看到各统计信息画面
##############################################
阅读(1162) | 评论(1) | 转发(0) |