用MRTG监测Linux系统网络、CPU、内存和硬盘情况

 

随着Linux应用的日益广泛,有大量的网络服务器使用Linux操作系统。为了全面衡量网络运行状况,就需要对网络状态做更细致、更精确的测量。 SNMP协议的制订为互联网测量提供了有力支持。MRTG(MultiRouter Traffic Grapher, MRTG)就是基于SNMP的典型网络流量统计分析工具。它耗用的系统资源很小,因此有很多外挂的程序也依附在MRTG下。它通过SNMP协议从设备得到 其流量信息,并将流量负载以包含JPEG格式图形的HTML文档的方式显示给用户,以非常直观的形式显示流量负载。
  工作原理
   网络服务器的资料总流量(网卡的资料传送总数)、CPU使用率以及特殊服务(例如Squid的代理服务)等的封包传送率(或者说是流量)是网络管理人员 所必须要注意的事项。因为当主机的CPU使用率过高时,系统可能呈现不稳定的状态,这就需要注意是哪一个服务或者谁在尝试窃取我们的资料。因此,网络管理 方面,有必要了解我们主机的流量状态,并视流量来加以限制或者是加大带宽。
  我们可以从MRTG的主页:http://www.mrtg.org下载MRTG软件。在网站:http://www.stat.ee.ethz.ch/mrtg/可以得到MRTG流量监测图的输出结果。
   MRTG是用Perl写成的,并且使用了zlib、gd以及png等函数(zlib用来压缩图表、gd用来绘制图表)。服务器中已经含有下列的软件: perl(perl-5.0xx以上)、zlib(zlib-1.1.3-xx以上)、gd(gd-1.3.xx以上)、libpng和Apache。

实验名称:用MRTG 监测Linux 系统的CPU、Memory 负载
操作系统:Redhat 9.0
所使用的内核:linux-2.4.20
实验用到的模块:mrtg-2.11.1.tar.gz
作者:vitter
功能描述:
我们知道MRTG 是很出色的流量监测工具,可以对很多的网络设备进行流量的负载监测,只要这个设备支持SNMP 协议。另外我们还可以来监测Linux 系统的CPU、Memory 的负载。
原理描述:
以 前我只是用MRTG 通过SNMP 来监测网络设备的流量,这次通过完成这次实验后对MRTG 有了更进一步的了解。MRTG 可以完成很强大的图表功能,只要你能给他两个数字,记住是两个数字,而且要定时的给。MRTG 就可以根据你定时提供的两个数字给你作出曲线图。
这里我们要监测的是CPU 和Memory 的负载,其中,
CPU 的监测包括:
CPU 的使用率,
CPU 的闲置率;
Memory 包括:
Memory 的使用率,
Memory 的总量。
所以在监测CPU时,我们要获得的数据是CPU的使用率,CPU的闲置率;在监测Memory时,我们要获得的数据是Memory 的使用率,Memory 的总量。好了,知道这些就够了,开始实验。

1、安装MRTG
  由于MRTG是通过SNMP通讯协议来要求资料,因此Linux上需要先安装相应的软件SNMP。
[root@intel root]#tar zxvf mrtg-2.11.1.tar.gz
[root@intel root]#cd mrtg-2.11.1
[root@intel mrtg-2.11.1]#./configure –prefix=/usr/local/mrtg
[root@intel mrtg-2.11.1]#make
[root@intel mrtg-2.11.1]#make install
2、用mrtg监控网络:
因为mrtg是通过snmp来监控信息的 所以要修改一下snmp配置文件
[root@intel bin]#vi /etc/snmp/snmpd.conf
#view mib2 included .iso.org.dod.internet.mgmt.mib-2 fc
的内容修改为:
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

启动snmp服务
[root@intel bin]#service snmpd start
接下来我们用cfgmaker来生成mrtg的配置文件
[root@intel bin]#./cfgmaker --global "WorkDir: /usr/local/apache_1.3.31/htdocs/mrtg" --global "Options[_]: growright,bits" --output /usr/local/mrtg/etc/net1.cfg [email protected]
global指对哪些设备有效(如果有多个设备的情况下)
workDir 是指你APACHE的访问目录
Options用来指定一些特定的选项,这里的growright,bits是用来指定默认options配置的对于常见的应用来说默认options配置就可以满足需求了。
ifref 用来指示用什么选项来标识设备接口,这里指定使用IP地址来标识网络设备接口。ifref可以指定为nr、ip、eth、descr、name。nr表示 用接口在MIBII库中Interface接口的ifIndex来识别接口;IP表示使用ip地址识别接口;eth表示使用接口的物理地址标识接口; descr表示使用接口的描述信息来标识接口;name表示使用接口名来标识接口。一般来说ip地址是唯一的,但是有些情况下接口是没有IP地址的,例如 交换机就会出现这种情况。对于接口来说nr(接口号)是唯一的,因此对于一般情况使用IP地址就可以了,而对于其他一些情况则需要采用nr了
--output /usr/local/mrtg/etc/net1.cfg 指把配置文件生成在/usr/local/mrtg/etc目录下
[email protected] 监控218.249.159.55 采用public作为共同体名通过snmp协议来监控设备218.249.159.55。
ok 最后一步 启动mrtg
[root@intel bin]#./mrtg /usr/local/mrtg/etc/net1.cfg
然后用indexmaker做index文件
[root@intel bin]#./indexmaker --title="流量监控" --output=/usr/local/apache_1.3.31/htdocs/mrtg/index.html /usr/local/mrtg/etc/net1.cfg
就可以了,然后用HTTP访问 http://218.249.159.55/mrtg
OK 网络监控完成

下面是CPU监控和内存监控。

3、获得我们需要的关于CPU 和Memory 的数据

1), 获得CPU 的使用率和CPU 的闲置率
为了获得CPU 的这两个数据,我们使用sar –u 1 3 这个命令,
[root@intel zwz]# sar -u 1 3
Linux 2.4.20-8 (intel) 08/30/2005

05:46:16 PM CPU %user %nice %system %idle
05:46:17 PM all 0.00 0.00 1.00 99.00
05:46:18 PM all 0.00 0.00 0.00 100.00
05:46:19 PM all 0.00 0.00 0.00 100.00
Average: all 0.00 0.00 0.33 99.67
sar 命令执行后生成如上图所示的数据。我们需要的是三个带下划线的数据。其中:
CPU 的使用率为:%user + %system = 0.00 + 0.33 = 0.33
CPU 的闲置率为:%idle = 99.67
好了只有sar 是不行的,因为mrtg 不能识别这些数据,mrtg 能识别两个数据,所以我们要把0.33 和99.67 传给mrtg,这很容易实现,我用perl 写了一个脚本(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 :[email protected]#####################

注 意:在脚本里system ("/usr/local/bin/sar -u 1 3|grep Average >cpu_info_file") ,这句话中必须将sar 的全路径写全,而不能用system ("sar -u 1 3|grep Average >cpu_info_file") 。因为cpu.pl 是由mrtg 调用,mrtg 不知道你的系统路径。
我将cpu.pl 脚本放在/usr/local/mrtg/bin 下,执行cpu.pl 会得到下面的结果:

[root@intel bin]# ./cpu.pl
0
100.00
12:07am up 1 day, 7:22, 2 users, load average: 0.07, 0.12, 0.09
TRSB
好, 我们需要的数值已经输出来了,下一步的工作就是要交给mrtg 了,mrtg 是通过一个配置文件来获得这两个数值的,通常情况下这个配置文件是由mrtg 的cfgmaker 命令来生成的, 但这次我们要自己写这个配置文件(cpu.cfg) : 这个配置文件我把他放在/usr/local/mrtg/etc 下,内容如下:
[root@intel etc]# vi cpu.cfg

WorkDir:/usr/local/apache_1.3.31/htdocs/mrtg/cpu/
Target[localhost]:`/usr/local/mrtg/bin/cpu.pl`
Xsize[localhost]: 300
Ysize[localhost]: 100
Ytics[localhost]: 10
MaxBytes[localhost]:100
Title[localhost]:CPU State
PageTop[localhost]:<H1>CPU State of Vitter-test Server</H1>
ShortLegend[localhost]: %
YLegend[localhost]: CPU (%)
Legend1[localhost]: Used
Legend2[localhost]: Total
LegendI[localhost]: CPU Used
LegendO[localhost]: CPU IDEL
Options[localhost]: growright,gauge,nopercent

下面我们可以执行mrtg 了:
[root@intel etc]#/usr/local/mrtg/bin/mrtg /usr/local/mrtg/etc/cpu.cfg
当第一次执行时会有报警,执行三次,就没有报警了。

2)、获得内存的使用率和总量
为了获得内存的使用量和总量我使用free -m 这个命令:
[root@intel etc]# free -m
total used free shared buffers cached
Mem: 501 454 47 0 71 234
-/+ buffers/cache: 148 353
Swap: 1019 8 1010

free 命令执行后生成如上图所示的数据。我们需要的是两个带下划线的数据。其中:
Memory 的使用情况为:454 M
Memory 的总量为:501 M
下面我们通过一个perl 脚本(mem.pl)来获得并输出这两个数据,脚本如下:

[root@intel bin]# vi 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 [email protected] ##################
我同样把mem.pl 这个脚本放在/usr/local/mrtg/bin/下,这个脚本由mrtg 来调用,现在我们来完成相应的配置文件(mem.cfg),我把mem.cfg 放在/usr/local/mrtg/etc 目录下,内容如下:
[root@intel etc]# vi mem.cfg

WorkDir:/usr/local/apache_1.3.31/htdocs/mrtg/mem/
Target[localhost]: `/usr/local/mrtg/bin/mem.pl`
Xsize[localhost]:300
Ysize[localhost]:100
Ytics[localhost]:10
MaxBytes[localhost]: 1006
Title[localhost]:Memory State of Vitter-test Server
PageTop[localhost]:<H1>Memory State of Vitter-test Server</H1>
ShortLegend[localhost]: B
kmg[localhost]: M
YLegend[localhost]: Memory Usage
Legend1[localhost]: Used
Legend2[localhost]: Total
LegendI[localhost]: Used
LegendO[localhost]: Total
Options[localhost]: growright,gauge,nopercent

下面我们可以执行mrtg 了:
[root@intel etc]# /usr/local/mrtg/bin/mrtg /usr/local/mrtg/etc/mem.cfg
当第一次执行时会有报警,执行三次,就没有报警了。

3)、获得硬盘空间总量和使用量
用命令df -kl查看硬盘空间情况
[root@intel bin]# df -kl
Filesystem 1k-blocks Used Available Use% Mounted on
/dev/sda5 2063504 1169204 789480 60% /
/dev/sda1 101089 8127 87743 9% /boot
/dev/sda2 8262068 2324004 5518368 30% /data
/dev/sda7 2063504 1364412 594272 70% /home
/dev/sda6 2063504 1288716 669968 66% /usr
/dev/sda3 2063536 350040 1608672 18% /var
/dev/sdb1 17654736 2964312 13793600 18% /data1
总量=2063504+ 101089+8262068+2063504+2063504+2063536 +17654736 =34271941
使用量=1169204+8127+2324004+1364412+1288716+350040+2964312=9469231
[root@intel bin]# vi df.pl

#!/usr/bin/perl
# This script was written on RedHat 9.0, it assumes that the command
# output(df -kl) looks like this:
# Filesystem 1k-blocks Used Available Use% Mounted on
# /dev/sda5 2063504 1169204 789480 60% /
# /dev/sda1 101089 8127 87743 9% /boot
# /dev/sda2 8262068 2324004 5518368 30% /data
# /dev/sda7 2063504 1364412 594272 70% /home
# /dev/sda6 2063504 1288716 669968 66% /usr
# /dev/sda3 2063536 350040 1608672 18% /var
# /dev/sdb1 17654736 2964312 13793600 18% /data1
#
# In which case, this script returns :
#
# 34271941
# 9469231
# 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");

编辑配置文件(df.cfg)
[root@intel etc]#vi df.cfg
WorkDir:/usr/local/apache_1.3.31/htdocs/mrtg/df/
Target[localhost]: `/usr/local/mrtg/bin/df.pl`
Xsize[localhost]:300
Ysize[localhost]:100
Ytics[localhost]:10
Title[localhost]: Vfocus.Net SERVER Disk Space (34,271,941 kB / 36GB) Megabytes used
Unscaled[localhost]: dwym
MaxBytes[localhost]: 34271941
PageTop[localhost]: <H1>Vfocus.Net SERVER Disk Space(34,271,941 kB / 36GB) Megabytes used</H1>
kmg[localhost]: KB,MB,GB
LegendI[localhost]: Total Disk Space
LegendO[localhost]: Used Disk Space
Legend1[localhost]: Total Disk Space
Legend2[localhost]: Used Disk Space
YLegend[localhost]: Megabytes
ShortLegend[localhost]: &
Options[localhost]: growright,gauge,nopercent

[root@intel etc]# /usr/local/mrtg/bin/mrtg /usr/local/mrtg/etc/df.cfg
执行三次,就没有报警了。
用indexmaker作index文件
[root@intel bin]# ./indexmaker --title="硬盘监控" --output=/usr/local/apache_1.3.31/htdocs/mrtg/df/index.html /usr/local/mrtg/etc/df.cfg
[root@intel bin]# ./indexmaker --title="CPU监控" --output=/usr/local/apache_1.3.31/htdocs/mrtg/cpu/index.html /usr/local/mrtg/etc/cpu.cfg
[root@intel bin]# ./indexmaker --title="内存监控" --output=/usr/local/apache_1.3.31/htdocs/mrtg/mem/index.html /usr/local/mrtg/etc/mem.cfg
我们把mrtg 加入到crontab 中,让他定时执行:
*/3 * * * * /usr/local/mrtg/bin/mrtg /usr/local/mrtg/etc/mem.cfg
*/3 * * * * /usr/local/mrtg/bin/mrtg /usr/local/mrtg/etc/cpu.cfg
*/3 * * * * /usr/local/mrtg/bin/mrtg /usr/local/mrtg/etc/net1.cfg
*/3 * * * * /usr/local/mrtg/bin/mrtg /usr/local/mrtg/etc/df.cfg

现在就可以监控网络、CPU 和Memory 了。这里是我的示例:http://www2.vfocus.net/mrtg/ http://www2.vfocus.net/mrtg/cpu/ http://www2.vfocus.net/mrtg/mem/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章