python之psutil模块详解(Linux)--小白博客

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)
上一篇:git删除Tag


下一篇:UG二次开发 实体转装配零件并将实体属性填写到装配零件中(C#)