Python about getting the status of the current system
System Performance Information Module Psutil #可跨平台
Psutil is a cross-platform library that makes it easy to get the process and system utilization of the system running
can achieve PS, top, LSO, nice, Netstat, ifconfig, who, DF, kill, free
Ionice, Iostat, Iotop, uptime, pidof, TTY, Taskset, pmap the functions of these commands
First determine if the current system has no Psutil module
Import Psutil
If there is, the direct import module succeeds, if not, it will prompt the error
Then download the psutil-2.0.0.tar.gz source package on the official website.
TAR-ZXF psutil-2.0.0.tar.gz & CD psutil-2.0.0
Python setup.py Install
Read the entire information of the CPU
Import Psutil
Psutil.cpu_times () #显示cpu的整个信息
Get a single value
Psutil.cpu_times (). User #如果要只但看那个的话就在后边加上. XXX on the line.
Gets the logical number of CPUs
Psutil.cpu_count ()
Get the physical number of CPUs
Psutil.cpu_count (Logical=false)
Read memory information
Linux system memory utilization information involves to-tal (total memory), used (to use memory), free (idle memory), buffers (buffer usage)
Cache (number of caches), swap (swap partition usage), using psutil.virtual_memory () and Psuti.swap_memory () method to get
Import Psuti
MEM = psuti.virtual_memory () #获取内存的完整信息
Mem.total #获取内存总数
Mem.free #获取空闲的内存信息
Get swap partition information
Psutil.swap_memory ()
Read disk parameters
Disk utilization is obtained using the Psutil.disk_usage method, disk IO information includes read_count (read IO number), Write_count (write IO number)
Read_bytes (io Write bytes), read_time (disk read time), Write_time (disk write time), these IO information with psutil.disk_io_counters ()
Get full information about a disk
Psutil.disk_partitions ()
Get parameters for a partitioned table
Psutil.disk_usage ('/') #获取/partition status
Get the total number of HDD IO
Psutil.disk_io_counters ()
Get the number of individual partition IO
Psutil.disk_io_counters (perdisk=true) #perdisk =true parameter gets the number of individual partition IO
Read Network information
Network information is similar to disk IO information, involving several key points, including byes_sent (number of bytes sent), Byte_recv=xxx (bytes accepted),
Pack-ets_sent=xxx (number of bytes sent), pack-ets_recv=xxx (number of packets received), these network information with Psutil.net_io_counters ()
Psutil.net_io_counters () #获取网络总IO信息
Psutil.net_io_counters (pernic=true) #pernic =true output Network per interface information
Get current system User login information
Psutil.users ()
Get boot time
Import Psutil, DateTime
Psutil.boot_time () #以linux时间格式返回
Datetime.datetime.fromtimestamp (Psutil.boot_time ()). Strftime ("%y-%m-%d%H:%m:%s") #转换成自然时间格式
System Process Management
Get the current system's process information, get the current English program running status, including the start time of the process, check the settings CPU affinity, memory usage, IO information
Socket connection, number of threads, etc.
Get process Information
The Psutil module is well supported in getting the process, using the Psutil.pids () method to get the PID of all processes,
Use Psutil. The process () method gets the name of a single process, the path state, and so on
View all system processes
Psutil.pids ()
viewing individual processes
p = psutil. Process (2423)
P.name () #进程名
P.exe () #进程的bin路径
P.CWD () #进程的工作目录绝对路径
P.status () #进程状态
P.create_time () #进程创建时间
P.uids () #进程uid信息
P.gids () #进程的gid信息
P.cpu_times () #进程的cpu时间信息, including User,system two CPU information
P.cpu_affinity () #get进程cpu亲和度, if you want to set CPU affinity, use the CPU number as a reference.
P.memory_percent () #进程内存利用率
P.memory_info () #进程内存rss, VMS info
P.io_counters () #进程的IO信息, including read/write IO numbers and parameters
P.connectios () #返回进程列表
P.num_threads () #进程开启的线程数
Heard the Psutil Popen method to start the application, you can track information about the program
From subprocess Import PIPE
p = psutil. Popen (["/usr/bin/python", "-C", "Print (' Hello ')"],stdout=pipe)
P.name ()
P.username ()
This article is from the "Bucket Month" blog, please be sure to keep this source http://douyue.blog.51cto.com/10174393/1654865
Python Gets the System State Psutil module