# yum -y install gd-devel
# yum -y install libmcrypt
# yum -y install libmcrypt-devel
# yum -y install freetype
# yum -y install freetype-devel
# yum -y install libtool-ltdl
# yum -y install libtool-ltdl-devel
# yum -y install libjpeg-devel
# yum -y install libpng-devel
# yum -y install libmcrypt
# yum -y install libmcrypt-devel
# yum -y install zlib-1.2.3
一、yum安裝snmp
1.
# yum -y install net-snmp*
2.修改/etc/snmp/snmpd.conf
# vim /etc/snmp/snmpd.conf
找到如下行:# sec.name source community
com2sec notConfigUser default public
把public
改爲其他名字,爲了防止黑客的攻擊,我改的是jdeyes去掉如下一行的註釋
#view mib2 included .iso.org.dod.internet.mgmt.mib-2 fc
在第55行處添加如下一行view systemview included .1.3.6.1.2.1.2
把如下行access notConfigGroup "" any noauth exact systemview none none
改作:access notConfigGroup "" any noauth exact mib2 none none
3.啓用snmpd服務# service snmpd start
# chkconfig snmpd on
4.查看端口的開啓狀況
# 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
二、安裝配置MRTG
1.下載mrtg
# wget http://oss.oetiker.ch/mrtg/pub/mrtg-2.17.4.tar.gz
2.安裝mrtg-2.17.4# tar zxvf mrtg-2.17.4.tar.gz
# cd mrtg-2.17.4
# ./configure --prefix=/usr/local/mrtg --sysconfdir=/etc/mrtg --with-gd=/usr/local/gd2/include --with-gd-lib=/usr/local/gd2/lib --with-gd-inc=/usr/local/gd2/include --with-png=/usr/local/include --with-png-lib=/usr/local/lib --with-png-inc=/usr/local/include --with-zlib=/usr/local/zlib/include --with-zlib-lib=/usr/local/zlib/include --with-zlib-inc=/usr/local/zlib/include
# make
# make install
3.基本配置生成主配置文件
# /usr/local/mrtg/bin/cfgmaker jdeyes@localhost > /etc/mrtg.cfg
編輯/etc/mrtg.cfg# vim /etc/mrtg.cfg
將
# WorkDir:/home/http/mrtg
去掉註釋並改爲WorkDir: /home/htdocs/monitor/mrtg (此處是你的httpd默認的主目錄)
去掉如下行的註釋
# Options[_]: growright, bits
生成MRTG網頁主頁面文件# /usr/local/mrtg/bin/indexmaker /etc/mrtg.cfg --output=/home/htdocs/monitor/mrtg/index.html --title="MRTG"
4.啓動MRTG# env LANG=C /usr/local/mrtg/bin/mrtg /etc/mrtg.cfg
這個命令會輸出一些錯誤信息,一般可以安全忽略,連續執行三次此命令即可。5.MRTG生成的web頁面是靜態的,爲了能讓其不斷的刷新,需要將以上命令添加進crontab
# vim /etc/crontab
添加如下一行*/5 * * * * root env LANG=C /usr/local/mrtg/bin/mrtg /etc/mrtg.cfg
注:此行表示每三分鐘刷新一次,你可以根據自己的需要修改刷新時間間隔。
重啓SNMP服務:
# sudo /etc/init.d/snmpd restart
可以通過命令:# snmpwalk -v 2c -c ferdinand localhost system
來檢測是否安裝成功三、一個示例:內存使用監視
1.新建一個存放腳本的文件夾
# mkdir -pv /home/htdocs/monitor/mrtg/sh
2.建立腳本文件# vim /home/htdocs/monitor/mrtg/sh/mrtg.memory
添加如下腳本:#!/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
echo localhost
3.讓其具有運行權限# chmod +x /home/htdocs/monitor/mrtg/sh/mrtg.memory
四、一個示例:CPU使用監視
sysstat 使用yum安裝
# yum install sysstat
sysstat的安裝包是:sysstat-5.0.5-1.i386.rpm,裝完了sysstat-5.0.5-1.i386.rpm後就會有iostat、mpstat、sar、sa的功能,sysstat-5.0.5-1.i386.rpm
啓動sysstat
# /etc/init.d/sysstat start
設置sysstat自啓動# /sbin/chkconfig sysstat on
1.建立腳本文件# vim /home/htdocs/monitor/mrtg/sh/mrtg.cpu
添加如下腳本:#!/bin/bash
# 這個程式是用來偵測 CPU 的小外掛程式!
# 1. 開始使用 sar 來監測 CPU 的 user 及 System 負載率
cpuusr=`/usr/bin/sar -u 1 3 | grep Average | awk '{print $3}'`
cpusys=`/usr/bin/sar -u 1 3 | grep Average | awk '{print $5}'`
UPtime=`/usr/bin/uptime | awk '{print $3 " " $4 " " $5}'`
echo $cpuusr
echo $cpusys
echo $UPtime
echo localhost
2.讓其具有運行權限# chmod +x /home/htdocs/monitor/mrtg/sh/mrtg.cpu
編輯mrtg配置文件# vim /etc/mrtg.cfg
在其最後添加#Memory
Target[memory]: `/home/htdocs/monitor/mrtg/sh/mrtg.memory`
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>
#CPU
Target[cpu]: `/home/htdocs/monitor/mrtg/sh/mrtg.cpu`
MaxBytes[cpu]: 100
Options[cpu]: gauge, nopercent, growright
YLegend[cpu]: CPU loading (%)
ShortLegend[cpu]: %
LegendO[cpu]: CPU 使用者負載;
LegendI[cpu]: CPU 純系統負載;
Title[cpu]: CPU Usages
PageTop[cpu]: <H1>CPU Usages</H1>
到此,MRTG已經安裝配置完成,最後只需要在nginx或者apache上面配置一個訪問地址就行了!最後效果如下: