OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # | |
3 # $Id: process_detail.py 1143 2011-10-05 19:11:59Z g.rodola $ | |
4 # | |
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 | |
7 # found in the LICENSE file. | |
8 | |
9 """ | |
10 Print detailed information about a process. | |
11 """ | |
12 | |
13 import os | |
14 import datetime | |
15 import socket | |
16 import sys | |
17 | |
18 import psutil | |
19 from psutil._compat import namedtuple | |
20 | |
21 | |
22 def convert_bytes(n): | |
23 if n == 0: | |
24 return '0B' | |
25 symbols = ('k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y') | |
26 prefix = {} | |
27 for i, s in enumerate(symbols): | |
28 prefix[s] = 1 << (i+1)*10 | |
29 for s in reversed(symbols): | |
30 if n >= prefix[s]: | |
31 value = float(n) / prefix[s] | |
32 return '%.1f%s' % (value, s) | |
33 | |
34 def print_(a, b): | |
35 if sys.stdout.isatty(): | |
36 fmt = '\x1b[1;32m%-17s\x1b[0m %s' %(a, b) | |
37 else: | |
38 fmt = '%-15s %s' %(a, b) | |
39 print fmt | |
40 | |
41 def run(pid): | |
42 p = psutil.Process(pid) | |
43 if p.parent: | |
44 parent = '(%s)' % p.parent.name | |
45 else: | |
46 parent = '' | |
47 started = datetime.datetime.fromtimestamp(p.create_time).strftime('%Y-%M-%d
%H:%M') | |
48 io = p.get_io_counters() | |
49 mem = p.get_memory_info() | |
50 mem = '%s%% (resident=%s, virtual=%s) ' %(round(p.get_memory_percent(), 1), | |
51 convert_bytes(mem.rss), | |
52 convert_bytes(mem.vms)) | |
53 cpu_times = p.get_cpu_times() | |
54 cpu_percent = p.get_cpu_percent(0) | |
55 children = p.get_children() | |
56 files = p.get_open_files() | |
57 threads = p.get_threads() | |
58 connections = p.get_connections() | |
59 | |
60 print_('pid', p.pid) | |
61 print_('name', p.name) | |
62 print_('exe', p.exe) | |
63 print_('parent', '%s %s' % (p.ppid, parent)) | |
64 print_('cmdline', ' '.join(p.cmdline)) | |
65 print_('started', started) | |
66 print_('user', p.username) | |
67 if os.name == 'posix': | |
68 print_('uids', 'real=%s, effective=%s, saved=%s' % p.uids) | |
69 print_('gids', 'real=%s, effective=%s, saved=%s' % p.gids) | |
70 print_('terminal', p.terminal or '') | |
71 if hasattr(p, 'getcwd'): | |
72 print_('cwd', p.getcwd()) | |
73 print_('memory', mem) | |
74 print_('cpu', '%s%% (user=%s, system=%s)' % (cpu_percent, | |
75 cpu_times.user, | |
76 cpu_times.system)) | |
77 print_('status', p.status) | |
78 print_('niceness', p.nice) | |
79 print_('num threads', p.get_num_threads()) | |
80 if hasattr(p, 'get_io_counters'): | |
81 print_('I/O', 'bytes-read=%s, bytes-written=%s' % \ | |
82 (convert_bytes(io.read_bytes), | |
83 convert_bytes(io.write_bytes))) | |
84 if children: | |
85 print_('children', '') | |
86 for child in children: | |
87 print_('', 'pid=%s name=%s' % (child.pid, child.name)) | |
88 | |
89 if files: | |
90 print_('open files', '') | |
91 for file in files: | |
92 print_('', 'fd=%s %s ' % (file.fd, file.path)) | |
93 | |
94 if threads: | |
95 print_('running threads', '') | |
96 for thread in threads: | |
97 print_('', 'id=%s, user-time=%s, sys-time=%s' \ | |
98 % (thread.id, thread.user_time, thread.system_time)) | |
99 if connections: | |
100 print_('open connections', '') | |
101 for conn in connections: | |
102 type = 'TCP' if conn.type == socket.SOCK_STREAM else 'UDP' | |
103 lip, lport = conn.local_address | |
104 if not conn.remote_address: | |
105 rip, rport = '*', '*' | |
106 else: | |
107 rip, rport = conn.remote_address | |
108 print_('', '%s:%s -> %s:%s type=%s status=%s' \ | |
109 % (lip, lport, rip, rport, type, conn.status)) | |
110 | |
111 def main(argv=None): | |
112 if argv is None: | |
113 argv = sys.argv | |
114 if len(argv) == 1: | |
115 sys.exit(run(os.getpid())) | |
116 elif len(argv) == 2: | |
117 sys.exit(run(int(argv[1]))) | |
118 else: | |
119 sys.exit('usage: %s [pid]' % __file__) | |
120 | |
121 if __name__ == '__main__': | |
122 sys.exit(main()) | |
OLD | NEW |