{"id":1209,"date":"2009-09-12T22:36:55","date_gmt":"2009-09-13T02:36:55","guid":{"rendered":"http:\/\/blogs.sungeek.net\/unixwiz\/?p=1209"},"modified":"2009-09-12T22:36:55","modified_gmt":"2009-09-13T02:36:55","slug":"poor-mans-network-traffic-meter","status":"publish","type":"post","link":"https:\/\/blogs.sungeek.net\/unixwiz\/2009\/09\/12\/poor-mans-network-traffic-meter\/","title":{"rendered":"Poor Man&#8217;s Network Traffic Meter"},"content":{"rendered":"<p>Set out tonight to find a way to log &#8220;network traffic&#8221; through the interfaces on my solaris box. What I was wanting was the actually amount of traffic going through the interfaces. First thought was to use netstat. But that only shows &#8220;packets&#8221; and the packets could be differing sizes. So I ended up using kstat. I wrote this simple little script to grab the interface names, and then use kstat to get the data out of the network module for each card:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n#!\/bin\/ksh\r\n#Get list of Ethernet Cards in machine:\r\nMyHOST=&quot;`hostname`&quot;\r\nOS=&quot;`uname -r`&quot;\r\nif [ ${OS} == &quot;5.10&quot; ] ; then\r\n   MyETHERS=&quot;`\/usr\/sbin\/dladm show-dev | awk &#039;{print $1}&#039;`&quot;\r\nelse\r\n   MyETHERS=&quot;`\/usr\/sbin\/ifconfig -a | awk &#039;{print $1}&#039; | grep \\&quot;:\\&quot; | awk -F&#039;:&#039; &#039;{print $1}&#039; | sort -u | grep -v \\&quot;^lo0\\&quot;`&quot;\r\nfi\r\nCOUNT=0\r\nwhile [ $COUNT -lt 800 ]; \r\n  do\r\n  for i in `echo $MyETHERS`\r\n  do\r\n    OBYTES=&quot;`\/usr\/bin\/kstat -p -c net -n $i -s obytes64 | awk &#039;{print $2}&#039;`&quot;\r\n    RBYTES=&quot;`\/usr\/bin\/kstat -p -c net -n $i -s rbytes64 | awk &#039;{print $2}&#039;`&quot;\r\n    SNAPTIME=&quot;`perl -e \\&quot;print(time());\\&quot;`&quot;\r\n    echo &quot;${MyHOST},${i},${SNAPTIME},${OBYTES},${RBYTES}&quot;\r\n    OBYTES=\r\n    RBYTES= \r\n    SNAPTIME=\r\n  done\r\n  sleep 10\r\n  COUNT=&quot;`expr $COUNT + 1`&quot;\r\ndone\r\n<\/pre>\n<p>You have to be root to run this, but that is only because of the dladm command I am using on Solaris 10. If you don&#8217;t want to run it as root, then comment out the if statement and just leave the line that uses ifconfig. When you run it, it will produce an output like this:<\/p>\n<blockquote><p>\ngonzo,elxl0,1252806095,37255837,715035<br \/>\ngonzo,rge0,1252806096,605012664015,863919572622<br \/>\ngonzo,elxl0,1252806106,37255837,715035<br \/>\ngonzo,rge0,1252806107,605012664377,863919573090\n<\/p><\/blockquote>\n<p>The output is formated as hostname, ethernet, time of the run, sending bytes, and receiving bytes. (The time is the epoch time.) The above script will only run 800 times, pausing 10 seconds between each run of the kstat. You can change how long it runs by changing the line:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nwhile [ $COUNT -lt 800 ]; \r\n<\/pre>\n<p>Just change the 800 to some other number. The second item to change is the &#8220;interval&#8221; time and that is controled by the :<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsleep 10\r\n<\/pre>\n<p>You probably don&#8217;t want to run this every second. Every 10 is about right, as it will allow me to get the traffic with out much overhead.<\/p>\n<p>The second script I did, was a little php script (but can be done in probably any language, but I use php for just about everything. This script takes output from the file you created above (just run the above script, redirect it to a file) and gives you a human readable output.<\/p>\n<p>Note if you have more than one ethernet card active in your system, currently you will need to<br \/>\n&#8220;grep&#8221; out each card to it&#8217;s own file. If you have a bunch of machines, you should probably import the data from above in to a mysql db, and then modify this script to pull the info from it.<\/p>\n<p>Here is the script to just parse one network card:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt; ?php\r\ndate_default_timezone_set(&quot;EST&quot;);\r\n$fp=fopen(&quot;Netstat.csv&quot;,r);\r\nif ($fp) {\r\n  $i=0;\r\n  while (!feof($fp)) {\r\n    $buffer=fgets($fp);\r\n    if ($buffer) { \r\n      list($hostname&amp;#91;$i&amp;#93;,$ethernet&amp;#91;$i&amp;#93;,$time&amp;#91;$i&amp;#93;,$sending&amp;#91;$i&amp;#93;,$receiving&amp;#91;$i&amp;#93;) = explode(&quot;,&quot;,$buffer);\r\n      $newtime=date(&#039;r&#039;,$time&amp;#91;$i&amp;#93;);\r\n      if ($i != 0 ) {\r\n        $TDIFF=($time&amp;#91;$i&amp;#93;-$time&amp;#91;$i-1&amp;#93;);\r\n        $SDIFF=($sending&amp;#91;$i&amp;#93;-$sending&amp;#91;$i-1&amp;#93;)\/$TDIFF\/1024\/1024;\r\n        $RDIFF=($receiving&amp;#91;$i&amp;#93;-$receiving&amp;#91;$i-1&amp;#93;)\/$TDIFF\/1024\/1024;\r\n        printf(&quot;%s|%s|%s|%3.3f|%3.3f\\n&quot;,$hostname&amp;#91;$i&amp;#93;,$ethernet&amp;#91;$i&amp;#93;,$newtime,$SDIFF,$RDIFF);\r\n        $SDIFF=&quot;&quot;;\r\n        $RDIFF=&quot;&quot;;\r\n        $TDIFF=&quot;&quot;;\r\n      }\r\n      $i++;\r\n    }\r\n  }\r\n}\r\nfclose($fp);\r\n?&gt;\r\n<\/pre>\n<p>In the above, I named my redirected output to be Netstat.csv. What the above script outputs will look like this:<\/p>\n<blockquote><p>\ngonzo|rge0|Sat, 12 Sep 2009 15:44:38 -0500|0.000|0.000<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:44:49 -0500|0.000|0.007<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:45:04 -0500|6.677|0.065<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:45:18 -0500|3.148|0.027<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:45:41 -0500|5.377|0.076<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:45:55 -0500|8.678|0.111<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:46:16 -0500|9.499|0.117<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:46:30 -0500|8.861|0.117<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:46:46 -0500|9.183|0.120<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:47:02 -0500|10.783|0.139<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:47:15 -0500|7.103|0.093<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:47:29 -0500|7.165|0.100<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:47:44 -0500|6.995|0.095<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:48:01 -0500|6.986|0.099<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:48:15 -0500|5.678|0.069<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:48:28 -0500|6.530|0.090<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:48:53 -0500|3.477|0.046<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:49:14 -0500|6.459|0.083<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:49:31 -0500|7.754|0.105<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:49:58 -0500|9.416|0.121<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:50:10 -0500|10.854|0.139<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:50:21 -0500|11.922|0.152<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:50:31 -0500|12.556|0.165<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:50:43 -0500|12.813|0.170<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:50:54 -0500|14.783|0.188<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:51:05 -0500|12.729|0.168<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:51:16 -0500|12.018|0.148<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:51:27 -0500|10.786|0.141<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:51:38 -0500|13.566|0.167<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:51:49 -0500|11.234|0.144<br \/>\ngonzo|rge0|Sat, 12 Sep 2009 15:52:01 -0500|12.914|0.165\n<\/p><\/blockquote>\n<p>The output is : hostname, ethernet, time of query,sending speed in Mbps, receiving speed in Mbps. As you can see from the above, I was copying some large amounts of data. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Set out tonight to find a way to log &#8220;network traffic&#8221; through the interfaces on my solaris box. What I was wanting was the actually amount of traffic going through the interfaces. First thought was to use netstat. But that only shows &#8220;packets&#8221; and the packets could be differing sizes. So I ended up using &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blogs.sungeek.net\/unixwiz\/2009\/09\/12\/poor-mans-network-traffic-meter\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Poor Man&#8217;s Network Traffic Meter&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,16,2],"tags":[193,468,458,444],"class_list":["post-1209","post","type-post","status-publish","format-standard","hentry","category-php","category-shell-scripts","category-solaris","tag-netstat","tag-php","tag-shell-scripts","tag-solaris"],"_links":{"self":[{"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/posts\/1209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/comments?post=1209"}],"version-history":[{"count":4,"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/posts\/1209\/revisions"}],"predecessor-version":[{"id":1213,"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/posts\/1209\/revisions\/1213"}],"wp:attachment":[{"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/media?parent=1209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/categories?post=1209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/tags?post=1209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}