“Python系统信息监控”的版本间的差异

来自CloudWiki
跳转至: 导航搜索
获取进程信息
获取进程信息
第136行: 第136行:
  
 
思考:如何获得每个元素 info的 详细信息 ?
 
思考:如何获得每个元素 info的 详细信息 ?
 +
 +
获取进程的其他信息 还可以通过以下方式:
 +
 +
<nowiki>In [18]: psutil.Process(10848).cpu_times()#获取cpu占用
 +
Out[18]: pcputimes(user=1103.40625, system=462.203125, children_user=0.0, children_system=0.0)
 +
 +
In [19]: psutil.Process(10848).memory_info()#获取内存占用,rss就是实际占用的内存
 +
Out[19]: pmem(rss=403660800, vms=533426176, num_page_faults=22055661, peak_wset=541741056, wset=403660800, peak_paged_pool=1336168, paged_pool=1140336, peak_nonpaged_pool=363148, nonpaged_pool=234576, pagefile=533426176, peak_pagefile=563355648, private=533426176)
 +
 +
In [20]: psutil.Process(10848).num_threads()#获取线程数
 +
Out[20]: 96
 +
 +
In [21]: psutil.Process(10848).memory_percent()#获取内存占比
 +
Out[21]: 2.233989187849646</nowiki>

2020年2月9日 (日) 10:23的版本

在Python中获取系统信息最便捷的模块是psutil(process and system utilities)。通过简短的几行代码就可以获取系统相关信息,而且还是跨平台库。psutil不属于标准库,需要手动安装。安装psutil非常简单,执行以下命令即可。

pip install psutil

获取CPU信息

In [1]: import psutil

In [2]: psutil.cpu_times() #获取cpu占用时间的详细信息
Out[2]: scputimes(user=274971.40625, system=399154.53125, idle=1959806.1875, interrupt=13831.578125, dpc=5731.515625)

In [3]: psutil.cpu_times(percpu=True) #获取每个cpu占用时间的详细信息
Out[3]:
[scputimes(user=69532.3125, system=121357.06250000006, idle=467653.01562499994, interrupt=6815.046875, dpc=3065.390625),
 scputimes(user=55834.59375, system=84942.84375000006, idle=517764.23437499994, interrupt=3982.78125, dpc=1313.03125),
 scputimes(user=81075.28125, system=110418.953125, idle=467047.375, interrupt=1964.03125, dpc=1013.953125),
 scputimes(user=68573.21875, system=82476.46875, idle=507491.953125, interrupt=1070.84375, dpc=339.671875)]

In [4]: psutil.cpu_count()   #cpu逻辑数量
Out[4]: 4
In [5]: psutil.cpu_count(logical=False)
Out[5]: 2

In [6]: psutil.cpu_percent()#CPU占用率
Out[6]: 40.7

In [7]: psutil.cpu_percent(percpu=True) #每个CPU的占用率
Out[7]: [39.4, 32.8, 46.0, 43.3]

监控内存信息

In [8]: psutil.virtual_memory()#查看物理内存
Out[8]: svmem(total=17057337344, available=4142526464, percent=75.7, used=12914810880, free=4142526464)

In [9]: psutil.swap_memory()#查看虚拟内存
Out[9]: sswap(total=27257884672, used=12678332416, free=14579552256, percent=46.5, sin=0, sout=0)



思考:内存的利用率应该怎样计算?

监控磁盘信息

In [9]: psutil.disk_partitions()
Out[9]:
[sdiskpart(device='C:\\', mountpoint='C:\\', fstype='NTFS', opts='rw,fixed'),
 sdiskpart(device='D:\\', mountpoint='D:\\', fstype='NTFS', opts='rw,fixed'),
 sdiskpart(device='F:\\', mountpoint='F:\\', fstype='', opts='cdrom')]</nowiki>


监控网络信息

In [10]:  psutil.net_io_counters()#获取网络读写字节的个数
Out[10]: snetio(bytes_sent=8445215479, bytes_recv=7761730094, packets_sent=5203357, packets_recv=10378093, errin=0, errout=0, dropin=0, dropout=0)

 

思考:从中能获得什么信息 ?

In [11]:  psutil.net_if_addrs() #获取网络接口信息
Out[11]:
{'VPN - Driver': [snicaddr(family=<AddressFamily.AF_LINK: -1>, address='00-AC-AC-2C-4C-8C', netmask=None, broadcast=None, ptp=None),
  snicaddr(family=<AddressFamily.AF_INET: 2>, address='169.254.252.233', netmask='255.255.0.0', broadcast=None, ptp=None),
  snicaddr(family=<AddressFamily.AF_INET6: 23>, address='fe80::68d0:7a05:cf58:fce9', netmask=None, broadcast=None, ptp=None)],
 '以太网': [snicaddr(family=<AddressFamily.AF_LINK: -1>, address='50-7B-9D-99-AE-C6', netmask=None, broadcast=None, ptp=None),
  snicaddr(family=<AddressFamily.AF_INET: 2>, address='169.254.247.20', netmask='255.255.0.0', broadcast=None, ptp=None),
  snicaddr(family=<AddressFamily.AF_INET6: 23>, address='fe80::2c1b:6dc0:feac:f714', netmask=None, broadcast=None, ptp=None)],
 '本地连接* 3': [snicaddr(family=<AddressFamily.AF_LINK: -1>, address='08-D4-0C-40-C3-57', netmask=None, broadcast=None, ptp=None),
  snicaddr(family=<AddressFamily.AF_INET: 2>, address='169.254.163.245', netmask='255.255.0.0', broadcast=None, ptp=None),
  snicaddr(family=<AddressFamily.AF_INET6: 23>, address='fe80::d16d:3604:8b75:a3f5', netmask=None, broadcast=None, ptp=None)],
 '本地连接* 5': [snicaddr(family=<AddressFamily.AF_LINK: -1>, address='0A-D4-0C-40-C3-56', netmask=None, broadcast=None, ptp=None),
  snicaddr(family=<AddressFamily.AF_INET: 2>, address='169.254.130.96', netmask='255.255.0.0', broadcast=None, ptp=None),
  snicaddr(family=<AddressFamily.AF_INET6: 23>, address='fe80::c9ad:1742:ec84:8260', netmask=None, broadcast=None, ptp=None)],
 'VMware Network Adapter VMnet1': [snicaddr(family=<AddressFamily.AF_LINK: -1>, address='00-50-56-C0-00-01', netmask=None, broadcast=None, ptp=None),
  snicaddr(family=<AddressFamily.AF_INET: 2>, address='169.254.189.71', netmask='255.255.0.0', broadcast=None, ptp=None),
  snicaddr(family=<AddressFamily.AF_INET6: 23>, address='fe80::4d68:1bd9:83f3:bd47', netmask=None, broadcast=None, ptp=None)],
 'VMware Network Adapter VMnet8': [snicaddr(family=<AddressFamily.AF_LINK: -1>, address='00-50-56-C0-00-08', netmask=None, broadcast=None, ptp=None),
  snicaddr(family=<AddressFamily.AF_INET: 2>, address='10.0.0.1', netmask='255.255.255.0', broadcast=None, ptp=None),
  snicaddr(family=<AddressFamily.AF_INET6: 23>, address='fe80::31a9:a276:a6ab:c4d1', netmask=None, broadcast=None, ptp=None)],
 '以太网 2': [snicaddr(family=<AddressFamily.AF_LINK: -1>, address='00-FF-D3-C0-D1-56', netmask=None, broadcast=None, ptp=None),
  snicaddr(family=<AddressFamily.AF_INET: 2>, address='169.254.110.136', netmask='255.255.0.0', broadcast=None, ptp=None),
  snicaddr(family=<AddressFamily.AF_INET: 2>, address='192.168.137.1', netmask='255.255.255.0', broadcast=None, ptp=None),
  snicaddr(family=<AddressFamily.AF_INET6: 23>, address='fe80::e540:e9f9:4a4b:6e88', netmask=None, broadcast=None, ptp=None)],
 'WLAN': [snicaddr(family=<AddressFamily.AF_LINK: -1>, address='08-D4-0C-40-C3-56', netmask=None, broadcast=None, ptp=None),
  snicaddr(family=<AddressFamily.AF_INET: 2>, address='192.168.1.102', netmask='255.255.255.0', broadcast=None, ptp=None),
  snicaddr(family=<AddressFamily.AF_INET6: 23>, address='fe80::10d4:4318:dfb8:9360', netmask=None, broadcast=None, ptp=None)],
 'Loopback Pseudo-Interface 1': [snicaddr(family=<AddressFamily.AF_INET: 2>, address='127.0.0.1', netmask='255.0.0.0', broadcast=None, ptp=None),
  snicaddr(family=<AddressFamily.AF_INET6: 23>, address='::1', netmask=None, broadcast=None, ptp=None)]}

获取网络接口状态:

In [13]: psutil.net_if_stats()
Out[13]:
{'以太网': snicstats(isup=False, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=0, mtu=1500),
 'VMware Network Adapter VMnet8': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=100, mtu=1500),
 '以太网 2': snicstats(isup=False, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=100, mtu=1500),
 'VPN - Driver': snicstats(isup=False, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=100, mtu=1500),
 'Loopback Pseudo-Interface 1': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=1073, mtu=1500),
 'WLAN': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=135, mtu=1500),
 '本地连接* 5': snicstats(isup=False, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=0, mtu=1500)}

获取当前网络连接信息:

In [14]: psutil.net_connections()
Out[14]:
[sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='127.0.0.1', port=10000), raddr=(), status='LISTEN', pid=9532),
 sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='192.168.1.104', port=58020), raddr=addr(ip='206.189.32.61', port=443), status='ESTABLISHED', pid=10916),
 sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='192.168.1.104', port=50557), raddr=addr(ip='40.90.189.152', port=443), status='ESTABLISHED', pid=4676),
 sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='127.0.0.1', port=52856), raddr=addr(ip='127.0.0.1', port=52857), status='ESTABLISHED', pid=9228),
 sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type=<SocketKind.SOCK_DGRAM: 2>, laddr=addr(ip='::', port=5353), raddr=(), status='NONE', pid=2324),
。。。

获取进程信息

#获取所有进程的pid :
In [13]: for pid in psutil.pids():
    ...:     print(pid,end=',')
    ...:
0,4,76,96,472,544,580,584,604,620,636,748,756,848,892,900,940,1028,1096,1152,1220,1300,1388,1480,1488,1504,1516,1548,1552,1612,1616,1644,1712,1744,1800,1916,1928,1944,1984,2028,2060,2164,2280,2288,2296,2340,2348,2404,2408,2444,2456,2476,2484,2524,2652,2664,2672,2760,2928,2944,2960,3244,3348,3372,3388,3416,3520,3544,3560,3572,3596,3608,3644,3676,3688,3700,3704,3724,3732,3764,3784,3796,3808,3864,3876,3896,3904,3912,3920,3928,3936,3944,3976,3992,4012,4028,4040,4052,4064,4076,4136,4172,4204,4244,4260,4372,4388,4408,4416,4424,4468,4492,4536,4620,4640,4652,4800,5112,5228,5236,5244,5264,5328,5412,5592,5712,6096,6512,6564,6572,6920,6932,7176,7276,7376,7500,7520,7620,7696,7968,8080,8184,8200,8400,8812,8840,8864,8888,9228,9584,9608,9656,9688,9752,9792,9832,10020,10184,10260,10428,10500,10612,10780,10796,10812,11068,11108,11152,11324,11344,11444,11880,12056,12116,12260,12312,12380,13380,13556,13892,14004,14544,15552,15564,16204,16388,17296,17944,18276,18444,19092,19172,19204,20824,20964,22556,22968,25300,26272,26936,27816,27932,28688,28952,30060,30180,31080,31104,34032,34048,35136,35744,37200,38292,40108,40460,40968,41484,43596,44236,44308,45552,46420,47496,47932,48192,48892,49004,49740,50008,50224,50520,50564,50860,51448,51728,54740,54872,55464,56368,59672,59896,60344,61744,62640,64024,64376,64580,64616,64640,65068,65500,66092,66268,67896,68908,69388,69508,70016,70284,70828,71500,72948,73120,73332,75048,75356,75884,75952,76284,77516,77572,78160,79152,79232,79836,79908,80592,80652,80708,81884,84016,84408,84468,84612,85244,85260,85672,85780,86176,86488,86712,86732,87804,87856,89396,

#查找qq程序的有关信息:

In [14]: for proc in psutil.process_iter(attrs=['pid','name','username']):
    ...:     if proc.info['name'].startswith('QQ'):
    ...:         print(proc.info)
    ...:
{'pid': 3976, 'name': 'QQProtect.exe', 'username': None}
{'pid': 11444, 'name': 'QQLiveService.exe', 'username': 'DESKTOP-LN2H42N\\thinkpad'}
{'pid': 47496, 'name': 'QQ.exe', 'username': 'DESKTOP-LN2H42N\\thinkpad'}
{'pid': 69388, 'name': 'QQExternal.exe', 'username': 'DESKTOP-LN2H42N\\thinkpad'}

前面使用psutil.process_iter获取了进程相关的信息,返回结果是一个可迭代对象,每个元素的info是一个字典,通过字典可以获取我们关心的信息。

思考:如何获得每个元素 info的 详细信息 ?

获取进程的其他信息 还可以通过以下方式:

In [18]: psutil.Process(10848).cpu_times()#获取cpu占用
Out[18]: pcputimes(user=1103.40625, system=462.203125, children_user=0.0, children_system=0.0)

In [19]: psutil.Process(10848).memory_info()#获取内存占用,rss就是实际占用的内存
Out[19]: pmem(rss=403660800, vms=533426176, num_page_faults=22055661, peak_wset=541741056, wset=403660800, peak_paged_pool=1336168, paged_pool=1140336, peak_nonpaged_pool=363148, nonpaged_pool=234576, pagefile=533426176, peak_pagefile=563355648, private=533426176)

In [20]: psutil.Process(10848).num_threads()#获取线程数
Out[20]: 96

In [21]: psutil.Process(10848).memory_percent()#获取内存占比
Out[21]: 2.233989187849646