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
python之psutil的更多相关文章
- CentOS7--64安装python的psutil模块
1.以root身份登陆CentOS依次 执行以下命令: wget https://pypi.python.org/packages/source/p/psutil/psutil-2.1.3.tar.g ...
- Python模块 (psutil)
psutil psutil是Python中一个系统信息检索模块,可以获取(系统.CPU.内存.网络.磁盘)等信息,可以应用于系统的监控.健康状态检查,等同于shell中的ps.free.top.df功 ...
- python使用psutil获取服务器信息
>>> import psutil 获取cpu信息>>> psutil.cpu_times()scputimes(user=128258.38, nice=12.2 ...
- Python 中psutil 模块的安装
第一步下载psutil 的安装包 网址:https://pypi.python.org 第二步解压 .tar.gz cd psutil- 第三步安装: python setup.py build py ...
- [转载] python利用psutil遍历进程名字和exe所在目录
本文转载自: http://www.duanzhihe.com/1594.html http://www.jianshu.com/p/64e265f663f6 import psutil,os,tim ...
- Linux-Centos7----安装Python的psutil模块插件
# wget https://pypi.python.org/packages/source/p/psutil/psutil-2.1.3.tar.gz # tar zxvf psutil-2.1.3. ...
- python之psutil模块(获取系统性能数据)
psutil模块 1.介绍 psutil是一个跨平台库(http://code.google.com/p/psutil/),能够轻松实现获取系统运行的进程和系统利用率(包括CPU.内存.磁盘.网络等) ...
- python之psutil模块详解(Linux)--小白博客
Python-psutil模块 windows系统监控实例,查询 https://www.cnblogs.com/zhou2019/p/10567282.html 1.简单介绍 psutil是一个跨平 ...
- python模块psutil的使用
介绍 psutil是一个跨平台库(http://code.google.com/p/psutil/),能够轻松实现获取系统运行的进程和系统利用率(包括CPU.内存.磁盘.网络等)信息.它主要应用于系统 ...
- python之psutil模块(获取系统性能信息(CPU,内存,磁盘,网络)
一.psutil模块 1. psutil是一个跨平台库(http://code.google.com/p/psutil/),能够轻松实现获取系统运行的进程和系统利用率(包括CPU.内存.磁盘.网络等) ...
随机推荐
- 软件测试_MYSQL
# MYSQL## 基础知识点### 进入数据库:在偏好设置中打开 — 打开终端 /usr/local/mysql/bin/mysql -u root -p### 可以把完整的命令分成几行打,完后用分 ...
- 使用delimiter //,解决mysql end报错问题
这是我的初始报错的代码: )) begin select sc.* from sc where sno= student_no end; 会报这个错误,“Error Code: 1064. You h ...
- springmvc 在非controller下使用@autowired
在SpringMVC框架中,我们经常要使用@Autowired注解注入Service或者Mapper接口,我们也知道,在controller层中注入service接口,在service层中注入其它的s ...
- 安装grub到U盘分区,实现多系统引导
目录 1.分区工具及分区类型 1.1 显示分区表和分区信息 1.1.1 fdisk -l 1.1.2 gdisk -l 1.1.3 parted -l 1.2 常见分区类型 1.3 分区样例 1.3. ...
- 建荣AX3298作为航拍启动流程
启动函数: SDK-3298\demo\helloftv\main.c 板级配置文件:SDK-3298\board\AX3292\mvl8801\board_config.h 1,定义了 debug串 ...
- 《用Python玩转数据》项目—线性回归分析入门之波士顿房价预测(二)
接上一部分,此篇将用tensorflow建立神经网络,对波士顿房价数据进行简单建模预测. 二.使用tensorflow拟合boston房价datasets 1.数据处理依然利用sklearn来分训练集 ...
- java虚拟机 之 垃圾回收机制
一.如何判断对象已死 垃圾回收器并不是java独有的,垃圾回收器的作用就是回收对象释放内存空间,那么如何判断哪些对象应该被回收呢? 在Java语言中是采用GC Roots来解决这个问题.如果一个对象和 ...
- Android游戏引擎总汇 原文出处:http://software.intel.com/en-us/blogs/2012/03/13/game-engines-for-android?page=1
随着Android系统的使用越来越广泛,了解一下Android平台下的游戏引擎就非常有必要.而同时因为基于Intel x86的移动设备越来越多,我也非常关注支持x86的移动游戏引擎.然而就目前为止游戏 ...
- H5外包团队 android视频压缩,使用ffmpeg方案
android视频压缩,使用ffmpeg方案,集成fdk-aac与264编码,适用于32位系统与64位系统,支持ARM 32/64 cpu与x86 32/64 cpu,mips 32/64 cpu,即 ...
- os常用模块,json,pickle,shelve模块,正则表达式(实现运算符分离),logging模块,配置模块,路径叠加,哈希算法
一.os常用模块 显示当前工作目录 print(os.getcwd()) 返回上一层目录 os.chdir("..") 创建文件包 os.makedirs('python2/bin ...