这几天把上周弄的MRTG流量测试的工具加强了一下,借鉴了网上一些大牛的资料,自己做了一下测试,现在把操作步骤留存如下:(当前系统CentOS5.5 mrtg ver.2.17.0)
以上就完成了服务器流量的监控配置,接下来再生成几个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 :[email protected]#####################
#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 [email protected] ##################
建立好以上三个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]: <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 <[email protected]> (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:[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]: <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 <[email protected]> (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:[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]: <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 <[email protected]> (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
- <host id='www.monitor.com'>
- <app-dir>/var/www/html/mrtg</app-dir>
- <web-app id='/'>
- <class-update-interval id='2'/>
- <welcome-file-list>index.html</welcome-file-list>
- <cache-mapping url-pattern='/' expires='2s'/>
- </web-app>
- </host>
最后,让系统每隔5分钟执行一次mrtg,生成新的MRTG流量图
# crontab –e
*/5 * * * * env LANG=C /usr/local/mrtg/bin/mrtg /usr/local/mrtg/cfg/mrtg.cfg
本文出自 “生命不止,战斗不息!” 博客,转载请与作者联系!