Multi RouterTraffic Grapher(MRTG)是一个监控网络链路流量负载的工具软件,通过snmp协议得到设备的流量信息,并将流量负载以包含PNG格式的图形的HTML 文档方式显示给用户,以非常直观的形式显示流量负载。
一、安装依赖
1、安装SNMP服务
#rpm -qa |grep snmp
#yum install -y net-snmp
#yum install -y net-snmp-devel
#yum install -y net-snmp-libs
#yum install -y net-snmp-perl
#yum install -y net-snmp-utils
启动snmp服务
#service snmpd start
#service snmpd status
snmpd (pid 2440) is running...
修改SNMP的配置:
#vi /etc/snmp/snmpd.conf
a.把第89行前面的注释符号#去掉,变成
view mib2 included.iso.org.dod.internet.mgmt.mib-2 fc
b.将62行:accessnotConfigGroup "" any noauth exact systemview none none
改为access notConfigGroup "" any noauth exact mib2 none none
2、启动http服务和snmp服务
#service nginx start
#service snmpd start
3、MRTG生成供浏览图像需要http服务的支持,同时也需要gd、libpng和zlib三个软件包的支持,而gd的正常运行也需要其它的几个软件
(1)安装zlib-1.2.3
#tar zxvf zlib-1.2.3.tar.gz
#cd zlib-1.2.3
#./configure --prefix=/usr/local/zlib
#make
#make install
(2)安装httpd-2.2.4
#tar zxvf httpd-2.2.4.tar.gz
#cd httpd-2.2.4
#./configure --prefix=/usr/local/apache--sysconfdir=/etc/httpd --enable-so --enable-track-vars --enable-rewrite--with-z-dir=/usr/local/zlib
#make
#make install
启动httpd
#/usr/local/apache/bin/apachectl -k start
(3)安装libpng-1.2.14
#tar zxvf libpng-1.2.14.tar.gz
# cd libpng-1.2.14
# cp scripts/makefile.linux makefile
# make
# make install
(4)安装freetype-2.1.10
# tar -zvxf freetype-2.1.10.tar.gz
# cd freetype-2.1.10
# mkdir -p /usr/local/freetype
# ./configure --prefix=/usr/local/freetype
# make;make install
(5)安装jpegsrc.v6b
# mkdir -pv /usr/local/jpeg6/{,bin,lib,include,man/{,man1},man1}
#tar zxvf jpegsrc.v6b.tar.gz
# ./configure --prefix=/usr/local/jpeg6/--enable-shared --enable-static
# make
# make install
# make install-lib
(6)安装libxml2-2.6.19
# tar -zxf libxml2-2.6.19.tar.gz
# cd libxml2-2.6.19
# mkdir -p /usr/local/libxml2
# ./configure --prefix=/usr/local/libxml2
# make; make install
#cp xml2-config /usr/bin
(7)安装GD-2.0.33库
# tar -zvxf gd-2.0.33.tar.gz
# mkdir -p /usr/local/gd2
# cd gd-2.0.33
# ./configure --prefix=/usr/local/gd2--with-jpeg=/usr/local/jpeg6/ --with-png=/usr/local/lib/--with-zlib=/usr/local/zlib/ --with-freetype=/usr/local/freetype/
# make
# make install
4、修改系統語言配置
#vi /etc/profile
增加如下兩行:
LC_ALL=C
LANGUAGE=on
更新环境变量
#Source /etc/profile
二、安装配置MRTG
(1)开始安装mrtg
#cd /usr/local/src
#tar zvxf mrtg-2.14.5.tar.gz
#cd mrtg-2.14.5
#./configure --prefix=/usr/local/mrtg --with-jpeg=/usr/local/jpeg6/--with-png=/usr/local/lib/ --with-zlib=/usr/local/zlib/--with-freetype=/usr/local/freetype/ --with-gd-lib=/usr/local/gd2/lib/--with-gd-inc=/usr/local/gd2/include
#make
#make install
(2)配置MRTG
首先为mrtg的主页在web目录下建立一个目录,假如web目录是:/home/wwwroot/
#cd /home/wwwroot/
#mkdir mrtg
把一些图片复制过去
#cd /usr/local/src/mrtg-2.14.5
#cp ./images/*.* /home/wwwroot/mrtg/
创建配置文件:
#/usr/local/mrtg/bin/cfgmaker public@改成本機IP--output=/root/mrtg/mrtg.cfg
修改配置文件:
#vi /root/mrtg/mrtg.cfg
a.将#WorkDir:/home/http/mrtg 去掉注释并改为 WorkDir: /home/wwwroot/mrtg (此处是MRTG的WEB目录)
b.将 #Options[_]:growright, bits 前面的#去掉,靠左对齐
c.在下一行加上Language: big5,然后保存
(3)MRTG生成的web页面是静态的,为了能让其不断的刷新,需要将以下命令添加进crontab
#crontab -e
添加如下一行
*/1 * * * * env LANG=C /usr/local/mrtg/bin/mrtg/root/mrtg/mrtg.cfg
(4)生成MRTG首页文件
#/usr/local/mrtg/bin/indexmaker--output=/home/wwwroot/mrtg/index.html --title="SCE's MRTG"/root/mrtg/mrtg.cfg
(5)运行程序
#env LANG=C /usr/local/mrtg/bin/mrtg/root/mrtg/mrtg.cfg
如果有警告,则多运行几次,警告不再出现时就可以了
三、mrtg.cfg里面参数的意思
Target:是要执行的脚本
Xsize:生成图表的横向宽度(最大600)
Ysize:生成图表的纵向高度(最大200)
Title:标题
kmg: Change the default multiplier prefixes
Ytics:纵向划分为几个块(格子)
MaxBytes:图表纵向数值的最大上限,根据实际情况修改
PageTop:页面上面的提示
kilo:一般是写1024,如果需要的话,是1000在计算机里的单位
LegendI:从SHELL返回的数据中的第一个
LegendO:从SHELL返回的数据中的第二个
Options: growright,表示图表向右延展
四、各个SHELL(修改配置之后需要重新生成首页)
#cd /root/mrtg/sh
建立各个脚本文件
(1)MEMORY:mem.sh
#Vi mem.sh: #!/bin/bash # This script to monitor the mem usage. totalmem=`/usr/bin/free |grep Mem |awk '{print $2}'` usedmem=`/usr/bin/free |grep Mem |awk '{print $3}'` echo "$totalmem" echo "$usedmem"
编辑/root/mrtg/mrtg.cfg并添加如下内容:
# Mem
Target[memory]: `/root/mrtg/sh/mem.sh`
Unscaled[memory]: dwym
MaxBytes[memory]: 2048000
Title[memory]:Memory
ShortLegend[memory]: &
kmg[memory]:kB,MB
kilo[memory]:1024
YLegend[memory]: Memory Usage :
Legend1[memory]: Total Memory :
Legend2[memory]: Used Memory :
LegendI[memory]: Total Memory :
LegendO[memory]: Used Memory :
Options[memory]: growright,gauge,nopercent
PageTop[memory]:<H1>Memory</H1>
(2)SWAP:swap.sh
#Vi swap.sh:
#!/bin/bash
# This script to monitor the swap usage.
totalswap=`/usr/bin/free |grep Swap |awk '{print $2}'`
usedswap=`/usr/bin/free |grep Swap |awk '{print $3}'`
echo "$totalswap"
echo "$usedswap"
编辑/root/mrtg/mrtg.cfg并添加如下内容:
# Swap
Target[swap]:`/root/mrtg/sh/swap.sh`
Unscaled[swap]: dwym
MaxBytes[swap]: 3048000
Title[swap]:SWAP
ShortLegend[swap]: &
kmg[swap]:kB,MB
kilo[swap]:1024
YLegend[swap]: Swap Usage
Legend1[swap]: Total Swap
Legend2[swap]: Used Swap
LegendI[swap]: Total Swap
LegendO[swap]: Used Swap
Options[swap]: growright,gauge,nopercent
PageTop[swap]:<H1>Swap</H1>
(3)CPU:cpu.sh
#Vi cpu.sh: #!/bin/bash # run this script to check the mem usage. totalmem=`/usr/bin/free |grep Mem |awk '{print $2}'` usedmem=`/usr/bin/free |grep Mem |awk '{print $3}'` UPtime=`/usr/bin/uptime | awk '{print$3""$4""$5}'` echo $totalmem echo $usedmem echo $UPtime hostname
编辑/root/mrtg/mrtg.cfg并添加如下内容:
# Cpu
Target[cpu]: `/root/mrtg/sh/cpu.sh`
MaxBytes[cpu]: 100
Title[cpu]: CPU
Options[cpu]: gauge,nopercent,growright
YLegend[cpu]: CPU loading (%)
ShortLegend[cpu]:%
LegendO[cpu]: & CPU USER
LegendI[cpu]: & CPU SYSTEM
PageTop[cpu]: <H1>CPU</H1>
(4)磁盘占用:df.pl
#Vi df.pl:
#!/usr/bin/perl
foreach $filesystem (`df -kl | grep -v "Filesystem"`){
@df = split(/\s+/,$filesystem);
$total += $df[1];
$usage += $df[2];
}
print "$total\n";
print "$usage\n";
hostname
编辑/root/mrtg/mrtg.cfg并添加如下内容:
# Disk used
Target[disk]: `/root/mrtg/sh/df.pl`
Title[disk]: Disk Space
Unscaled[disk]: dwym
MaxBytes[disk]: 115247550
kmg[disk]: KB,MB,GB
LegendI[disk]: Total Disk Space
LegendO[disk]: Used Disk Space
Legend1[disk]: Total Disk Space
Legend2[disk]: Used Disk Space
YLegend[disk]: Megabytes
ShortLegend[disk]: &
Options[disk]: growright,gauge,nopercent
PageTop[disk]: <H1>Disk Space</H1>
修改.sh和.pl让其可执行
#cd /root/mrtg/sh
#chmod 0755 *.sh
#chmod 0755 *.pl
注意:如果你设置了中级防火墙而又没有开启8080端口访问,需要编辑/etc/sysconfig/iptables允许别人访问8080端口
#vi /etc/sysconfig/iptables
-ARH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
打开浏览器输入如下地址
http://IP:8080/
https://www.jianshu.com/p/3f8729232559 简书上是centos6版本下