1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/python/psutil/examples/nettop.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,144 @@ 1.4 +#!/usr/bin/env python 1.5 +# 1.6 +# $Id: iotop.py 1160 2011-10-14 18:50:36Z g.rodola@gmail.com $ 1.7 +# 1.8 +# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. 1.9 +# Use of this source code is governed by a BSD-style license that can be 1.10 +# found in the LICENSE file. 1.11 + 1.12 +""" 1.13 +Shows real-time network statistics. 1.14 + 1.15 +Author: Giampaolo Rodola' <g.rodola@gmail.com> 1.16 +""" 1.17 + 1.18 +import sys 1.19 +import os 1.20 +if os.name != 'posix': 1.21 + sys.exit('platform not supported') 1.22 +import curses 1.23 +import atexit 1.24 +import time 1.25 + 1.26 +import psutil 1.27 + 1.28 + 1.29 +# --- curses stuff 1.30 +def tear_down(): 1.31 + win.keypad(0) 1.32 + curses.nocbreak() 1.33 + curses.echo() 1.34 + curses.endwin() 1.35 + 1.36 +win = curses.initscr() 1.37 +atexit.register(tear_down) 1.38 +curses.endwin() 1.39 +lineno = 0 1.40 + 1.41 +def print_line(line, highlight=False): 1.42 + """A thin wrapper around curses's addstr().""" 1.43 + global lineno 1.44 + try: 1.45 + if highlight: 1.46 + line += " " * (win.getmaxyx()[1] - len(line)) 1.47 + win.addstr(lineno, 0, line, curses.A_REVERSE) 1.48 + else: 1.49 + win.addstr(lineno, 0, line, 0) 1.50 + except curses.error: 1.51 + lineno = 0 1.52 + win.refresh() 1.53 + raise 1.54 + else: 1.55 + lineno += 1 1.56 +# --- curses stuff 1.57 + 1.58 + 1.59 +def bytes2human(n): 1.60 + """ 1.61 + >>> bytes2human(10000) 1.62 + '9.8 K' 1.63 + >>> bytes2human(100001221) 1.64 + '95.4 M' 1.65 + """ 1.66 + symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y') 1.67 + prefix = {} 1.68 + for i, s in enumerate(symbols): 1.69 + prefix[s] = 1 << (i+1)*10 1.70 + for s in reversed(symbols): 1.71 + if n >= prefix[s]: 1.72 + value = float(n) / prefix[s] 1.73 + return '%.2f %s' % (value, s) 1.74 + return '%.2f B' % (n) 1.75 + 1.76 +def poll(interval): 1.77 + """Retrieve raw stats within an interval window.""" 1.78 + tot_before = psutil.net_io_counters() 1.79 + pnic_before = psutil.net_io_counters(pernic=True) 1.80 + # sleep some time 1.81 + time.sleep(interval) 1.82 + tot_after = psutil.net_io_counters() 1.83 + pnic_after = psutil.net_io_counters(pernic=True) 1.84 + return (tot_before, tot_after, pnic_before, pnic_after) 1.85 + 1.86 + 1.87 +def refresh_window(tot_before, tot_after, pnic_before, pnic_after): 1.88 + """Print stats on screen.""" 1.89 + global lineno 1.90 + 1.91 + # totals 1.92 + print_line("total bytes: sent: %-10s received: %s" \ 1.93 + % (bytes2human(tot_after.bytes_sent), 1.94 + bytes2human(tot_after.bytes_recv)) 1.95 + ) 1.96 + print_line("total packets: sent: %-10s received: %s" \ 1.97 + % (tot_after.packets_sent, tot_after.packets_recv) 1.98 + ) 1.99 + 1.100 + 1.101 + # per-network interface details: let's sort network interfaces so 1.102 + # that the ones which generated more traffic are shown first 1.103 + print_line("") 1.104 + nic_names = list(pnic_after.keys()) 1.105 + nic_names.sort(key=lambda x: sum(pnic_after[x]), reverse=True) 1.106 + for name in nic_names: 1.107 + stats_before = pnic_before[name] 1.108 + stats_after = pnic_after[name] 1.109 + templ = "%-15s %15s %15s" 1.110 + print_line(templ % (name, "TOTAL", "PER-SEC"), highlight=True) 1.111 + print_line(templ % ( 1.112 + "bytes-sent", 1.113 + bytes2human(stats_after.bytes_sent), 1.114 + bytes2human(stats_after.bytes_sent - stats_before.bytes_sent) + '/s', 1.115 + )) 1.116 + print_line(templ % ( 1.117 + "bytes-recv", 1.118 + bytes2human(stats_after.bytes_recv), 1.119 + bytes2human(stats_after.bytes_recv - stats_before.bytes_recv) + '/s', 1.120 + )) 1.121 + print_line(templ % ( 1.122 + "pkts-sent", 1.123 + stats_after.packets_sent, 1.124 + stats_after.packets_sent - stats_before.packets_sent, 1.125 + )) 1.126 + print_line(templ % ( 1.127 + "pkts-recv", 1.128 + stats_after.packets_recv, 1.129 + stats_after.packets_recv - stats_before.packets_recv, 1.130 + )) 1.131 + print_line("") 1.132 + win.refresh() 1.133 + lineno = 0 1.134 + 1.135 + 1.136 +def main(): 1.137 + try: 1.138 + interval = 0 1.139 + while 1: 1.140 + args = poll(interval) 1.141 + refresh_window(*args) 1.142 + interval = 1 1.143 + except (KeyboardInterrupt, SystemExit): 1.144 + pass 1.145 + 1.146 +if __name__ == '__main__': 1.147 + main()