python之psutil

psutil = process and system utilities,

psutil是个跨平台库,能够轻松实现获取系统运行的进程和系统利用率,包括CPU、内存、磁盘、网络等信息。

Linux系统下的安装

pip install  psutil

现在开始看看它的使用

一 cpu

#查看逻辑cpu的个数
>>> psutil.cpu_count()
2
#查看物理cpu的个数
>>> psutil.cpu_count(logical=False)
2
>>> psutil.cpu_times_percent() #cpu的总使用情况
scputimes(user=0.0, nice=0.0, system=0.1, idle=99.9, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
>>> psutil.cpu_times_percent(percpu=True) #每个cpu的使用情况
[scputimes(user=0.0, nice=0.0, system=0.1, idle=99.9, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0), 
scputimes(user=0.0, nice=0.0, system=0.1, idle=99.9, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)]
>>> psutil.cpu_percent() #cpu的使用率
0.1
#要查看cpu的负载呢,我们还是使用其他的命令吧

  >>> import os
  >>> os.getloadavg()
  (0.01, 0.06, 0.06)

 二 内存

#查看系统缓存的信息
>>> psutil.swap_memory()
sswap(total=536866816, used=0, free=536866816, percent=0.0, sin=0, sout=0)
>>> psutil.swap_memory().total  #单位是字节
536866816
>>> psutil.swap_memory().total/1024 #swap总大小,以kb单位表示
524284
>>> psutil.swap_memory().free/1024  #空闲swap大小,以kb单位表示
524284

#统计内存使用情况
##############总内存 ,使用的,空闲  ,使用百分比,buffers ,cached  单位是字节
>>> for i in ['total','used','free','percent','buffers','cached']:
...     ret=getattr(psutil.virtual_memory(),i)
...     print(ret)
... 
1514450944   
227557376
852000768
25.5
3215360
431677440

 三 磁盘

#获取磁盘信息
>>> psutil.disk_partitions()
[sdiskpart(device='/dev/sda3', mountpoint='/', fstype='xfs', opts='rw,seclabel,relatime,attr2,inode64,noquota'),
 sdiskpart(device='/dev/sda1', mountpoint='/boot', fstype='xfs', opts='rw,seclabel,relatime,attr2,inode64,noquota')]
 #获取挂载点的分区信息,通过device去的话显示有问题
 >>> psutil.disk_usage("/boot")
sdiskusage(total=206213120, used=117985280, free=88227840, percent=57.2)
>>> psutil.disk_usage("/boot").percent
57.2

四 网卡

#获取网卡ens33的IP地址
>>> psutil.net_if_addrs()['ens33'][0].address
'192.168.137.16'
#子网掩码
>>> psutil.net_if_addrs()['ens33'][0].netmask
'255.255.255.0'
#查看网卡是否开启
>>> psutil.net_if_stats()['ens33'].isup
True
#查看网卡的速率,命令有ifconfig,ethtool
>>> psutil.net_if_stats()['ens33'].speed
1000  #单位 Mb/s

 五 网络

#psutil.net_connections 查看网络的连接情况
>>> for i in psutil.net_connections():
...     if i.raddr:  #判断外部地址情况不为空的情况,避免raddr=()产生错误
...             print(i.laddr.ip,i.laddr.port,i.raddr.ip,i.raddr.port,i.status,i.pid)
... 
# 本地地址       ,端口,外部地址     ,外部端口,   连接情况 ,    pid
('192.168.137.16', 22, '192.168.137.1', 52783, 'ESTABLISHED', 1327)
('192.168.137.16', 22, '192.168.137.1', 52083, 'ESTABLISHED', 1189)

六进程

#进程的情况
>>> p=psutil.Process(1094) #1094是nginx的master进程
>>> p.num_threads()  #打开的线程数,由于nginx是一个主进程和多个工作进程,因此都为1
1
>>> p.cwd()          #进程的工作目录路径
'/'
>>> p.cmdline()      #nginx的命令进程信息
['nginx:', 'master', 'process', '/usr/sbin/nginx']
>>> p.exe()           #执行的命令
'/usr/sbin/nginx' 
>>> p.is_running()    #是否存活
True
>>> p.name()           #进程名称
'nginx'
>>> p.nice()           #进程的nice值
0
>>> p.status()         #状态
'sleeping' 
>>> p.threads()        #它的线程情况
[pthread(id=1094, user_time=0.0, system_time=0.0)]
>>> p.ppid()          #它的父进程
1
>>> p.username()      #它的执行用户
'root'
>>> p.memory_percent() #内存利用率
0.14280343701908643
>>> p.cpu_percent()    #cpu利用率
0.0
>>> psutil.pid_exists(1111) #查看进程是否存在
False
>>> psutil.pid_exists(1094)
True

猜你喜欢

转载自www.cnblogs.com/mmyy-blog/p/10791579.html