如果你是服务器管理员,MRTG会为你做很多事情,如网络流量监控,CPU使用情况及温度监控,内存监控,Web连接数量监控等等,当然,并不是说MRTG有多强大,他也是依赖于snmp服务,也有很多人更喜欢高级点的cacti,但MRTG优点还是很多的,这里就介绍下使用mrtg来监视服务器的性能及状态.
一:安装必须的软件进行流量监控:apt-get install apache2 (这个也是可以用Nginx,这里以apache2为例子,如果已经安装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/ (所有都用这个地址访问)
中文化:# vi /etc/mrtg.cfg
添加 Language: chinese
二:安装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]: CPU Loading
重新生成索引页面:indexmaker /etc/mrtg.cfg > /var/www/mrtg/index.html
三:安装WWW连接数监视
切换到超级用户: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]: WWW Connect
重新生成索引页面:indexmaker /etc/mrtg.cfg > /var/www/mrtg/index.html
四:安装内存使用监视
切换到超级用户: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]:Memory
重新生成索引页面:indexmaker /etc/mrtg.cfg > /var/www/mrtg/index.html
五:安装CPU温度监视
切换到超级用户: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]: CPU Temperature
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
六:硬盘读写监视
切换到超级用户: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]: Disk I/O Utilization Report
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.df
#!/usr/bin/perl
foreach $filesystem (`df -kl | grep -v “Filesystem”`)
{
@df = split(//s+/,$filesystem);
$total += $df[1];
$usage += $df[2];
}
print “$total/n”;
print “$usage/n”;
hostname
使脚本可以执行:chmod 755 /opt/mrtg/mrtg.df
修改 /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]:Swap
重新生成索引页面:indexmaker /etc/mrtg.cfg > /var/www/mrtg/index.html
最后,使用htpasswd保护MRTG
1>,首先确定apache是否加载mod_auth.so模块,查看/etc/apache2/mods-enable目录下是否有auth_basic.load,
默认情况我的Debian已经加载此模块。
2>,编辑/etc/apache2/sites-enable/000-default,直接在此文件最下面添加以下内容:
Options Indexes FollowSymLinks MultiViews ExecCGI
AllowOverride AuthConfig
Order allow,deny
Allow from all
3>,假如我们要设置保护的目录是/var/www/mrtg,那么在/var/www/mrtg新增一个.htaccess文件,添加以下内容:AuthName “Admin zone”
AuthType “Basic”
AuthUserFile “/var/http.pw”
require valid-user
4,添加用户及密码,执行以下命令后要求输入两次密码即可,如果要再添加第多个使用者,不需要加参数”-c”debian#htpasswd -c /var/http.pw username
MRTG運作是認IP或URL,不是認MAC………