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)

最新文章

  1. android Bundle savedInstanceState用途
  2. Gevent中的同步与异步详解
  3. design philosophy
  4. oracle中的nvl(), nvl2()函数
  5. MVC分页之MvcPager使用
  6. PHP之set_error_handler()函数讲解
  7. 删除单链表的倒数第k个结点
  8. PAT-乙级-1023. 组个最小数 (20)
  9. android获取设备屏幕大小的方法
  10. Angularjs Scope 原型链
  11. .Net平台下ActiveMQ入门实例(转)
  12. C# 通过Attribute制作的一个消息拦截器
  13. BZOJ 1264: [AHOI2006]基因匹配Match( LCS )
  14. cmstop中实例化controller_admin_content类传递$this,其构造方法中接收到的是--名为cmstop的参数--包含cmstop中所有属性
  15. [认证授权] 5.OIDC(OpenId Connect)身份认证(扩展部分)
  16. UCloud双11活动 - 新人UCloud代金券最低年100元香港云服务器
  17. git添加远程仓库
  18. GeoServer中使用样式化图层描述符(sld)给WMS加注记
  19. eclipse 部署项目
  20. go相关知识点

热门文章

  1. [20181226]简单探究cluster table.txt
  2. .svn文件夹特别大
  3. 计数排序与桶排序python实现
  4. openSUSE Leap 15.0 Adobe Flash Player 安装说明
  5. Microsoft .NET Framework 3.5 离线安装方法 (仅适用于Win8以上的系统)
  6. mysql中的升序和降序以及一个字段升序和一个字段降序
  7. CISCO ACL配置
  8. npm方法
  9. D. Diverse Garland Codeforces Round #535 (Div. 3) 暴力枚举+贪心
  10. 2018年6月,最新php工程师面试总结