歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Unix知識 >> 關於Unix >> 用bandwidthd進行網絡流量檢測和分析

用bandwidthd進行網絡流量檢測和分析

日期:2017/3/6 15:22:44   编辑:關於Unix
又一篇教學檔 我的原創,分享給大家,覺得好的就頂一下啦,以便更多的人共同探索,發現問題,解決問題,總結問題.謝謝 在流量統計軟件中有MRTG,NTOP,BANDWIDTHD等多種, MRTG 只是顯示某一 網絡 接口的總量, NTOP詳細得來也很占系統資源. 但不知道大家用過bandwidthd 又一篇教學檔


我的原創,分享給大家,覺得好的就頂一下啦,以便更多的人共同探索,發現問題,解決問題,總結問題.謝謝

在流量統計軟件中有MRTG,NTOP,BANDWIDTHD等多種,
MRTG 只是顯示某一網絡接口的總量,
NTOP詳細得來也很占系統資源.
但不知道大家用過bandwidthd沒有,我就很喜歡它,因為它精簡,直觀.很容易配置.
好處:在任何一台電腦可以通過web界面用浏覽器查看經過網關的各個ip流量,而且是分協議,分顏色顯示,

已經有直觀的圖象曲線.

在日常維護中, 網管人員最頭痛的是內部網經常有人在大量傳送文件而導致本來可憐的帶寬變得更加狼狽

.如果在網關上裝上'bandwidthd' ,就可以追蹤的是各個的 IP 的流量,而且可以用圖象曲線顯示各個ip

的相應不同協議顯示,還能分時段查看
例如包含HTTP, TCP, UDP, ICMP, VPN 以及 P2P 協議的各自流量,以ip為統計對象.
默認的顏色,TCP為綠色;HTTP為藍色;FTP為淺藍;P2P為粉紅;UDP為棕褐色; ICMP為紅色 . 打了補丁還可以

增加你想檢測端口.

例子:
http://bandwidthd.sourceforge.net/demo/

安裝 bandwidthd 很簡單.
1.條件:要先安裝有以下庫:
如果沒有請到相關網址下載:
libpcap 相關網址 http://www.tcpdump.org/
libpng相關網址(http://www.libpng.org/
gd相關網址 http://www.boutell.com/gd/
系統須有支持 System V IPC (基本上Linux都有)

RedHat 9.0 自帶的rpm包也可以,要裝有下面的包:
gd-1.8.4-4
gd-devel-1.8.4-4
libpng-1.2.2-16
libpng-devel-1.2.2-16
libpcap-0.7.2-1
分別執行 rpm -ivh 軟件包 進安裝即可.

2.下載:
bandwidthd原始網址如下:
http://bandwidthd.sourceforge.net/
裡面有下載地址的連接,目前最新的版本是 bandwidthd 2.0.1
下載bandwidthd-1.2.1b.tgz 放在/home/中(這個目錄是我的習慣而已).

3. 安裝

3.1 tar -xvzf bandwidthd-2.0.1.tgz
顯示:
[root@mail bandwidthd]# tar zxvf bandwidthd-2.0.1.tgz
bandwidthd-1.2.1b/
bandwidthd-1.2.1b/etc/
bandwidthd-1.2.1b/etc/bandwidthd.conf
bandwidthd-1.2.1b/CHANGELOG
bandwidthd-1.2.1b/Makefile
bandwidthd-1.2.1b/Makefile.BSD
bandwidthd-1.2.1b/Makefile.Solaris
bandwidthd-1.2.1b/README
bandwidthd-1.2.1b/TODO
bandwidthd-1.2.1b/bandwidthd-0.4.kdevprj
bandwidthd-1.2.1b/bandwidthd.c
bandwidthd-1.2.1b/bandwidthd.h
bandwidthd-1.2.1b/conf.l
bandwidthd-1.2.1b/conf.y
bandwidthd-1.2.1b/graph.c
bandwidthd-1.2.1b/htdocs/
bandwidthd-1.2.1b/htdocs/index.html
bandwidthd-1.2.1b/htdocs/legend.gif
bandwidthd-1.2.1b/htdocs/logo.gif
bandwidthd-1.2.1b/htdocs/index2.html
bandwidthd-1.2.1b/htdocs/index3.html
bandwidthd-1.2.1b/htdocs/index4.html
bandwidthd-1.2.1b/conf.tab.c
bandwidthd-1.2.1b/conf.tab.h
bandwidthd-1.2.1b/conf.l.c

3.2 cd bandwidthd-2.0.1

3.3 執行 ./configure && make install
顯示:
[root@mail bandwidthd-1.2.1b]# make install
cc -I/usr/local/include -O3 -Wall -c -o bandwidthd.o bandwidthd.c
cc -I/usr/local/include -O3 -Wall -c -o graph.o graph.c
bison -d conf.y
cc -I/usr/local/include -O3 -Wall -c -o conf.tab.o conf.tab.c
lex -s -i -t -I conf.l > conf.l.c
cc -O3 -c -o conf.l.o conf.l.c
cc -I/usr/local/include -O3 -Wall -c -o tables.o tables.c
cc -I/usr/local/include -O3 -Wall bandwidthd.o graph.o conf.tab.o conf.l.o tables.o -o

bandwidthd -L/usr/local/lib -lgd -lpng -lpcap
if [ ! -d /usr/local/bandwidthd/etc ] ; then mkdir -p /usr/local/bandwidthd/etc ; fi
if [ ! -d /usr/local/bandwidthd/htdocs ] ; then mkdir -p /usr/local/bandwidthd/htdocs ; fi
cp bandwidthd /usr/local/bandwidthd
if [ ! -f /usr/local/bandwidthd/etc/bandwidthd.conf ] ; then cp etc/bandwidthd.conf

/usr/local/bandwidthd/etc/ ; fi
cp htdocs/legend.gif /usr/local/bandwidthd/htdocs/
cp htdocs/logo.gif /usr/local/bandwidthd/htdocs/

3.5 至此即已安裝完成,它會將 bandwidthd 安裝 至 /usr/local/bandwidthd 下

4. 設定,配置bandwidthd.conf
bandwidthd 的設定也很簡單,一個配置文件搞定,
一般修改一下網段和掩碼,其他的用默認值即可,如果你熟悉英文,就看看其他選項.大概如下
編輯:
/usr/local/bandwidthd/etc/bandwidthd.conf
####################################################
# Bandwidthd.conf
#
# Commented out options are here to provide
# documentation and represent defaults

# Subnets to collect statistics on
#subnet 10.0.0.0 255.0.0.0
#subnet 208.16.191.0 255.255.255.0
subnet 192.168.1.250 255.255.255.0 #以一網段為例
# Device to listen on
dev "eth0" #(這是你要檢測的網卡,可以調整為對應的網絡連接設備)
把其中 subnet 的設定,以一網段為例:
subnet 192.168.1.0 255.255.255.0
###################################################
# Options that don't usually get changed
# An interval is 2.5 minutes, this is how many
# intervals to skip before doing a graphing run
#skip_intervals 0 #默間隔認2.5 minutes 刷新

# Graph cutoff is how many k must be transfered by an
# ip before we bother to graph it
#graph_cutoff 1024 #默間1M 以上的流量才有圖形

#Put interface in promiscuous mode to score to traffic
#that may not be routing through the host machine.
#promiscuous true #讓網卡在混雜模式中記錄

#Log data to cdf file htdocs/log.cdf 在bandwidthd中生成log2.cdf log.cdf格式數據記錄
output_cdf true

#Read back the cdf file on startup 在啟動bandwidth時重新讀取cdf的數據
recover_cdf true

#Libpcap format filter string used to control what bandwidthd see's
#Please always include "ip" in the string to avoid strange problems
#以ip為過濾對象
filter "ip"

#Draw Graphs
graph true

存盤之後,便可執行。

5. 運行 ,要先轉到安裝目錄才能執行
cd /usr/local/bandwidthd
./bandwidthd

6. 顯示:
在web 主目錄下做個軟連接,執行
ln -s /usr/local/bandwidthd/htdocs bandwidthd
接著您便可用 http://你的主機/bandwidthd

PS:web主目錄,如果使用linux9.0默認apache設置,則在/var/www/html下;DocumenRoot在/etc/httpd/conf/httpd.conf下查看

這樣就可以在浏覽器裡面檢測你需要的流量了

另外,更多配置方法:
1. 把設定檔中的 dev 增加更多的dev,或設成 any 可以監控更多設備
2. 如果認為TOP20太少了,
就在還沒MAKE時候先改graph.c
graph.c中查找
for (Counter=0; Counter < 21 && Counter < NumIps; Counter++)
把21改成101
就變成TOP100
然後查找TOP20改為字符串TOP100
然後存盤,再make install 即可.

此方法我已經實踐通過,特意寫成文檔,以分享並感謝提供bandwidthd的開發者,如果覺得有更好的配置和

使用方法,請互相交流.

本bandwidthd進行網絡流量檢測和分析如果結合tc+iptables進行流量控制,是一個絕妙的組合. 這樣你就可以隨時檢測和控制網內所有機器及端口的流量了.

關於tc+iptables可以參考http://bbs.chinaunix.net/forum/viewtopic.php?t=482589

更多的bandwidthd可以參考: http://free.tnc.edu.tw/modules/news/article.php?storyid=710

Copyright © Linux教程網 All Rights Reserved