psutil

python之psutil学习

在Python中使用psutil这个第三方模块来监控系统状态,通过一两行代码实现系统监控,还可以跨平台使用,支持Linux/UNIX/OSX/Windows等,是系统管理员和运维小伙伴不可或缺的必备模块。

1、获取CPU信息

在监控系统cpu时,多是关心cpu的物理核数,逻辑核数,内核态时间,用户态时间等,服务器这些信息都可以简单的用psutil来获取,使用python交互模式

cpu相关命令

C:\xxx>python
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
>>> psutil.cpu_count() #逻辑核数
8
>>> psutil.cpu_count(logical=False) #物理核数
4
>>> psutil.cpu_times() #cpu时间
scputimes(user=802494.25, system=1400849.7499999981, idle=13850578.921875, interrupt=79538.078125, dpc=9606.78125)
>>> psutil.cpu_percent(interval=1,percpu=True) #cpu使用率
[74.6, 1.6, 14.1, 3.1, 1.6, 80.0, 7.8, 3.1]

2、 获取内存信息

在监控内存时,多是关心内存的 total(内存总数)、used(已使用的内存数)、free(空闲内存数)、buffers(缓存使用数)、cache(缓存使用数)、swap(交换区内存使用数)等;windows下无缓存信息

内存相关命令

>>> import psutil
>>> memory=psutil.virtual_memory()#内存完整信息
>>> memory
svmem(total=8480055296, available=2243362816, percent=73.5, used=6236692480, free=2243362816)
>>> swapMemory=psutil.swap_memory()#内存交换区信息
>>> swapMemory
sswap(total=24586182656, used=18480758784, free=6105423872, percent=75.2, sin=0, sout=0)

3、 获取磁盘信息

在监控磁盘时,多是关心磁盘分区、磁盘的利用率及IO信息

磁盘相关命令

>>> psutil.disk_partitions() #获取电脑磁盘分区信息
[sdiskpart(device='C:\\', mountpoint='C:\\', fstype='NTFS', opts='rw,fixed'), sdiskpart(device='D:\\', mountpoint='D:\\', fstype='NTFS', opts='rw,fixed')]
>>> psutil.disk_usage('c:\\')#获取某个分区使用情况
sdiskusage(total=977734201344, used=226661421056, free=751072780288, percent=23.2)
>>> psutil.disk_io_counters()#获取磁盘读写情况
sdiskio(read_count=17481178, write_count=18511969, read_bytes=778636374528, write_bytes=581470380544, read_time=328642, write_time=88331)
>>> psutil.disk_io_counters(perdisk=True)#获取每个分区磁盘读取信息
{'PhysicalDrive0': sdiskio(read_count=17481218, write_count=18512310, read_bytes=778637824512, write_bytes=581473853952, read_time=328643, write_time=88332)}

5、进程信息

相关命令

>>> psutil.pids() #获取系统进程
[0, 4, 120, 200, 408, 492, 724, 780, 904, 948, 952, 960, 972, 1004...]
>>> chrome=psutil.Process(30368)#获取一个进程信息赋予某个变量
>>> chrome.name()#获取进程的名称
>>> chrome.exe()#获取进程的执行文件位置
'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'
'chrome.exe'
>>> chrome.cwd()#获取进程的工作目录的绝对路径
'C:\\Program Files (x86)\\Google\\Chrome\\Application\\79.0.3945.88'
>>> chrome.cmdline()#进程启动的命令行
['C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe', '--type=watcher', '--main-thread-id=32268', '--on-initialized-event-handle=16', '--parent-handle=524', '/prefetch:6']
>>> chrome.status()#进程的状态
'running'
>>> datetime.datetime.fromtimestamp(chrome.create_time()).strftime('%Y-%m-%d %H:%M:%S')#进程的启动时间
'2019-12-21 09:41:38'
>>> chrome.num_threads()#进程的线程数量
>>> chrome.environ()#进程的环境变量
>>> chrome.connections()#进程相关网络链接

6、其他系统信息

相关命令

>>> psutil.users() #获取计算机用户信息
[suser(name='qiang', terminal=None, host='0.0.0.0', started=1575273424.0, pid=None)]
>>> import datetime
>>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S") #获取计算机开机时间
'2019-12-02 11:48:27'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值