# 这是学习廖雪峰老师 python 教程的学习笔记
在 Linux 下, 有许多系统命令可以让我们时刻监控系统运行的状态, 如 ps,top,free 等等. 要获取这些系统信息, Python 可以通过 subprocess 模块调用并获取结果. 但最好的办法是用 psutil 这个第三方模块
psutil = process and system utilities, 它不仅可以通过一两行代码实现系统监控, 还可以跨平台使用, 支持 Linux/UNIX/OSX/Windows 等, 是系统管理员和运维小伙伴不可或缺的必备模块
1, 获取 CPU 信息
1) 获取 CPU 的信息
- >>> import psutil
- >>> psutil.cpu_count() # CPU 逻辑数量
- 4
- >>> psutil.cpu_count(logical=False) # CPU 物理核心
- 2 # 2 说明是双核超线程, 4 则是 4 核非超线程
2) 统计 CPU 的用户 / 系统 / 空闲时间
- >>> psutil.cpu_times()
- scputimes(user=10963.31, nice=0.0, system=5138.67, idle=356102.45)
实现类似 top 命令的 CPU 使用率
- >>> for x in range(10): #显示 10 次
- ... psutil.cpu_percent(interval=1, percpu=True) # 显示间隔为 1 秒
2, 获取内存信息
1) 使用 psutil 获取虚拟内存和交换内存信息
>>> psutil.virtual_memory()
svmem(total=8589934592, available=2866520064, percent=66.6, used=7201386496, free=216178688, active=3342192640, inactive=2650341376, wired=1208852480)
>>> psutil.swap_memory()
sswap(total=1073741824, used=150732800, free=923009024, percent=14.0, sin=10705981440, sout=40353792)
3, 获取磁盘信息
1) 通过 psutil 获取磁盘分区, 磁盘使用率和磁盘 IO 信息
>>> psutil.disk_partitions() # 磁盘分区信息
[sdiskpart(device='/dev/disk1', mountpoint='/', fstype='hfs', opts='rw,local,rootfs,dovolfs,journaled,multilabel')]
- >>> psutil.disk_usage('/') # 磁盘使用情况, 这里的 / 是挂载点
- sdiskusage(total=998982549504, used=390880133120, free=607840272384, percent=39.1)
- >>> psutil.disk_io_counters() # 磁盘 IO
- sdiskio(read_count=988513, write_count=274457, read_bytes=14856830464, write_bytes=17509420032, read_time=2228966, write_time=1618405)
4, 获取网络信息
1)psutil 可以获取网络接口和网络连接信息
>>> psutil.net_io_counters() # 获取网络读写字节 / 包的个数
snetio(bytes_sent=3885744870, bytes_recv=10357676702, packets_sent=10613069, packets_recv=10423357, errin=0, errout=0, dropin=0, dropout=0)
- >>> psutil.net_if_addrs() # 获取网络接口信息
- >>> psutil.net_if_stats() # 获取网络接口状态
- >>> psutil.net_connections() # 获取当前网络连接信息
5, 获取进程信息
1)psutil 可以获取到所有进程的详细信息
- >>> psutil.pids() # 所有进程 ID
- [3865, 3864, 3863, 3856, 3855, 3853, 3776, ..., 45, 44, 1, 0]
- >>> p = psutil.Process(3776) # 获取指定进程 ID=3776, 其实就是当前 Python 交互环境
- >>> p.name() # 进程名称
- 'python3.6'
- >>> p.exe() # 进程 exe 路径
- '/Users/michael/anaconda3/bin/python3.6'
- >>> p.cwd() # 进程工作目录
- '/Users/michael'
- >>> p.cmdline() # 进程启动的命令行
- ['python3']
- >>> p.ppid() # 父进程 ID
- 3765
- >>> p.parent() # 父进程
- <psutil.Process(pid=3765, name='bash') at 4503144040>
- >>> p.children() # 子进程列表
- []
- >>> p.status() # 进程状态
- 'running'
- >>> p.username() # 进程用户名
- 'michael'
- >>> p.create_time() # 进程创建时间
- 1511052731.120333
- >>> p.terminal() # 进程终端
- '/dev/ttys002'
- >>> p.cpu_times() # 进程使用的 CPU 时间
- pcputimes(user=0.081150144, system=0.053269812, children_user=0.0, children_system=0.0)
- >>> p.memory_info() # 进程使用的内存
pmem(rss=8310784, vms=2481725440, pfaults=3207, pageins=18)
- >>> p.open_files() # 进程打开的文件
- []
- >>> p.connections() # 进程相关网络连接
- []
- >>> p.num_threads() # 进程的线程数量
- 1
- >>> p.threads() # 所有线程信息
- [pthread(id=1, user_time=0.090318, system_time=0.062736)]
- >>> p.environ() # 进程环境变量
- {'SHELL': '/bin/bash', 'PATH': '/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:...', 'PWD': '/Users/michael', 'LANG': 'zh_CN.UTF-8', ...}
- >>> p.terminate() # 结束进程
Terminated: 15 <-- 自己把自己结束了
>>> psutil.test() # 模拟出 ps 命令的效果
6, 扩展文档
psutil 的官网 ( https://github.com/giampaolo/psutil )
来源: http://www.bubuko.com/infodetail-2604253.html