current position:Home>Python collects and monitors system data -- psutil

Python collects and monitors system data -- psutil

2021-08-21 20:53:22 Python researcher

Hello everyone , I'm brother Chen ~

Today, I'd like to introduce a library that can obtain the current system information ——psutil

utilize psutil Library can get some information about the system , Such as cpu, Memory usage, etc , This allows you to view the current system usage , Collecting these information in real time can achieve the purpose of real-time monitoring system .

psutil library

psutil It's easy to install

pip install psutil

psutil What system information can the library get ?

psutil What are the functions 
1. Memory usage 
2. Disk usage 
3.cpu Usage rate 
4. The network interface sends and receives traffic 
5. Get the current network speed 
6. The current process of the system 

The following is a demonstration through a specific code case

Memory usage

import psutil
# Memory 
mem = psutil.virtual_memory()
#  Total system memory 
zj = float( / 1024 / 1024 / 1024
#  The system has used memory 
ysy = float(mem.used) / 1024 / 1024 / 1024
#  System free memory 
kx = float( / 1024 / 1024 / 1024
print(' Total system memory :%d.4GB' % zj)
print(' The system has used memory :%d.4GB' % ysy)
print(' System free memory :%d.4GB' % kx)

Gets the total memory of the current system , Used memory , And free memory

The unit of memory obtained here is bytes , So you need to divide by 1024 To G, The same is true below, so I won't repeat the explanation .

Get the system cpu Information

# Show cpu All logical information 
#  see cpu Information of logical number 
print(u" Logic CPU Number : %s" % psutil.cpu_count())
#  see cpu Information about the number of Physics 
print(u" Physics CPU Number : %s" % psutil.cpu_count(logical=False))
#CPU The usage rate of 
cpu = (str(psutil.cpu_percent(1))) + '%'
print(u"cup Usage rate : %s" % cpu)

obtain cpu Information , And this machine cpu Number ( Including logic cpu Numbers and physics cpu Count ), Current cpu Usage rate ( every other 1 Seconds to get once , Get and view real-time cpu Usage )

System disk usage

part = psutil.disk_partitions()
for i in part:
dk = psutil.disk_usage('/')
# Total disk 
total = / 1024 / 1024 / 1024
used = dk.used / 1024 / 1024 / 1024
free = / 1024 / 1024 / 1024
print(' Total system disks :%d.3GB' % total)
print(' The system is already using disk :%d.3GB' % used)
print(' System free disk :%d.3GB' % free)
print(u" Disk usage : %s%%" % dk.percent)
#  Get the total number of disks io Number , Read and write information 

The first few lines are the disk information that the current system can access

Among them the first 1 That's ok mountpoint='/', Indicates the current local default disk

Among them the first 6 That's ok mountpoint='/Volumes/Extreme SSD', Indicates an external solid state mobile hard disk

Here is the local disk mountpoint='/' For example , Check disk usage ( Total capacity , Already used , Free capacity , Usage rate )

The meaning of each field in the last line is as follows :

    read_count  read IO Count 
    write_count  Write IO Count 
    read_bytes  read IO Number of bytes 
    write_bytes  Write IO Number of bytes 
    read_time  Disk read time 
    write_time  Disk write time 

Get the system network card information

#  Get total network IO Information 
#  Send packet 
print(" Send data bytes :", psutil.net_io_counters().bytes_sent,"bytes")
# Receive packets 
print(" Receive data bytes :",psutil.net_io_counters().bytes_recv,"bytes")
#  Output information of each network interface 
net_counter = psutil.net_io_counters(pernic=True)
for i in net_counter:
    print(" network card :"+i+" , Network card information :",net_counter[i])

You can get which network cards exist in the current machine , And how much traffic is sent and received

Check whether the local network card is consistent in the terminal

mac and linux System commands :ifconfig

window System commands :ipconfig

Some screenshots are as follows :

You can see that the network card data obtained by the program is consistent with that obtained by the local terminal

Get the current network speed

Obtain the current network speed by obtaining the traffic sent and received by the network card through the above program

s1 = psutil.net_io_counters(pernic=True)['en0']
s2 = psutil.net_io_counters(pernic=True)['en0']
result = s2.bytes_recv - s1.bytes_recv
print(str('%d' % (result / 1024)) + 'kb/s')

In code ['en0'] Said to get en0 Network card data , Because chenge's network card is en0

Finally, you can see the current network speed

Every 1 Seconds to execute the code can achieve real-time access to network speed

Other features

#  System Up Time 
#  Convert to natural time format 
print(datetime.datetime.fromtimestamp(psutil.boot_time ()).strftime("%Y-%m-%d %H: %M: %S"))
# Get the current system user login information 
users = psutil.users()
for i in users:

You can get the startup time of this machine , And the current users

Today's article is here

If you have any questions, please leave a message below to discuss ~


1. This article introduces in detail python adopt psutil Get system information ( Memory , disk ,cpu etc. )

2. This article is only for readers to learn and use , Not for any other purpose !

copyright notice
author[Python researcher],Please bring the original link to reprint, thank you.

Random recommended