OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # $Id: process_detail.py 1143 2011-10-05 19:11:59Z g.rodola $ | 3 # $Id: process_detail.py 1213 2011-10-29 03:30:41Z g.rodola $ |
4 # | 4 # |
5 # Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. | 5 # Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. |
6 # Use of this source code is governed by a BSD-style license that can be | 6 # Use of this source code is governed by a BSD-style license that can be |
7 # found in the LICENSE file. | 7 # found in the LICENSE file. |
8 | 8 |
9 """ | 9 """ |
10 Print detailed information about a process. | 10 Print detailed information about a process. |
11 """ | 11 """ |
12 | 12 |
13 import os | 13 import os |
14 import datetime | 14 import datetime |
15 import socket | 15 import socket |
16 import sys | 16 import sys |
17 | 17 |
18 import psutil | 18 import psutil |
19 from psutil._compat import namedtuple | |
20 | 19 |
21 | 20 |
22 def convert_bytes(n): | 21 def convert_bytes(n): |
23 if n == 0: | 22 if n == 0: |
24 return '0B' | 23 return '0B' |
25 symbols = ('k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y') | 24 symbols = ('k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y') |
26 prefix = {} | 25 prefix = {} |
27 for i, s in enumerate(symbols): | 26 for i, s in enumerate(symbols): |
28 prefix[s] = 1 << (i+1)*10 | 27 prefix[s] = 1 << (i+1)*10 |
29 for s in reversed(symbols): | 28 for s in reversed(symbols): |
30 if n >= prefix[s]: | 29 if n >= prefix[s]: |
31 value = float(n) / prefix[s] | 30 value = float(n) / prefix[s] |
32 return '%.1f%s' % (value, s) | 31 return '%.1f%s' % (value, s) |
| 32 return n |
33 | 33 |
34 def print_(a, b): | 34 def print_(a, b): |
35 if sys.stdout.isatty(): | 35 if sys.stdout.isatty() and os.name == 'posix': |
36 fmt = '\x1b[1;32m%-17s\x1b[0m %s' %(a, b) | 36 fmt = '\x1b[1;32m%-17s\x1b[0m %s' %(a, b) |
37 else: | 37 else: |
38 fmt = '%-15s %s' %(a, b) | 38 fmt = '%-15s %s' %(a, b) |
39 print fmt | 39 print fmt |
40 | 40 |
41 def run(pid): | 41 def run(pid): |
42 p = psutil.Process(pid) | 42 p = psutil.Process(pid) |
43 if p.parent: | 43 if p.parent: |
44 parent = '(%s)' % p.parent.name | 44 parent = '(%s)' % p.parent.name |
45 else: | 45 else: |
46 parent = '' | 46 parent = '' |
47 started = datetime.datetime.fromtimestamp(p.create_time).strftime('%Y-%M-%d
%H:%M') | 47 started = datetime.datetime.fromtimestamp(p.create_time).strftime('%Y-%M-%d
%H:%M') |
48 io = p.get_io_counters() | 48 if hasattr(p, 'get_io_counters'): |
| 49 io = p.get_io_counters() |
49 mem = p.get_memory_info() | 50 mem = p.get_memory_info() |
50 mem = '%s%% (resident=%s, virtual=%s) ' %(round(p.get_memory_percent(), 1), | 51 mem = '%s%% (resident=%s, virtual=%s) ' %(round(p.get_memory_percent(), 1), |
51 convert_bytes(mem.rss), | 52 convert_bytes(mem.rss), |
52 convert_bytes(mem.vms)) | 53 convert_bytes(mem.vms)) |
53 cpu_times = p.get_cpu_times() | 54 cpu_times = p.get_cpu_times() |
54 cpu_percent = p.get_cpu_percent(0) | 55 cpu_percent = p.get_cpu_percent(0) |
55 children = p.get_children() | 56 children = p.get_children() |
56 files = p.get_open_files() | 57 files = p.get_open_files() |
57 threads = p.get_threads() | 58 threads = p.get_threads() |
58 connections = p.get_connections() | 59 connections = p.get_connections() |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 print_('', 'fd=%s %s ' % (file.fd, file.path)) | 93 print_('', 'fd=%s %s ' % (file.fd, file.path)) |
93 | 94 |
94 if threads: | 95 if threads: |
95 print_('running threads', '') | 96 print_('running threads', '') |
96 for thread in threads: | 97 for thread in threads: |
97 print_('', 'id=%s, user-time=%s, sys-time=%s' \ | 98 print_('', 'id=%s, user-time=%s, sys-time=%s' \ |
98 % (thread.id, thread.user_time, thread.system_time)) | 99 % (thread.id, thread.user_time, thread.system_time)) |
99 if connections: | 100 if connections: |
100 print_('open connections', '') | 101 print_('open connections', '') |
101 for conn in connections: | 102 for conn in connections: |
102 type = 'TCP' if conn.type == socket.SOCK_STREAM else 'UDP' | 103 if conn.type == socket.SOCK_STREAM: |
| 104 type = 'TCP' |
| 105 elif conn.type == socket.SOCK_DGRAM: |
| 106 type = 'UDP' |
| 107 else: |
| 108 type = 'UNIX' |
103 lip, lport = conn.local_address | 109 lip, lport = conn.local_address |
104 if not conn.remote_address: | 110 if not conn.remote_address: |
105 rip, rport = '*', '*' | 111 rip, rport = '*', '*' |
106 else: | 112 else: |
107 rip, rport = conn.remote_address | 113 rip, rport = conn.remote_address |
108 print_('', '%s:%s -> %s:%s type=%s status=%s' \ | 114 print_('', '%s:%s -> %s:%s type=%s status=%s' \ |
109 % (lip, lport, rip, rport, type, conn.status)) | 115 % (lip, lport, rip, rport, type, conn.status)) |
110 | 116 |
111 def main(argv=None): | 117 def main(argv=None): |
112 if argv is None: | 118 if argv is None: |
113 argv = sys.argv | 119 argv = sys.argv |
114 if len(argv) == 1: | 120 if len(argv) == 1: |
115 sys.exit(run(os.getpid())) | 121 sys.exit(run(os.getpid())) |
116 elif len(argv) == 2: | 122 elif len(argv) == 2: |
117 sys.exit(run(int(argv[1]))) | 123 sys.exit(run(int(argv[1]))) |
118 else: | 124 else: |
119 sys.exit('usage: %s [pid]' % __file__) | 125 sys.exit('usage: %s [pid]' % __file__) |
120 | 126 |
121 if __name__ == '__main__': | 127 if __name__ == '__main__': |
122 sys.exit(main()) | 128 sys.exit(main()) |
OLD | NEW |