| 
	MRTG非常強大,他可以監控你的服務器的一舉一動,並且用web方式呈現給你,告訴你網絡,cpu,內存,硬盤等使用狀況,但是配置起來這個軟件實在是非常麻煩. 
	爲了安裝這個軟件且將其配置好,我上網查了兩天資料,最後整理總結了這一份完整的出來,分享給大家,希望對大家有用.如要轉載請注明出處www.7di.net 
	一、前期準備(1)MRTG需要以SNMP服务为基础,所以请确保你的系统已经安装并启用了此服务,
 
	[root@extw2 ~]# rpm -qa |grep snmp 
	[root@extw2 ~]# yum install -y net-snmp [root@extw2 ~]# yum install -y net-snmp-devel
 [root@extw2 ~]# yum install -y net-snmp-libs
 [root@extw2 ~]# yum install -y net-snmp-perl
 [root@extw2 ~]# yum install -y net-snmp-utils
 
	启动snmp服务[root@extw2 ~]# snmpd
 [root@extw2 ~]#
 [root@extw2 ~]# service snmpd start
 查看snmp服务
 [root@extw2 ~]# service snmpd status
 snmpd (pid 2440) is running...
 
	修改SNMP的配置:#vi /etc/snmp/snmpd.conf
 把第89行前面的注释符号#去掉,变成
 view mib2 included .iso.org.dod.internet.mgmt.mib-2 fc
 将62行:
 access notConfigGroup "" any noauth exact systemview none none
 改为
 access notConfigGroup "" any noauth exact mib2 none none
 
	(2)linux Mrtg监控主机流量启动http服务和snmp服务:首先查看两个服务在系统启动时是否已经开启
 #chkconfig --list | grep nginx
 没有启动的话需要修改下在什么启动模式下要自己启动:
 # chkconfig --level 35 nginx on
 对 snmpd 服务采用同样的设置,现在手动启动下
 #service nginx start
 #service snmpd start
 啟動之後最好是用setup配置服務,讓其隨機自動啟動
 
	查看端口的开启状况也可確認其是否已經被啟動# netstat -tunlp |grep snmp
 tcp 0 0 0.0.0.0:199 0.0.0.0:* LISTEN 4973/snmpd
 udp 0 0 0.0.0.0:161 0.0.0.0:* 4973/snmpd
 
	(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 (若是提示找不到zlib库文件或者头文件,多半是makefile文件里zlib的默认路径有误。可编辑makefile文件,找到zlib项并重新指定路径到/usr/local/zlib/lib和/usr/local/zlib/include)。
 # 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.v6bjpeg默认不会自建目录,因此需手动建立目录:
 # 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
 
	二、開始安裝(1)开始安装mrtg
 #cd /usr/local/src
 #tar zvxf mrtg-2.14.5.tar.gz
 #cd mrtg-2.14.5
 #./configure --prefix=/usr/local/mrtg 注意:“--prefix=/usr/local/mrtg”是指定的安装路径
 #make
 #make install
 
	(2)配置MRTG首先为mrtg的主页在web目錄下建立一个目录,假如web目錄是:/home/wwwroot/
 在此目录下新建目录mrtg和sh即:
 #cd /home/wwwroot/
 #mkdir mrtg
 #cd mrtg
 #mkdir sh
 
	把一些圖片複製過去#cd /usr/local/src/mrtg-2.14.5
 #cp ./images/*.* /root/mrtg/
 
	如果路径嫌长感到不方便的话可以建立軟連接:#cd /root
 #ln –s /home/wwwroot/mrtg mrtg
 #ln –s /usr/local/mrtg/bin mrtgbin
 
	然后需要创建一个配置文件:#/root/mrtgbin/cfgmaker public@改成本機IP --output=/root/mrtg/lxy.cfg
 
	修改配置文件:#vi /root/mrtg/lxy.cfg
 将 #WorkDir:/home/http/mrtg 去掉注释并改为 WorkDir: /home/wwwroot/mrtg (此处是MRTG的WEB目录)
 将 #Options[_]: growright, bits 前面的#去掉,靠左对齐
 在最后加上Language: big5,然后保存
 
	(3)修改.sh檔案讓其可執行(.sh檔案內容在下方)#cd /root/mrtg/sh
 #chmod 0755 *.sh
 #chmod 0755 *.pl
 
	(4)測試配置檔LANG=C /root/mrtgbin/mrtg /root/mrtg/lxy.cfg
 
	(5)生成MRTG網站首頁文件:已在root目录下#/root/mrtgbin/indexmaker --output=/root/mrtg/index.html --title="Lidalin's MRTG" /root/mrtg/lxy.cfg
 
	(6)接下来运行程序#/root/mrtgbin/mrtg /root/mrtg/lxy.cfg 有的人用這種方法啟動#env LANG=C /root/mrtgbin/mrtg /mrtg/lxy.cfg
 如果有警告或錯誤,則多运行几次,當警告或錯誤不再出现時就OK了
 
	(7)MRTG生成的web页面是静态的,为了能让其不断的刷新,需要将以上命令添加进crontab#crontab -e
 添加如下一行
 */1 * * * * /root/mrtgbin/mrtg /root/mrtg/lxy.cfg
 保存,然后reboot
 打開瀏覽器,輸入 http://改成本機IP/mrtg 应该就可以看到网络使用情况的曲线了,一分鐘后再看比較準確!
 
	注意:如果你设置了中级防火墙而又没有开启80端口访问,需要编辑/etc/sysconfig/iptables允许别人访问80端口 
	三、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,表示图表向右延展
 
	四、配置其他範例
 注意:修改了配置一定要重設第二大步驟中的第(3)(4)(5)(6)步驟
 
	其他配置的部份均放入此資料夾下#cd /root/mrtg/sh
 
	(1)監視内存使用狀況建立脚本文件
 #vi mem.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
 保存后要让其具有執行权限
 #chmod 755/root/mrtg/sh/mem.sh
 
	(2)编辑/mrtg/lxy.cfg并添加如下内容:Target[memory]: `/root/mrtg/sh/mem.sh
 MaxBytes[memory]: 4096000
 Title[memory]:Memory Usages
 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 Usages</H1>
 
	五、各个SHELL(修改了配置之後要重新生成首頁,然後重啟)(1)MEMORY: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"
 
	编辑MRTG的配置文件:MaxBytes的值根據實際狀況修改# 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
 #!/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"
 
	编辑MRTG的配置文件:MaxBytes根據實際狀況修改# 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
 #!/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
 
	编辑MRTG的配置文件:MaxBytes根據實際狀況修改# 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
 #!/usr/bin/perl
 # output(df -kl) looks like this:
 # Filesystem 1k-blocks Used Available Use% Mounted on
 # /dev/md0 95645100 30401312 64272080 33% /
 # /dev/hde1 14119 1159 12231 9% /boot
 #
 # In which case, this script returns :
 # 95659219
 # 30402503
 # 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";
 hostname
 
	编辑MRTG的配置文件:MaxBytes根據實際狀況修改# 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>
 (5)磁盤I/O:io.sh
 #!/bin/bash
 # This script will monitor the KBread/sec &KBwriten/sec of Disk.
 # Creater: CCC IT loren ext:2288 2005/8/3
 # As sda ,sdb,sdc,sdd,hda.
 # disk=sda
 hd=sda
 disk=/dev/$hd
 KBread_sec=`iostat -x $disk|grep $hd |awk '{print $8}'`
 KBwrite_sec=`iostat -x $disk|grep $hd |awk '{print $9}'`
 echo "$KBread_sec"
 echo "$KBwrite_sec"
 hostname
 
	编辑MRTG的配置文件:如果需要檢測更多的分区則修改io.sh# Disk I/O
 Target[diskIO]: `/root/mrtg/sh/io.sh`
 Title[diskIO]: Disk HDA I/O
 Unscaled[diskIO]: dwym
 MaxBytes[diskIO]: 100
 kmg[diskIO]: KB,MB,GB
 LegendI[diskIO]: Disk I/O KBread/sec
 LegendO[diskIO]: Disk I/O KBwrite/sec
 Legend1[diskIO]: Disk I/O KBread/sec
 Legend2[diskIO]: Disk I/O KBwrite/sec
 YLegend[diskIO]: Megabytes
 ShortLegend[diskIO]: &
 Options[diskIO]: growright,gauge,nopercent
 PageTop[diskIO]: <H1>Disk I/O</H1>
 
 (责任编辑:IT)
 |