SoFunction
Updated on 2025-03-02

In-depth study on monitoring and management of psutil modules for Python operation and maintenance automation

introduction

In the field of operation and maintenance, monitoring and managing system resources is a crucial task. Python provides a powerful tool, i.e.psutilModule, used to obtain system information, monitor processes, manage network connections, etc. This article will introduce in-depthpsutilThrough rich sample codes, readers can understand and use this operation and maintenance automation tool more comprehensively.

Introduction to psutil module

psutilModules are a cross-platform library for obtaining information about system utilization (CPU, memory, disk, network, etc.) and processes.

First, the following examples allow you to obtain basic information of the system.

import psutil
# Get CPU informationcpu_info = psutil.cpu_percent(interval=1, percpu=True)
print(f"CPU Usage: {cpu_info}%")
# Get memory informationmemory_info = psutil.virtual_memory()
print(f"Memory Usage: {memory_info.percent}%")

Monitor CPU and memory

psutilThe module can monitor CPU and memory usage in real time, as shown below.

import psutil
import time
# Real-time monitoring of CPU and memoryfor _ in range(5):
    cpu_percent = psutil.cpu_percent(interval=1)
    memory_percent = psutil.virtual_memory().percent
    print(f"CPU Usage: {cpu_percent}% | Memory Usage: {memory_percent}%")

Process Management

psutilThe module provides powerful process management functions, allowing easy access to and running process information on the operating system.

import psutil
# Get a list of all processesprocess_list = psutil.process_iter()
for process in process_list:
    print(f"Process ID: {} | Name: {()}")

Network connection monitoring

passpsutilModule can obtain information about network connections, such as local address, remote address, connection status, etc.

import psutil

# Get network connection informationconnections = psutil.net_connections(kind='inet')
for conn in connections:
    print(f"Local Address: {} | Remote Address: {} | Status: {}")

Disk monitoring

psutilThe module can also monitor the usage of disks, including disk space, disk IO, etc.

import psutil

# Get disk usagedisk_usage = psutil.disk_usage('/')
print(f"Total Disk Space: {disk_usage.total} bytes | Free Disk Space: {disk_usage.free} bytes")

Exception handling and security

In usepsutilWhen using modules, pay attention to handling possible exceptions to ensure the robustness of the code. At the same time, since this module involves system information, it is necessary to ensure that sufficient permissions are available before use.

import psutil

try:
    # Try to obtain system information    cpu_info = psutil.cpu_percent()
    print(f"CPU Usage: {cpu_info}%")
except  as e:
    print(f"Error: {e}")
except  as e:
    print(f"Error: {e}")

Practical application scenarios

psutilModules have wide applications in actual operation and maintenance scenarios, including automated monitoring system resources, process management, network connection tracking, etc.

Here is a simple example of automated monitoring scripts.

import psutil
import time

#Automated monitoring system resourceswhile True:
    cpu_percent = psutil.cpu_percent(interval=1)
    memory_percent = psutil.virtual_memory().percent
    print(f"CPU Usage: {cpu_percent}% | Memory Usage: {memory_percent}%")
    (5)

Summarize

In the field of operation and maintenance automation,psutilAs a powerful tool for Python, modules provide developers with powerful and convenient means to monitor and manage system resources. Through the in-depth introduction of this article, I shared how to use itpsutilThe module obtains real-time CPU and memory usage, performs process management, network connection monitoring, and tracks disk usage. The application of this module is not only limited to obtaining information, but also realizes automated monitoring scripts for actual operation and maintenance tasks.

Sample code lets you know how to use itpsutilThe module monitors various indicators of the system and demonstrates its flexibility and power in practical application scenarios. At the same time, the importance of exception handling is also emphasized to ensure sufficient permissions and security when obtaining system information.

Overall,psutilIn-depth learning of modules will provide operation and maintenance engineers and developers with rich tools to better understand and manage operating systems. The flexibility and cross-platform support of this module make it an integral part of automation tasks. By usingpsutil, can build an automated monitoring system more efficiently, improve system stability, and bring more convenience to actual operation and maintenance work.

The above is the detailed content of the in-depth investigation of the monitoring and management of the psutil module of Python operation and maintenance automation. For more information about the monitoring and management of the Python psutil module, please pay attention to my other related articles!