2008年9月1日 星期一

在 CentOS 5 安裝 MRTG

1.安裝 snmp 與 MRTG

bang@linuxdiary:~$ yum -y install net-snmp mrtg net-snmp-utils

說明:安裝 net-snmp-utils 是為了使用 snmpwalk。

2.設定 snmp

先備分一份設定檔

bang@linuxdiary:~$ cp /etc/snmp/snmpd.conf /etc/snmp/snmpd.conf.bak

編輯設定檔 /etc/snmp/snmpd.conf
在 snmpd.conf 有好幾段範例,請註解第一段範例,使用者由第二個範例修改。
在第二段範例設定一開始你可以看到

# Here is a commented out example configuration that allows less
# restrictive access.

你需要修改的地方只有 community string,其餘請依序移除註解。

a.設定 community string

#       sec.name  source          community
com2sec local localhost hplus

說明:hplus 即為 community string

b. Map Group name

#       groupName      securityModel securityName
#group MyRWGroup any local

說明:移除紅色 #

c. 設定檢視權限

#           incl/excl subtree                          mask
#view all included .1 80

說明:移除紅色 #


d. 最後,讓群組可以唯讀方式檢視系統

#                context sec.model sec.level prefix read   write  notif
#access MyRWGroup "" any noauth 0 all all all

說明:移除紅色 #

e.啟動 SNMP

bang@linuxdiary:~$ /etc/rc.d/init.d/snmpd start

f.測試 SNMP

bang@linuxdiary:~$ snmpwalk -v 1 localhost -c hplus

若 snmp 安裝完成,會顯示一大堆 snmp 回傳訊息,以下節錄一小段

SNMPv2-MIB::sysDescr.0 = STRING: Linux www.hundredplus.com 2.6.15-1.2054_FC5 #1 Tue Mar 14 15:48:33 EST 2006 i686
SNMPv2-MIB::sysObjectID.0 = OID: NET-SNMP-MIB::netSnmpAgentOIDs.10
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (2289589) 6:21:35.89
SNMPv2-MIB::sysContact.0 = STRING: Root (configure /etc/snmp/snmp.local.conf)

g.防火牆

如果不是觀察本機端 SNMP,請記得對方要開啟 UDP PROT 161

3.設定 MRTG

架構說明

觀察對象的 Community String:hplus
輸出設定目錄:/etc/mrtg/
輸出網頁目錄:/opt/lampp/htdocs/mrtg/
目的:建立主機流量圖

a.產生 cfg 檔案
執行以下命令

bang@linuxdiary:~$ cfgmaker --global 'WorkDir: /opt/lampp/htdocs/mrtg' \
--global 'Options[_]: growright' \
--output /etc/mrtg/network.cfg \
hplus@localhost

說明:更多 cfgmaker 設定請參考
http://mrtg.cs.pu.edu.tw/doc/cfgmaker.en.html

b.執行 mrtg

bang@linuxdiary:~$ env LANG=C /usr/bin/mrtg /etc/mrtg/ network.cfg

說明:會出現多行警告訊息如下

Rateup WARNING: /usr/bin/rateup Can't remove localhost_2.old updating log file

請重複執行命令,直到警告消失為止

c.產生網頁

bang@linuxdiary:~$ indexmaker /etc/mrtg/network.cfg > /opt/lampp/htdocs/mrtg/network.html

說明:輸出的網頁只是一種顯示方式,可依據喜好編輯網頁。

e.檢視網頁是否建立成功

http://localhost/mrtg

你應該要看到以下圖片

f.自動排程

編輯 /etc/cron.d/mrtg ,加入已下一行

*/5 * * * * root env LANG=C /usr/bin/mrtg /etc/mrtg/network.cfg

說明:每五分鐘執行一次,需重新啟動 crond

bang@linuxdiary:~$ /etc/rc.d/init.d/crond restart

4.其他觀察指標

下載 mrtg.zip,利用現成範例檔案,完成其他觀察指標,以記憶體為例,解開壓縮檔後,上傳 mem.cfg 至 /etc/mrtg,並編輯

Target[freemem]: .1.3.6.1.4.1.2021.4.11.0&.1.3.6.1.4.1.2021.4.11.0: hplus@localhost
Title[freemem]: Free Memory
MaxBytes[freemem]: 10000000
kMG[freemem]: k,M
YLegend[freemem]: bytes
ShortLegend[freemem]: bytes
PageTop[freemem]: Free memory, not including swap, in bytes
Legend1[freemem]: Free memory, not including swap, in bytes
LegendI[freemem]: Free Memory:
LegendO[freemem]:
Options[freemem]: nopercent,growright,gauge,noinfo
WorkDir:/opt/lampp/htdocs/mrtg/
Language:big5

編輯檔案時,需確認 Community String@觀察 IP,輸出網頁目錄是否正確。
完成後,執行回到之前 設定 MRTG介紹,重複執行 b,c,e,f 動作即可。

同理依序上傳並編輯各個檔案,包含

AttemptFails.cfg
連線嘗試失敗
cpu_act.cfg
CPU 負載率
cpu_idle.cfg
CPU 閒置率
cpu_usr.cfg
CPU 使用者與系統使用率
dsk.cfg
磁碟使用率
estabcons.cfg
目前連線數
hrStorage.cfg
磁碟目前用量
mem.cfg
記憶體使用率
newconn.cfg
新建立連線數

5.監控硬碟項,要注意的地方

a.監控硬碟必須先知道我們掛載的情況

輸入命令

bang@linuxdiary:~$ df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
471016352 14887036 431817100 4% /
/dev/sda1 101086 10960 84907 12% /boot
tmpfs 1037820 0 1037820 0% /dev/shm

利用以上資訊,編輯 snmp.conf

b.然後在 snmp.conf 指定硬碟

開啟 /etc/snmp.conf`,尋找 # disk checks
你可以看到一些設定範例,可將 df 所得資訊,插入以下兩行

disk / 471016352       #代表第一個要觀察的分割區

disk /boot 101086 #代表第二個要觀察的分割區

c.於/etc/mrtg 編輯 dsk.cfg,內容如下

LoadMIBs: /usr/share/snmp/mibs/UCD-SNMP-MIB.txt
Target[kontor.root]:dskPercent.1&dskPercent.2:MY_SRTING@localhost
MaxBytes[kontor.root]: 100
Title[kontor.root]: DISK USAGE
PageTop[kontor.root]: DISK / and /boot Usage %
Unscaled[kontor.root]: ymwd
ShortLegend[kontor.root]: %
YLegend[kontor.root]: DISK Utilization
Legend1[kontor.root]: Root disk
Legend2[kontor.root]: /boot disk
Legend3[kontor.root]:
Legend4[kontor.root]:
LegendI[kontor.root]: Root disk
LegendO[kontor.root]: /boot disk
Options[kontor.root]: growright,gauge,nopercent
WorkDir:/opt/lampp/htdocs/mrtg

編輯檔案時,只需確認 Community String,觀察 IP,輸出網頁目錄是否正確。
完成後,執行 設定 MRTG:b,c,e,f 動作即可。

6.編輯 index.html

這是最後結果,可視情況要不要作,當所有範例檔都修改並上傳完成,可根據範例包中 index.html,
修改連結與圖片正確的位址,即完成所有安裝。

範例

<div>
<a href="freemem.html"><img border="1" alt="freemem Traffic Graph" src="freemem-day.png"></a><br>
<small><!--#flastmod file="kontor.root.html" --></small>
</div>