{"id":492,"date":"2009-05-04T22:29:26","date_gmt":"2009-05-04T19:29:26","guid":{"rendered":"http:\/\/www.void.gr\/kargig\/blog\/?p=492"},"modified":"2009-05-04T22:54:29","modified_gmt":"2009-05-04T19:54:29","slug":"iftraffic-perl-script-to-measure-inout-traffic-in-realtime","status":"publish","type":"post","link":"https:\/\/www.void.gr\/kargig\/blog\/2009\/05\/04\/iftraffic-perl-script-to-measure-inout-traffic-in-realtime\/","title":{"rendered":"iftraffic.pl: perl script to measure in\/out traffic in realtime"},"content":{"rendered":"<p>During some QoS tests on Linux I needed to measure the traffic of the system in realtime without being able to compile any new software on it. The system had already perl installed so I googled to find a script that could monitor in\/out traffic of an interface. The first script I found was this: <a href=\"http:\/\/perlmonks.org\/?node_id=635792\">http:\/\/perlmonks.org\/?node_id=635792<\/a><\/p>\n<p>While it&#8217;s actually doing what it says, it only runs just once. I wanted the script to run for a period of time. So I changed it a bit.<br \/>\nHere&#8217;s the outcome:<br \/>\n<code>#!\/usr\/bin\/perl<br \/>\nmy $dev=$ARGV[0];<br \/>\nsub get_measures {<br \/>\n    my $data = `cat \/proc\/net\/dev | grep \"$dev\" | head -n1`;<br \/>\n    $data =~ \/$dev\\:(\\d+)\\D+\\d+\\D+\\d+\\D+\\d+\\D+\\d+\\D+\\d+\\D+\\d+\\D+\\d+\\D+(\\d+)\\D+\/;<br \/>\n    my $recv = int($1\/1024);<br \/>\n    my $sent= int($2\/1024);<br \/>\n    return ($recv,$sent);<br \/>\n}<br \/>\nmy @m1 = get_measures;<br \/>\nwhile(1) {<br \/>\n    sleep 1;<br \/>\n    my @m2 = get_measures;<br \/>\n    my @rates = ($m2[0] - $m1[0], $m2[1]-$m1[1]);<br \/>\n    foreach ('received' , ' transmit') {<br \/>\n        printf \"$_ rate:%sKB\",shift @rates;<br \/>\n    }<br \/>\n    print \"\\n\";<br \/>\n    @m1=@m2;<br \/>\n}<\/code><\/p>\n<p>I&#8217;ve changed it so that it&#8217;s:<br \/>\na) running continuously until someone presses ctrl+c to stop it,<br \/>\nb) parsing the \/proc\/net\/dev output instead of the ifconfig output. I think this is more efficient\/fast than parsing the ifconfig output.<\/p>\n<p>Sample output:<\/p>\n<blockquote><p> $iftraffic.pl eth0<br \/>\nreceived rate:1564KB transmit rate:71KB<br \/>\nreceived rate:1316KB transmit rate:44KB<br \/>\nreceived rate:1415KB transmit rate:48KB<br \/>\nreceived rate:1579KB transmit rate:76KB\n<\/p><\/blockquote>\n<p>I am sure that someone with more insight into perl than me can make it even more efficient.<\/p>\n<p>You can also download a version with comments that I made so that one can make the script run for X number of repetitions instead of running until someone stops it.<br \/>\n<strong>Download: <a href='http:\/\/www.void.gr\/kargig\/blog\/wp-content\/iftraffic.pl'>iftraffic.pl<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>During some QoS tests on Linux I needed to measure the traffic of the system in realtime without being able to compile any new software on it. The system had already perl installed so I googled to find a script that could monitor in\/out traffic of an interface. The first script I found was this: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ep_exclude_from_search":false,"footnotes":""},"categories":[3,8],"tags":[72,595,70,599,73,48,71,69],"class_list":["post-492","post","type-post","status-publish","format-standard","hentry","category-linux","category-networking","tag-incoming","tag-linux","tag-monitor","tag-networking","tag-outgoing","tag-perl","tag-realtime","tag-traffic"],"aioseo_notices":[],"views":16681,"_links":{"self":[{"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/posts\/492","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/comments?post=492"}],"version-history":[{"count":13,"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/posts\/492\/revisions"}],"predecessor-version":[{"id":506,"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/posts\/492\/revisions\/506"}],"wp:attachment":[{"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/media?parent=492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/categories?post=492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/tags?post=492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}