psutil 简单使用

在 Python 中,可以使用psutil获取系统信息,用于系统监控、性能分析、进程管理等。

安装

pip install psutil

获取CPU信息

  • 使用psutil.cpu_times()获取CPU的完整信息,统计CPU的用户/系统/空闲时间
>>> psutil.cpu_times()
scputimes(user=63246.1, nice=0.0, system=49096.97, idle=1209937.23)
  • 使用psutil.cpu_count(logical)获取CPU逻辑个数
# CPU 逻辑数量,logical默认True
>>> psutil.cpu_count()
4
# CPU 物理数量
>>> psutil.cpu_count(logical=False)
2
  • 使用psutil.cpu_percent(interval, percpu)获取CPU使用率,interval指定的是计算CPU使用率的时间间隔,percpu指定的是总的使用率还是每个CPU的使用率
>>> psutil.cpu_percent()
4.2
# percpu默认值为False
>>> psutil.cpu_percent(percpu=True)
[7.0, 1.6, 5.4, 1.4]

获取内存信息

  • 使用psutil.virtual_memory()获取系统内存的使用情况
>>> psutil.virtual_memory()
# total总内存,available可用的,percent已使用百分比,used已使用
svmem(total=8589934592, available=2317025280, percent=73.0, used=3988090880, free=66154496, active=2224459776, inactive=2235772928, wired=1763631104)
  • 使用psutil.swap_memory()获取系统交换内存的统计信息
>>> psutil.swap_memory()
# total总内存,available可用的,percent已使用百分比,used已使用
sswap(total=1073741824, used=646184960, free=427556864, percent=60.2, sin=69755117568, sout=526426112)

获取磁盘信息

  • 使用psutil.disk_partitions()获取磁盘分区的信息
>>> psutil.disk_partitions()
[sdiskpart(device='/dev/disk1s5', mountpoint='/', fstype='apfs', opts='ro,local,rootfs,dovolfs,journaled,multilabel'), sdiskpart(device='/dev/disk1s1', mountpoint='/System/Volumes/Data', fstype='apfs', opts='rw,local,dovolfs,dontbrowse,journaled,multilabel'), sdiskpart(device='/dev/disk1s4', mountpoint='/private/var/vm', fstype='apfs', opts='rw,local,dovolfs,dontbrowse,journaled,multilabel')]
  • 使用psutil.disk_usage("/")获取磁盘的使用情况
>>> psutil.disk_usage("/")
sdiskusage(total=121123069952, used=10966528000, free=13031055360, percent=45.7)
  • 使用psutil.disk_io_counters()获取磁盘的IO统计信息(读写速度等)
>>> psutil.disk_io_counters()
sdiskio(read_count=6678926, write_count=4484622, read_bytes=104868990464, write_bytes=53424848896, read_time=1948889, write_time=791271)

获取网络信息

  • 使用psutil.net_io_counters()获取网络IO信息
# 获取总的网络IO信息
>>> psutil.net_io_counters()
snetio(bytes_sent=1180297216, bytes_recv=4190517248, packets_sent=14631012, packets_recv=17305791, errin=0, errout=0, dropin=0, dropout=0)
# 获取网卡的IO信息
>>> psutil.net_io_counters(pernic=True)
{'lo0': snetio(bytes_sent=806161408, bytes_recv=806161408, packets_sent=11780886, packets_recv=11780886, errin=0, errout=0, dropin=0, dropout=0), 'gif0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'stf0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'en0': snetio(bytes_sent=373991424, bytes_recv=3381935104, packets_sent=2852304, packets_recv=5517417, errin=0, errout=0, dropin=0, dropout=0), 'en1': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'en2': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'bridge0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'p2p0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'awdl0': snetio(bytes_sent=602112, bytes_recv=2925568, packets_sent=2411, packets_recv=13248, errin=0, errout=0, dropin=0, dropout=0), 'llw0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'utun0': snetio(bytes_sent=67584, bytes_recv=0, packets_sent=352, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'utun1': snetio(bytes_sent=67584, bytes_recv=0, packets_sent=352, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0)}
  • 使用psutil.net_if_addrs()获取网络接口信息
>>> psutil.net_if_addrs()
{'lo0': [snicaddr(family=, address='127.0.0.1', netmask='255.0.0.0', broadcast=None, ptp=None), snicaddr(family=, address='::1', netmask='ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', broadcast=None, ptp=None), snicaddr(family=, address='fe80::1%lo0', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None)], 'en0': [snicaddr(family=, address='192.168.31.100', netmask='255.255.255.0', broadcast='192.168.31.255', ptp=None), snicaddr(family=, address='ac:bc:32:7e:8e:c1', netmask=None, broadcast=None, ptp=None), snicaddr(family=, address='fe80::5b:b89a:7e65:fc29%en0', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None)], 'en1': [snicaddr(family=, address='82:13:0e:b6:7b:40', netmask=None, broadcast=None, ptp=None)], 'en2': [snicaddr(family=, address='82:13:0e:b6:7b:41', netmask=None, broadcast=None, ptp=None)], 'bridge0': [snicaddr(family=, address='82:13:0e:b6:7b:40', netmask=None, broadcast=None, ptp=None)], 'p2p0': [snicaddr(family=, address='0e:bc:32:7e:8e:c1', netmask=None, broadcast=None, ptp=None)], 'awdl0': [snicaddr(family=, address='9a:ed:7b:bb:f5:ee', netmask=None, broadcast=None, ptp=None), snicaddr(family=, address='fe80::98ed:7bff:febb:f5ee%awdl0', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None)], 'llw0': [snicaddr(family=, address='9a:ed:7b:bb:f5:ee', netmask=None, broadcast=None, ptp=None), snicaddr(family=, address='fe80::98ed:7bff:febb:f5ee%llw0', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None)], 'utun0': [snicaddr(family=, address='fe80::4926:d5a4:30d4:87f7%utun0', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None)], 'utun1': [snicaddr(family=, address='fe80::fc8a:aea8:87ff:508a%utun1', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None)]}
  • 使用psutil.net_if_stats()获取网络接口状态信息
>>> psutil.net_if_stats()
{'lo0': snicstats(isup=True, duplex=, speed=0, mtu=16384), 'gif0': snicstats(isup=False, duplex=, speed=0, mtu=1280), 'stf0': snicstats(isup=False, duplex=, speed=0, mtu=1280), 'en0': snicstats(isup=True, duplex=, speed=0, mtu=1500), 'en1': snicstats(isup=True, duplex=, speed=0, mtu=1500), 'en2': snicstats(isup=True, duplex=, speed=0, mtu=1500), 'bridge0': snicstats(isup=True, duplex=, speed=0, mtu=1500), 'p2p0': snicstats(isup=True, duplex=, speed=0, mtu=2304), 'awdl0': snicstats(isup=True, duplex=, speed=0, mtu=1484), 'llw0': snicstats(isup=True, duplex=, speed=0, mtu=1500), 'utun0': snicstats(isup=True, duplex=, speed=0, mtu=1380), 'utun1': snicstats(isup=True, duplex=, speed=0, mtu=2000)}

获取其他系统信息

  • 使用psutil.boot_time()获取系统的开机时间
>>> psutil.boot_time()
1620261504.0
  • 使用psutil.users()获取连接系统的用户列表
>>> psutil.users()
[suser(name='he', terminal='console', host=None, started=1620261504.0, pid=159), suser(name='he', terminal='ttys000', host=None, started=1620959104.0, pid=15425)]
  • 使用psutil.pids()获取系统的全部进程信息
>>> psutil.pids()
[0, 1, 90, 91, 94, 95, 96, 97, 100, 101, 103, 107, 108, 111, 115, 116, 117, 120, 123, 124, 125, 126, 127, 128, 129, 131, 134, 135, 136, 140, 141, 142, 143, 145, 147, 148, 149, 150, 151, 152, 153, 154, 156, 157, 158, 159, 161, 162, 170, 172, 175, 178, 184, 202, 203, 204, 209, 211, 212, 216, 243, 249, 250, 251, 256, 257, 261, 262, 263, 271, 272, 276, 279, 284, 289, 290, 291, 294, 296, 297, 298, 299, 300, 301, 302, 303, 304, 306, 307, 308, 310, 311, 312, 316, 317, 318, 325, 326, 327, 329, 330, 331, 332, 338, 339, 342, 344, 345, 347, 350, 351, 352, 353, 355, 356, 357, 360, 361, 362, 363, 364, 365, 369, 370, 371, 372, 373, 374, 375, 378, 379, 381, 382, 383, 384, 385, 389, 390, 396, 397, 398, 401, 403, 407, 410, 414, 415, 416, 418, 421, 422, 423, 425, 431, 434, 437, 450, 457, 458, 459, 460, 462, 466, 481, 482, 483, 484, 485, 489, 491, 501, 515, 518, 521, 523, 526, 532, 533, 570, 577, 578, 600, 615, 619, 622, 623, 624, 633, 657, 661, 669, 670, 671, 691, 704, 737, 766, 1099, 1107, 1108, 1109, 1122, 1247, 1568, 1696, 1699, 1700, 1703, 1704, 2364, 2368, 2369, 2793, 2887, 3604, 3605, 3624, 3953, 3954, 4114, 4115, 5027, 5033, 5115, 5119, 5158, 5163, 5164, 5426, 5427, 9234, 9237, 9238, 9239, 9451, 9471, 10834, 10840, 10997, 10998, 11045, 11047, 11059, 11068, 11069, 11070, 11071, 11083, 11088, 11089, 11090, 11091, 11093, 11094, 11095, 11096, 11097, 11100, 11102, 11115, 11117, 11119, 11120, 11121, 11155, 11177, 11181, 11192, 11257, 11259, 11260, 11261, 11262, 11263, 11292, 11297, 11299, 11300, 11301, 11308, 11340, 11414, 11422, 11688, 11747, 11831, 11832, 12020, 12061, 12087, 12089, 12145, 12181, 12182, 12185, 12186, 12251, 12628, 14252, 14265, 14398, 14399, 14442, 14481, 14736, 14741, 14748, 14751, 14757, 14758, 14789, 14790, 15052, 15163, 15270, 15307, 15416, 15420, 15421, 15422, 15423, 15425, 15426, 15432, 15438, 15441, 15443, 15445, 15447, 15449, 15455, 15465, 15507, 15797, 15798, 15804, 15889, 15936, 16042, 16083, 16095, 16107, 16139, 16140, 16141, 16145, 16146, 16160, 16161, 16165, 16171, 16244, 16254, 16256, 16289, 16299, 16300, 16355]
  • 获取单个进程信息
# 获取指定进程ID=2793
>>> process = psutil.Process(16355)

# 进程名称
>>> process.name()
'Python'

# 获取exe路径
>>> process.exe()
'/Library/Frameworks/Python.framework/Versions/3.7/Resources/Python.app/Contents/MacOS/Python'

# 进程工作目录
>>> process.cwd()
'/Users/he'

# 进程启动的命令行
>>> process.cmdline()
['/Library/Frameworks/Python.framework/Versions/3.7/Resources/Python.app/Contents/MacOS/Python']

# 父进程ID
>>> process.ppid()
16352

# 父进程
>>> process.parent()
psutil.Process(pid=16352, name='zsh', started='09:52:49')

# 子进程列表
>>> process.children()
[]

# 进程状态
>>> process.status()
'running'

# 进程用户名
>>> process.username()
'he'

# 进程创建时间
>>> process.create_time()
1621043575.215562

# 进程终端
>>> process.terminal()
'/dev/ttys000'

# 进程使用CPU时间
>>> process.cpu_times()
pcputimes(user=0.070758784, system=0.065384012, children_user=0.0, children_system=0.0)

# 进程使用的内存
>>> process.memory_info()
pmem(rss=9789440, vms=4359475200, pfaults=3122, pageins=688)

# 进程打开的文件
>>> process.open_files()
[]

# 进程相关网络连接
>>> process.connections()
[]

# 进程的线程数量
>>> process.num_threads()
1

# 所有线程信息
>>> process.threads()
[pthread(id=1, user_time=0.072339, system_time=0.066793)]

# 进程环境变量
>>> process.environ()
{'TMPDIR': '/var/folders/zv/c2d0_ndj7mqd59v9hvl7_z_40000gn/T/', 'XPC_FLAGS': '0x0', 'TERM_PROGRAM_VERSION': '433', 'LANG': 'zh_CN.UTF-8', 'TERM_PROGRAM': 'Apple_Terminal', 'XPC_SERVICE_NAME': '0', 'TERM_SESSION_ID': 'D7879C9B-55A7-4DF0-9664-59ED3BD9D8F7', 'TERM': 'xterm-256color', 'SSH_AUTH_SOCK': '/private/tmp/com.apple.launchd.zlIjVLQGQx/Listeners', 'SHELL': '/bin/zsh', 'HOME': '/Users/he', 'LOGNAME': 'he', 'USER': 'he', 'PATH': '/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin', 'SHLVL': '1', 'PWD': '/Users/he', 'OLDPWD': '/Users/he', '_': '/Library/Frameworks/Python.framework/Versions/3.7/bin/python3', '__CF_USER_TEXT_ENCODING': '0x1F5:0x19:0x34', '__PYVENV_LAUNCHER__': '/Library/Frameworks/Python.framework/Versions/3.7/bin/python3'}

# 结束进程
>>> process.terminate()

你可能感兴趣的:(psutil 简单使用)