这几天把上周弄的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
perl-5.8.8-18.el5
mod_perl-2.0.4-6.el5
libpng-1.2.10-7.1.el5_5.3
libpng-devel-1.2.10-7.1.el5_5.3
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
net-snmp-libs-5.3.2.2-9.el5_5.1
net-snmp-5.3.2.2-9.el5_5.1
Starting snmpd: [ OK ]
如果命令输出如上所示,就表示 snmp服务器启动正常。
将下面这个的 #注给去掉
#view mib2 included .iso.org.dod.internet.mgmt.mib-2 fc
view systemview included .1.3.6.1.2.1.2
生成 MRTG配置文件
以上就完成了服务器流量的监控配置,接下来再生成几个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=
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 :
#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=
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
建立好以上三个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 [email protected]
### 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 <[email protected]> (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:[email protected]:
# 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]: Traffic Analysis for 1 -- OAtest
#
#
#
# System:
# OAtest in Unknown (edit /etc/snmp/snmpd.conf)
#
#
# Maintainer:
# Root <[email protected]> (configure /etc/snmp/snmp.local.conf)
#
#
# Description:
# lo
#
#
# ifType:
# softwareLoopback (24)
#
#
# ifName:
# lo
#
#
# Max Speed:
# 10.0 Mbits/s
#
#
# Ip:
# 127.0.0.1 (192.168.1.210.localdomain)
#
#
#
### 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:[email protected]:
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]: TestServer Traffic Analysis
System:
OAtest in Unknown (edit /etc/snmp/snmpd.conf)
Maintainer:
Root <[email protected]> (configure /etc/snmp/snmp.local.conf)
Description:
eth0
ifType:
ethernetCsmacd (6)
ifName:
eth0
Max Speed:
100.0 Mbits/s
Ip:
192.168.1.210 (www.monitor.com) >
### 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:[email protected]:
# 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]: Traffic Analysis for 3 -- OAtest
#
#
#
# System:
# OAtest in Unknown (edit /etc/snmp/snmpd.conf)
#
#
# Maintainer:
# Root <[email protected]> (configure /etc/snmp/snmp.local.conf)
#
#
# Description:
# sit0
#
#
# ifType:
# Encapsulation Interface (131)
#
#
# ifName:
# sit0
#
#
# Max Speed:
# 0.0 bits/s
#
#
#
#---------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]:CPU State of TestServer
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]:Memory State of TestServer
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]: TestSERVER Disk Space Megabytes used
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
servername mrgt.yourdomanin.com
documentroot /Monitor/mrtg
customlog logs/mrgt.yourdomanin.com common
options followsymlinks includes
allowoverride none
order allow,deny
allow from all
authname "MRTG流量察看"
authtype basic
authuserfile /data/apache2/passdir/mrtgpass
require user 4ujk
mkdir /data/apache2/passdir/
/data/apache2/bin/htpasswd -bc /data/apache2/passdir/mrtgpass username userpass
- <host id='www.monitor.com'>
- <app-dir>/var/www/html/mrtgapp-dir>
- <web-app id='/'>
- <class-update-interval id='2'/>
- <welcome-file-list>index.htmlwelcome-file-list>
- <cache-mapping url-pattern='/' expires='2s'/>
- web-app>
- host>
env LANG=C /usr/local/mrtg-2/bin/mrtg /usr/local/mrtg-2/cfg/mrtg.cfg
最后,让系统每隔5分钟执行一次mrtg,生成新的MRTG流量图
# crontab –e
*/5 * * * * env LANG=C /usr/local/mrtg/bin/mrtg /usr/local/mrtg/cfg/mrtg.cfg