psutil是一个跨平台库(http://code.google.com/p/psutil/),能够轻松实现获取系统运行的进程和系统利用率(包括CPU、内存、磁盘、网络等)信息。它主要应用于系统监控,分析和限制系统资源及进程的管理。它实现了同等命令行工具提供的功能,如ps、top、lsof、netstat、ifconfig、who、df、kill、free、nice、ionice、iostat、iotop、uptime、pidof、tty、taskset、pmap等。目前支持32位和64位的Linux、Windows、OS X、FreeBSD和Sun Solaris等操作系统
一、下载安装
下载地址:
https://pypi.python.org/pypi/psutil#downloads
下载最新的psutil-5.4.0.tar.gz到本地目录,然后解压安装
tar -xzvf psutil-5.4.0.tar.gz
cd psutil-5.4.0
sudo python setup.py install
二、使用
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import psutil
print "CPU篇"
print "查看cpu所有信息",psutil.cpu_times()
print "查看cpu逻辑个数:",psutil.cpu_count()
print "-"*50
print "内存篇"
mem = psutil.virtual_memory()
print "系统内存所有信息:",mem
print "物理内存total值:",mem.total
print "物理内存used值:",mem.used
print "物理内存free值:",mem.free
print "-"*50
print "IO篇"
print psutil.disk_io_counters()
print "-"*50
print "网络信息篇"
print "获取网络总IO信息:",psutil.net_io_counters()
print "输出网络每个接口信息:",psutil.net_io_counters(pernic=True)
print "-"*50
print "系统篇"
print "获取当前系统用户登录信息:",psutil.users()
print "获取开机时间:",psutil.boot_time()
print "-"*50
print "进程篇"
print "全部进程:",psutil.pids()
print "查看单个进程:"
p = psutil.Process(psutil.pids()[0])
print p.name() #进程名
print p.exe() #进程的bin路径
print p.cwd() #进程的工作目录绝对路径
print p.status() #进程状态
print p.create_time() #进程创建时间
print p.uids() #进程uid信息
print p.gids() #进程的gid信息
print p.cpu_times() #进程的cpu时间信息,包括user,system两个cpu信息
print p.memory_percent() #进程内存利用率
print p.memory_info() #进程内存rss,vms信息
print p.num_threads() #进程开启的线程数
输入:
CPU篇
查看cpu所有信息 scputimes(user=199778.34, nice=0.0, system=129090.97, idle=2147992.8)
查看cpu逻辑个数: 4
--------------------------------------------------
内存篇
系统内存所有信息: svmem(total=8589934592L, available=2206568448L, percent=74.3, used=6914883584L, free=69648384L, active=2192187392L, inactive=2136920064L, wired=2585776128L)
物理内存total值: 8589934592
物理内存used值: 6914883584
物理内存free值: 69648384
--------------------------------------------------
IO篇
sdiskio(read_count=16768800L, write_count=6494421L, read_bytes=490265200128L, write_bytes=427495276032L, read_time=8553809L, write_time=4596681L)
--------------------------------------------------
网络信息篇
获取网络总IO信息: snetio(bytes_sent=2002375048L, bytes_recv=11383527651L, packets_sent=15315862L, packets_recv=17018710L, errin=0L, errout=0L, dropin=0L, dropout=0)
输出网络每个接口信息: {'gif0': snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0), 'utun0': snetio(bytes_sent=268L, bytes_recv=0L, packets_sent=3L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0), 'en2': snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0), 'en0': snetio(bytes_sent=1687986426L, bytes_recv=10973766141L, packets_sent=11555082L, packets_recv=12806110L, errin=0L, errout=0L, dropin=0L, dropout=0), 'en1': snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0), 'lo0': snetio(bytes_sent=310665891L, bytes_recv=310665891L, packets_sent=3746872L, packets_recv=3746872L, errin=0L, errout=0L, dropin=0L, dropout=0), 'bridge0': snetio(bytes_sent=342L, bytes_recv=0L, packets_sent=1L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0), 'p2p0': snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0), 'stf0': snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0), 'awdl0': snetio(bytes_sent=3722121L, bytes_recv=99095619L, packets_sent=13904L, packets_recv=465728L, errin=0L, errout=0L, dropin=0L, dropout=0)}
--------------------------------------------------
系统篇
获取当前系统用户登录信息: [suser(name='bestfei', terminal='console', host=None, started=1507604480.0, pid=94), suser(name='bestfei', terminal='ttys000', host=None, started=1507604864.0, pid=609), suser(name='bestfei', terminal='ttys001', host=None, started=1510044800.0, pid=33488), suser(name='bestfei', terminal='ttys002', host=None, started=1509589632.0, pid=24702), suser(name='bestfei', terminal='ttys004', host=None, started=1509535872.0, pid=24351)]
获取开机时间: 1507604480.0
--------------------------------------------------
进程篇
全部进程: [33652, 33649, 33643, 33642, 33637, 33636, 33634, 33633, 33629, 33628, 33623, 33574, 33498, 33497, 33496, 33495, 33489, 33488, 33482, 33425, 33401, 33400, 33365, 33364, 33348, 33194, 33069, 33035, 33023, 33021, 33017, 33007, 33006, 33005, 33001, 32987, 32963, 32952, 32900, 32899, 32897, 32895, 32894, 32889, 32888, 32887, 32854, 32853, 32848, 32847, 32846, 32845, 32844, 32843, 32839, 32837, 32836, 32831, 32824, 32823, 31435, 31434, 31134, 31133, 31128, 31127, 31126, 31125, 31124, 31123, 31121, 31118, 31094, 31090, 31062, 31061, 31056, 31052, 31046, 31045, 31031, 31030, 31027, 31025, 31024, 31023, 31021, 31015, 31014, 31013, 31006, 31005, 31004, 31003, 31002, 31000, 30999, 30998, 30997, 30995, 30993, 30992, 30991, 30988, 30972, 30833, 30831, 30588, 30460, 29649, 29401, 29306, 29305, 29298, 29296, 29295, 28701, 28392, 28390, 28388, 25459, 25454, 24711, 24706, 24703, 24702, 24701, 24352, 24351, 23717, 23283, 23281, 23280, 23211, 23131, 22750, 22713, 22694, 21476, 21234, 21219, 21198, 21197, 21195, 21194, 19373, 19318, 16101, 13469, 11762, 11759, 10128, 9320, 5215, 4644, 4112, 3380, 2682, 2238, 2082, 1942, 1665, 1663, 808, 677, 620, 610, 609, 538, 537, 520, 440, 436, 414, 413, 411, 410, 408, 407, 405, 393, 389, 385, 377, 365, 364, 363, 361, 360, 359, 358, 357, 354, 352, 348, 337, 333, 332, 331, 330, 326, 322, 316, 309, 307, 305, 303, 298, 289, 286, 285, 284, 275, 272, 268, 264, 255, 254, 250, 249, 248, 239, 236, 234, 233, 232, 221, 210, 181, 180, 178, 176, 175, 174, 172, 168, 161, 159, 158, 157, 155, 135, 132, 108, 106, 104, 102, 101, 100, 99, 98, 96, 95, 94, 93, 90, 89, 88, 87, 85, 83, 81, 80, 79, 77, 76, 68, 60, 59, 57, 53, 47, 46, 45, 40, 39, 38, 36, 35, 1]
查看单个进程:
Python
/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
/Users/bestfei/Documents/fei/python
running
1510048067.4
puids(real=501, effective=501, saved=501)
puids(real=20, effective=20, saved=20)
pcputimes(user=0.036896284, system=0.030256296, children_user=0.0, children_system=0.0)
0.150680541992
pmem(rss=12951552L, vms=2524672000L, pfaults=5189, pageins=81)
1