How to use PsUtil in Python to monitor system status in real time

WBOY
Release: 2023-05-11 09:46:14
forward
1738 people have browsed it

PSUtil is a cross-platform Python library for retrieving information about running processes and system utilization (CPU, memory, disk, network, sensors).

It is cross-platform and runs on Linux, Windows and macOS.

1. Install the psutil module

First, we need to install the psutil module. You can use the following command to install:

pip install psutil
Copy after login

2. Get system information

Use the psutil.cpu_count() function to get the number of CPUs:

import psutil
print("CPU数量:", psutil.cpu_count(logical=True))
Copy after login

As you can see, we use psutil The .cpu_count() function obtains the number of logical CPUs in the system.

Next, we can use the psutil.cpu_stats() function to obtain CPU statistical information:

import psutil
cpu_stats = psutil.cpu_stats()
print("CPU统计信息:", cpu_stats)
Copy after login

3. Obtain CPU information

Use the psutil.cpu_percent() function to obtain CPU usage:

import psutil
print("CPU使用率:", psutil.cpu_percent(interval=1))
Copy after login

As you can see, we use the psutil.cpu_percent() function to obtain the CPU usage. The interval parameter specifies the time interval in seconds.

Next, we can use the psutil.cpu_times() function to obtain the CPU time information:

import psutil
cpu_times = psutil.cpu_times()
print("CPU时间信息:", cpu_times)
Copy after login

4. Obtain the memory information

Use the psutil.virtual_memory() function to obtain Memory information:

import psutil
memory = psutil.virtual_memory()
print("内存信息:", memory)
Copy after login

5. Get disk information

Use psutil.disk_partitions() function to get disk partition information:

import psutil
partitions = psutil.disk_partitions()
print("磁盘分区信息:", partitions)
Copy after login

As you can see, we use psutil.disk_partitions( ) function obtains the disk partition information in the system.

Next, we can use the psutil.disk_usage() function to get the disk usage:

import psutil
usage = psutil.disk_usage('/')
print("磁盘使用情况:", usage)
Copy after login

6. Get network information

Use the psutil.net_io_counters() function to get the network Information:

import psutil
net_io_counters = psutil.net_io_counters()
print("网络信息:", net_io_counters)
Copy after login

As you can see, we used the psutil.net_io_counters() function to obtain network information, including the number of bytes sent and received.

7. Obtain sensor information

Use the psutil.sensors_temperatures() function to obtain sensor information:

import psutil
sensors_temperatures = psutil.sensors_temperatures()
print("传感器信息:", sensors_temperatures)
Copy after login

As you can see, we use the psutil.sensors_temperatures() function to obtain the system sensor information.

8. Use psutil for process management

Use the psutil.process_iter() function to obtain the running process:

import psutil
for process in psutil.process_iter():
    print(process.name())
Copy after login

As you can see, we use psutil.process_iter() The function gets the running process.

We can use various properties of the process object to obtain detailed information about the process.

Use psutil.Process() function to obtain detailed information of a specific process:

import psutil
process_id = 1234
process = psutil.Process(process_id)
print(process.name())
print(process.cpu_percent(interval=1))
print(process.memory_info().rss)
Copy after login

9. Use psutil for system monitoring

Use psutil to implement a simple system monitoring program:

import time
import psutil
while True:
    cpu_usage = psutil.cpu_percent(interval=1)
    memory_usage = psutil.virtual_memory().percent
    disk_usage = psutil.disk_usage('/').percent
    network_usage = psutil.net_io_counters().bytes_sent + psutil.net_io_counters().bytes_recv
    print("CPU使用率:", cpu_usage)
    print("内存使用率:", memory_usage)
    print("磁盘使用率:", disk_usage)
    print("网络使用量:", network_usage)
    time.sleep(1)
Copy after login

As you can see, we use psutil to implement a simple system monitoring program that can monitor CPU, memory, disk and network usage in real time.

The above is the detailed content of How to use PsUtil in Python to monitor system status in real time. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template