[Python Study notes]psutil Module

Source: Internet
Author: User

System Performance Information Module Psutil

Psutil is a cross-platform library that makes it easy to get the process and system utilization (CPU, memory, disk, network, etc.) information of the system running, mainly used for system monitoring, analyzing and restricting the management of system resources and processes, which implements the functions provided by equivalent command line tools, such as Ps,top,lsof, Netstat,ifconfig,who,df,kill,free,nice and so on. Supports 32-bit, and 64-bit Linux,windows,os X,FREEBSD and other operating systems.

1 CPU Information #linux System CPU utilization has the following parts #user time, the percentage of times that the user process is executing, the percentage of time that executes the kernel process and the interrupt #system #wait IO, The percentage of time that the CPU is idle (idle) while waiting for the IO to wait for the #idle,cpu to be free (% >>> import psutil# get CPU finish information >>> psutil.cpu_ Times () Scputimes (user=29.36, nice=0.0, system=26.59, idle=9619.35, iowait=64.78, irq=0.5, softirq=1.67, steal=0.0, guest=0.0) #获取用户user的cpu时间比 >>> psutil.cpu_times (). user34.54 #获取cpu逻辑个数 >>> Psutil.cpu_count () 4 # Get CPU Physical number >>> Psutil.cpu_count (logical=false) 4 2 Memory Information #linux system memory utilization information involves #total total memory #used amount of memory used #free free memory # Buffers buffer use number of #cache cache usage number #swap swap partition usage # use Psutil.virtual_memory () and Psutil.memory () method to get #获取内存完整信息 >>>mem = Psutil.virtual_memory () >>> memsvmem (total=1961488384, available=1483986944, percent=24.3, used=341475328, free=941473792, active=677801984, inactive=212774912, buffers=32038912, cached=646500352, shared=4116480) #获取内存总数 >>> mem.total1961488384 #获取空闲内存数 >>> mem.free941473792 #获取SWAP分区信息 >>> psutil.swap_memory ( )Sswap (total=2147475456, Used=0, free=2147475456, percent=0.0, Sin=0, sout=0) 3 disk information # Disk utilization using the Psutil.disk_usage () method Get # IO information #read_count read IO number #write_count write io #read_bytes io read bytes #write_bytes io write bytes #read_time disk read Time #write_time disk write Time # Use Psutil.disk_io_counters () to get #获取磁盘完整信息 >>> psutil.disk_partitions () [Sdiskpart (device= '/dev/sda3 '), Mountpoint= '/', fstype= ' ext4 ', opts= ' RW '), Sdiskpart (device= '/dev/sda1 ', mountpoint= ' boot ', fstype= ' ext4 ', opts= ' RW ')] #获取/partition usage >>> psutil.disk_usage ('/') sdiskusage (total=18506760192, used=4438568960, free=13128093696, percent=25.3) #获取硬盘总IO数 >>> psutil.disk_io_counters () 4 Network information #bytes_sent Bytes sent #bytes_recv bytes received #packets_sent Number of packets sent #packest_recv receive packets # Use Psutil.net_io_counters () get >>> psutil.net_io_counters () 5 Other System Information # The Psutil module also supports obtaining user login, boot time and other information # displays user information for the currently logged on System >>> psutil.users () #获取开机时间 >>> psutil.boot_time ()


System Process Management Methods

Psutil also provides good support for obtaining process information, including using the Psutil.pids () method to get all the PID, using Psutil. The process () method obtains information such as the name, path, system resource utilization, and so on for a single process.

#列出所有进程的PID >>> psutil.pids () #实例化一个Process对象, the parameter is a pid>>>p = Psutil. Process (3197) #进程名 >>> p.name () ' Python3 ' #进程bin路径 >>> p.exe () '/opt/python3/bin/python3.5 ' #进程工作绝对路径 >>> p.cwd () #进程状态 >>> p.status () #进程创建时间 >>> p.create_time () #uid信息 >>> p.uids () # GID Information >>> p.gids () #进程cpu时间, including User,system two time >>> p.cpu_times () #进程内存利用率 >>> P.memory_ Percent () #进程内存rss, VMS info >>> p.memory_info () #进程IO信息 >>> p.io_counters () #进程开启的线程数 >>> p.num _threads ()


[Python Study notes]psutil Module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.