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. 银盒子智慧餐厅硬件尺寸规格&推荐机型

  2. 转:敏捷开发之Scrum扫盲篇

    现在敏捷开发是越来越火了,人人都在谈敏捷,人人都在学习Scrum和XP... 为了不落后他人,于是我也开始学习Scrum,今天主要是对我最近阅读的相关资料,根据自己的理解,用自己的话来讲述Scrum中 ...

  3. 转:Redis 使用经验总结

    转自:Redis 总结精讲 看一篇成高手系统-4 本文围绕以下几点进行阐述 1.为什么使用redis2.使用redis有什么缺点3.单线程的redis为什么这么快4.redis的数据类型,以及每种数据 ...

  4. [Hive_10] Hive 的分析函数

    0. 说明 Hive 的分析函数 窗口函数  | 排名函数 | 最大值 | 分层次 | lead && lag 统计活跃用户 | cume_dist 1. 窗口函数(开窗函数) ove ...

  5. puppet 和 apache passenger的配置

    目录 1. 概要 2. apache passenger 安装测试 2.1. 什么是 apache passenger 2.2. 安装 apache passenger 2.3. 配置 apache ...

  6. hashcode相等两个类一定相等吗?equals呢?相反呢?

    hashCode相等,equals也不一定相等, 两个类也不一定相等 equals相同, 说明是同一个对象, 那么hashCode一定相同 哈希表是结合了直接寻址和链式寻址两种方式,所需要的就是将需要 ...

  7. JavaScript函数式编程

        一段糟糕透顶的海鸥seagulls程序   鸟群合并conjoin则变成了一个更大的鸟群,繁殖breed则增加了鸟群的数量,增加的数量就是它们繁殖出来的海鸥的数量 //Flock 群 var ...

  8. JavaScript闭包理解【关键字:普通函数、变量访问作用域、闭包、解决获取元素标签索引】

        一.闭包(Closure)模糊概述 之前总觉得闭包(Closure)很抽象而且难理解,百度一下"闭包"名词,百度的解释是:“闭包是指可以包含自由(未绑定到特定对象)变量的代 ...

  9. 用栈来实现队列的golang实现

    使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列首部的元素. empty() -- 返回队列是否为空. ...

  10. 02.Python网络爬虫第二弹《http和https协议》

    一.HTTP协议 1.官方概念: HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文 ...