psutil获取系统信息

2021-12-31 15:30:47 浏览数 (1)

CPU

  1. cpu_times CPU运行时间
代码语言:txt复制
psutil.cpu_times()
代码语言:javascript复制
   scputimes(user=125.65, nice=20.8, system=243.42, idle=5515413.34, iowait=15.87, irq=0.0, softirq=45.79, steal=0.0, guest=0.0, guest_nice=0.0)

获取每个逻辑CPU的信息

代码语言:javascript复制
psutil.cpu_times(percpu=True)
代码语言:javascript复制
   scputimes(user=18.38, nice=1.51, system=95.18, idle=689387.87, iowait=1.96, irq=0.0, softirq=8.87, steal=0.0, guest=0.0, guest_nice=0.0), scputimes(user=9.84, nice=3.23, system=7.0, idle=689816.01, iowait=1.94, irq=0.0, softirq=1.85, steal=0.0, guest=0.0, guest_nice=0.0), scputimes(user=8.84, nice=1.94, system=6.57, idle=689669.6, iowait=1.7, irq=0.0, softirq=3.93, steal=0.0, guest=0.0, guest_nice=0.0), scputimes(user=4.52, nice=0.0, system=19.14, idle=689599.71, iowait=2.48, irq=0.0, softirq=4.34, steal=0.0, guest=0.0, guest_nice=0.0), scputimes(user=59.46, nice=2.18, system=60.39, idle=689222.57, iowait=2.21, irq=0.0, softirq=4.96, steal=0.0, guest=0.0, guest_nice=0.0), scputimes(user=7.16, nice=3.16, system=7.64, idle=689752.36, iowait=1.38, irq=0.0, softirq=1.74, steal=0.0, guest=0.0, guest_nice=0.0), scputimes(user=7.9, nice=5.46, system=37.03, idle=689078.94, iowait=2.8, irq=0.0, softirq=8.95, steal=0.0, guest=0.0, guest_nice=0.0), scputimes(user=9.52, nice=3.29, system=10.46, idle=689145.78, iowait=1.37, irq=0.0, softirq=11.13, steal=0.0, guest=0.0, guest_nice=0.0)

CPU运行状态

解释

user

进程执行用户代码耗费的时间

nice

在用县级高的用户级别执行时CPU占用率的百分比

system

内核执行系统调度所使用的时间

idle

CPU空闲且没有未完成磁盘io请求的时间的百分比

iowait

CPU等待io输入输出时间

irq

CPU用户维护硬件中断所花费的时间百分比

softirq

CPU用于维护软件中断所花费的时间百分比

steal

虚拟化环境中运行其他操作系统所花费的时间百分比

guest

CPU用于运行虚拟处理器的时间百分比

dpc

服务延迟过程调用所花费的时间

interrupt

服务硬件中断花费的时间

  1. cpu_percent CPU利用率
代码语言:javascript复制
psutil.cpu_percent()

10.1

代码语言:javascript复制
psutil.cpu_percent(interval=1)

8.7

代码语言:javascript复制
psutil.cpu_percent(interval=1,percpu=True)
代码语言:javascript复制
   16.0, 13.1, 3.1, 6.9, 19.2, 4.1, 11.1, 10.1, 4.1, 4.1, 22.0, 17.2, 5.1, 39.0, 5.1, 6.0, 4.0, 3.1, 4.0, 5.0, 5.1, 15.0, 7.1, 20.2
  1. cpu_times_percent 特定CPU状态的利用率
代码语言:javascript复制
psutil.cpu_times_percent(interval=1 )
代码语言:javascript复制
   scputimes(user=10.6, nice=0.0, system=2.0, idle=87.4, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
  1. cpu_count cpu的数量
代码语言:javascript复制
psutil.cpu_count()		# 逻辑cpu个数

24

代码语言:javascript复制
psutil.cpu_count(logical=False)	# 物理cpu个数

12

代码语言:javascript复制
len(psutil.Process().cpu_affinity())	# 当前可用cpu个数

24

  1. cpu_stats cpu统计信息
代码语言:javascript复制
psutil.cpu_stats()
代码语言:javascript复制
   scpustats(ctx_switches=134328863006, interrupts=64447553228, soft_interrupts=37972326039, syscalls=0)

CPU信息

解释

ctx_switchese

启动后的上下文切换次数

interrupts

自引导以来的中断数

soft_interrupts

自引导以来的软件中断次数

syscalls

自引导以来的系统调用次数

  1. cpu_freq CPU频率

min/max如果无法确认贼设置为0

代码语言:javascript复制
psutil.cpu_freq()
代码语言:javascript复制
   scpufreq(current=2776.311875000001, min=2200.0, max=3800.0)

内存
  1. virtual_memory物理内存
代码语言:javascript复制
psutil.virtual_memory()
代码语言:javascript复制
   svmem(total=135165038592, available=117457821696, percent=13.1, used=16417964032, free=56929009664, active=59796267008, inactive=6194962432, buffers=1170616320, cached=60647448576, shared=37347328, slab=8931905536)

内存信息

解释

total

总物理内存

available

在没有系统进入swap下立即提供的内存

percent

使用内存占比

used

使用的物理内存

free

没有使用的物理内存

active

当前使用或最近使用的物理内存

inactive

标记未使用的内存

buffers

buffers使用的缓存

cached

cached使用的缓存

shared

显示被共享使用的物理内存

slab

内核数据结构缓存

  1. swap_memory() 交换内存使用情况
代码语言:javascript复制
psutil.swap_memory()
代码语言:javascript复制
   sswap(total=4294963200, used=0, free=4294963200, percent=0.0, sin=0, sout=0)

磁盘
  1. disk_partitions 获取所有磁盘信息
代码语言:javascript复制
psutil.disk_partitions()
代码语言:javascript复制
   sdiskpart(device='/dev/sda2', mountpoint='/', fstype='ext4', opts='rw,relatime,data=ordered'), sdiskpart(device='/dev/sda1', mountpoint='/boot/efi', fstype='vfat', opts='rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro'), sdiskpart(device='/dev/loop2', mountpoint='/snap/core/11743', fstype='squashfs', opts='ro,nodev,relatime'), sdiskpart(device='/dev/loop1', mountpoint='/snap/core/11798', fstype='squashfs', opts='ro,nodev,relatime')
  1. disk_usage 获取单个磁盘信息
代码语言:javascript复制
psutil.disk_usage('/dev/sda')
代码语言:javascript复制
   sdiskusage(total=67510288384, used=0, free=67510288384, percent=0.0)
   disk_io_counters 获取io信息
  1. disk_io_counters 获取io信息
代码语言:javascript复制
psutil.disk_io_counters()
代码语言:javascript复制
   sdiskio(read_count=30304, 	# 读取次数
代码语言:txt复制
       write_count=46146, 	# 写入次数
代码语言:txt复制
       read_bytes=762495488, # 读取字节数
代码语言:txt复制
       write_bytes=1490812928, 	# 写入字节数
代码语言:txt复制
       read_time=21036, 					# 读取时间
代码语言:txt复制
       write_time=56484, 				# 写入时间
代码语言:txt复制
       read_merged_count=5388, 	# 合并读取的次数
代码语言:txt复制
       write_merged_count=35669, # 合并写入的次数
代码语言:txt复制
       busy_time=17936						# 实际做io操作的时间
代码语言:txt复制
      )

网络
  1. net_connections 获取网络套接字
代码语言:javascript复制
psutil.net_connections()
代码语言:javascript复制
   sconn(fd=4, family=<AddressFamily.AF_INET6: 10>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='::', port=22), raddr=(), status='LISTEN', pid=1240), sconn(fd=12, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_DGRAM: 2>, laddr=addr(ip='127.0.0.53', port=53), raddr=(), status='NONE', pid=959), sconn(fd=3, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='0.0.0.0', port=22), raddr=(), status='LISTEN', pid=1240), sconn(fd=6, family=<AddressFamily.AF_INET6: 10>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='::', port=6379), raddr=(), status='LISTEN', pid=1264), sconn(fd=7, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='0.0.0.0', port=6379), raddr=(), status='LISTEN', pid=1264), sconn(fd=3, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='10.122.1.77', port=22), raddr=addr(ip='10.122.2.5', port=26119), status='ESTABLISHED', pid=27261), sconn(fd=13, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='127.0.0.53', port=53), raddr=(), status='LISTEN', pid=959), sconn(fd=36, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='0.0.0.0', port=3306), raddr=(), status='LISTEN', pid=1472)
  1. net_if_addrs 获取网卡的信息
代码语言:javascript复制
psutil.net_if_addrs()
代码语言:javascript复制
   {'lo': 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: 10>, address='::1', netmask='ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', broadcast=None, ptp=None), snicaddr(family=<AddressFamily.AF_PACKET: 17>, address='00:00:00:00:00:00', netmask=None, broadcast=None, ptp=None), 'enp4s0': snicaddr(family=<AddressFamily.AF_INET: 2>, address='xxxxxx', netmask='xxxxx', broadcast='10.120.0.255', ptp=None), snicaddr(family=<AddressFamily.AF_INET6: 10>, address='fe80::aa5e:45ff:fee1:865c%enp4s0', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None), snicaddr(family=<AddressFamily.AF_PACKET: 17>, address='a8:5e:45:e1:86:5c', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)
  1. net_if_stats 获取网络接口的状态
代码语言:javascript复制
psutil.net_if_stats()
代码语言:javascript复制
   {'lo': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=65536), 'ens33': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=1000, mtu=1500)}
  1. net_io_counter 获取网口的流量
代码语言:javascript复制
psutil.net_io_counters()

snetio(bytes_sent=32690381, # 发送字节

代码语言:txt复制
      bytes_recv=34820697, 	# 接收字节
代码语言:txt复制
      packets_sent=39503, 		# 发送包数
代码语言:txt复制
      packets_recv=74172, 		# 接收包数
代码语言:txt复制
      errin=0, 							# 接收错误数
代码语言:txt复制
      errout=0, 							# 发送错误数
代码语言:txt复制
      dropin=0, 							# 丢弃传入数据包数量
代码语言:txt复制
      dropout=0							# 丢弃传入数据包数量
代码语言:txt复制
     )

其他
  1. 开机时间
代码语言:javascript复制
   import datetime, psutil
   datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
   '2021-12-23 14:25:56'
  1. 获取当前用户
代码语言:javascript复制
psutil.users()
代码语言:javascript复制
   suser(name='xxx', terminal='tty1', host='', started=1640240768.0, pid=2254), suser(name='ipfsmain', terminal='pts/0', host='10.122.2.5', started=1640930560.0, pid=27171)
  1. 获取pid
代码语言:javascript复制
psutil.pids()
代码语言:javascript复制
   1, 2, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 20, 21, 22, 24, 25, 26, 27, 28, 30, 31, 32, 33, 34, 36, 37, 38, 39, 40, 42, 43, 44, 45, 46, 48, 49, 50, 51, 52, 54, 55, 56, 57, 58, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 76, 77, 78, 122, 125, 128, 129, 130, 131, 132, 138, 147, 164, 239, 244, 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, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 364, 365, 370, 371, 375, 377, 379, 450, 482, 501, 502, 531, 555, 582, 585, 592, 593, 594, 595, 596, 597, 607, 788, 789, 790, 791, 792, 793, 794, 795, 796, 797, 812, 850, 869, 870, 941, 959, 1049, 1055, 1062, 1068, 1075, 1094, 1191, 1225, 1229, 1240, 1264, 1299, 1303, 1369, 1472, 2226, 2232, 2254, 3111, 3247, 6954, 10977, 12254, 14051, 14144, 15117, 24202, 24203, 24204, 24205, 24206, 24207, 24208, 24209, 24210, 24211, 26176, 26565, 26669, 26836, 26957, 27171, 27261, 27264, 27669, 27670, 27671, 27745, 27759, 27760, 27762, 27764, 27769
  1. 获取进程信息
代码语言:javascript复制
   for x in psutil.process_iter():
   	print(x)
   psutil.Process(pid=1, name='systemd', status='sleeping', started='2021-12-23 14:25:56')
   psutil.Process(pid=2, name='kthreadd', status='sleeping', started='2021-12-23 14:25:56')
   psutil.Process(pid=4, name='kworker/0:0H', status='idle', started='2021-12-23 14:25:56')
   psutil.Process(pid=6, name='mm_percpu_wq', status='idle', started='2021-12-23 14:25:56')
   psutil.Process(pid=7, name='ksoftirqd/0', status='sleeping', started='2021-12-23 14:25:56')
   psutil.Process(pid=8, name='rcu_sched', status='idle', started='2021-12-23 14:25:56')
   psutil.Process(pid=9, name='rcu_bh', status='idle', started='2021-12-23 14:25:56')
   psutil.Process(pid=10, name='migration/0', status='sleeping', started='2021-12-23 14:25:56')
   psutil.Process(pid=11, name='watchdog/0', status='sleeping', started='2021-12-23 14:25:56')
   psutil.Process(pid=12, name='cpuhp/0', status='sleeping', started='2021-12-23 14:25:56')
   psutil.Process(pid=13, name='cpuhp/1', status='sleeping', started='2021-12-23 14:25:56')
   psutil.Process(pid=14, name='watchdog/1', status='sleeping', started='2021-12-23 14:25:56')
   psutil.Process(pid=15, name='migration/1', status='sleeping', started='2021-12-23 14:25:56')
   psutil.Process(pid=16, name='ksoftirqd/1', status='sleeping', started='2021-12-23 14:25:56')

0 人点赞