1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/python/psutil/examples/process_detail.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,134 @@ 1.4 +#!/usr/bin/env python 1.5 + 1.6 +# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. 1.7 +# Use of this source code is governed by a BSD-style license that can be 1.8 +# found in the LICENSE file. 1.9 + 1.10 +""" 1.11 +Print detailed information about a process. 1.12 + 1.13 +Author: Giampaolo Rodola' <g.rodola@gmail.com> 1.14 +""" 1.15 + 1.16 +import os 1.17 +import datetime 1.18 +import socket 1.19 +import sys 1.20 + 1.21 +import psutil 1.22 + 1.23 + 1.24 +def convert_bytes(n): 1.25 + symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y') 1.26 + prefix = {} 1.27 + for i, s in enumerate(symbols): 1.28 + prefix[s] = 1 << (i+1)*10 1.29 + for s in reversed(symbols): 1.30 + if n >= prefix[s]: 1.31 + value = float(n) / prefix[s] 1.32 + return '%.1f%s' % (value, s) 1.33 + return "%sB" % n 1.34 + 1.35 +def print_(a, b): 1.36 + if sys.stdout.isatty() and os.name == 'posix': 1.37 + fmt = '\x1b[1;32m%-17s\x1b[0m %s' %(a, b) 1.38 + else: 1.39 + fmt = '%-15s %s' %(a, b) 1.40 + # python 2/3 compatibility layer 1.41 + sys.stdout.write(fmt + '\n') 1.42 + sys.stdout.flush() 1.43 + 1.44 +def run(pid): 1.45 + ACCESS_DENIED = '' 1.46 + try: 1.47 + p = psutil.Process(pid) 1.48 + pinfo = p.as_dict(ad_value=ACCESS_DENIED) 1.49 + except psutil.NoSuchProcess: 1.50 + sys.exit(str(sys.exc_info()[1])) 1.51 + 1.52 + try: 1.53 + if p.parent: 1.54 + parent = '(%s)' % p.parent.name 1.55 + else: 1.56 + parent = '' 1.57 + except psutil.Error: 1.58 + parent = '' 1.59 + started = datetime.datetime.fromtimestamp(pinfo['create_time'] 1.60 + ).strftime('%Y-%M-%d %H:%M') 1.61 + io = pinfo.get('io_counters', ACCESS_DENIED) 1.62 + mem = '%s%% (resident=%s, virtual=%s) ' % ( 1.63 + round(pinfo['memory_percent'], 1), 1.64 + convert_bytes(pinfo['memory_info'].rss), 1.65 + convert_bytes(pinfo['memory_info'].vms)) 1.66 + children = p.get_children() 1.67 + 1.68 + print_('pid', pinfo['pid']) 1.69 + print_('name', pinfo['name']) 1.70 + print_('exe', pinfo['exe']) 1.71 + print_('parent', '%s %s' % (pinfo['ppid'], parent)) 1.72 + print_('cmdline', ' '.join(pinfo['cmdline'])) 1.73 + print_('started', started) 1.74 + print_('user', pinfo['username']) 1.75 + if os.name == 'posix' and pinfo['uids'] and pinfo['gids']: 1.76 + print_('uids', 'real=%s, effective=%s, saved=%s' % pinfo['uids']) 1.77 + if os.name == 'posix' and pinfo['gids']: 1.78 + print_('gids', 'real=%s, effective=%s, saved=%s' % pinfo['gids']) 1.79 + if os.name == 'posix': 1.80 + print_('terminal', pinfo['terminal'] or '') 1.81 + if hasattr(p, 'getcwd'): 1.82 + print_('cwd', pinfo['cwd']) 1.83 + print_('memory', mem) 1.84 + print_('cpu', '%s%% (user=%s, system=%s)' % (pinfo['cpu_percent'], 1.85 + getattr(pinfo['cpu_times'], 'user', '?'), 1.86 + getattr(pinfo['cpu_times'], 'system', '?'))) 1.87 + print_('status', pinfo['status']) 1.88 + print_('niceness', pinfo['nice']) 1.89 + print_('num threads', pinfo['num_threads']) 1.90 + if io != ACCESS_DENIED: 1.91 + print_('I/O', 'bytes-read=%s, bytes-written=%s' % \ 1.92 + (convert_bytes(io.read_bytes), 1.93 + convert_bytes(io.write_bytes))) 1.94 + if children: 1.95 + print_('children', '') 1.96 + for child in children: 1.97 + print_('', 'pid=%s name=%s' % (child.pid, child.name)) 1.98 + 1.99 + if pinfo['open_files'] != ACCESS_DENIED: 1.100 + print_('open files', '') 1.101 + for file in pinfo['open_files']: 1.102 + print_('', 'fd=%s %s ' % (file.fd, file.path)) 1.103 + 1.104 + if pinfo['threads']: 1.105 + print_('running threads', '') 1.106 + for thread in pinfo['threads']: 1.107 + print_('', 'id=%s, user-time=%s, sys-time=%s' \ 1.108 + % (thread.id, thread.user_time, thread.system_time)) 1.109 + if pinfo['connections'] != ACCESS_DENIED: 1.110 + print_('open connections', '') 1.111 + for conn in pinfo['connections']: 1.112 + if conn.type == socket.SOCK_STREAM: 1.113 + type = 'TCP' 1.114 + elif conn.type == socket.SOCK_DGRAM: 1.115 + type = 'UDP' 1.116 + else: 1.117 + type = 'UNIX' 1.118 + lip, lport = conn.laddr 1.119 + if not conn.raddr: 1.120 + rip, rport = '*', '*' 1.121 + else: 1.122 + rip, rport = conn.raddr 1.123 + print_('', '%s:%s -> %s:%s type=%s status=%s' \ 1.124 + % (lip, lport, rip, rport, type, conn.status)) 1.125 + 1.126 +def main(argv=None): 1.127 + if argv is None: 1.128 + argv = sys.argv 1.129 + if len(argv) == 1: 1.130 + sys.exit(run(os.getpid())) 1.131 + elif len(argv) == 2: 1.132 + sys.exit(run(int(argv[1]))) 1.133 + else: 1.134 + sys.exit('usage: %s [pid]' % __file__) 1.135 + 1.136 +if __name__ == '__main__': 1.137 + sys.exit(main())