{"id":488,"date":"2012-08-30T10:55:24","date_gmt":"2012-08-30T16:55:24","guid":{"rendered":"http:\/\/blog.the-erm.com\/?p=488"},"modified":"2012-10-03T11:44:13","modified_gmt":"2012-10-03T17:44:13","slug":"php-command-line-bandwidth-monitor","status":"publish","type":"post","link":"https:\/\/blog.the-erm.com\/?p=488","title":{"rendered":"php command line bandwidth monitor"},"content":{"rendered":"<p>I wrote this quick &#038; dirty bandwidth monitor for use with htop.  It also displays the average bandwidth for the last 10 seconds.<\/p>\n<p>It displays a line similar to this at the top of the screen:<br \/>\n<code>eth0:RX:600b   TX:944b   Avg:77b   eth2:RX:0b     TX:0b     Avg:0b<\/code><\/p>\n<p>Usage is simple:<br \/>\n<code>$ .\/bw.monitor.php & htop <\/code><br \/>\n<!--more--><\/p>\n<pre style=\"overflow:auto; width:100%; border:1px solid; height:500px;\">\r\n#!\/usr\/bin\/php\r\n&lt;?php\r\n    set_time_limit(0);\r\n    define('ESC',chr(27));\r\n    $ignore_interfaces = array('lo','wlan0');\r\n    function human($bytes, $decimals = 1) {\r\n      $sz = 'bkmgtp';\r\n      $factor = floor((strlen($bytes) - 1) \/ 3);\r\n      if ($factor &gt;= 1) {\r\n        return sprintf(\"%.{$decimals}f%s\", $bytes \/ pow(1024, $factor), @$sz[$factor]);\r\n      }\r\n      return sprintf(\"%d%s\", $bytes \/ pow(1024, $factor),@$sz[$factor]);\r\n    }\r\n    \/*\r\n    $data=\"Inter-|   Receive                                                |  Transmit\r\n face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed\r\n    lo: 70513902  759179    0    0    0     0          0         0 70513902  759179    0    0    0     0       0          0\r\n  eth2:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0\r\n  eth0: 591403745 1474829    0    0    0     0          0      1492 2162660750 2294779    0    0    0     0       0          0\";\r\n    *\/\r\n    $map = array(\r\n        2 =&gt; 'RX',\r\n        10 =&gt; 'TX'\r\n    );\r\n    $last_data = array();\r\n    $readout = array();\r\n    $total = array();\r\n    $history = array();\r\n    $cnt = 0;\r\n    while (true) {\r\n        $matches = array();\r\n        $associated = array();\r\n        $readout = array();\r\n        $data = file_get_contents('\/proc\/net\/dev');\r\n        preg_match_all('\/([a-z0-9]+)\\:\\s+([0-9]+)\\s+([\\d]+)\\s+([\\d]+)\\s+([\\d]+)\\s+([\\d]+)\\s+([\\d]+)\\s+([\\d]+)\\s+([\\d]+)\\s+([\\d]+)\\s+([\\d]+)\\s+([\\d]+)\\s+([\\d]+)\\s+([\\d]+)\\s+([\\d]+)\\s+([\\d]+)\\s+([\\d]+)\/msu',$data, $matches, PREG_SET_ORDER);\r\n        if (!$matches) {\r\n            sleep(1);\r\n            continue;\r\n        }\r\n        $cnt++;\r\n        \/\/ echo $data;\r\n        \/\/ print_r($matches);\r\n        \r\n        foreach ($matches as $match) {\r\n            $interface = $match[1]; \r\n             if (in_array($interface, $ignore_interfaces)) {\r\n                continue;\r\n            }\r\n            if (!isset($total[$interface])) {\r\n                $total[\"$interface\"] = 0;\r\n            }\r\n            if (!isset($history[$interface])) {\r\n                for ($i=0;$i&lt;20;$i++) {\r\n                    $history[\"$interface\"][] = 0;\r\n                }\r\n            }\r\n            $associated[$interface] = array();\r\n            foreach ($map as $k=&gt;$v) {\r\n                 $associated[$interface][$v] = $match[$k];\r\n            }\r\n        }\r\n        \/\/ print_r($associated);\r\n        foreach ($associated as $i=&gt;$d) {\r\n            if (isset($last_data[$i])) {\r\n                foreach ($d as $k=&gt;$v) {\r\n                    $diff = $v - $last_data[$i][$k];\r\n                    $readout[\"$i\"][\"$k\"] = human($diff);\r\n                    $total[\"$i\"] = ($total[\"$i\"] + $diff);\r\n                    $history[\"$i\"][] = $diff;\r\n                }\r\n                $history[\"$i\"] = array_slice($history[\"$i\"], -20, 20);\r\n                $readout[\"$i\"][\"Avg\"] = human(floor(array_sum($history[\"$i\"]) \/ (count($history[\"$i\"]) \/ 2)));\r\n                \/\/ print_r($history);\r\n            }\r\n        }\r\n        $last_data = $associated;\r\n        \/\/ print_r($readout);\r\n        echo ESC,'[s';\r\n        echo ESC,'[0;0H';\r\n        echo ESC,'[K';\r\n        ksort($readout);\r\n        foreach ($readout as $i=&gt;$d) {\r\n            echo $i,':';\r\n            foreach ($d as $k=&gt;$v) {\r\n                printf(\"%-10s\", \"$k:$v\");\r\n            }\r\n            \/\/ echo \" \";\r\n        }\r\n        \/\/ echo 'mem:',human(memory_get_usage(),2);\r\n        echo ESC,'[u';\r\n        flush();\r\n        sleep(1);\r\n    }\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I wrote this quick &#038; dirty bandwidth monitor for use with htop. It also displays the average bandwidth for the last 10 seconds. It displays a line similar to this at the top of the screen: eth0:RX:600b TX:944b Avg:77b eth2:RX:0b TX:0b Avg:0b Usage is simple: $ .\/bw.monitor.php &#038; htop<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-488","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=\/wp\/v2\/posts\/488","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=488"}],"version-history":[{"count":10,"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=\/wp\/v2\/posts\/488\/revisions"}],"predecessor-version":[{"id":517,"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=\/wp\/v2\/posts\/488\/revisions\/517"}],"wp:attachment":[{"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=488"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=488"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=488"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}