python/psutil/examples/nettop.py

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rwxr-xr-x

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 #!/usr/bin/env python
michael@0 2 #
michael@0 3 # $Id: iotop.py 1160 2011-10-14 18:50:36Z g.rodola@gmail.com $
michael@0 4 #
michael@0 5 # Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
michael@0 6 # Use of this source code is governed by a BSD-style license that can be
michael@0 7 # found in the LICENSE file.
michael@0 8
michael@0 9 """
michael@0 10 Shows real-time network statistics.
michael@0 11
michael@0 12 Author: Giampaolo Rodola' <g.rodola@gmail.com>
michael@0 13 """
michael@0 14
michael@0 15 import sys
michael@0 16 import os
michael@0 17 if os.name != 'posix':
michael@0 18 sys.exit('platform not supported')
michael@0 19 import curses
michael@0 20 import atexit
michael@0 21 import time
michael@0 22
michael@0 23 import psutil
michael@0 24
michael@0 25
michael@0 26 # --- curses stuff
michael@0 27 def tear_down():
michael@0 28 win.keypad(0)
michael@0 29 curses.nocbreak()
michael@0 30 curses.echo()
michael@0 31 curses.endwin()
michael@0 32
michael@0 33 win = curses.initscr()
michael@0 34 atexit.register(tear_down)
michael@0 35 curses.endwin()
michael@0 36 lineno = 0
michael@0 37
michael@0 38 def print_line(line, highlight=False):
michael@0 39 """A thin wrapper around curses's addstr()."""
michael@0 40 global lineno
michael@0 41 try:
michael@0 42 if highlight:
michael@0 43 line += " " * (win.getmaxyx()[1] - len(line))
michael@0 44 win.addstr(lineno, 0, line, curses.A_REVERSE)
michael@0 45 else:
michael@0 46 win.addstr(lineno, 0, line, 0)
michael@0 47 except curses.error:
michael@0 48 lineno = 0
michael@0 49 win.refresh()
michael@0 50 raise
michael@0 51 else:
michael@0 52 lineno += 1
michael@0 53 # --- curses stuff
michael@0 54
michael@0 55
michael@0 56 def bytes2human(n):
michael@0 57 """
michael@0 58 >>> bytes2human(10000)
michael@0 59 '9.8 K'
michael@0 60 >>> bytes2human(100001221)
michael@0 61 '95.4 M'
michael@0 62 """
michael@0 63 symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
michael@0 64 prefix = {}
michael@0 65 for i, s in enumerate(symbols):
michael@0 66 prefix[s] = 1 << (i+1)*10
michael@0 67 for s in reversed(symbols):
michael@0 68 if n >= prefix[s]:
michael@0 69 value = float(n) / prefix[s]
michael@0 70 return '%.2f %s' % (value, s)
michael@0 71 return '%.2f B' % (n)
michael@0 72
michael@0 73 def poll(interval):
michael@0 74 """Retrieve raw stats within an interval window."""
michael@0 75 tot_before = psutil.net_io_counters()
michael@0 76 pnic_before = psutil.net_io_counters(pernic=True)
michael@0 77 # sleep some time
michael@0 78 time.sleep(interval)
michael@0 79 tot_after = psutil.net_io_counters()
michael@0 80 pnic_after = psutil.net_io_counters(pernic=True)
michael@0 81 return (tot_before, tot_after, pnic_before, pnic_after)
michael@0 82
michael@0 83
michael@0 84 def refresh_window(tot_before, tot_after, pnic_before, pnic_after):
michael@0 85 """Print stats on screen."""
michael@0 86 global lineno
michael@0 87
michael@0 88 # totals
michael@0 89 print_line("total bytes: sent: %-10s received: %s" \
michael@0 90 % (bytes2human(tot_after.bytes_sent),
michael@0 91 bytes2human(tot_after.bytes_recv))
michael@0 92 )
michael@0 93 print_line("total packets: sent: %-10s received: %s" \
michael@0 94 % (tot_after.packets_sent, tot_after.packets_recv)
michael@0 95 )
michael@0 96
michael@0 97
michael@0 98 # per-network interface details: let's sort network interfaces so
michael@0 99 # that the ones which generated more traffic are shown first
michael@0 100 print_line("")
michael@0 101 nic_names = list(pnic_after.keys())
michael@0 102 nic_names.sort(key=lambda x: sum(pnic_after[x]), reverse=True)
michael@0 103 for name in nic_names:
michael@0 104 stats_before = pnic_before[name]
michael@0 105 stats_after = pnic_after[name]
michael@0 106 templ = "%-15s %15s %15s"
michael@0 107 print_line(templ % (name, "TOTAL", "PER-SEC"), highlight=True)
michael@0 108 print_line(templ % (
michael@0 109 "bytes-sent",
michael@0 110 bytes2human(stats_after.bytes_sent),
michael@0 111 bytes2human(stats_after.bytes_sent - stats_before.bytes_sent) + '/s',
michael@0 112 ))
michael@0 113 print_line(templ % (
michael@0 114 "bytes-recv",
michael@0 115 bytes2human(stats_after.bytes_recv),
michael@0 116 bytes2human(stats_after.bytes_recv - stats_before.bytes_recv) + '/s',
michael@0 117 ))
michael@0 118 print_line(templ % (
michael@0 119 "pkts-sent",
michael@0 120 stats_after.packets_sent,
michael@0 121 stats_after.packets_sent - stats_before.packets_sent,
michael@0 122 ))
michael@0 123 print_line(templ % (
michael@0 124 "pkts-recv",
michael@0 125 stats_after.packets_recv,
michael@0 126 stats_after.packets_recv - stats_before.packets_recv,
michael@0 127 ))
michael@0 128 print_line("")
michael@0 129 win.refresh()
michael@0 130 lineno = 0
michael@0 131
michael@0 132
michael@0 133 def main():
michael@0 134 try:
michael@0 135 interval = 0
michael@0 136 while 1:
michael@0 137 args = poll(interval)
michael@0 138 refresh_window(*args)
michael@0 139 interval = 1
michael@0 140 except (KeyboardInterrupt, SystemExit):
michael@0 141 pass
michael@0 142
michael@0 143 if __name__ == '__main__':
michael@0 144 main()

mercurial