1 system performance information module psutil

Keywords: Python

1.1 system performance information module psutil

psutil is a cross platform library, which can easily obtain the process and system utilization information of the system. It is mainly used for system monitoring, analyzing and limiting system resources and process management. It realizes the functions provided by the same command-line tools.

>>> import psutil
>>> psutil.virtual_memory()
svmem(total=8184156160, available=6610153472, percent=19.2, used=1244897280, free=3709169664, active=2863775744, inactive=983326720, buffers=2134016, cached=3227955200, shared=15917056, slab=344432640)
>>> mem = psutil.virtual_memory()
>>> print(mem)
svmem(total=8184156160, available=6625705984, percent=19.0, used=1229377536, free=3724574720, active=2846990336, inactive=983355392, buffers=2134016, cached=3228069888, shared=15917056, slab=344645632)
>>> print(mem.total)

1.1.1 obtaining system performance information
The CPU utilization of Linux has the following parts:
user time: the percentage of time the user process was executed
system time: the percentage of time spent executing kernel processes and terminals
wait io: the percentage of time that the CPU is idle due to IO wait
Idle: the percentage of time the CPU is idle
(1) CPU Information

>>> import psutil
>>> psutil.cpu_times()  #Get complete CPU Information
scputimes(user=618.58, nice=41.44, system=307.79, idle=142080.4, iowait=18.77, irq=0.0, softirq=10.61, steal=0.0, guest=0.0, guest_nice=0.0)
>>> psutil.cpu_times().user  ï¿¥Obtain individual data information, such as user user of CPU Time ratio
>>> psutil.cpu_count()    #Gets the logical number of CPU s. The default logical=True4
>>> psutil.cpu_count(logical=False)  #Gets the physical number of CPU s

(2) Memory information
The memory utilization information of Linux system involves Total (total memory), used (used memory), free (free memory), buffers (buffer usage), cache (cache usage), sqap (swap partition usage), etc.

>>> import psutil
>>> mem = psutil.virtual_memory()  #Obtain the complete memory information and store it in the variable mem
>>> mem
svmem(total=8184156160, available=6622851072, percent=19.1, used=1232232448, free=3719753728, active=2849816576, inactive=984956928, buffers=2134016, cached=3230035968, shared=15917056, slab=344829952)
>>> mem.total   #Get total memory
>>> mem.free  #Get free memory
>>> psutil.swap_memory()  #Get SWAP partition information
sswap(total=3221221376, used=0, free=3221221376, percent=0.0, sin=0, sout=0)

(3) Disk information
Read IO count: read_count
Write IO count: write_count
IO read bytes: read_bytes
IO write sections: write_bytes
Disk read time: read_time
Disk write time: write_time

>>> import psutil
>>> psutil.disk_partitions()  #Get full disk information
[sdiskpart(device='/dev/sda3', mountpoint='/', fstype='xfs', opts='rw,seclabel,relatime,attr2,inode64,noquota', maxfile=255, maxpath=4096), sdiskpart(device='/dev/sda1', mountpoint='/boot', fstype='xfs', opts='rw,seclabel,relatime,attr2,inode64,noquota', maxfile=255, maxpath=4096)]
>>> psutil.disk_usage('/')  #Gets the usage of the partition (parameter)
sdiskusage(total=28661407744, used=5996216320, free=22665191424, percent=20.9)
>>> psutil.disk_io_counters()  #Get the total IO number of hard disk and read and write information
sdiskio(read_count=33569, write_count=64587, read_bytes=935135744, write_bytes=2736269312, read_time=14209, write_time=30843, read_merged_count=80, write_merged_count=7844, busy_time=18540)
>>> psutil.disk_io_counters(perdisk=True)  #Get the IO number of a single partition and read / write information
{'sda': sdiskio(read_count=33539, write_count=64601, read_bytes=934033920, write_bytes=2736373760, read_time=14174, write_time=30855, read_merged_count=80, write_merged_count=7848, busy_time=18516), 'sda1': sdiskio(read_count=1814, write_count=2087, read_bytes=11977216, write_bytes=12877312, read_time=176, write_time=190, read_merged_count=0, write_merged_count=3, busy_time=342), 'sda2': sdiskio(read_count=57, write_count=0, read_bytes=2285568, write_bytes=0, read_time=7, write_time=0, read_merged_count=0, write_merged_count=0, busy_time=7), 'sda3': sdiskio(read_count=31638, write_count=62514, read_bytes=918697984, write_bytes=2723496448, read_time=13989, write_time=30665, read_merged_count=80, write_merged_count=7845, busy_time=18173), 'sr0': sdiskio(read_count=12, write_count=0, read_bytes=49152, write_bytes=0, read_time=11, write_time=0, read_merged_count=0, write_merged_count=0, busy_time=11), 'sr1': sdiskio(read_count=18, write_count=0, read_bytes=1052672, write_bytes=0, read_time=24, write_time=0, read_merged_count=0, write_merged_count=0, busy_time=19)}

(4) Network information
Number of bytes sent: byte_ sent
Number of bytes accepted: byte_ recv
Number of packets sent: packets_sent
Number of packets accepted: packets_recv

>>> psutil.net_io_counters()  #Get the total IO information of the network. The default is pernic=False
snetio(bytes_sent=20628918, bytes_recv=1123313159, packets_sent=339979, packets_recv=851621, errin=0, errout=0, dropin=0, dropout=0)
>>> psutil.net_io_counters(pernic=True)  #Output IO information of each network interface
{'lo': snetio(bytes_sent=352, bytes_recv=352, packets_sent=4, packets_recv=4, errin=0, errout=0, dropin=0, dropout=0), 'virbr0-nic': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'virbr0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'ens33': snetio(bytes_sent=20628566, bytes_recv=1123314307, packets_sent=339975, packets_recv=851642, errin=0, errout=0, dropin=0, dropout=0)}

(5) Other system information
It also supports obtaining user rate, startup time and other information

>>> import psutil
>>> psutil.users()  #Returns the user information currently logged in to the system
[suser(name='manekl', terminal=':0', host='localhost', started=1636339328.0, pid=10547), suser(name='manekl', terminal='pts/0', host='localhost', started=1636339328.0, pid=11607)]
>>> import datetime
>>> psutil.boot_time()  #Get the boot time and return it in Linux timestamp format
>>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")  #Convert to natural time format
'2021-11-07 19:13:15'

1.1.2 system process management method
Obtaining the process information of the current system can let the operation and maintenance personnel know the operation status of the application, and provide a good data reference for problem location
(1) Process information
Get all process PID: psutil.pids()
Get various information about a single process: psutil.Process()

>>> import psutil
>>> psutil.pids()
[1, 2, 3, 5, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19, 21, 22, 23, 24, 26, 27, 28, 29, 31, 32, 33, 34, 36, 37, 38, 39, 41, 42, 43, 44, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 62, 63, 64, 65, 73, 75, 76, 78, 79, 81, 94, 95, 130, 144, 283, 284, 287, 293, 294, 296, 297, 298, 299, 302, 303, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 419, 443, 454, 486, 577, 578, 579, 580, 581, 582, 583, 584, 634, 636, 6]
>>> p = psutil.Process(1)
>>> p.name()
>>> p.exe()
>>> p.cwd()
>>> p.status()
>>> p.create_time()
>>> p.uids()
puids(real=0, effective=0, saved=0)
>>> p.gids()
pgids(real=0, effective=0, saved=0)
>>> p.cpu_times()
pcputimes(user=0.67, system=4.5, children_user=35.65, children_system=39.67, iowait=0.07)
>>> p.cpu_affinity()
[0, 1, 2, 3, 4, 5, 6, 7]
>>> p.memory+percent()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Process' object has no attribute 'memory'
>>> p.memory_percent()
>>> p.memory_info()
pmem(rss=7340032, vms=198631424, shared=4235264, text=1441792, lib=0, data=153952256, dirty=0)
>>> p.io_counters()
pio(read_count=2066246, write_count=214550, read_bytes=336731648, write_bytes=89939968, read_chars=1137880808, write_chars=891194721)
>>> p.connections()
>>> p.num_threads()

(2) Use of the. Popen class
The popen class provided by psutil is used to obtain the application process information started by the user, so as to track the running status of the program process. The specific implementation methods are as follows

>>> import psutil
>>> from subprocess import PIPE  #Through the application launched by popen, you can track all relevant information about the operation of the program
>>> p = psutil.Popen(["/usr/bin/python","-c", "print('hello')"], stdout=PIPE) 
>>> p.name()
>>> p,username()
>>> p,communicate()
>>> p.cpu_times()  #Get the CPU time of the process

Posted by AshrakTheWhite on Tue, 09 Nov 2021 16:22:27 -0800