{"id":2004,"date":"2015-03-14T20:36:10","date_gmt":"2015-03-14T12:36:10","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2004"},"modified":"2015-03-14T20:36:10","modified_gmt":"2015-03-14T12:36:10","slug":"%e5%a6%82%e4%bd%95%e8%ae%a1%e7%ae%97linux%e7%b3%bb%e7%bb%9f%e4%b8%8b%e9%ab%98%e9%80%9f%e7%bd%91%e7%bb%9cio%e6%83%85%e5%86%b5%e4%b8%8b%e7%9a%84%e6%94%b6%e5%8f%91%e5%8c%85%e9%80%9f%e7%8e%87%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2004.html","title":{"rendered":"\u5982\u4f55\u8ba1\u7b97Linux\u7cfb\u7edf\u4e0b\u9ad8\u901f\u7f51\u7edcI\/O\u60c5\u51b5\u4e0b\u7684\u6536\/\u53d1\u5305\u901f\u7387\uff1f"},"content":{"rendered":"<p>\u6700\u8fd1\u5728\u8fdb\u884c\u6293\u5305\u91cd\u7ec4\u65b9\u9762\u7684\u7f16\u7a0b\u5b66\u4e60\uff0c\u5728\u4ee3\u7801\u5199\u5b8c\u4e86\u4e4b\u540e\u9700\u8981\u5bf9\u6293\u5305\u6027\u80fd\u8fdb\u884c\u5206\u6790\uff0c\u5176\u4e2d\u6bd4\u8f83\u91cd\u8981\u7684\u4e00\u70b9\u5c31\u662f\u4e22\u5305\u7387\uff0c\u8fd9\u70b9\u5f02\u5e38\u5173\u952e\uff0c\u5982\u679c\u4e22\u5305\u7387\u9ad8\u4e86\uff0c\u90a3\u65b9\u6848\u53ef\u80fd\u5c31\u9700\u8981\u91cd\u65b0\u8bbe\u8ba1\u4e86\u3002\u4f46\u5982\u4f55\u4e86\u89e3\u5728\u4e00\u4e2a\u9ad8\u901f\u7f51\u7edcI\/O\u60c5\u51b5\u4e0b\u7a0b\u5e8f\u7684\u4e22\u5305\u7387\u5462\uff1f\u4e00\u79cd\u6bd4\u8f83\u5bb9\u6613\u60f3\u5230\u7684\u65b9\u5f0f\u5c31\u662f\u548ctcpdump\u7684\u6293\u5305\u6548\u679c\u8fdb\u884c\u5bf9\u6bd4\uff0c\u4f46\u56e0\u4e3atcpdump\u5e95\u5c42\u4f9d\u8d56\u7684\u662flibpcap\u5e93\uff0c\u5f53\u7f51\u7edcI\/O\u8f83\u5927\u65f6\uff0c\u5b83\u4e5f\u4f1a\u51fa\u73b0\u4e22\u5305\u7684\u60c5\u51b5\uff0c\u6240\u4ee5\u51c6\u786e\u6027\u6709\u5f85\u5546\u69b7\uff1b\u8fd8\u6709\u4e00\u79cd\u65b9\u5f0f\u5c31\u662f\u8bfb\u53d6Linux\u7cfb\u7edf\u4e2d\u7684\u67d0\u4e9b\u7279\u5b9a\u6587\u4ef6\u83b7\u53d6\u76f8\u5173\u4fe1\u606f\uff08\u8fd9\u4e2a\u5e94\u8be5\u662f\u4e00\u79cd\u6bd4\u8f83\u7b80\u5355\u4e5f\u8f83\u4e3a\u51c6\u786e\u7684\u65b9\u6cd5\uff0c\u7b80\u5355\u8bf4\u7684\u662f\u65b9\u6cd5\u7b80\u5355\u2014\u2014\u8bfb\u6587\u4ef6\u7136\u540e\u8fdb\u884c\u76f8\u5173\u6362\u7b97\u5c31\u884c\uff0c\u4e3a\u4ec0\u4e48\u662f\u8f83\u4e3a\u51c6\u786e\uff0c\u800c\u4e0d\u662f\u51c6\u786e\u5462\uff1f\u56e0\u4e3a\u8be5\u6587\u4ef6\u4e2d\u5b58\u653e\u7684\u662f\u603b\u4f53\u7684\u4fe1\u606f\uff0c\u65e0\u6cd5\u50cftcpdump\u90a3\u6837\u6839\u636e\u67d0\u4e9b\u89c4\u5219\u8fdb\u884c\u8fc7\u6ee4\uff0c\u8ba1\u7b97\u7684\u662f\u4e00\u4e2a\u603b\u6570\uff0c\u6240\u4ee5\u4e0e\u5b9e\u9645\u76f8\u6bd4\u503c\u8981\u504f\u5927\u4e00\u4e9b\uff09\uff0c\u4e0b\u9762\u5c31\u8bb0\u5f55\u4e00\u4e0b\u8be5\u65b9\u6cd5\uff1a<\/p>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/search.aol.com\/aol\/search?q=linux+%2Fproc%2Fnet%2Fdev\" target=\"_blank\">http:\/\/search.aol.com\/aol\/search?q=linux+%2Fproc%2Fnet%2Fdev<\/a><\/li>\n<li><a href=\"http:\/\/search.aol.com\/aol\/search?q=linux+%2Fsys%2Fclass%2Fnet%2Feth0%2Fstatistics%2Frx_packets\" target=\"_blank\">http:\/\/search.aol.com\/aol\/search?q=linux+%2Fsys%2Fclass%2Fnet%2Feth0%2Fstatistics%2Frx_packets<\/a><\/li>\n<\/ul>\n<h6>\u53c2\u8003\u5173\u952e\u5b57\uff1a<\/h6>\n<pre class=\"lang:default decode:true\">\/proc\/net\/dev\n\n\/proc\/net\/raw -- RAW socket information\n\/proc\/net\/tcp -- TCP socket information\n\/proc\/net\/udp -- UDP socket information\n\n\/sys\/class\/net\/eth0\/statistics\/rx_bytes\n\/sys\/class\/net\/eth0\/statistics\/rx_packets\n\/sys\/class\/net\/eth0\/statistics\/tx_bytes\n\/sys\/class\/net\/eth0\/statistics\/tx_packets<\/pre>\n<p>==<\/p>\n<p>There are many traffic monitoring tools available on\u00a0Linux, which can monitor\/classify network traffic, and report real-time traffic statistics in fancy user interfaces. Most of these tools (e.g.,\u00a0<a href=\"http:\/\/xmodulo.com\/2013\/10\/set-web-based-network-traffic-monitoring-linux.html\">ntopng<\/a>,\u00a0<a href=\"http:\/\/xmodulo.com\/2012\/06\/how-to-install-iftop-on-linux.html\">iftop<\/a>) are powered by\u00a0<a href=\"http:\/\/www.tcpdump.org\/pcap3_man.html\">libpcap<\/a>, which is a packet capture library used to monitor network traffic in user space. Despite their versatility, however,\u00a0libpcap-based network monitoring tools cannot scale to handle traffic on multi Gigabit rate network interfaces, due to the overhead associated with user-space packet capture.\uff08\u867d\u7136\u57fa\u4e8elibpcap\u7684\u7f51\u7edc\u76d1\u63a7\u5de5\u5177\u529f\u80fd\u9f50\u5168\uff0c\u4f46\u662f\u56e0\u4e3a\u7528\u6237\u6001\u6293\u5305\u80fd\u529b\u7684\u5148\u5929\u6027\u9650\u5236\uff0c\u5b83\u4eec\u65e0\u6cd5\u6269\u5c55\u53bb\u5904\u7406Gigabit\u901f\u7387\u7684\u7f51\u7edc\u63a5\u53e3\u3002\uff09<\/p>\n<p>In this tutorial, I will present simple shell scripts that can\u00a0<strong>monitor network traffic on per-interface basis, without relying on slow libpcap library<\/strong>. These scripts are fast enough to support multi Gigabit rates, but only suitable if you are interested in &#8220;aggregate&#8221; network statistics on per interface basis.\uff08\u4e0b\u9762\u7684\u811a\u672c\u53ea\u9002\u7528\u4e8e\u5bf9\u603b\u4f53\u7684\u7f51\u5361\u6d41\u91cf\u8fdb\u884c\u7edf\u8ba1\u7684\u60c5\u5f62\uff09<\/p>\n<p>The secret for the scripts lies in\u00a0<a href=\"http:\/\/lwn.net\/Articles\/31185\/\">sysfs<\/a>\u00a0virtual filesystem which is used by the kernel to export device- or driver-related information to user space. Network interface related statistics are exported via \/sys\/class\/net\/&lt;ethX&gt;\/statistics.\uff08\u56e0\u4e3a\u5185\u6838\u5c06\u8bbe\u5907\u3001\u9a71\u52a8\u76f8\u5173\u7684\u4fe1\u606f\u5bfc\u51fa\u81f3\u7528\u6237\u6001\u7684 sysfs \u865a\u62df\u6587\u4ef6\u7cfb\u7edf\u7684\u76f8\u5e94\u6587\u4ef6\u4e2d\uff09<\/p>\n<p>For example, the statistics on eth0 interface are found in these files:<\/p>\n<ul>\n<li><strong>\/sys\/class\/net\/eth0\/statistics\/rx_packets:<\/strong>\u00a0number of packets received<\/li>\n<li><strong>\/sys\/class\/net\/eth0\/statistics\/tx_packets:<\/strong>\u00a0number of packets transmitted<\/li>\n<li><strong>\/sys\/class\/net\/eth0\/statistics\/rx_bytes:<\/strong>\u00a0number of bytes received<\/li>\n<li><strong>\/sys\/class\/net\/eth0\/statistics\/tx_bytes:<\/strong>\u00a0number of bytes transmitted<\/li>\n<li><strong>\/sys\/class\/net\/eth0\/statistics\/rx_dropped:<\/strong>\u00a0number of packets dropped while received<\/li>\n<li><strong>\/sys\/class\/net\/eth0\/statistics\/tx_dropped:<\/strong>\u00a0number of packets dropped while transmitted<\/li>\n<\/ul>\n<p>The numbers stored in the files are automatically refreshed in real-time by the kernel. Therefore, you can write scripts that calculate traffic statistics based on these files.<\/p>\n<p>The following are two such scripts (thanks to\u00a0<a href=\"https:\/\/gist.github.com\/joemiller\/4069513\" target=\"_blank\">joemiller<\/a>). The first script\u00a0<strong>counts the number of packets per second, received (RX) or sent (TX) on an interface<\/strong>, while the latter scripts\u00a0<strong>measures the network bandwidth of incoming (RX) and outgoing (TX) traffic on an interface<\/strong>.<\/p>\n<h6>Reference:<\/h6>\n<p><a href=\"http:\/\/xmodulo.com\/measure-packets-per-second-throughput-high-speed-network-interface.html\">http:\/\/xmodulo.com\/measure-packets-per-second-throughput-high-speed-network-interface.html<\/a><\/p>\n<pre class=\"lang:default decode:true\">#!\/bin\/bash\n\ninterval=\"1\"  # update interval in seconds\ninterface_name=$(awk '$1 !~ \/lo\/' \/proc\/net\/dev | awk 'BEGIN{max=0} {if($2+0 &gt; max+0) max=$2 fi} END{print $1}' | awk -F':' '{print $1}')\n\nwhile true\ndo\n\tR1=`cat \/sys\/class\/net\/$interface_name\/statistics\/rx_packets`\n\tT1=`cat \/sys\/class\/net\/$interface_name\/statistics\/tx_packets`\n\trb1=`cat \/sys\/class\/net\/$interface_name\/statistics\/rx_bytes`\n\ttb1=`cat \/sys\/class\/net\/$interface_name\/statistics\/tx_bytes`\n\tsleep $interval\n\tR2=`cat \/sys\/class\/net\/$interface_name\/statistics\/rx_packets`\n\tT2=`cat \/sys\/class\/net\/$interface_name\/statistics\/tx_packets`\n\trb2=`cat \/sys\/class\/net\/$interface_name\/statistics\/rx_bytes`\n\ttb2=`cat \/sys\/class\/net\/$interface_name\/statistics\/tx_bytes`\n\tTXPPS=`expr $T2 - $T1`\n\tRXPPS=`expr $R2 - $R1`\n\tTBPS=`expr $tb2 - $tb1`\n\tRBPS=`expr $rb2 - $rb1`\n\tTKBPS=`expr $TBPS \/ 1024`\n\tRKBPS=`expr $RBPS \/ 1024`\n\techo -e \"Send $interface_name: $TXPPS pkts\/s\\tRecv $interface_name: $RXPPS pkts\/s\"\n\techo -e \"Send $interface_name: $TKBPS KB\/s\\tRecv $interface_name: $RKBPS KB\/s\"\ndone<\/pre>\n<p>==<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/xmodulo.com\/measure-packets-per-second-throughput-high-speed-network-interface.html\" target=\"_blank\">How to measure packets per second or throughput on high speed network interface &#8211; Xmodulo<\/a><\/li>\n<li><a href=\"http:\/\/serverfault.com\/questions\/533513\/how-to-get-tx-rx-bytes-without-ifconfig\" target=\"_blank\">http:\/\/serverfault.com\/questions\/533513\/how-to-get-tx-rx-bytes-without-ifconfig<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/349576\/linux-retrieve-per-interface-sent-received-packet-counters-ethernet-ipv4-ipv\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/349576\/linux-retrieve-per-interface-sent-received-packet-counters-ethernet-ipv4-ipv<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/3521678\/linux-what-are-means-of-fields-in-proc-net-dev\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/3521678\/linux-what-are-means-of-fields-in-proc-net-dev<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u5728\u8fdb\u884c\u6293\u5305\u91cd\u7ec4\u65b9\u9762\u7684\u7f16\u7a0b\u5b66\u4e60\uff0c\u5728\u4ee3\u7801\u5199\u5b8c\u4e86\u4e4b\u540e\u9700\u8981\u5bf9\u6293\u5305\u6027\u80fd\u8fdb\u884c\u5206\u6790\uff0c\u5176\u4e2d\u6bd4\u8f83\u91cd\u8981\u7684\u4e00\u70b9\u5c31\u662f\u4e22\u5305\u7387\uff0c\u8fd9\u70b9\u5f02 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11,12],"tags":[74,246,475,476],"class_list":["post-2004","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-tools","tag-awk","tag-eth0","tag-libpcap","tag-nic"],"views":15942,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2004","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2004"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2004\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2004"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}