Python system monitoring module psutil function and classical usage analysis
- 2020-09-16 07:32:46
- OfStack
This paper introduces the psutil system monitoring module Python function and classical usage. To share for your reference, the details are as follows:
1. Overview of psutil module
psutil is a cross-platform library (http: / / code. google. com p/psutil /), can easily realize the acquisition system running processes and system utilization, including CPU, memory, disk, network, etc.) information. It is mainly used for system monitoring, analyzing and limiting the management of system resources and processes. It implements functions provided by equivalent command line tools, such as ps, top, lsof, netstat, ifconfig, who, df, kill, free, nice, ionice, iostat, iotop, uptime, pidof, tty, taskset, pmap, etc. It currently supports 32-bit and 64-bit Linux, Windows, OS X, FreeBSD and Sun Solaris operating systems,
2. psutil installation
wget https://pypi.python.org/packages/source/p/psutil/psutil-2.0.0.tar.gz
tar -xzvf psutil-2.0.0.tar.gz
cd psutil-2.0.0
python setup.py install
Supplement: On Windows platform, you only need to enter the following command on the command line interface to quickly install:
pip install psutil
3. psutil use
Get system performance information (CPU, memory, disk, network, etc.)
1) CPU related
View cpu information
import Psutil
View all information on cpu
>>> psutil.cpu_times()
scputimes(user=11677.09, nice=57.93, system=148675.58, idle=2167147.79, iowait=260828.48, irq=7876.28, softirq=0.0, steal=3694.59, guest=0.0, guest_nice=0.0)
Displays all logical information for cpu
>>> psutil.cpu_times(percpu=True)
[scputimes(user=11684.17, nice=57.93, system=148683.01, idle=2168982.08, iowait=260833.18, irq=7882.35, softirq=0.0, steal=3697.3, guest=0.0, guest_nice=0.0)]
View the user's cpu time ratio
>>> psutil.cpu_times().user
11684.4
View the logical number of cpu
>>> psutil.cpu_count()
1
View the number of cpu physics
>>> psutil.cpu_count(logical=False)
1
View the system memory
>>> import psutil
>>> mem = psutil.virtual_memory()
>>> mem
# All information about system memory
svmem(total=1040662528, available=175054848, percent=83.2, used=965718016, free=74944512, active=566755328, inactive=59457536, buffers=9342976, cached=90767360)
Total system memory
>>> mem.total
1040662528
The system is already using memory
pip install psutil
0
System free memory
pip install psutil
1
Gets swap memory information
pip install psutil
2
Read disk parameters
Disk utilization
psutil.disk_usage
Method acquisition,
Disk IO information includes read_count(read IO number), write_count(write IO number)
read_bytes(IO writing), read_time(disk read time), write_time(disk write time), these IO messages are used
psutil.disk_io_counters()
Get complete information about the disk
pip install psutil
4
Gets the parameters of the partition table
pip install psutil
5
Gets the total number of hard disk IO
pip install psutil
6
Gets the number of IO for a single partition
pip install psutil
7
Read the network information
The network information is similar to the disk IO information and involves several key points, including byes_sent(number of bytes sent),byte_recv=xxx(number of bytes accepted),
pack-ets_sent =xxx(number of bytes sent), ES152en-ES153en_ES154en =xxx(number of packets received), which are used for network information
Obtain total IO information
psutil.net_io_counters()
Output information for each interface of the network
pip install psutil
9
Obtain the login information of current system users
psutil.users()
Get the boot time
psutil.boot_time() # In order to linux Time format return
datetime.datetime.fromtimestamp(psutil.boot_time ()).strftime("%Y-%m-%d %H: %M: %S") # Convert to natural time format
System process management
Get the process information of the current system, get the running state of the current program, including the start time of the process, view the Settings of CPU affinity, memory utilization,IO information, socket connection, number of threads, etc
Get process information
View all system processes
psutil.pids()
View a single process
import Psutil
3
More about Python related topics: interested readers to view this site "Python process and thread skills summary", "Python Socket programming skills summary", "Python data structure and algorithm tutorial" and "Python function using techniques", "Python string skills summary", "Python introduction and advanced tutorial" and "Python file and directory skills summary"
I hope this article is helpful for Python programming.