[Ports] Trafd 統計流量

#cd /usr/ports/net-mgmt/trafd
#make install clean
它會跑一隻 daemon , 去 log 指定 interface 收到封包的資料
預設是記下 packet 的 protocol、src ip、dst ip、src port、dst port、size
同一個 socket 的資料 , 會把 size 往上累加
因此不會說收到多少 packets 就有幾筆資料 …
用 ports 簡單裝完後
到 /etc/rc.conf 加幾行
trafd_enable="YES"
trafd_ifaces="rl0" # depend on your interface
trafd_flags="-p" # don't put interface into promiscuous mode
trafd_log="/var/log/trafd.log" # daemon log position
From:http://blog.outian.net/archives/211

然後
cp /usr/local/etc/rc.d/trafd.sh.sample /usr/local/etc/rc.d/trafd.sh
/usr/local/etc/rc.d/trafd.sh start
就會動了
( ps ax | grep trafd 應該會看到 process 在跑 )
在 /usr/local/bin 會有幾隻 traf* 開頭的工具
(/usr/local/share/doc/trafd/README 有說明)
我比較常用的是
trafstat -n -i fxp0 就可以看目前累積的流量狀況
然後為什麼會需要這隻 daemon 呢 ?
首先學術網路上的各校都有流量限制 , 東海的限制是一天 in+out 10G
既然我和計中關係密切 , 就更不能監守自盜惡搞了
於是就要控制自己機器的流量 …
但是在區分流量的時候 , 要把對校內和對校外的分開來
否則校內隨便抓就破 10G .. 然後停掉service的話外面都不用抓了 :p
於是寫隻 script 放 crontab
去 parse "trafstat -n -i fxp0" 的結果
把 對校內/非校內 的流量區分開來
當發現對校外的流量達到設定的限制 (8G左右)
就修改 ftp daemon 的 .ftpaccess , 改成限校內 ip 進入
每天凌晨 00:00 , 學校的 netflow 重新計算的時候
也跑 /usr/local/bin/trafsave fxp0 把結果 reset
再把 .ftpaccess 的設定改成全開
這樣就沒什麼問題了 ….
但是前陣子發現怎麼有時還是會爆流量 …
於是trace了一下 …..
發現有時 trafstat 會噴這樣的結果出來 -
From Port To Port Proto All
140.128.101.156 60272 140.128.194.76 client tcp -2045143144
怎麼合計起來會有 - 的大小咧 !?
查了一下 source , 原來在 trafstat/trafstat.c:171 是這樣寫的
printf("%-4.4s %9ld %10ldn", proto, entries[i].n_bytes, entries[i].n_psize);
首先是 %ld 只能噴 –2,147,483,648 ~ 2,147,483,647 ,
先改一下 … 改用 %lu … 馬上現賺一倍 …
再者 struct definition 長這樣

struct t_entry {
struct in_addr in_ip, out_ip; /* src ip addr and dst ip addr */
u_char ip_protocol;    /* which protocol been used (/etc/protocols)*/
u_char who_srv;     /* who was server flag */
u_short p_port;      /* which port been used (/etc/services) */
u_long n_psize;     /* how many bytes in ip datagrams passed */
u_long n_bytes;     /* how many data bytes passed */
} *entries;

u_long 只能 handle 到 4294967295 ….
設計的人也沒想到同一組 socket 會傳到把 u_long 爆掉吧 :p
有空再來 trace 一下其他部份 …
看有沒有需要把 u_long (4 bytes) 整個換去 long long (8 bytes) ….
然後 printf 時要用 %llu …
---------From:rossen.bbs@bbs.mgt.ncu.edu.tw & wilson@bbs.ee.ntu.edu.tw----------
經過安裝後的執行檔都會擺在 /usr/local/bin
trafd - ip traffic collector daemon.
trafdump - Script to dump current traffic to disk.
trafsave - Script to save current traffic to disk.
trafstart - Startup script for trafd.
trafstop - Shutgown script for trafd.
trafstatd - ip traffic statics daemon.
trafstat - ip traffic statics.
trafd.sh - Start/stop script for trafd, placed to the `local startup
directory' (*/rc.d).
其中 trafd.sh 是用來啟動/停止 trafd 的 shell script, 可以放在 rc.d
(在 trafd 3.0.1 沒有附這隻程式, 要自己寫=>使用 trafstart/trafstop)
在 /usr/local/doc/trafd/README 是這隻程式的小小說明檔 (這程式目前沒有man!)
另外 trafstart 需要手動修改一下內容:
原本 ./trafd -p -r -i xl0 這行要改成 (xl0=要監視流量的網路卡)
/usr/local/bin/trafd -p -r -i xl0 -F /etc/trafd.filter
^^^^^^^^^^^^^^^^^過濾規則定義檔
接著編寫 /etc/trafd.filter (兩行連成一行)
(dst host 140.115.xxx.2 and src net ! 140.115.xxx.0/24) or
(src host 140.115.xxx.2 and dst net ! 140.115.xxx.0/24)
PS.此處的過濾規則主要是以紀錄自身的流進/流出量為主,
trafd 的過濾規則語法其實就跟 tcpdump 一樣,可參考 man tcpdump
然後就要把前面的 trafdump 及 trafsave 排入 cron 定時執行,
依照 README 的建議,trafdump 執行的越頻繁越好
但 trafsave 每天只需執行一次 (trafsave 會覆寫舊的流量記錄, trafdump 不會)
0,10,20,30,40,50 * * * * /usr/local/bin/trafdump xl0
5 0 * * * /usr/local/bin/trafsave xl0
基本的設定已經都完成了,只需要執行 trafd.sh start 就可以錄下流量
然後使用 trafstat 觀看
使用文字介面觀看流量多少有些不便,
這時可利用 http://bpft4.sourceforge.net/ 右邊的 Add-on 裡的 perl script
來計算每日/每月的流量,並且還可以把流量統計轉成 html 方便觀看
但是使用 README 建議的方法還是有其缺點,
根據觀察 trafd 的負載是跟他執行的時間成正比,雖然一開始所佔的負載還不到 5%
但是如果一天只執行一次 trafsave 的話,
trafd 在尚未歸零的狀況下負載可達到 30% (執行12小時沒有使用 trafsave)
對於處理器速度較慢的機器來說會是另一種負擔...
所以又有另一種方式可以統計流量
也是利用 http://bpft4.sourceforge.net/ 右邊的 Add-on 裡的 perl script
定時把流量直接寫入 mysql 資料庫中 (程式中使用 traflog)
這樣一來便可以縮短執行 trafsave 的間隔時間,降低系統負載
而統計流量的工作就交給 mysql+php 來做,一樣可以做成 html 來觀看^^
文件上說還有 trafshow 及 traflog ,
但 trafshow 並沒有包含在 bpft 或 trafd 內 , 而是另外單獨裝 ports/net/trafshow
可以觀看即時的網路流量 .
而 traflog 可用來看歷史的流量記錄
裝的 bpft 可以 man trafd , trafstatd , trafstat , traflog
在我的印象中 , trafstat 只能看最近一次 trafsave 之後的流量
以本範例 , 就是只能看當日 00:05 之後的流量
要看歷史的流量 , 要用 traflog ,
若每日只 trafsave 一次 , 好像只能做到日統計資料 , 不能看到小時統計情形
要看到小時的統計 , 就要每小時 trafsave 一次

Please follow and like us:

One comment on “[Ports] Trafd 統計流量

  1. 如果要照上面寫的:
    /usr/local/bin/trafd -p -r -i xl0 -F /etc/trafd.filter
    這樣一定要用命令列~
    即使更改/usr/local/bin/trafstart裡面的參數也沒用~
    所以要在/usr/local/etc/rc.d/trafd.sh裡面動手腳:
    把本來start那一段的 ${command} -i ${iface} ${trafd_flags} 後面繼續加上參數,就變成:
    ${command} -i ${iface} ${trafd_flags} -r -F /etc/trafd.filter
    這樣只要把/etc/rc.conf設定好,就可以在開機後執行了~ :mrgreen:

denniswave 發表迴響 取消回覆

你的電子郵件位址並不會被公開。 必要欄位標記為 *