準備工作
# 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 snmptcp 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 $totalmemecho $usedmemecho $UPtimeecho 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
在其最後添加
#MemoryTarget[memory]: `/home/htdocs/monitor/mrtg/sh/mrtg.memory`MaxBytes[memory]: 4096000Title[memory]: Memory UsagesShortLegend[memory]: &kmg[memory]: kB,MBkilo[memory]: 1024YLegend[memory]: Memory Usage :Legend1[memory]: Total Memory :Legend2[memory]: Used Memory :LegendI[memory]: Total Memory :LegendO[memory]: Used Memory :Options[memory]: growright,gauge,nopercentPageTop[memory]: <H1>Memory Usages</H1>#CPUTarget[cpu]: `/home/htdocs/monitor/mrtg/sh/mrtg.cpu`MaxBytes[cpu]: 100Options[cpu]: gauge, nopercent, growrightYLegend[cpu]: CPU loading (%)ShortLegend[cpu]: %LegendO[cpu]: CPU 使用者負載;LegendI[cpu]: CPU 純系統負載;Title[cpu]: CPU UsagesPageTop[cpu]: <H1>CPU Usages</H1>
到此,MRTG已經安裝配置完成,最後只需要在nginx或者apache上面配置一個訪問地址就行了!
最後效果如下: