3rd
2011,12
因为需要,需要监控下网络环境,试着安装了一下mrtg,ubuntu虽然安装简单,但也遇到了一点小波折。
安装软件:
apt-get install apache2
apt-get install mrtg
apt-get install snmpd
编辑snmpd:
vim /etc/snmp/snmpd.conf
查找到类似的行,修改为如下所示:(仅仅修改屏蔽或放开)
# sec.name source community
# com2sec paranoid default public # kommentieren
com2sec readonly default public # <- auskommentieren
#com2sec readwrite default private
重启snmpd服务:
/etc/init.d/snmpd restart
重新生成mrtg的配置文件:
cfgmaker public@localhost > /etc/mrtg.cfg
(注意)如果仅仅监视一个IP地址采用如下命令:
cfgmaker public@192.168.0.1 >> /etc/mrtg.cfg
生成mrtg的主页:
indexmaker /etc/mrtg.cfg > /var/www/mrtg/index.html
访问:
http://localhost/mrtg/
一般来说上面的步骤不会有一点问题,但有的时候会出现“Use of uninitialized value $first in hash element at /usr/bin/indexmaker line 353.”的错误。
这个错误呢一般是由于mrtg模板问题造成的,下面发一个我改的模板上来
# Created by # /usr/bin/cfgmaker public@localhost ### Global Config Options # for UNIX # WorkDir: /home/http/mrtg # for Debian WorkDir: /var/www/mrtg # or for NT # WorkDir: c:\mrtgdata ### Global Defaults # to get bits instead of bytes and graphs growing to the right # Options[_]: growright, bits EnableIPv6: no ###################################################################### # System: mrtg-test # Description: Linux mrtg-test 2.6.32-4-pve #1 SMP Tue Mar 29 09:08:37 CEST 2011 i686 # Contact: Root <root@localhost> (configure /etc/snmp/snmpd.local.conf) # Location: Unknown (configure /etc/snmp/snmpd.local.conf) ###################################################################### ### Interface 1 >> Descr: 'lo' | Name: 'lo' | Ip: '127.0.0.1' | Eth: '' ### ### The following interface is commented out because: ### * it is a Software Loopback interface # #Target[localhost_lo]: #lo:public@localhost: #SetEnv[localhost_lo]: MRTG_INT_IP="127.0.0.1" MRTG_INT_DESCR="lo" #MaxBytes[localhost_lo]: 1250000 #Title[localhost_lo]: Traffic Analysis for lo -- mrtg-test #PageTop[localhost_lo]: <h1>Traffic Analysis for lo -- mrtg-test</h1> # <div id="sysdetails"> # <table> # <tr> # <td>System:</td> # <td>mrtg-test in Unknown (configure /etc/snmp/snmpd.local.conf)</td> # </tr> # <tr> # <td>Maintainer:</td> # <td>Root <root@localhost> (configure /etc/snmp/snmpd.local.conf)</td> # </tr> # <tr> # <td>Description:</td> # <td>lo </td> # </tr> # <tr> # <td>ifType:</td> # <td>softwareLoopback (24)</td> # </tr> # <tr> # <td>ifName:</td> # <td>lo</td> # </tr> # <tr> # <td>Max Speed:</td> # <td>1250.0 kBytes/s</td> # </tr> # <tr> # <td>Ip:</td> ##### <td>127.0.0.1 (localhost.localdomain)</td> # </tr> # </table> # </div> # ### Interface 2 >> Descr: 'venet0' | Name: 'venet0' | Ip: '192.168.0.1' | Eth: '' ### ### The following interface is commented out because: ### * has a speed of 0 which makes no sense # Target[localhost_venet0]: #venet0:public@localhost: SetEnv[localhost_venet0]: MRTG_INT_IP="61.189.20.55" MRTG_INT_DESCR="venet0" MaxBytes[localhost_venet0]: 125000 Title[localhost_venet0]: Traffic Analysis for venet0 -- mrtg-test PageTop[localhost_venet0]: <h1>Traffic Analysis for venet0 -- mrtg-test</h1> <div id="sysdetails"> <table> <tr> <td>System:</td> <td>mrtg-test in Unknown (configure /etc/snmp/snmpd.local.conf)</td> </tr> <tr> <td>Maintainer:</td> <td>Root <root@localhost> (configure /etc/snmp/snmpd.local.conf)</td> </tr> <tr> <td>Description:</td> <td>venet0 </td> </tr> <tr> <td>ifType:</td> <td>Other (1)</td> </tr> <tr> <td>ifName:</td> <td>venet0</td> </tr> <tr> <td>Max Speed:</td> <td>0.0 Bytes/s</td> </tr> <tr> <td>Ip:</td> <td>192.168.0.1</td> </tr> </table> </div>
修改完这个配置文件之后就可以用刚才访问的地址来查看网络的监控状态了。
下面在几个其他的监控:
二安装CPU负载监视[默认5分钟采集一次]
切换到超级用户:
sudo -sH
安装软件:
apt-get install sysstat
建立CPU脚本:
mkdir /opt/mrtg
vim /opt/mrtg/mrtg.cpu
#!/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
使脚本可以执行:
chmod +755 /opt/mrtg/mrtg.cpu
修改 /etc/mrtg.cfg 在文件最后加入cpu项目
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]: <H1>CPU Loading</H1>
重新生成索引页面:
indexmaker /etc/mrtg.cfg > /var/www/mrtg/index.html
三安装WWW连接数监视[默认5分钟采集一次]
切换到超级用户:
sudo -sH
建立WWW脚本:
mkdir /opt/mrtg
vim /opt/mrtg/mrtg.www
#!/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
使脚本可以执行:
chmod +755 /opt/mrtg/mrtg.www
修改 /etc/mrtg.cfg 在文件最后加入www项目
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]: <H1> WWW Connect </H1>
重新生成索引页面:
indexmaker /etc/mrtg.cfg > /var/www/mrtg/index.html
四安装内存使用监视[默认5分钟采集一次]
切换到超级用户:
sudo -sH
建立RAM脚本:
mkdir /opt/mrtg
vim /opt/mrtg/mrtg.ram
#!/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
使脚本可以执行:
chmod +755 /opt/mrtg/mrtg.ram
修改 /etc/mrtg.cfg 在文件最后加入ram项目
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]:<H1>Memory</H1>
重新生成索引页面:
indexmaker /etc/mrtg.cfg > /var/www/mrtg/index.html
五安装FTP连接数监视[默认5分钟采集一次]
切换到超级用户:
sudo -sH
建立FTP脚本:
mkdir /opt/mrtg
vim /opt/mrtg/mrtg.ftp
#!/bin/bash
all=`netstat -a | grep ftp|awk '{print $5}'|sort | wc -l|awk '{print$1 - 1}'`
user=`netstat -a | grep ftp|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
使脚本可以执行:
chmod +755 /opt/mrtg/mrtg.ftp
修改 /etc/mrtg.cfg 在文件最后加入ftp项目
Target[ftp]: `/opt/mrtg/mrtg.ftp`
MaxBytes[ftp]: 500
Options[ftp]: nopercent, growright
YLegend[ftp]: Online Users
ShortLegend[ftp]: %
LegendI[ftp]: Connect :
LegendO[ftp]: Online :
Title[ftp]: FTP Connect
PageTop[ftp]: <H1> FTP Connect </H1>
重新生成索引页面:
indexmaker /etc/mrtg.cfg > /var/www/mrtg/index.html
六安装CPU温度监视[默认5分钟采集一次] 切换到超级用户: sudo -sH 安装软件: apt-get install mbmon 建立CPU温度脚本: mkdir /opt/mrtg vim /opt/mrtg/mrtg.temp #!/bin/bash /usr/bin/mbmon -c 1 -i -T 4 -u -n exit 0 使脚本可以执行: chmod +755 /opt/mrtg/mrtg.temp 修改 /etc/mrtg.cfg 在文件最后加入cpu项目 Target[temp]: `/opt/mrtg/mrtg.temp` MaxBytes[temp]: 100 Title[temp]: CPU Temperature PageTop[temp]: <H1>CPU Temperature</H1> Options[temp]: gauge,absolute,unknaszero,growright YLegend[temp]: Temperature(C) ShortLegend[temp]: (C) Legend1[temp]: CPU Temperature Legend2[temp]: M/B Temperature LegendI[temp]: CPU Temp. LegendO[temp]: M/B Temp. 重新生成索引页面: indexmaker /etc/mrtg.cfg > /var/www/mrtg/index.html
七硬盘读写监视[默认5分钟采集一次]
切换到超级用户:
sudo -sH
建立硬盘读写脚本:
mkdir /opt/mrtg
vim /opt/mrtg/mrtg.disk
#!/bin/bash
hd=sda
disk=/dev/$hd
UPtime=`/usr/bin/uptime |awk '{print $3""$4""$5}'`
KBread_sec=`iostat -x $disk|grep $hd |awk '{print 8$}'`
KBwrite_sec=`iostat -x $disk|grep $hd |awk '{print 9$}'`
echo $KBread_sec
echo $KBwrite_sec
echo $UPtime
hostname
使脚本可以执行:
chmod +755 /opt/mrtg/mrtg.disk
修改 /etc/mrtg.cfg 在文件最后加入硬盘读写
Target[disk]: `/opt/mrtg/mrtg.disk`
Title[disk]: Disk HDA I/O Utilization Report
#Unscaled[disk]: dwym
MaxBytes[disk]: 10240000
PageTop[disk]: <H1>Disk I/O Utilization Report</H1>
kmg[disk]: KB,MB,GB
LegendI[disk]: Disk I/O KBread/sec
LegendO[disk]: Disk I/O KBwrite/sec
Legend1[disk]: Disk I/O KBread/sec
Legend2[disk]: Disk I/O KBwrite/sec
YLegend[disk]: Megabytes
ShortLegend[disk]: &
Options[disk]: growright,gauge,nopercent
重新生成索引页面:
indexmaker /etc/mrtg.cfg > /var/www/mrtg/index.html
九:交换分区
切换到超级用户:
sudo -sH
建立交换分区脚本:
mkdir /opt/mrtg
vim /opt/mrtg/mrtg.swap
#!/bin/bash
# This script to monitor the swap usage.
totalswap=`/usr/bin/free |grep Swap |awk '{print $2}'`
usedswap=`/usr/bin/free |grep Swap |awk '{print $3}'`
echo "$totalswap"
echo "$usedswap"
使脚本可以执行:
chmod +755 /opt/mrtg/mrtg.swap
修改 /etc/mrtg.cfg 在文件最后加入
Target[swap]: `/opt/mrtg/mrtg.swap`
MaxBytes[swap]: 2048000
Title[swap]:Memory State of Server
ShortLegend[swap]: &
kmg[swap]:kB,MB
kilo[swap]:1024
YLegend[swap]: Swap Usage
Legend1[swap]: Total Swap
Legend2[swap]: Used Swap
LegendI[swap]: Total Swap
LegendO[swap]: Used Swap
Options[swap]: growright,gauge,nopercent
PageTop[swap]:<H1>Swap</H1>
重新生成索引页面:
indexmaker /etc/mrtg.cfg > /var/www/mrtg/index.html
下面的是全套mrtg.conf的配置文件
# Created by # /usr/bin/cfgmaker public@localhost ### Global Config Options # for UNIX # WorkDir: /home/http/mrtg # for Debian WorkDir: /var/www/mrtg # or for NT # WorkDir: c:\mrtgdata ### Global Defaults # to get bits instead of bytes and graphs growing to the right # Options[_]: growright, bits EnableIPv6: no ###################################################################### # System: mrtg-test # Description: Linux mrtg-test 2.6.32-4-pve #1 SMP Tue Mar 29 09:08:37 CEST 2011 i686 # Contact: Root <root@localhost> (configure /etc/snmp/snmpd.local.conf) # Location: Unknown (configure /etc/snmp/snmpd.local.conf) ###################################################################### ### Interface 1 >> Descr: 'lo' | Name: 'lo' | Ip: '127.0.0.1' | Eth: '' ### ### The following interface is commented out because: ### * it is a Software Loopback interface # #Target[localhost_lo]: #lo:public@localhost: #SetEnv[localhost_lo]: MRTG_INT_IP="127.0.0.1" MRTG_INT_DESCR="lo" #MaxBytes[localhost_lo]: 1250000 #Title[localhost_lo]: Traffic Analysis for lo -- mrtg-test #PageTop[localhost_lo]: <h1>Traffic Analysis for lo -- mrtg-test</h1> # <div id="sysdetails"> # <table> # <tr> # <td>System:</td> # <td>mrtg-test in Unknown (configure /etc/snmp/snmpd.local.conf)</td> # </tr> # <tr> # <td>Maintainer:</td> # <td>Root <root@localhost> (configure /etc/snmp/snmpd.local.conf)</td> # </tr> # <tr> # <td>Description:</td> # <td>lo </td> # </tr> # <tr> # <td>ifType:</td> # <td>softwareLoopback (24)</td> # </tr> # <tr> # <td>ifName:</td> # <td>lo</td> # </tr> # <tr> # <td>Max Speed:</td> # <td>1250.0 kBytes/s</td> # </tr> # <tr> # <td>Ip:</td> ##### <td>127.0.0.1 (localhost.localdomain)</td> # </tr> # </table> # </div> # ### Interface 2 >> Descr: 'venet0' | Name: 'venet0' | Ip: '61.189.20.55' | Eth: '' ### ### The following interface is commented out because: ### * has a speed of 0 which makes no sense # Target[localhost_venet0]: #venet0:public@localhost: SetEnv[localhost_venet0]: MRTG_INT_IP="61.189.20.55" MRTG_INT_DESCR="venet0" MaxBytes[localhost_venet0]: 125000 Title[localhost_venet0]: Traffic Analysis for venet0 -- mrtg-test PageTop[localhost_venet0]: <h1>Traffic Analysis for venet0 -- mrtg-test</h1> <div id="sysdetails"> <table> <tr> <td>System:</td> <td>mrtg-test in Unknown (configure /etc/snmp/snmpd.local.conf)</td> </tr> <tr> <td>Maintainer:</td> <td>Root <root@localhost> (configure /etc/snmp/snmpd.local.conf)</td> </tr> <tr> <td>Description:</td> <td>venet0 </td> </tr> <tr> <td>ifType:</td> <td>Other (1)</td> </tr> <tr> <td>ifName:</td> <td>venet0</td> </tr> <tr> <td>Max Speed:</td> <td>0.0 Bytes/s</td> </tr> <tr> <td>Ip:</td> <td>61.189.20.55 (mrtg-test.vps.207.bfidc.com)</td> </tr> </table> </div> ##################### 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]: <H1>CPU Loading</H1> ##################### 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]: <H1> WWW Connect </H1> ##################### 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]:<H1>Memory</H1> ##################### Target[temp]: `/opt/mrtg/mrtg.temp` MaxBytes[temp]: 100 Title[temp]: CPU Temperature PageTop[temp]: <H1>CPU Temperature</H1> Options[temp]: gauge,absolute,unknaszero,growright YLegend[temp]: Temperature(C) ShortLegend[temp]: (C) Legend1[temp]: CPU Temperature Legend2[temp]: M/B Temperature LegendI[temp]: CPU Temp. LegendO[temp]: M/B Temp. ##################### Target[disk]: `/opt/mrtg/mrtg.disk` Title[disk]: Disk HDA I/O Utilization Report #Unscaled[disk]: dwym MaxBytes[disk]: 10240000 PageTop[disk]: <H1>Disk I/O Utilization Report</H1> kmg[disk]: KB,MB,GB LegendI[disk]: Disk I/O KBread/sec LegendO[disk]: Disk I/O KBwrite/sec Legend1[disk]: Disk I/O KBread/sec Legend2[disk]: Disk I/O KBwrite/sec YLegend[disk]: Megabytes ShortLegend[disk]: & Options[disk]: growright,gauge,nopercent ##################### ##################### Target[swap]: `/opt/mrtg/mrtg.swap` MaxBytes[swap]: 2048000 Title[swap]:Memory State of Server ShortLegend[swap]: & kmg[swap]:kB,MB kilo[swap]:1024 YLegend[swap]: Swap Usage Legend1[swap]: Total Swap Legend2[swap]: Used Swap LegendI[swap]: Total Swap LegendO[swap]: Used Swap Options[swap]: growright,gauge,nopercent PageTop[swap]:<H1>Swap</H1> #2011/12/3
看完了要说点啥么?