~ubuntu-branches/ubuntu/trusty/python-psutil/trusty-proposed

« back to all changes in this revision

Viewing changes to examples/nettop.py

  • Committer: Package Import Robot
  • Author(s): Leo Iannacone
  • Date: 2012-01-07 13:18:54 UTC
  • mfrom: (2.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20120107131854-6l22ulo9hwrroa8m
Tags: 0.4.1-1ubuntu1
* Merge with Debian unstable (LP: #913100).  Remaining Ubuntu changes:
  - Switch to dh_python2. (LP: #788514)

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
        lineno += 1
53
53
# --- curses stuff
54
54
 
55
 
        
 
55
 
56
56
def bytes2human(n):
57
57
    """
58
58
    >>> bytes2human(10000)
68
68
        if n >= prefix[s]:
69
69
            value = float(n) / prefix[s]
70
70
            return '%.2f %s' % (value, s)
71
 
    return "0.00 B"
 
71
    return '%.2f B' % (n)
72
72
 
73
73
def poll(interval):
74
74
    """Retrieve raw stats within an interval window."""
79
79
    tot_after = psutil.network_io_counters()
80
80
    pnic_after = psutil.network_io_counters(pernic=True)
81
81
    return (tot_before, tot_after, pnic_before, pnic_after)
82
 
    
 
82
 
83
83
 
84
84
def refresh_window(tot_before, tot_after, pnic_before, pnic_after):
85
85
    """Print stats on screen."""
86
86
    global lineno
87
 
    
88
 
    # totals   
 
87
 
 
88
    # totals
89
89
    print_line("total bytes:           sent: %-10s   received: %s" \
90
90
          % (bytes2human(tot_after.bytes_sent),
91
91
             bytes2human(tot_after.bytes_recv))
92
 
    )   
 
92
    )
93
93
    print_line("total packets:         sent: %-10s   received: %s" \
94
94
          % (tot_after.packets_sent, tot_after.packets_recv)
95
95
    )
96
 
    
97
 
    # per network interface
 
96
 
 
97
 
 
98
    # per-network interface details: let's sort network interfaces so
 
99
    # that the ones which generated more traffic are shown first
98
100
    print_line("")
99
 
    for nic in pnic_after:
100
 
        stats_before = pnic_before[nic]
101
 
        stats_after = pnic_after[nic]
 
101
    nic_names = pnic_after.keys()
 
102
    nic_names.sort(key=lambda x: sum(pnic_after[x]), reverse=True)
 
103
    for name in nic_names:
 
104
        stats_before = pnic_before[name]
 
105
        stats_after = pnic_after[name]
102
106
        templ = "%-15s %15s %15s"
103
 
        print_line(templ % (
104
 
                nic, "TOTAL", "PER-SEC"),
105
 
            highlight=True
106
 
        )
 
107
        print_line(templ % (name, "TOTAL", "PER-SEC"), highlight=True)
107
108
        print_line(templ % (
108
109
            "bytes-sent",
109
110
            bytes2human(stats_after.bytes_sent),
134
135
        interval = 0
135
136
        while 1:
136
137
            args = poll(interval)
137
 
            refresh_window(*args) 
 
138
            refresh_window(*args)
138
139
            interval = 1
139
140
    except (KeyboardInterrupt, SystemExit):
140
 
        print
 
141
        pass
141
142
 
142
143
if __name__ == '__main__':
143
144
    main()