qphoney記

beta

iptrafでトラフィックを感じる生活

トラフィックを肌で感じる季節になってきましたね。

というわけで、iptrafについてちょっと調べたのでログに残しておきます。

iptrafとは

トラフィックを素敵に見える化してくれるツールです。

本家
http://iptraf.seul.org/

repoforge
http://pkgs.repoforge.org/iptraf/

yumのリポジトリ追加は適当にググッて下さい。
今回はCentOS5.7にyumでインストールしました。

# yum install iptraf

実行は以下のコマンド
(※実行はrootアカウントでね♪)

# iptraf

f:id:qphoney:20111209154851p:image

実行すると以下のような対話式のシェルが起動します。
このシェルの中で操作するとリアルタイムのトラフィック状況が確認出来ます。

f:id:qphoney:20111209154852p:image

他にもトラフィックを監視するツールは存在しますが、今回iptrafを採用した理由は

  • バックグラウンドで実行出来る
  • ログ出力が(簡単に)出来る

という点。
Webで調べただけなので、もしかしたら他のツールでもログ出力が出来るかも知れません。

主な使い方とログ出力サンプル

詳細はmanなり、Google先生で調べていただく方が早いと思います。
ここでは簡単に使い方を
リアルタイムでトラフィックを見たい時はiptrafを引数なしに実行します。

# iptraf

バックグラウンドで実行する場合は -B を引数に与えます。

# iptraf -B

この状態で実行するとプロセスをkillするまで実行し続けます。
時間を指定して止める場合は -t をつけて時間を指定(分)します。
以下の例は5分間の統計をログに出力します。

# iptraf -B -t 5

出力内容はいくつか選択出来ます。
以下はman(8)からの抜粋です

       -i iface
              immediately start the IP traffic monitor on the specified  interface,  or
              all interfaces if "-i all" is specified

       -g     immediately start the general interface statistics

       -d iface
              allows  you  to immediately start the detailed on the indicated interface
              (iface)

       -s iface
              allows you to immediately monitor TCP and UDP traffic  on  the  specified
              interface (iface)

       -z iface
              shows packet counts by size on the specified interface

       -l iface
              start  the  LAN  station  monitor  on the specified interface, or all LAN
              interfaces if "-l all" is specified

-i オプションをつける事でtcpdumpライクなIPアドレス同士の通信が記録されます。
-i オプションをつけたログのサンプルは以下になります。

Fri Dec  9 14:57:49 2011; ******** IP traffic monitor started ********
Fri Dec  9 14:57:49 2011; TCP; eth0; 76 bytes; from 192.168.10.15:22 to 192.168.10.10:62000; first packet
Fri Dec  9 14:57:49 2011; TCP; eth0; 46 bytes; from 192.168.10.10:62000 to 192.168.10.15:22; first packet
Fri Dec  9 14:58:18 2011; UDP; eth0; 139 bytes; from 192.168.10.13:17500 to 255.255.255.255:17500
Fri Dec  9 14:58:18 2011; UDP; eth0; 139 bytes; from 192.168.10.13:17500 to 192.168.10.255:17500
Fri Dec  9 14:58:43 2011; UDP; eth0; 78 bytes; from 192.168.10.9:137 to 192.168.10.255:137
Fri Dec  9 14:58:43 2011; UDP; eth0; 78 bytes; from 192.168.10.9:137 to 192.168.10.255:137
Fri Dec  9 14:58:44 2011; UDP; eth0; 78 bytes; from 192.168.10.9:137 to 192.168.10.255:137
Fri Dec  9 14:58:48 2011; UDP; eth0; 139 bytes; from 192.168.10.13:17500 to 255.255.255.255:17500
Fri Dec  9 14:58:48 2011; UDP; eth0; 139 bytes; from 192.168.10.13:17500 to 192.168.10.255:17500
Fri Dec  9 14:58:49 2011; ******** IP traffic monitor stopped ********

-g オプションをつける事でおおざっぱな情報が記録されます。
この解説がおおざっぱというのは触れないで下さい。
-g オプションをつけたログのサンプルは以下になります。

Fri Dec  9 15:02:15 2011; ******** General interface statistics started ********

*** General interface statistics log generated Fri Dec  9 15:03:15 2011

lo: 0 total, 0 IP, 0 non-IP, 0 IP checksum errors, average activity 0.00 kbits/s, peak activity 0.00 kbits/s, last 5-second activity 0.00 kbits/s
eth0: 98 total, 98 IP, 0 non-IP, 0 IP checksum errors, average activity 1.02 kbits/s, peak activity 4.20 kbits/s, last 5-second activity 0.00 kbits/s

60 seconds running time
Fri Dec  9 15:03:15 2011; ******** General interface statistics stopped ********

-d オプションをつける事で -g オプションよりもう少し詳細だけどおおざっぱな情報が記録されます。
-d オプションをつけたログのサンプルは以下になります。

Fri Dec  9 14:52:41 2011; ******** Detailed interface statistics started ********

*** Detailed statistics for interface eth0, generated Fri Dec  9 14:53:41 2011

Total:  116 packets, 10679 bytes
        (incoming: 65 packets, 5769 bytes; outgoing: 51 packets, 4910 bytes)
IP:     116 packets, 8893 bytes
        (incoming: 65 packets, 4697 bytes; outgoing: 51 packets, 4196 bytes)
TCP: 106 packets, 7404 bytes
        (incoming: 55 packets, 3208 bytes; outgoing: 51 packets, 4196 bytes)
UDP: 10 packets, 1489 bytes
        (incoming: 10 packets, 1489 bytes; outgoing: 0 packets, 0 bytes)
ICMP: 0 packets, 0 bytes
        (incoming: 0 packets, 0 bytes; outgoing: 0 packets, 0 bytes)
Other IP: 0 packets, 0 bytes
        (incoming: 0 packets, 0 bytes; outgoing: 0 packets, 0 bytes)
Non-IP: 0 packets, 0 bytes
        (incoming: 0 packets, 0 bytes; outgoing: 0 packets, 0 bytes)
Broadcast: 10 packets, 1629 bytes

Average rates:
  Total:        1.42 kbits/s, 1.93 packets/s
  Incoming:     0.77 kbits/s, 1.08 packets/s
  Outgoing:     0.65 kbits/s, 0.85 packets/s

Peak total activity: 6.24 kbits/s, 9.00 packets/s
Peak incoming rate: 2.64 kbits/s, 4.60 packets/s
Peak outgoing rate: 3.60 kbits/s, 4.40 packets/s

IP checksum errors: 0

Running time: 60 seconds
Fri Dec  9 14:53:41 2011; ******** Detailed interface statistics stopped ********

-s オプションをつける事でTCP/UDPの使用されたportまで含めた情報が記録されます。
詳しく確認していませんが、恐らく待受ているportに対しての通信内容だと思います。
-s オプションをつけたログのサンプルは以下になります。

Fri Dec  9 14:58:18 2011; ******** TCP/UDP service monitor started ********

*** TCP/UDP traffic log, generated Fri Dec  9 14:59:18 2011

TCP/22: 210 packets, 15712 bytes total, 2.08 kbits/s; 114 packets, 7296 bytes incoming, 0.97 kbits/s; 96 packets, 8416 bytes outgoing, 1.12 kbits/s

UDP/137: 3 packets, 234 bytes total, 0.03 kbits/s; 3 packets, 234 bytes incoming, 0.03 kbits/s; 3 packets, 234 bytes outgoing, 0.03 kbits/s


Running time: 60 seconds
Fri Dec  9 14:59:18 2011; ******** TCP/UDP service monitor stopped ********

-z オプションをつける事で受信したパケットサイズの統計情報が記録されます。
主にデバッグで使用する感じでしょうか。
-z オプションをつけたログのサンプルは以下になります。

Fri Dec  9 14:58:31 2011; ******** Packet size distribution facility started ********
*** Packet Size Distribution, generated Fri Dec  9 14:59:31 2011

Interface: eth0   MTU: 1500

Packet Size (bytes)     Count
1 to 75:                46
76 to 150:              184
151 to 225:             0
226 to 300:             1
301 to 375:             0
376 to 450:             0
451 to 525:             0
526 to 600:             0
601 to 675:             0
676 to 750:             0
751 to 825:             0
826 to 900:             0
901 to 975:             0
976 to 1050:            0
1051 to 1125:           0
1126 to 1200:           0
1201 to 1275:           0
1276 to 1350:           0
1351 to 1425:           0
1426 to 1500:           0

Running time: 60 seconds
Fri Dec  9 14:59:31 2011; ******** Packet size distribution facility stopped ********

-l オプションをつける事でLANの統計情報が記録されます。
通信先の仲良し度が分かりますね。
-l オプションをつけたログのサンプルは以下になります。

Fri Dec  9 14:58:49 2011; ******** LAN traffic monitor started ********

*** LAN traffic log, generated Fri Dec  9 14:59:49 2011


Ethernet address: f0def1857e9c
        Incoming total 62 packets, 5144 bytes; 0 IP packets
        Outgoing total 64 packets, 3994 bytes; 0 IP packets
        Average rates: 0.68 kbits/s incoming, 0.52 kbits/s outgoing
        Last 5-second rates: 0.00 kbits/s incoming, 0.00 kbits/s outgoing

Ethernet address: 000c29f1b6f9
        Incoming total 64 packets, 3994 bytes; 0 IP packets
        Outgoing total 62 packets, 5144 bytes; 0 IP packets
        Average rates: 0.52 kbits/s incoming, 0.68 kbits/s outgoing
        Last 5-second rates: 0.00 kbits/s incoming, 0.00 kbits/s outgoing

Ethernet address: 0013d4feea1e
        Incoming total 0 packets, 0 bytes; 0 IP packets
        Outgoing total 4 packets, 556 bytes; 0 IP packets
        Average rates: 0.00 kbits/s incoming, 0.07 kbits/s outgoing
        Last 5-second rates: 0.00 kbits/s incoming, 0.40 kbits/s outgoing

Ethernet address: ffffffffffff
        Incoming total 8 packets, 868 bytes; 0 IP packets
        Outgoing total 0 packets, 0 bytes; 0 IP packets
        Average rates: 0.10 kbits/s incoming, 0.00 kbits/s outgoing
        Last 5-second rates: 0.40 kbits/s incoming, 0.00 kbits/s outgoing

ログ出力

rpmでインストール(CentOS)した場合、/var/log/iptraf/にログが出力されます。
出力されたログはローテーションされないので、logrotate等に設定する必要があります。

# ls -l /var/log/iptraf/
合計 24
-rw-r--r-- 1 root root 1275 12月  9 14:53 iface_stats_detailed-eth0.log
-rw-r--r-- 1 root root  557 12月  9 15:03 iface_stats_general.log
-rw-r--r-- 1 root root 2628 12月  9 14:58 ip_traffic-1.log
-rw-r--r-- 1 root root 1540 12月  9 14:59 lan_statistics-1.log
-rw-r--r-- 1 root root  631 12月  9 14:59 packet_size-eth0.log
-rw-r--r-- 1 root root  530 12月  9 14:59 tcp_udp_services-eth0.log

出力先のログを指定する場合は -L オプションを引数に与えて続けてログファイルを指定します。

# iptraf -B -t 1 -i eth0 -L /path/to/logfile

というわけで、簡単だけどログを交えてたっぷりお伝えしました。
素敵なトラフィック監視ライフを!