{"id":516,"date":"2012-09-12T20:20:52","date_gmt":"2012-09-13T02:20:52","guid":{"rendered":"http:\/\/blog.the-erm.com\/?p=516"},"modified":"2012-10-03T11:42:52","modified_gmt":"2012-10-03T17:42:52","slug":"python-command-line-bandwidth-monitor","status":"publish","type":"post","link":"https:\/\/blog.the-erm.com\/?p=516","title":{"rendered":"python command line bandwidth monitor"},"content":{"rendered":"<p><a href=\"https:\/\/blog.the-erm.com\/archives\/488\">In a previous post I wrote a bandwidth monitory in php.<\/a>  It was running about 300k (on my laptop it was < 100k), and figured it was time to test out my python foo.\n\nThis version also displays a line similar to this at the top of your terminal window:\n<code>eth0:RX:600b   TX:944b   Av:77b   eth2:RX:0b     TX:0b     Av:0b<\/p>\n<p>Usage is simple:<br \/>\n<code>$ .\/bw.monitor.py & htop <\/code><br \/>\n<!--more--><\/p>\n<pre style=\"overflow:auto; width:100%; border:1px solid; height:500px;\">#!\/usr\/bin\/env python\r\nimport time, re, copy, sys\r\n\r\nESC = chr(27)\r\n#\r\ndef sizeof_fmt(num):\r\n    num = int(num)\r\n    # sizeof_fmt stolen from:\r\n    # http:\/\/stackoverflow.com\/questions\/1094841\/reusable-library-to-get-human-readable-version-of-file-size\r\n    for x in ['b','k','m','g','t']:\r\n        if num < 1024.0:\r\n            return \"%3.1f%s\" % (num, x)\r\n        num \/= 1024.0\r\n\r\nprog = re.compile(\"([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]+)\",re.M + re.S)\r\n\r\nnumeric = re.compile(\"^[0-9]+$\")\r\n\r\nkey_map = {\r\n    1: 'RX',\r\n    9: 'TX'\r\n}\r\n\r\nlast_data = {}\r\nreadout = {}\r\nassociated = {}\r\nhistory = {}\r\n\r\nwhile True:\r\n    fp = open('\/proc\/net\/dev','r')\r\n    data = fp.read()\r\n    fp.close()\r\n    matches = prog.findall(data)\r\n    \r\n    for m in matches:\r\n        # print m\r\n        interface = m[0]\r\n        associated[interface] = {}\r\n        for k,v in key_map.iteritems():\r\n            associated[interface][v] = int(m[k])\r\n\r\n    for iface, dta in associated.iteritems():\r\n        if not history.has_key(iface):\r\n            history[iface] = []\r\n        if last_data.has_key(iface):\r\n            for k, v in dta.iteritems():\r\n                diff = v - last_data[iface][k]\r\n                if not readout.has_key(iface):\r\n                    readout[iface] = {}\r\n                readout[iface][k] = diff\r\n                history[iface].append(diff)\r\n            history[iface] = history[iface][-20:]\r\n            total = sum(history[iface])\r\n            readout[iface][\"Av\"] = total \/ (len(history[iface]) \/ 2);\r\n\r\n    sys.stdout.write(ESC+\"[s\")\r\n    sys.stdout.write(ESC+\"[0;0H\")\r\n    sys.stdout.write(ESC+\"[K\")\r\n   \r\n    for iface in sorted(readout.iterkeys()):\r\n        if iface in(\"lo\",'wlan0'):\r\n            continue\r\n        dta = readout[iface]\r\n        sys.stdout.write(iface+\" \")\r\n        for k, v in dta.iteritems():\r\n            sys.stdout.write(\"%-11s\" % (\"%s:%s\" % (k,sizeof_fmt(v)),))\r\n        sys.stdout.write(\" \")\r\n\r\n    sys.stdout.write(ESC+\"[u\")\r\n    sys.stdout.flush()\r\n        \r\n    last_data = copy.deepcopy(associated)\r\n    \r\n    time.sleep(1)\r\n\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In a previous post I wrote a bandwidth monitory in php. It was running about 300k (on my laptop it was < 100k), and figured it was time to test out my python foo. This version also displays a line similar to this at the top of your terminal window: eth0:RX:600b TX:944b Av:77b eth2:RX:0b TX:0b [&hellip;]\n<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-516","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=\/wp\/v2\/posts\/516","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=516"}],"version-history":[{"count":5,"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=\/wp\/v2\/posts\/516\/revisions"}],"predecessor-version":[{"id":524,"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=\/wp\/v2\/posts\/516\/revisions\/524"}],"wp:attachment":[{"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}