michael@0: =========== michael@0: Quick links michael@0: =========== michael@0: michael@0: * `Home page `_ michael@0: * `Download `_ michael@0: * `Documentation `_ michael@0: michael@0: ======= michael@0: Summary michael@0: ======= michael@0: michael@0: psutil is a module providing an interface for retrieving information on all michael@0: running processes and system utilization (CPU, memory, disks, network, users) in michael@0: a portable way by using Python, implementing many functionalities offered by michael@0: command line tools such as: **ps, top, df, kill, free, lsof, free, netstat, michael@0: ifconfig, nice, ionice, iostat, iotop, uptime, pidof, tty, who, taskset, pmap**. michael@0: michael@0: It currently supports **Linux**, **Windows**, **OSX**, **FreeBSD**, michael@0: **Sun Solaris** both **32-bit** and **64-bit** with Python versions from **2.4** michael@0: to **3.3** by using a single code base. michael@0: michael@0: ============== michael@0: Example usages michael@0: ============== michael@0: michael@0: CPU michael@0: === michael@0: michael@0: >>> import psutil michael@0: >>> psutil.cpu_times() michael@0: cputimes(user=3961.46, nice=169.729, system=2150.659, idle=16900.540, michael@0: iowait=629.509, irq=0.0, softirq=19.422, steal=0.0, guest=0, nice=0.0) michael@0: >>> michael@0: >>> for x in range(3): michael@0: ... psutil.cpu_percent(interval=1) michael@0: ... michael@0: 4.0 michael@0: 5.9 michael@0: 3.8 michael@0: >>> michael@0: >>> for x in range(3): michael@0: ... psutil.cpu_percent(interval=1, percpu=True) michael@0: ... michael@0: [4.0, 6.9] michael@0: [7.0, 8.5] michael@0: [1.2, 9.0] michael@0: >>> michael@0: >>> for x in range(3): michael@0: ... psutil.cpu_times_percent(interval=1, percpu=False) michael@0: ... michael@0: cpupercent(user=1.5, nice=0.0, system=0.5, idle=96.5, iowait=1.5, irq=0.0, michael@0: softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0) michael@0: cpupercent(user=1.0, nice=0.0, system=0.0, idle=99.0, iowait=0.0, irq=0.0, michael@0: softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0) michael@0: cpupercent(user=2.0, nice=0.0, system=0.0, idle=98.0, iowait=0.0, irq=0.0, michael@0: softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0) michael@0: michael@0: Memory michael@0: ====== michael@0: michael@0: >>> psutil.virtual_memory() michael@0: vmem(total=8374149120L, available=2081050624L, percent=75.1, used=8074080256L, michael@0: free=300068864L, active=3294920704, inactive=1361616896, buffers=529895424L, michael@0: cached=1251086336) michael@0: >>> psutil.swap_memory() michael@0: swap(total=2097147904L, used=296128512L, free=1801019392L, percent=14.1, michael@0: sin=304193536, sout=677842944) michael@0: >>> michael@0: michael@0: michael@0: Disks michael@0: ===== michael@0: michael@0: >>> psutil.disk_partitions() michael@0: [partition(device='/dev/sda1', mountpoint='/', fstype='ext4', opts='rw,nosuid'), michael@0: partition(device='/dev/sda2', mountpoint='/home', fstype='ext, opts='rw')] michael@0: >>> michael@0: >>> psutil.disk_usage('/') michael@0: usage(total=21378641920, used=4809781248, free=15482871808, percent=22.5) michael@0: >>> michael@0: >>> psutil.disk_io_counters() michael@0: iostat(read_count=719566, write_count=1082197, read_bytes=18626220032, michael@0: write_bytes=24081764352, read_time=5023392, write_time=63199568) michael@0: >>> michael@0: michael@0: michael@0: Network michael@0: ======= michael@0: michael@0: >>> psutil.net_io_counters(pernic=True) michael@0: {'lo': iostat(bytes_sent=799953745, bytes_recv=799953745, michael@0: packets_sent=453698, packets_recv=453698), michael@0: 'eth0': iostat(bytes_sent=734324837, bytes_recv=4163935363, michael@0: packets_sent=3605828, packets_recv=4096685)} michael@0: >>> michael@0: michael@0: Other system info michael@0: ================= michael@0: michael@0: >>> psutil.get_users() michael@0: [user(name='giampaolo', terminal='pts/2', host='localhost', started=1340737536.0), michael@0: user(name='giampaolo', terminal='pts/3', host='localhost', started=1340737792.0)] michael@0: >>> michael@0: >>> psutil.get_boot_time() michael@0: 1365519115.0 michael@0: michael@0: michael@0: Process management michael@0: ================== michael@0: michael@0: >>> import psutil michael@0: >>> psutil.get_pid_list() michael@0: [1, 2, 3, 4, 5, 6, 7, 46, 48, 50, 51, 178, 182, 222, 223, 224, michael@0: 268, 1215, 1216, 1220, 1221, 1243, 1244, 1301, 1601, 2237, 2355, michael@0: 2637, 2774, 3932, 4176, 4177, 4185, 4187, 4189, 4225, 4243, 4245, michael@0: 4263, 4282, 4306, 4311, 4312, 4313, 4314, 4337, 4339, 4357, 4358, michael@0: 4363, 4383, 4395, 4408, 4433, 4443, 4445, 4446, 5167, 5234, 5235, michael@0: 5252, 5318, 5424, 5644, 6987, 7054, 7055, 7071] michael@0: >>> michael@0: >>> p = psutil.Process(7055) michael@0: >>> p.name michael@0: 'python' michael@0: >>> p.exe michael@0: '/usr/bin/python' michael@0: >>> p.getcwd() michael@0: '/home/giampaolo' michael@0: >>> p.cmdline michael@0: ['/usr/bin/python', 'main.py'] michael@0: >>> michael@0: >>> str(p.status) michael@0: 'running' michael@0: >>> p.username michael@0: 'giampaolo' michael@0: >>> p.create_time michael@0: 1267551141.5019531 michael@0: >>> p.terminal michael@0: '/dev/pts/0' michael@0: >>> michael@0: >>> p.uids michael@0: user(real=1000, effective=1000, saved=1000) michael@0: >>> p.gids michael@0: group(real=1000, effective=1000, saved=1000) michael@0: >>> michael@0: >>> p.get_cpu_times() michael@0: cputimes(user=1.02, system=0.31) michael@0: >>> p.get_cpu_percent(interval=1.0) michael@0: 12.1 michael@0: >>> p.get_cpu_affinity() michael@0: [0, 1, 2, 3] michael@0: >>> p.set_cpu_affinity([0]) michael@0: >>> michael@0: >>> p.get_memory_percent() michael@0: 0.63423 michael@0: >>> michael@0: >>> p.get_memory_info() michael@0: meminfo(rss=7471104, vms=68513792) michael@0: >>> p.get_ext_memory_info() michael@0: meminfo(rss=9662464, vms=49192960, shared=3612672, text=2564096, lib=0, data=5754880, dirty=0) michael@0: >>> p.get_memory_maps() michael@0: [mmap(path='/lib/x86_64-linux-gnu/libutil-2.15.so', rss=16384, anonymous=8192, swap=0), michael@0: mmap(path='/lib/x86_64-linux-gnu/libc-2.15.so', rss=6384, anonymous=15, swap=0), michael@0: mmap(path='/lib/x86_64-linux-gnu/libcrypto.so.1.0.0', rss=34124, anonymous=1245, swap=0), michael@0: mmap(path='[heap]', rss=54653, anonymous=8192, swap=0), michael@0: mmap(path='[stack]', rss=1542, anonymous=166, swap=0), michael@0: ...] michael@0: >>> michael@0: >>> p.get_io_counters() michael@0: io(read_count=478001, write_count=59371, read_bytes=700416, write_bytes=69632) michael@0: >>> michael@0: >>> p.get_open_files() michael@0: [openfile(path='/home/giampaolo/svn/psutil/somefile', fd=3)] michael@0: >>> michael@0: >>> p.get_connections() michael@0: [connection(fd=115, family=2, type=1, laddr=('10.0.0.1', 48776), michael@0: raddr=('93.186.135.91', 80), status='ESTABLISHED'), michael@0: connection(fd=117, family=2, type=1, laddr=('10.0.0.1', 43761), michael@0: raddr=('72.14.234.100', 80), status='CLOSING'), michael@0: connection(fd=119, family=2, type=1, laddr=('10.0.0.1', 60759), michael@0: raddr=('72.14.234.104', 80), status='ESTABLISHED'), michael@0: connection(fd=123, family=2, type=1, laddr=('10.0.0.1', 51314), michael@0: raddr=('72.14.234.83', 443), status='SYN_SENT')] michael@0: >>> michael@0: >>> p.get_num_threads() michael@0: 4 michael@0: >>> p.get_num_fds() michael@0: 8 michael@0: >>> p.get_threads() michael@0: [thread(id=5234, user_time=22.5, system_time=9.2891), michael@0: thread(id=5235, user_time=0.0, system_time=0.0), michael@0: thread(id=5236, user_time=0.0, system_time=0.0), michael@0: thread(id=5237, user_time=0.0707, system_time=1.1)] michael@0: >>> michael@0: >>> p.get_num_ctx_switches() michael@0: amount(voluntary=78, involuntary=19) michael@0: >>> michael@0: >>> p.get_nice() michael@0: 0 michael@0: >>> p.set_nice(10) michael@0: >>> michael@0: >>> p.suspend() michael@0: >>> p.resume() michael@0: >>> michael@0: >>> p.terminate() michael@0: >>> p.wait(timeout=3) michael@0: 0 michael@0: >>> michael@0: >>> psutil.test() michael@0: USER PID %CPU %MEM VSZ RSS TTY START TIME COMMAND michael@0: root 1 0.0 0.0 24584 2240 ? Jun17 00:00 init michael@0: root 2 0.0 0.0 0 0 ? Jun17 00:00 kthreadd michael@0: root 3 0.0 0.0 0 0 ? Jun17 00:05 ksoftirqd/0 michael@0: ... michael@0: giampaolo 31475 0.0 0.0 20760 3024 /dev/pts/0 Jun19 00:00 python2.4 michael@0: giampaolo 31721 0.0 2.2 773060 181896 ? 00:04 10:30 chrome michael@0: root 31763 0.0 0.0 0 0 ? 00:05 00:00 kworker/0:1 michael@0: >>>