前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python运维之psutil模块

Python运维之psutil模块

作者头像
py3study
发布2020-01-09 15:10:12
1.3K0
发布2020-01-09 15:10:12
举报
文章被收录于专栏:python3python3python3

最近开始学习Python自动化运维,特记下笔记。 学习中使用的系统是Kali Linux2017.2,Python版本为2.7.14+ 因为在KALI里面没有自带psutil模块,需要使用pip进行安装

Python运维之psutil模块
Python运维之psutil模块

安装完成后进入python交互模式 查看psutil 的帮助信息,可以发现psutil支持跨平台的环境,同时也支持python3

Python运维之psutil模块
Python运维之psutil模块

获取CPU信息

使用cpu_times获取CPU信息,用户/系统/空闲时间

Python运维之psutil模块
Python运维之psutil模块

也可以获取单条信息

Python运维之psutil模块
Python运维之psutil模块

如果需要获取所有CPU核心的信息需要加一个参数percpu=True

Python运维之psutil模块
Python运维之psutil模块

获取CPU核心个数,方法为cpu_count()

Python运维之psutil模块
Python运维之psutil模块

如果只要获取CPU物理核心的数量需要加一个参数,logical=False 注:logical默认为True

Python运维之psutil模块
Python运维之psutil模块

获取内存信息

内存信息可以使用virtual_memory()和swap_memory()方法进行获取 注:单位为字节

Python运维之psutil模块
Python运维之psutil模块

只获取内存总数

Python运维之psutil模块
Python运维之psutil模块

只获取空闲总数

Python运维之psutil模块
Python运维之psutil模块

获取swap分区信息,使用swap_memory()方法进行获取

Python运维之psutil模块
Python运维之psutil模块

获取磁盘信息

获取磁盘分区信息可以使用disk_partitions()方法获取

Python运维之psutil模块
Python运维之psutil模块

可以使用disk_usage()方法获取分区的使用情况

Python运维之psutil模块
Python运维之psutil模块

获取硬盘IO信息可以使用disk_io_counters()方法获取

Python运维之psutil模块
Python运维之psutil模块

如果要获取单个分区的IO数等情况,需要在disk_io_counters()下加一个参数,perdisk=True

Python运维之psutil模块
Python运维之psutil模块

获取网络信息

获取网卡的总IO等信息可以使用net_io_counters()方法

Python运维之psutil模块
Python运维之psutil模块

如果需要输出每一个网卡的IO数需要加一个参数pernic=True

Python运维之psutil模块
Python运维之psutil模块

也可以获取网卡IO的单个信息

Python运维之psutil模块
Python运维之psutil模块

获取其他的一些系统信息

返回当前登录系统的用户信息 ,users()方法 注:started返回的为Linux时间戳

Python运维之psutil模块
Python运维之psutil模块

获取开机时间,同样使用Linux时间戳来返回,使用boot_time()方法

Python运维之psutil模块
Python运维之psutil模块

系统进程管理

获取所有进程PID,使用pids()方法

Python运维之psutil模块
Python运维之psutil模块

可以使用Process()方法获取单个进程的信息,先要实例化一个对象,参数为pid值,例:pid为960

Python运维之psutil模块
Python运维之psutil模块

获取实例化的进程名,name()方法

Python运维之psutil模块
Python运维之psutil模块

获取进程bin路径,exe()方法

Python运维之psutil模块
Python运维之psutil模块

获取进程工作绝对路径目录,cwd()方法

Python运维之psutil模块
Python运维之psutil模块

获取进程状态,status()方法

Python运维之psutil模块
Python运维之psutil模块

获取进程创建时间,create_time()方法 注:时间为Linux时间戳

Python运维之psutil模块
Python运维之psutil模块

获取进程uid与gid信息,uids()与gids()方法

Python运维之psutil模块
Python运维之psutil模块

获取进程cpu时间信息,cpu_times()方法

Python运维之psutil模块
Python运维之psutil模块

获取进程与CPU的亲和度,cpu_affinity()方法,可以将CPU号作为参数来设置进程CPU亲和性

Python运维之psutil模块
Python运维之psutil模块

获取进程内存使用率,memory_percent()方法,单位为百分比

Python运维之psutil模块
Python运维之psutil模块

获取进程IO信息,io_counters()方法

Python运维之psutil模块
Python运维之psutil模块

获取进程内存rss、vms等信息,memory_info()方法

Python运维之psutil模块
Python运维之psutil模块

获取进程开启的线程数,num_threads()方法

Python运维之psutil模块
Python运维之psutil模块

获取进程的线程信息,threads()方法

Python运维之psutil模块
Python运维之psutil模块

Popen类的使用

Popen类可以获取用户启动应用程序的情况,以便跟踪程序的运行状态 例:跟踪python的运行情况

Python运维之psutil模块
Python运维之psutil模块

name()方法获取进程名,username()方法获取启动用户,cpu_times()方法获取进程运行的CPU时间

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用cpu_times获取CPU信息,用户/系统/空闲时间
  • 内存信息可以使用virtual_memory()和swap_memory()方法进行获取 注:单位为字节
  • 获取磁盘分区信息可以使用disk_partitions()方法获取
  • 获取网卡的总IO等信息可以使用net_io_counters()方法
  • 返回当前登录系统的用户信息 ,users()方法 注:started返回的为Linux时间戳
  • 获取所有进程PID,使用pids()方法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档