这几天把上周弄的MRTG流量测试的工具加强了一下,借鉴了网上一些大牛的资料,自己做了一下测试,现在把操作步骤留存如下:(当前系统CentOS5.5 mrtg ver.2.17.0)

一些依赖的软件包检查 :
[root@LinuxTest ~]# rpm -qa|grep gd
gd-2.0.33-9.4.el5_4.2
gd-devel-2.0.33-9.4.el5_4.2
[root@LinuxTest ~]# rpm -qa|grep perl
perl-5.8.8-18.el5
mod_perl-2.0.4-6.el5
[root@LinuxTest ~]# rpm -qa|grep libp
libpng-1.2.10-7.1.el5_5.3
libpng-devel-1.2.10-7.1.el5_5.3
[root@LinuxTest ~]# rpm -qa|grep zlib
zlib-1.2.3-3
zlib-devel-1.2.3-3
[root@LinuxTest ~]# rpm -qa|grep gcc
libgcc-4.1.2-48.el5
libgcc-4.1.2-48.el5
gcc-4.1.2-48.el5

最近到官方网站 http://oss.oetiker.ch/mrtg,有个最新的版本mrtg-2.16.7,随即Down下并安装,步骤如下: 
[root@LinuxTest test]#wget http://oss.oetiker.ch/mrtg/pub/mrtg-2.17.0.zip
[root@LinuxTest test]# unzip mrtg-2.17.0.zip
 [root@LinuxTest test]# cd mrtg-2.17.0
[root@LinuxTest mrtg-2.17.0]# ./configure --prefix=/usr/local/mrtg-2
[root@LinuxTest mrtg-2.17.0]# make
[root@LinuxTest mrtg-2.17.0]# make install 
 注意:sar命令不可识别的系统,请安装sysstat工具包
到现在我们就已经正确地安装了 MRTG系统。
配置 SNMP服务
对于不同的设备,配置 SNMP支持的方法是不一致的,具体请参考设备的随机文档,一般里 面都有详细的介绍。这里我们讨论在Linux环境下配置SNMP服务器,以实现对本机流出流
入数据的分析和报表 (我的应用环境是使用Linux带动一个小型局域网上网,监控本机进出 流量)。
在 linux环境下安装snmp软件包是很容易的,只需要安装相应的软件包即可:
[root@LinuxTest ~]# rpm -qa|grep snmp
net-snmp-libs-5.3.2.2-9.el5_5.1
net-snmp-5.3.2.2-9.el5_5.1
net-snmp-utils-5.3.2.2-9.el5_5.1 (snmpwalk等大部分snmp的部分都在这个包内)
[root@LinuxTest ~]# /etc/rc.d/init.d/snmpd start
Starting snmpd: [ OK ]

如果命令输出如上所示,就表示 snmp服务器启动正常。
为了配合 mrtg使用,还要修改snmpd的配置,以使其允许mrtg读取其interface(网络接口) 流量数据。
vi /etc/snmp/snmpd.conf
将下面这个的 #注给去掉
#view mib2 included .iso.org.dod.internet.mgmt.mib-2 fc
 
然后将: access notConfigGroup "" any noauth exact systemview none none
修改为: access notConfigGroup "" any noauth exact mib2 none none
在 55行左右加入:
view    systemview    included   .1.3.6.1.2.1.2
然后再重新启动 snmpd:
/etc/rc.d/init.d/snmpd restart
注意:尽量更改掉snmp的团体字符串,以防止别人通过默认字符串探测,导致信息泄漏。将下面这段中的community字段,即public改成较复杂的字符串就可以了,不要太复杂,不然导致snmp信息读取失败的:),要小心Linux下符号解释成命令哦
com2sec notConfigUser  default       Youkipublic

生成 MRTG配置文件
# mkdir /usr/local/mrtg-2/cfg
配置文件:/usr/local/mrtg-2/cfg/ mrtg.cfg
# cd /usr/local/mrtg-2/bin
#mkdir /var/www/html/mrtg
# /usr/local/mrtg-2/bin/cfgmaker --global 'WorkDir: /var/www/html/mrtg' --global 'Options[_]: bits,growright'  --output /usr/local/mrtg-2/cfg/mrtg.cfg Youkipuplic@192.168.1.210
注意,在Linux环境下,如果想以Bytes/秒显示的话,不要加上'bits'参数,也不要替换成bytes,默认就是Bytes的,替换后反而会出错!

 以上就完成了服务器流量的监控配置,接下来再生成几个pl文件,分别用来监控CPU、硬盘空间、内存

生成CPU利用率监控的pl文件:

#vi /usr/local/mrtg-2/bin/cpu.pl

在文件编辑状态下,填充以下内容:

#!/usr/bin/perl
system ("/usr/bin/sar -u 1 3|grep Average >cpu_info_file"); #sar 输出写入文件cpu_info_file
open (CPUINFO,"cpu_info_file"); #打开cpu_info_file 文件
@cpuinfo=<CPUINFO>; # 读去文件内容
close (CPUINFO); #关闭文件
foreach $line(@cpuinfo) { #分别获得我们需要的
@cpustatus=split(/ +/,$line); #每一个数值
}
$cpuused=$cpustatus[2]+$cpustatus[4];
$cpuidle=$cpustatus[5];
print "$cpuused\n"; #输出两个数值
print "$cpuidle";
system ("uptime");
system ("uname -n");
############### By Vitter :
vitter@safechina.net#####################

 

#vi /usr/local/mrtg-2/bin/df.pl

#!/usr/bin/perl
# This script was written on CentOS5.5, it assumes that the command
# output(df -kl) looks like this:
#文件系统               1K-块        已用     可用 已用% 挂载点
#/dev/sda3            139206768  55292128  76729168  42% /
#/dev/sda1              2030736     42164   1883752   3% /boot
#tmpfs                  1025084    531940    493144  52% /dev/shm
#
# In which case, this script returns :
#
#142262589
#55866236
# when run.
foreach $filesystem (`df -kl | grep -v "Filesystem"`)
{
@df = split(/\s+/,$filesystem);
$total += $df[1];
$usage += $df[2];
}
print "$total\n";
print "$usage\n";
system ("uptime");
system ("uname -n");

内存使用状态的mem.pl文件内容为:

#vi /usr/local/mrtg-2/bin/mem.pl

#!/usr/bin/perl
system ("/usr/bin/free -m | grep Mem >mem_info_file");
open (MEMINFO,"mem_info_file");
@meminfo=<MEMINFO>;
close (MEMINFO);
foreach $line(@meminfo) {
@memstatus=split(/ +/,$line);
}
$memused=$memstatus[2];
$memtotal=$memstatus[1];
print "$memused\n";
print "$memtotal\n";
system ("uptime");
system ("uname -n");
########## By Vitter
vitter@safechina.net ##################

建立好以上三个pl文件后,要给文件加上可执行属性:

#chmod +x /usr/local/mrtg-2/bin/cpu.pl /usr/local/mrtg-2/bin/df.pl /usr/local/mrtg-2/bin/mem.pl 

打开mrtg.cfg文件,将CPU、硬盘、内存的配置加进去:

# Created by test
# /usr/local/mrtg-2/bin/cfgmaker --global "WorkDir: /var/www/html/mrtg" --global "Options[_]: bits,growright" --output /usr/local/mrtg-2/cfg/mrtg.cfg
Youkipuplic@192.168.1.210


### Global Config Options

#  for UNIX
# WorkDir: /home/http/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
WorkDir: /var/www/html/mrtg
Options[_]: bits,growright
Xsize[_]: 500
Ysize[_]: 300
Ytics[_]: 30

######################################################################
# System: OAtest
# Description: Linux OAtest 2.6.18-194.el5 #1 SMP Fri Apr 2 14:58:14 EDT 2010 x86_64
# Contact: Root <
root@192.168.1.210> (configure /etc/snmp/snmp.local.conf)
# Location: Unknown (edit /etc/snmp/snmpd.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[192.168.1.210_1]: 1:Youkipuplic@192.168.1.210:
# SetEnv[192.168.1.210_1]: MRTG_INT_IP="127.0.0.1" MRTG_INT_DESCR="lo"
# MaxBytes[192.168.1.210_1]: 1250000
# Title[192.168.1.210_1]: Traffic Analysis for 1 -- OAtest
# PageTop[192.168.1.210_1]: <h1>Traffic Analysis for 1 -- OAtest</h1>
#               <div id="sysdetails">
#                       <table>
#                               <tr>
#                                       <td>System:</td>
#                                       <td>OAtest in Unknown (edit /etc/snmp/snmpd.conf)</td>
#                               </tr>
#                               <tr>
#                                       <td>Maintainer:</td>
#                                       <td>Root &lt;root@192.168.1.210&gt; (configure /etc/snmp/snmp.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>10.0 Mbits/s</td>
#                               </tr>
#                               <tr>
#                                       <td>Ip:</td>
#                                       <td>127.0.0.1 (192.168.1.210.localdomain)</td>
#                               </tr>
#                       </table>
#               </div>


### Interface 2 >> Descr: 'eth0' | Name: 'eth0' | Ip: '192.168.1.210' | Eth: '00-e0-4c-b8-48-88' ###

Target[192.168.1.210_2]: 2:Youkipuplic@192.168.1.210:
SetEnv[192.168.1.210_2]: MRTG_INT_IP="192.168.1.210" MRTG_INT_DESCR="eth0"
MaxBytes[192.168.1.210_2]: 104857600
Title[192.168.1.210_2]: Traffic Analysis for 2 -- OAtest
PageTop[192.168.1.210_2]: <h1>TestServer Traffic Analysis</h1>
                <div id="sysdetails">
                        <table>
                                <tr>
                                        <td>System:</td>
                                        <td>OAtest in Unknown (edit /etc/snmp/snmpd.conf)</td>
                                </tr>
                                <tr>
                                        <td>Maintainer:</td>
                                        <td>Root &lt;root@192.168.1.210&gt; (configure /etc/snmp/snmp.local.conf)</td>
                                </tr>
                                <tr>
                                        <td>Description:</td>
                                        <td>eth0  </td>
                                </tr>
                                <tr>
                                        <td>ifType:</td>
                                        <td>ethernetCsmacd (6)</td>
                                </tr>
                                <tr>
                                        <td>ifName:</td>
                                        <td>eth0</td>
                                </tr>
                                <tr>
                                        <td>Max Speed:</td>
                                        <td>100.0 Mbits/s</td>
                                </tr>
                                <tr>
                                        <td>Ip:</td>
                                        <td>192.168.1.210 (
www.monitor.com)</td>
                                </tr>
                        </table>
                </div>


### Interface 3 >> Descr: 'sit0' | Name: 'sit0' | Ip: '' | Eth: '' ###
### The following interface is commented out because:
### * it is administratively DOWN
### * it is operationally DOWN
### * has a speed of 0 which makes no sense
#
# Target[192.168.1.210_3]: 3:Youkipuplic@192.168.1.210:
# SetEnv[192.168.1.210_3]: MRTG_INT_IP="" MRTG_INT_DESCR="sit0"
# MaxBytes[192.168.1.210_3]: 0
# Title[192.168.1.210_3]: Traffic Analysis for 3 -- OAtest
# PageTop[192.168.1.210_3]: <h1>Traffic Analysis for 3 -- OAtest</h1>
#               <div id="sysdetails">
#                       <table>
#                               <tr>
#                                       <td>System:</td>
#                                       <td>OAtest in Unknown (edit /etc/snmp/snmpd.conf)</td>
#                               </tr>
#                               <tr>
#                                       <td>Maintainer:</td>
#                                       <td>Root &lt;root@192.168.1.210&gt; (configure /etc/snmp/snmp.local.conf)</td>
#                               </tr>
#                               <tr>
#                                       <td>Description:</td>
#                                       <td>sit0  </td>
#                               </tr>
#                               <tr>
#                                       <td>ifType:</td>
#                                       <td>Encapsulation Interface (131)</td>
#                               </tr>
#                               <tr>
#                                       <td>ifName:</td>
#                                       <td>sit0</td>
#                               </tr>
#                               <tr>
#                                       <td>Max Speed:</td>
#                                       <td>0.0 bits/s</td>
#                               </tr>
#                       </table>
#               </div>
#---------CPU监控配置如下-------------------------------
Target[192.168.1.210_cpu]: `/usr/local/mrtg-2/bin/cpu.pl`
Xsize[192.168.1.210_cpu]: 500
Ysize[192.168.1.210_cpu]: 300
Ytics[192.168.1.210_cpu]: 30
MaxBytes[192.168.1.210_cpu]:100
Title[192.168.1.210_cpu]:CPU State
PageTop[192.168.1.210_cpu]:<H1>CPU State of TestServer</H1>
ShortLegend[192.168.1.210_cpu]: %
YLegend[192.168.1.210_cpu]: CPU (%)
Legend1[192.168.1.210_cpu]: Used
Legend2[192.168.1.210_cpu]: Total
LegendI[192.168.1.210_cpu]: CPU Used
LegendO[192.168.1.210_cpu]: CPU IDEL
Options[192.168.1.210_cpu]: growright,gauge,nopercent
#---------内存监控配置如下-------------------------------
Target[192.168.1.210_mem]: `/usr/local/mrtg-2/bin/mem.pl`
Xsize[192.168.1.210_mem]:500
Ysize[192.168.1.210_mem]:300
Ytics[192.168.1.210_mem]:30
MaxBytes[192.168.1.210_mem]: 2002#设置成你自己内存最大值,运行mem.pl的结果填到这里
Title[192.168.1.210_mem]:Memory State of TestServer
PageTop[192.168.1.210_mem]:<H1>Memory State of TestServer</H1>
ShortLegend[192.168.1.210_mem]: B
kmg[192.168.1.210_mem]: M
YLegend[192.168.1.210_mem]: Memory Usage
Legend1[192.168.1.210_mem]: Used
Legend2[192.168.1.210_mem]: Total
LegendI[192.168.1.210_mem]: Used
LegendO[192.168.1.210_mem]: Total
Options[192.168.1.210_mem]: growright,gauge,nopercent
#---------磁盘空间监控配置如下-------------------------
Target[192.168.1.210_df]: `/usr/local/mrtg-2/bin/df.pl`
Xsize[192.168.1.210_df]:500
Ysize[192.168.1.210_df]:300
Ytics[192.168.1.210_df]:30
Title[192.168.1.210_df]: TestSERVER Disk Space Used
Unscaled[192.168.1.210_df]: dwym
MaxBytes[192.168.1.210_df]: 142262589#设置成你自己硬盘的空间,运行df.pl的结果填到这里
PageTop[192.168.1.210_df]: <H1>TestSERVER Disk Space Megabytes used</H1>
kmg[192.168.1.210_df]: KB,MB,GB
LegendI[192.168.1.210_df]: Total Disk Space
LegendO[192.168.1.210_df]: Used Disk Space
Legend1[192.168.1.210_df]: Total Disk Space
Legend2[192.168.1.210_df]: Used Disk Space
YLegend[192.168.1.210_df]: Disk Used
ShortLegend[192.168.1.210_df]: &
Options[192.168.1.210_df]: growright,gauge,nopercent

现在利用上面的配置文件来创建监控服务器的Web页,如下操作:

生成mrtg的index文件

# /usr/local/mrtg-2/bin/indexmaker --output  /var/www/html/mrtg/index.html --title "TestServer Performance Monitoring" /usr/local/mrtg-2/cfg/mrtg.cfg

<VirtualHost mrgt.yourdomanin.com>
        servername mrgt.yourdomanin.com
        documentroot /Monitor/mrtg
        customlog logs/mrgt.yourdomanin.com common
</VirtualHost>
 
<directory "/var/www/html/mrtg/">
        options followsymlinks  includes
        allowoverride none
        order allow,deny
        allow from all
        authname "MRTG流量察看"
        authtype basic
        authuserfile /data/apache2/passdir/mrtgpass
        require user 4ujk
</directory>
生成密码:
mkdir /data/apache2/passdir/
/data/apache2/bin/htpasswd -bc /data/apache2/passdir/mrtgpass username userpass
 以上红色部分参考了网上某位仁兄的操作,以加密方式访问流量监控页面。不过我用的是resin,就没有进行设置了,这里仅作记录。
如果不用indexmaker 自定义监控显示页面的话,在使用 cfgmaker参数生成的页面包含的信息是比较全面的,但生成html名字是自己指定的 IP地址为前缀的。
不过,我用的是resin-2.1.14,在配置文件里里,加入了以下内容,然后客户端hosts文件里加上www.monitor.com 的映射,即可访问监控页面了:
 
 
  
  1. <host id='www.monitor.com'> 
  2.         <app-dir>/var/www/html/mrtg</app-dir> 
  3.         <web-app id='/'> 
  4.             <class-update-interval id='2'/> 
  5.             <welcome-file-list>index.html</welcome-file-list> 
  6.             <cache-mapping url-pattern='/' expires='2s'/> 
  7.            </web-app> 
  8. </host> 
更新信息 .
env LANG=C /usr/local/mrtg-2/bin/mrtg /usr/local/mrtg-2/cfg/mrtg.cfg
此时,Monitor主页面应有四幅图像显示,用鼠标点击任意一个图像,将进入不时的时间段报表页。

最后,让系统每隔5分钟执行一次mrtg,生成新的MRTG流量图
# crontab –e
*/5 * * * * env LANG=C /usr/local/mrtg/bin/mrtg /usr/local/mrtg/cfg/mrtg.cfg