系统性能信息模块--psutil

#安装psutil模块
#pip install psutil -i https://pypi.doubanio.com/simple


#导入psutil模块
import psutil
import datetime
#将虚拟内存写入变量
mem=psutil.virtual_memory()
#默认单位是字节,将单位换算成GB,这里按照1000来换算,理论上应该按照1024来计算
mem_total=mem.total / 1000000000
mem_userd=mem.used / 1000000000
mem_free=mem.free / 1000000000
mem_swap=psutil.swap_memory()
print('内存总数' + str(mem_total) + 'G')
print('内存剩余' + str(mem_userd) + 'G')
print(mem_swap)

#cpu相关信息
cpu_info=psutil.cpu_times()
cpu_user=psutil.cpu_times().user
cpu_count=psutil.cpu_count()
print(cpu_user)
print(cpu_info)
print('cpu核数:' + str(cpu_count) + '核')

#磁盘信息
disk_info=psutil.disk_partitions()
print('硬盘信息:' + str(disk_info))
disk_info_usage=psutil.disk_usage('/')
print('硬盘分区信息:' + str(disk_info_usage))

#网络信息:流量监控,指接收和发送流量的相关信息
net_info=psutil.net_io_counters()
print('网络信息:' + str(net_info))

#用户登录
user=psutil.users()
print(user)

#系统开机时间
host_boot_time=psutil.boot_time()
boot_time=datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%F %H:%M:%S")
print('开机时间:' + str(boot_time))

#进程信息
pro_pid=psutil.pids()#进程id
pro_name=psutil.Process(40)
print(pro_name)
print(pro_pid)

=====================================================================
输出结果:

内存总数17.179869184G
内存剩余8.441253888G
sswap(total=0, used=0, free=0, percent=0, sin=19010850816, sout=0)
7659.97
scputimes(user=7659.97, nice=0.0, system=3262.39, idle=166041.86)
cpu核数:8核
硬盘信息:[sdiskpart(device='/dev/disk0s2', mountpoint='/', fstype='hfs', opts='rw,local,rootfs,dovolfs,journaled,multilabel')]
硬盘分区信息:sdiskusage(total=250656219136, used=213723643904, free=36670431232, percent=85.4)
网络信息:snetio(bytes_sent=60475392, bytes_recv=779055104, packets_sent=618839, packets_recv=1389501, errin=0, errout=4, dropin=0, dropout=0)
[suser(name='admin', terminal='console', host=None, started=1561511424.0, pid=92), suser(name='admin', terminal='ttys001', host=None, started=1561539712.0, pid=2887)]
开机时间:2019-06-26 09:10:24
psutil.Process(pid=40, name='UserEventAgent', started='09:09:22')
[0, 1, 40, 41, 44, 45, 46, 48, 51, 53, 54, 55, 56, 58, 59, 63, 68, 69, 71, 72, 75, 77, 78, 79, 80, 81, 82, 85, 86, 88, 91, 92, 93, 94, 96, 97, 98, 99, 100, 101, 102, 104, 119, 123, 124, 128, 130, 132, 133, 135, 136, 137, 138, 139, 140, 141, 144, 145, 149, 150, 163, 164, 166, 167, 168, 173, 175, 176, 177, 180, 181, 182, 183, 184, 185, 194, 195, 201, 204, 205, 206, 211, 212, 213, 215, 216, 217, 218, 219, 220, 222, 223, 225, 229, 230, 231, 233, 235, 236, 237, 238, 239, 240, 241, 242, 245, 246, 247, 248, 250, 252, 254, 256, 258, 259, 260, 262, 263, 264, 265, 266, 267, 268, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 301, 302, 304, 308, 309, 310, 311, 312, 313, 315, 318, 319, 320, 323, 324, 325, 328, 329, 332, 335, 336, 337, 338, 339, 341, 342, 343, 344, 345, 346, 348, 349, 351, 352, 353, 355, 356, 357, 374, 375, 376, 378, 379, 387, 388, 389, 391, 392, 393, 394, 400, 401, 402, 404, 408, 409, 413, 423, 428, 429, 430, 436, 438, 487, 488, 490, 493, 494, 495, 496, 497, 502, 506, 507, 509, 510, 511, 513, 514, 515, 578, 579, 580, 581, 582, 590, 591, 593, 594, 595, 598, 603, 606, 607, 609, 610, 662, 788, 884, 1263, 1918, 1927, 1945, 1946, 1947, 1948, 1949, 1950, 1951, 1953, 1954, 1973, 2046, 2054, 2058, 2059, 2062, 2066, 2082, 2083, 2097, 2098, 2102, 2103, 2106, 2109, 2119, 2120, 2136, 2140, 2141, 2157, 2158, 2164, 2174, 2175, 2318, 2343, 2349, 2366, 2375, 2378, 2379, 2384, 2386, 2387, 2390, 2392, 2394, 2400, 2412, 2415, 2445, 2487, 2489, 2491, 2650, 2714, 2715, 2717, 2831, 2887, 2888, 2897, 2904, 2951]

进程已结束,退出代码0

原文地址:https://www.cnblogs.com/python-cat/p/11091091.html