Python-psutil模块

时间:2024-05-28 16:36:50

psutil

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等。目前支持32位和64位的Linux、Windows、OS X、FreeBSD和Sun Solaris等操作系统.有兴趣的同学可以去试试这些命令。自我感觉这个库做运维的同学还是值得玩味儿!

2.安装

[root@VM_46_121_centos python]# pip3 install psutil
我安装的是ipsutil-5.2.2这个版本!

3.基本使用

3.1 cpu相关

In [1]: import psutil
In [2]: psutil.cpu_times()#使用cpu_times获取cpu的完整信息
Out[2]: 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 [3]: psutil.cpu_count()#获取cpu的逻辑个数
Out[3]: 1
In [4]: psutil.cpu_times_percent()#获取cpu的所有逻辑信息
Out[4]: 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 [5]: psutil.virtual_memory()#获取内存的所有信息
Out[5]: svmem(total=1041309696, available=697958400, percent=33.0, used=176611328, free=91947008, active=516075520, inactive=323096576, buffers=102719488, cached=670031872, shared=12873728)
In [7]: psutil.virtual_memory().total
Out[7]: 1041309696
In [8]: psutil.virtual_memory().used
Out[8]: 176553984
In [9]: psutil.virtual_memory().free
Out[9]: 91901952
In [13]: psutil.swap_memory()#交换分区相关
Out[13]: sswap(total=0, used=0, free=0, percent=0, sin=0, sout=0)

3.3磁盘相关

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

3.4网络信息

In [19]: psutil.net_io_counters()#获取网络总信息
Out[19]: snetio(bytes_sent=24172706, bytes_recv=168785879, packets_sent=163657, packets_recv=442827, errin=0, errout=0, dropin=0, dropout=0)
In [20]: psutil.net_io_counters(pernic=True)#获取每个网络接口的信息
Out[20]:
{'eth0': snetio(bytes_sent=24177750, bytes_recv=168797166, packets_sent=163685, packets_recv=442948, errin=0, errout=0, dropin=0, dropout=0),
'lo': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0)}

3.5其它信息

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

4.系统进程的管理方法

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