Python-psutil模块

windows系统监控实例,查询

1.简单介绍

psutil是一个跨平台的库(http://code.google.com/p/psutil/),能够轻松的实现获取系统运行的进程和系统利用率(CPU、内存、磁盘、网络等)信息。它主要应用于系统监控,分析和限制系统资源及进程的管理。能实现同等命令行工具提供的功能,如ps、top、lsof、netstat、ifconfig、who、df、kill、free、nice、ionice、iostat、iotop、uptime、pidof、tty、taskset、pmap等。

2.安装

 pip3 install psutil

3.基本使用

3.1 cpu相关

In []: import psutil
In []: psutil.cpu_times()#使用cpu_times获取cpu的完整信息
Out[]: scputimes(user=769.84, nice=2.78, system=387.68, idle=83791.98, iowait=479.84, irq=0.0, softirq=0.81, steal=0.0, guest=0.0, guest_nice=0.0)
In []: psutil.cpu_count()#获取cpu的逻辑个数
Out[]:
In []: psutil.cpu_times_percent()#获取cpu的所有逻辑信息
Out[]: scputimes(user=0.7, nice=0.0, system=0.5, idle=98.4, iowait=0.4, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)

3.2内存相关

In []: psutil.virtual_memory()#获取内存的所有信息
Out[]: svmem(total=, available=, percent=33.0, used=, free=, active=, inactive=, buffers=, cached=, shared=)
In []: psutil.virtual_memory().total
Out[]:
In []: psutil.virtual_memory().used
Out[]:
In []: psutil.virtual_memory().free
Out[]:
In []: psutil.swap_memory()#交换分区相关
Out[]: sswap(total=, used=, free=, percent=, sin=, sout=)

3.3磁盘相关

In []: psutil.disk_partitions()#获取磁盘的详细信息
Out[]: [sdiskpart(device='/dev/vda1', mountpoint='/', fstype='ext3', opts='rw,noatime,data=ordered')]
In []: psutil.disk_usage('/')#获取分区的使用情况
Out[]: sdiskusage(total=, used=, free=, percent=11.2)
In []: psutil.disk_io_counters()#获取磁盘总的io个数,读写信息
Out[]: sdiskio(read_count=, write_count=, read_bytes=, write_bytes=, read_time=, write_time=, read_merged_count=, write_merged_count=, busy_time=)
补充说明下:
read_count(读IO数)
write_count(写IO数)
read_bytes(读IO字节数)
write_bytes(写IO字节数)
read_time(磁盘读时间)
write_time(磁盘写时间)

3.4网络信息

In []: psutil.net_io_counters()#获取网络总信息
Out[]: snetio(bytes_sent=, bytes_recv=, packets_sent=, packets_recv=, errin=, errout=, dropin=, dropout=)
In []: psutil.net_io_counters(pernic=True)#获取每个网络接口的信息
Out[]:
{'eth0': snetio(bytes_sent=, bytes_recv=, packets_sent=, packets_recv=, errin=, errout=, dropin=, dropout=),
'lo': snetio(bytes_sent=, bytes_recv=, packets_sent=, packets_recv=, errin=, errout=, dropin=, dropout=)}

3.5其它信息

In []: psutil.users()#返回当前登录系统的用户信息
Out[]: [suser(name='root', terminal='pts/0', host='X.X.X.X', started=1492844672.0)]
In []: psutil.boot_time()#获取开机时间
Out[]: 1492762895.0
In []: import datetime#转换成你能看懂的时间
In []: datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
Out[]: '2017-04-21 16:21:35'

4.系统进程的管理方法

[root@VM_46_121_centos ~]# ps -ef | grep ipython#这里首先我用ps获取ipython进程号
root : pts/ :: grep --color=auto ipython
root : pts/ :: /usr/local/bin/python3. /usr/local/bin/ipython
In []: psutil.Process()
Out[]: <psutil.Process(pid=, name='ipython') at >
In []: p=psutil.Process()#实例化一个进程对象,参数为ipython这个进程的PID
In []: p.name()#获得进程名
Out[]: 'ipython'
In []: p.exe()#获得进程的bin路径
Out[]: '/usr/local/bin/python3.5'
In []: p.cwd()获得进程工作目录的绝对路径
Out[]: '/usr/local/lib/python3.5/site-packages/psutil-5.2.2-py3.5.egg-info'
In []: p.status()#获得进程状态
Out[]: 'running'
In []: p.create_time()#获得进程创建的时间,时间戳格式
Out[]: 1492848093.13
In []: datetime.datetime.fromtimestamp(p.create_time()).strftime("%Y-%m-%d %H:%M:%S")
Out[]: '2017-04-22 16:01:33'
In []: p.uids()#获取进程的uid信息
Out[]: puids(real=, effective=, saved=)
In []: p.gids()#获取进程的gid信息
Out[]: pgids(real=, effective=, saved=)
In []: p.cpu_times()#获取进程的cpu的时间信息
Out[]: pcputimes(user=9.53, system=0.34, children_user=0.0, children_system=0.0)
In []: p.cpu_affinity()#获取进程的cpu亲和度
Out[]: []
In []: p.memory_percent()#获取进程的内存利用率
Out[]: 6.187014703452833
In []: p.memory_info()#获取进程的内存rss,vms信息
Out[]: pmem(rss=, vms=, shared=, text=, lib=, data=, dirty=)
In []: p.io_counters()#获取进程的io信息
Out[]: pio(read_count=, write_count=, read_bytes=, write_bytes=, read_chars=, write_chars=)
In []: p.num_threads()获取进程开启的线程数
Out[]: popen类的使用:获取用户启动的应用程序的进程信息
In []: from subprocess import PIPE
In []: p1=psutil.Popen(["/usr/bin/python","-c","print('hello')"], stdout=PIPE)
In []: p1.name()
Out[]: 'python'
In []: p1.username()
Out[]: 'root'
In []: p1.communicate()
Out[]: (b'hello\n', None)
In []: p.cpu_times()
Out[]: pcputimes(user=13.11, system=0.52, children_user=0.01, children_system=0.0)

python之psutil模块详解(Linux)--小白博客的更多相关文章

  1. python之OS模块详解

    python之OS模块详解 ^_^,步入第二个模块世界----->OS 常见函数列表 os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows ...

  2. python之sys模块详解

    python之sys模块详解 sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和我一起走进python的模块吧! sys模块的常见函数列表 sys.argv: 实现从程序外部向程序传 ...

  3. python中threading模块详解(一)

    python中threading模块详解(一) 来源 http://blog.chinaunix.net/uid-27571599-id-3484048.html threading提供了一个比thr ...

  4. Python中time模块详解

    Python中time模块详解 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time模块. ...

  5. Python的logging模块详解

          Python的logging模块详解 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.日志级别 日志级别指的是产生的日志的事件的严重程度. 设置一个级别后,严重程度 ...

  6. Python运维自动化psutil 模块详解(超级详细)

    psutil 模块 参考官方文档:https://pypi.org/project/psutil/ 一.psutil简介 psutil是一个开源且跨平台(http://code.google.com/ ...

  7. python中常用模块详解二

    log模块的讲解 Python 使用logging模块记录日志涉及四个主要类,使用官方文档中的概括最为合适: logger提供了应用程序可以直接使用的接口API: handler将(logger创建的 ...

  8. python中socket模块详解

    socket模块简介 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket.socket通常被叫做"套接字",用于描述IP地址和端口,是一个通信 ...

  9. python的re模块详解

    一.正则表达式的特殊字符介绍 正则表达式 ^ 匹配行首 $ 匹配行尾 . 任意单个字符 [] 匹配包含在中括号中的任意字符 [^] 匹配包含在中括号中的字符之外的字符 [-] 匹配指定范围的任意单个字 ...

随机推荐

  1. RHEL下SendMail修改发邮箱地址

    RHEL(Oracle Linxu/CentOS)系统下,如果使用sendmail发送邮件,如果不特殊设置,一般发件箱地址为user@hostname,例如,hostname为DB-Server.lo ...

  2. 下载安装Emacs和基本配置--待更新中

    Emacs下载地址 下载好后,解压到D:\Emacs,并设置环境变量D:\Emacs\bin 熟悉基本操作 光标的移动 文件的创建与打开

  3. django静态模版使用

    第一步:在app目录下建立static文件夹,将CSS文件.js文件放到static文件夹下 第二步:TEMPLATES = [ { 'BACKEND': 'django.template.backe ...

  4. EOS智能合约存储实例讲解

    EOS智能合约存储实例 智能合约中的基础功能之一是token在某种规则下转移.以EOS提供的token.cpp为例,定义了eos token的数据结构:typedef eos::token<ui ...

  5. c/c++ 标准容器 之 初始化, 赋值, swap, 比较

    c/c++ 标准容器 之 初始化, 赋值, swap, 比较 知识点 1,容器的初始化,对应代码里的test1 2,标准库array的初始化,对应代码里的test2 3,容器的赋值 ,对应代码里的te ...

  6. June 8. 2018 Week Week 23rd Friday

    You'll have bad times, but it'll always wake you up to the good stuff you weren't paying attention t ...

  7. 【汤鸿鑫 3D太极】肩与膀的细分

  8. MySQL高级知识系列目录

    MySQL高级知识(一)——基础 MySQL高级知识(二)——Join查询 MySQL高级知识(三)——索引 MySQL高级知识(四)——Explain MySQL高级知识(五)——索引分析 MySQ ...

  9. C++ 星号* 与 引用&

    星号 * 1. 声明的时候有*, 表示指针变量 int *p=&a;// '&'的作用就是把a变量在内存中的地址给提取出来 2. * +地址, 表示地址操作符 3. 数字*数字, 表示 ...

  10. 《JAVA程序设计》_第一周学习总结

    20175217吴一凡 <java程序设计> 第一周学习总结 虽然已经做好了心理准备,但第一周的学习任务着实让我忙了整整三天,还是挺充实的吧.寒假已经在自己的电脑上安装好了虚拟机,我就在我 ...