psutil module of automatic operation and maintenance to obtain system performance information

Posted by Lv-Kris on Thu, 02 Jan 2020 03:42:34 +0100

1, CPU Information

>>> import psutil  #Import psutil module
>>> psutil.cpu_times()  #View full CPU Information
scputimes(user=262.23, nice=0.27, system=170.81, idle=153135.17, iowait=50.04, irq=0.0, softirq=0.08, steal=0.0, guest=0.0, guest_nice=0.0)
>>> psutil.cpu_times(percpu=False)  # Another way to view complete CPU Information
scputimes(user=262.24, nice=0.27, system=170.83, idle=153151.74, iowait=50.04, irq=0.0, softirq=0.08, steal=0.0, guest=0.0, guest_nice=0.0)
>>> psutil.cpu_times().user  # View single type information, such as the CPU time ratio of users
262.3
>>> psutil.cpu_count()  # View the logical number of CPU s
1
>>> psutil.cpu_count(logical=False)  # View the physical number of CPU s
1
>>> 

2, Memory information

Total (total memory)

Used (used memory)

Free (free memory)

buffers (number of buffer uses)

Cache (cache usage)

Swap (swap partition usage)

>>> import psutil  # Import psutil module
>>> psutil.virtual_memory()  # View overall memory information
svmem(total=1040551936, available=701288448, percent=32.6, used=180355072, free=452206592, active=275603456, inactive=224522240, buffers=60026880, cached=347963392, shared=352256, slab=65908736)
>>> jier = psutil.virtual_memory()  # Assign memory information to a variable
>>> jier.total  # Check the total memory
1040551936
>>> jier.free  # View memory empty balance
452206592
>>> 

3, Disk information

Read? Count

Write "count

read_bytes

write_bytes

Read? Time

Write time

>>> import psutil  # Import module
>>> psutil.disk_partitions()  # View full disk information
[sdiskpart(device='/dev/vda1', mountpoint='/', fstype='ext4', opts='rw,relatime,data=ordered')]
>>> psutil.disk_usage('/')  # View the disk usage information of the root partition
sdiskusage(total=42139451392, used=3381141504, free=36594155520, percent=8.5)
>>> psutil.disk_usage('/run/')  # View disk usage information of run partition
sdiskusage(total=520273920, used=352256, free=519921664, percent=0.1)
>>> psutil.disk_io_counters()  # View IO read / write status of disk
sdiskio(read_count=16014, write_count=37632, read_bytes=329921536, write_bytes=278237184, read_time=146130, write_time=148078, read_merged_count=13, write_merged_count=16619, busy_time=55554)
>>> psutil.disk_io_counters(perdisk=True)  # Get the IO number and read-write information of a single partition
{'vda': sdiskio(read_count=16014, write_count=37632, read_bytes=329921536, write_bytes=278237184, read_time=146130, write_time=148078, read_merged_count=13, write_merged_count=16619, busy_time=55554), 'vda1': sdiskio(read_count=15930, write_count=36897, read_bytes=327808000, write_bytes=278237184, read_time=146088, write_time=147968, read_merged_count=13, write_merged_count=16619, busy_time=55428)}
>>> 

4, Network information

Bytes'sent

bytes_recv=28220119 (bytes received)

packets_sent=200978 (number of packets sent)

Packets? Recv = 212672 (number of packets received)

>>> import psutil  # Import module
>>> psutil.net_io_counters()  # View the overall network IO information flow
snetio(bytes_sent=9978681, bytes_recv=5290561, packets_sent=37611, packets_recv=44983, errin=0, errout=0, dropin=0, dropout=0)
>>> psutil.net_io_counters(pernic=True)  # View IO information flow of individual interface
{'lo': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'eth0': snetio(bytes_sent=9982647, bytes_recv=5294811, packets_sent=37641, packets_recv=45036, errin=0, errout=0, dropin=0, dropout=0)}
>>> 

5, Other system information

>>> import psutil  # Import module
>>> import datetime  # Import time module
>>> psutil.users()  # Get login user information
[suser(name='root', terminal='pts/0', host='124.205.208.206', started=1534906496.0, pid=22583), suser(name='root', terminal='pts/1', host='124.205.208.206', started=1534907136.0, pid=22739)]
>>> psutil.boot_time()  # Get system start time information
1534755312.0
>>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %G:%M:%S")  # Grid system start-up time information
'2018-08-20 2018:55:12'
>>> 

 

Topics: network