网络流量监测软件MRTG在RHEL6/CentOS上的配置,由瑞士奥尔滕的Tob


MRTG(Multi Router Traffic Grapher)是一套可用来绘出网络流量图的软件,通过snmp协议得到设备的流量信息,并将流量负载以包含PNG格式的图形的HTML 文档方式显示给用户,以非常直观的形式显示流量负载。由瑞士奥尔滕的Tobias Oetiker与Dave Rand所开发,此软件以GPL授权。

本文将介绍如何在RHEL6/CentOS Linux系统上的配置和配置:

安装snmpd和mrtg

# yum install mrtg net-snmp net-snmp-utils

配置SNMP,配置完成后重启snmpd

# vi /etc/snmp/snmpd.conf

com2sec notConfigUser localhost public
group notConfigGroup v1 notConfigUser
group notConfigGroup v2c notConfigUser
view systemview included .1.3.6.1.2.1.1
view systemview included .1.3.6.1.2.1.25.1.1
access notConfigGroup "" any noauth exact all all none
view all included .1 80
syslocation Unknown (edit /etc/snmp/snmpd.conf)
syscontact Root <root@localhost> (configure /etc/snmp/snmp.local.conf)

因为功能需求,我们再编写两个插件脚本,分别监视cpu和内存。

监视CPU脚本内容

#!/usr/bin/perl
system ("/usr/bin/sar -u 1 3|grep Average >cpu_info_file");
open (CPUINFO,"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");

监视MEM脚本内容

#!/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");

配置/etc/mrtg/mrtg.cfg

EnableIPv6: no
WorkDir: /var/www/mrtg
Target[localhost_2]: 2:public@localhost:
SetEnv[localhost_2]: MRTG_INT_IP="172.18.99.169" MRTG_INT_DESCR="eth0"
MaxBytes[localhost_2]: 12500000
Title[localhost_2]: Traffic Analysis for 2 -- abrhel777
PageTop[localhost_2]: <h1>Traffic Analysis for 2 -- abrhel777</h1>
 <div id="sysdetails">
  <table>
   <tr>
    <td>System:</td>
    <td>abrhel777 in Unknown (edit /etc/snmp/snmpd.conf)</td>
   </tr>
   <tr>
    <td>Maintainer:</td>
    <td>Root <root@localhost> (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>0.0 Bytes/s</td>
   </tr>
   <tr>
    <td>ip:</td>
    <td>172.18.99.169 ()</td>
   </tr>
  </table>
 </div>
# CPU
Target[localhost_cpu]: `/etc/mrtg/mrtg.cpu`
Ytics[localhost_cpu]: 10
YticsFactor[localhost_cpu]: 10
MaxBytes[localhost_cpu]:100
Title[localhost_cpu]:CPU State
PageTop[localhost_cpu]:<H1>Server CPU </H1>
ShortLegend[localhost_cpu]: %
YLegend[localhost_cpu]: CPU (%)
Legend1[localhost_cpu]: Used
Legend2[localhost_cpu]: Total
LegendI[localhost_cpu]: CPU Used
LegendO[localhost_cpu]: CPU IDEL
Options[localhost_cpu]: growright,gauge,nopercent
# Mem
Target[localhost_mem]: `/etc/mrtg/mrtg.mem`
Ytics[localhost_mem]:10
YticsFactor[localhost_mem]: 10
MaxBytes[localhost_mem]: 1000
Title[localhost_mem]:Memory State of TestServer
PageTop[localhost_mem]:<H1>Server Mem </H1>
ShortLegend[localhost_mem]: B
kmg[localhost_mem]: M
YLegend[localhost_mem]: Memory Usage
Legend1[localhost_mem]: Used
Legend2[localhost_mem]: Total
LegendI[localhost_mem]: Used
LegendO[localhost_mem]: Total
Options[localhost_mem]: growright,gauge,nopercent

生成index.html

# indexmaker --output=/var/www/mrtg/index.html /etc/mrtg/mrtg.cfg

添加crontab计划

# cat /etc/cron.d/mrtg

*/5 * * * * root LANG=C LC_ALL=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg --lock-file /var/lock/mrtg/mrtg_l --confcache-file /var/lib/mrtg/mrtg.ok

注:可以先运行一遍该命令,立即生成数据

# /usr/bin/mrtg /etc/mrtg/mrtg.cfg --lock-file /var/lock/mrtg/mrtg_l --confcache-file /var/lib/mrtg/mrtg.ok

修改httpd的配置中mrtg的网址Allow from 部分

通过浏览器访问MRTG

http://youripaddres/mrtg/

相关内容