2013-12-09 73 views
0

特別是可以檢查Linux中每個接口的ICMP數據包的統計信息嗎? 使用ifconfig命令它提供了每個接口的統計數據接收和發送的數據包:是否可以統計Linux中每個接口發送/接收的ICMP數據包的數量?

-> /sbin/ifconfig eth1 
eth1  Link encap:Ethernet HWaddr BC:30:5B:ED:DE:54 
      UP BROADCAST RUNNING SLAVE MULTICAST MTU:1500 Metric:1 
      RX packets:412327300 errors:0 dropped:0 overruns:0 frame:0 
      TX packets:765211747 errors:0 dropped:0 overruns:0 carrier:0 
      collisions:0 txqueuelen:1000 
      RX bytes:327931865613 (312740.1 Mb) TX bytes:803392590272 (766174.8 Mb) 
      Memory:dcc00000-dcd00000 

但是我期待的,是某些特定類型的每個接口的數據包(如ICMP)的。

而且Linux是提供這些統計數據,但在全球範圍內的/ proc /淨/ SNMP

-> cat /proc/net/snmp 
... log truncated ... 
Icmp: InMsgs InErrors InDestUnreachs InTimeExcds InParmProbs InSrcQuenchs InRedirects InEchos InEchoReps InTimestamps InTimestampReps InAddrMasks InAddrMaskReps OutMsgs OutErrors OutDestUnreachs OutTimeExcds OutParmProbs OutSrcQuenchs OutRedirects OutEchos OutEchoReps OutTimestamps OutTimestampReps OutAddrMasks OutAddrMaskReps 
Icmp: 29697 5 276 9 0 0 0 29409 3 0 0 0 0 29970 0 561 0 0 0 0 5 29404 0 0 0 0 
IcmpMsg: InType0 InType3 InType8 InType11 OutType0 OutType3 OutType8 
IcmpMsg: 3 276 29409 9 29404 561 5 
... log truncated ... 

或者更漂亮的印刷與的netstat -s命令(-s代表,當然統計) :

-> netstat -s 
... log truncated ... 
Icmp: 
    29697 ICMP messages received 
    5 input ICMP message failed. 
    ICMP input histogram: 
     destination unreachable: 276 
     timeout in transit: 9 
     echo requests: 29409 
     echo replies: 3 
    29970 ICMP messages sent 
    0 ICMP messages failed 
    ICMP output histogram: 
     destination unreachable: 561 
     echo request: 5 
     echo replies: 29404 
IcmpMsg: 
     InType0: 3 
     InType3: 276 
     InType8: 29409 
     InType11: 9 
     OutType0: 29404 
     OutType3: 561 
     OutType8: 5 
... log truncated ... 

所以,問題是。有沒有什麼辦法可以獲得某些特定接口的ICMP統計信息,而不是整個系統的全局ICMP統計信息?

回答

2

我不認爲內核爲每個接口的每個協議保留計數器。縱觀進入代碼提要/proc/net/netstat(除其他事項外),我們可以找到大量引用到rtnl_link_stats64這是在include/uapi/linux/if_link.h定義:

/* The main device statistics structure */ 
struct rtnl_link_stats64 { 
     __u64 rx_packets;    /* total packets received  */ 
     __u64 tx_packets;    /* total packets transmitted */ 
     __u64 rx_bytes;    /* total bytes received   */ 
     __u64 tx_bytes;    /* total bytes transmitted  */ 
     __u64 rx_errors;    /* bad packets received   */ 
     __u64 tx_errors;    /* packet transmit problems  */ 
     __u64 rx_dropped;    /* no space in linux buffers */ 
     __u64 tx_dropped;    /* no space available in linux */ 
     __u64 multicast;    /* multicast packets received */ 
     __u64 collisions; 

     /* detailed rx_errors: */ 
     __u64 rx_length_errors; 
     __u64 rx_over_errors;   /* receiver ring buff overflow */ 
     __u64 rx_crc_errors;   /* recved pkt with crc error */ 
     __u64 rx_frame_errors;  /* recv'd frame alignment error */ 
     __u64 rx_fifo_errors;   /* recv'r fifo overrun   */ 
     __u64 rx_missed_errors;  /* receiver missed packet  */ 

     /* detailed tx_errors */ 
     __u64 tx_aborted_errors; 
     __u64 tx_carrier_errors; 
     __u64 tx_fifo_errors; 
     __u64 tx_heartbeat_errors; 
     __u64 tx_window_errors; 

     /* for cslip etc */ 
     __u64 rx_compressed; 
     __u64 tx_compressed; 
}; 

如果我猜中了,這是結構在每個鏈接,(或per-interface在語義上來說在這種情況下是相同的),統計數據被保留下來,並且似乎沒有特定於協議的計數器。在實現自身

+0

感謝您的回答,實際上我也仔細檢查了實施本身(更新了我的問題),看起來您的答案完全正確。 –

+0

實際上,您指出的統計信息可以在/ sys/class/net//statistics中檢索。 –

0

尋找當它增加了櫃檯,似乎你提到Linux是不是爲特定的協議提供每個接口的這些統計數據:

struct icmp_mib icmp_statistics; 

... 

static void icmp_out_count(int type) 

{ 

     if(type>18) 

       return; 

     (*icmp_pointers[type].output)++; 

     icmp_statistics.IcmpOutMsgs++; 

} 

... 

int icmp_rcv(struct sk_buff *skb, struct device *dev, struct options *opt, 

     __u32 daddr, unsigned short len, 

     __u32 saddr, int redo, struct inet_protocol *protocol) 

{ 
... 
icmp_statistics.IcmpInMsgs++; 
     if(len < sizeof(struct icmphdr)) 

     { 

       icmp_statistics.IcmpInErrors++; 
     ... 
     } 
... 
} 

等。所以它似乎是所有接口的統計數據,從來沒有提到具體的接口。

相關問題