Mrtg安裝筆記:
作者:衡山飛狐
出處:HappyLinux,Just for fun!
本 著作 係採用 Creative Commons 授權條款授權.
前一台主機(CLE 1.0)是有裝MRTG這玩意的,但是換了新電腦及Gentoo Linux後就 一直沒裝MRTG(超過一年了),這幾天花時間再把MRTG裝上去,發現有些跟以前裝的時候不太一樣,順便做個安裝筆記。
一、MRTG簡介:
MRTG ( Multi Router Traffic Grapher) 是一套非常普遍的流量管理軟體,可以即時監測網路狀況並繪製成圖表,也由於MRTG可由數據的產生便能輕易的繪成圖表,因此可藉由一些外掛程式(如Shell Script,perl…)等來生成一些系統的監控圖表,如CPU負載、CPU溫度、RAM 使用率、網路卡流量監測、連線品質甚至天氣溫度變化等等,總之只要能產生數據,便能利用MRTG產生Time-Series的圖表,不過MRTG最多一個統計項只能包含兩個數值,功能上較rrdtool上來的小。
二、snmp安裝:
OS:Gentoo Linux。
2-1.安裝snmp協定(若不監控本機則非必要)。
2-1-1:本機
emerge net-snmp
然後然後編輯一個新的檔案(/usr/share/snmp/snmpd.conf):
nano -w /usr/share/snmp/snmpd.conf
內容:
rocommunity mrtg
這個檔案的內容只有短短一行便搞定,在這裡花了不少時間。
啟動本機snmpd服務:
/etc/init.d/snmpd start
開機時啟動snmpd服務:
rc-update add snmpd default
2-1-2:ADSL Modem
因為所有對外連線都是靠一台ADSL數據機連出去,一般中華電信的ADSL數據機都有SNMP功能,不過有些被disable了,必須打開才有。我的ADSL數據機是TECOM-AR4031B,經由http://www./vbb3/showthread.php?t=129054查知預設帳號密碼。
用瀏覽器http://192.168.1.1/將ADSL SNMP功能打開,然後reboot。
三、Mrtg安裝:
3-1:安裝
emerge mrtg
mkdir /var/www/localhost/htdocs/mrtg
mkdir /var/www/localhost/htdocs/mrtg/net
3-2:設定
MRTG 必須依照.cfg 檔案內容來繪製圖表,所以必須建立被控端專屬的.cfg 檔:
cfgmaker mrtg@192.168.1.1 >> /var/www/localhost/htdocs/mrtg/net/mrtg.cfg
192.168.1.1為我的ADSL數據機IP
cfgmaker mrtg@192.168.1.10 >> /var/www/localhost/htdocs/mrtg/net/mrtg.cfg
192.168.1.10為我的Gentoo Linux主機IP
修改 mrtg.cfg
nano -w /var/www/localhost/htdocs/mrtg/net/mrtg.cfg
mrtg.cfg檔案內容修改的部分:
workdir: /var/www/localhost/htdocs/mrtg/net
將這個工作目錄修改成之前所建立的目錄
Options[_]: growright, bits
預設顯示單位為 BITS 如要修改成 BYTES,將逗號之後拿掉即可(包含逗號)
Language: big5
3-3:測試
測試設定的cfg檔案是否可以正常執行:
/usr/bin/mrtg /var/www/localhost/htdocs/mrtg/net/mrtg.cfg
第一次安裝 必須執行該指令三次以上且無任何錯誤產生
indexmaker /var/www/localhost/htdocs/mrtg/net/mrtg.cfg >/var/www/localhost/htdocs/mrtg/net/index.html
四、其他外掛程式:
4-1:lm_sensors(Hardware Sensors Monitoring)
安裝lm_sensors尚需同時安裝i2c(I2C Bus support),在gentoo只需安裝lm_sensors,portage系統會依相依性先安裝i2c再安裝lm_sensors。
* sys-apps/lm_sensors
Latest version available: 2.8.7
Latest version installed: 2.8.7
Size of downloaded files: 772 kB
Homepage: http://www2./~lm78
Description: Hardware Sensors Monitoring by lm_sensors
License: GPL-2* sys-apps/i2c
Latest version available: 2.8.7
Latest version installed: 2.8.7
Size of downloaded files: 136 kB
Homepage: http://www2./~lm78/
Description: I2C Bus support for 2.4.x kernels
License: GPL-2
4-2:設定及測試lm_sensors
指令:
sensors-detect
We can start with probing for (PCI) I2C or SMBus adapters. You do not need any special privileges for this. Do you want to probe now? (YES/no):
………………(以下省略)
執行sensors-detect只要一直按Enter就好了。
To make the sensors modules behave correctly, add these lines to /etc/modules.conf:
#—-cut here—-
# I2C module options
alias char-major-89 i2c-dev
#—-end cut here—-
#—-cut here—-
# I2C adapter drivers
modprobe i2c-isa
# I2C chip drivers
modprobe w83627hf
指令:
sensors
顯示結果:
eeprom-i2c-0-50
Adapter: SMBus nForce2 adapter at 5000
Memory type: DDR SDRAM DIMM
Memory size (MB): 256w83627hf-isa-0290
Adapter: ISA adapter
VCore 1: +1.58 V (min = +1.89 V, max = +2.29 V)
VCore 2: +1.58 V (min = +1.89 V, max = +2.29 V)
+3.3V: +2.59 V (min = +2.82 V, max = +3.79 V)
+5V: +4.92 V (min = +4.38 V, max = +4.30 V)
+12V: +11.93 V (min = +13.91 V, max = +4.26 V)
-12V: -7.20 V (min = -11.47 V, max = -3.40 V)
-5V: -5.39 V (min = +4.76 V, max = +0.39 V)
V5SB: +5.46 V (min = +2.82 V, max = +5.75 V)
VBat: +3.10 V (min = +1.42 V, max = +1.62 V)
fan1: 2045 RPM (min = 8881 RPM, div = 4)
fan2: 0 RPM (min = 2836 RPM, div = 4)
fan3: 3375 RPM (min = 969 RPM, div = 8 )
temp1: +37°C (high = +69°C, hyst = -108°C) sensor = thermistor
temp2: +54.5°C (high = +100°C, hyst = +95°C) sensor = thermistor
temp3: +37.5°C (high = +100°C, hyst = +95°C) sensor = thermistor
vid: +2.100 V
alarms:
beep_enable:
Sound alarm disabled
4-3:開機便執行lm_sensors
rc-update add lm_sensors default
4-4:一個獲得CPU溫度數據然後餵送給MRTG繪圖的範例
由sensors的結果輸出便可寫成shell擷取所需要的數值:
mrtg_cpu_temp.sh內容:
#!/bin/bash
systemp=`sensors | grep temp3 |awk ‘{print $2}‘|cut -c 2-5`
cputemp=`sensors | grep temp2 |awk ‘{print $2}‘|cut -c 2-5`
UPtime=`/usr/bin/uptime | awk ‘{print $3 " " $4 " " $5}‘`
echo $cputemp
echo $systemp
echo $UPtime
echo gen2.homeip.net
mrtg_cpu_temp.sh輸出結果:
54.5
37.5
6 days, 12:09,
gen2.homeip.net
由第一行及第二行的數據便可讓mrtg來繪製圖表:
將下面內容加入/var/www/localhost/htdocs/mrtg/net/mrtg.cfg
Target[192.168.1.10_6]: `/usr/bin/mrtg_cpu_temp.sh`
MaxBytes[192.168.1.10_6]: 70
Options[192.168.1.10_6]: gauge, nopercent, growright
YLegend[192.168.1.10_6]: CPU Temperature (℃)
ShortLegend[192.168.1.10_6]: ℃
LegendO[192.168.1.10_6]: 系統溫度;
LegendI[192.168.1.10_6]: CPU溫度;
Title[192.168.1.10_6]: CPU溫度紀錄表
PageTop[192.168.1.10_6]: <H1>gen2 主機 CPU溫度紀錄表</H1>
<TABLE>
<TR><TD>System:</TD> <TD>Linux 2.4.25-gentoo-r2</TD></TR>
</TABLE>
如此便可以mrtg來繪製CPU溫度變化了。
說明:
Target:乃資料取得方式。
MaxBytes:限制最大值。
Title: HTML 網頁的title 語法。
PageTop: 在顯示畫面的標題。
Oprions: 採用標準格式,並且讓MRTG由右往左繪圖。
YLegend: 圖表的Y軸名稱。
ShotLegend: 圖表Y 軸的單位。
mrtg_ping.pl
#!/usr/bin/perl
$host="139.175.10.20";
$ping_str=`/bin/ping -c 3 -n -q $host`;
$val=(split(‘/‘,$ping_str))[4];
$val2=(split(‘/‘,$ping_str))[5];
$val=int($val);
$val2=int($val2);
print "$val\n";
print "$val2\n";
print "0\n";
print "0\n";
五、加入crontab排程:
mrtg_crontab.sh內容:
/usr/bin/mrtg /var/www/localhost/htdocs/mrtg/net/mrtg.cfg
crontab內容:(每5分鐘執行一次)
*/5 * * * * /usr/bin/mrtg_crontab.sh > /dev/null 2>&1
六、本站mrtg圖表紀錄:(已載入成為XOOPS模組)
MRTG Index Page
【圖一】:流量分析 – 外部網路
【圖二】:流量分析 – 內部網路
【圖三】:gen2 主機 CPU 負載率
【圖四】:gen2 主機 CPU溫度紀錄表
【圖五】:gen2 主機 風扇轉速紀錄表
【圖六】:gen2 主機 Core電壓紀錄表( x 1/100)
【圖七】:gen2 主機 記憶體空餘紀錄表
【圖八】:gen2 <==>SeedNet 連線品質紀錄表
【圖九】:gen2連線取樣紀錄表
【圖十】:ADSL 流量分析 – AR4031B
七、參考資料:
Gentoo Linux Taiwan 文件 – MRTG 安裝手冊
Study-Area MRTG文件
MRTG-Ping
《教學》將MRTG變成XOOPS模組一份子