MRTG

利用Mrtg监控服务器CPU、硬盘、内存及流量 2010-12-23 22:06:53
标签:monitor mrtg 性能监控
版权声明:原创作品,如需转载,请与作者联系。否则将追究法律责任。
这几天把上周弄的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 https://www.360docs.net/doc/f212149231.html,.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=; # 读去文件内容
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@https://www.360docs.net/doc/f212149231.html,#####################



#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 vitter@https://www.360docs.net/doc/f212149231.html, ##################

建立好以上三个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 (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]:

Traffic Analysis for 1 -- OAtest


#

#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#

System:OAtest in Unknown (edit /etc/snmp/snmpd.conf)
Maintainer:Root <root@192.168.1.210> (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: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]:

TestServer Traffic Analysis
































System:OAtest in Unknown (edit /etc/snmp/snmpd.conf)
Maintainer:Root <root@192.168.1.210> (configure /etc/snmp/snmp.local.conf)
Description:eth0
ifType:ethernetCsmacd (6)
ifName:eth0
Max Speed:100.0 Mbits/s
Ip:192.168.1.210 (https://www.360docs.net/doc/f212149231.html,)




### 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]:

Traffic Analysis for 3 -- OAtest


#

#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
System:OAtest in Unknown (edit /etc/snmp/snmpd.conf)
Maintainer:

d>
#

Root <root@192.168.1.210> (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 https://www.360docs.net/doc/f212149231.html,
documentroot /Monitor/mrtg
customlog logs/https://www.360docs.net/doc/f212149231.html, 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
以上红色部分参考了网上某位仁兄的操作,以加密方式访问流量监控页面。不过我用的是resin,就没有进行设置了,这里仅作记录。
如果不用indexmaker 自定义监控显示页面的话,在使用cfgmaker参数生成的页面包含的信息是比较全面的,但生成html名字是自己指定的IP地址为前缀的。
不过,我用的是resin-2.1.14,在配置文件里里,加入了以下内容,然后客户端hosts文件里加上https://www.360docs.net/doc/f212149231.html, 的映射,即可访问监控页面了:

/var/www/html/mrtg index.html 更新信息.
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



相关主题
相关文档
最新文档