Python System Monitoring and Operation and Maintenance Guide: Using the psutil module

Use on Linuxtopfree, you have to open the Task Manager when switching to Windows, and you have to find the Activity Monitor on macOS... If you have ever been tortured by these fragmented system tools, then psutil is your savior. It uses a unified Python interface to put all core information such as CPU, memory, disk, network, and process into your pocket. Its cross-platform compatibility is as high as over 95%. It is a must-have artifact for operation and maintenance engineers and back-end developers.


1. Introduction: What can psutil do?

psutil(process and system utilities) is a lightweight and high-performance Python library specifically used to capture the underlying operating status of the system. It can replace those traditional scriptstopvmstatiostatCommand line tool calls like this can make your code cleaner and more cross-platform.

Covered scenarios include but are not limited to: -CPU usage, number of physical/logical cores, time statistics

  • Detailed usage of physical memory and swap area (Swap)
  • Disk partitions, mount points, disk I/O statistics
  • Network interface status and global traffic statistics
  • Process list, process details (PID, CPU/memory usage, startup time, etc.)
  • Battery status, sensor temperature (supported by specific platforms)

Whether it is to temporarily troubleshoot problems or integrate monitoring into back-end services, it can do the job efficiently.


2. Installation

Install the stable version with one line of command:

pip install psutil

If you have insufficient permissions when installing to global packge on Linux/macOS, addsudo

sudo pip install psutil

After the installation is complete, importpsutilYou are ready to start using it.


3. Quick overview of core functions

The most practical codes are shown below in modules according to the high-frequency needs of operation, maintenance and development.

3.1 CPU Monitoring

Static information: number of cores, cumulative time

import psutil

# 逻辑 CPU 数量(包含超线程)
print(f"逻辑 CPU 数: {psutil.cpu_count()}")

# 物理核心数量
print(f"物理 CPU 核心数: {psutil.cpu_count(logical=False)}")

# CPU 各状态累计耗时(用户态、内核态、空闲等)
print(f"CPU 累计时间统计:\n{psutil.cpu_times()}")

Real-time usage

similartopFor real-time sampling, you can set the sampling interval and subdivide it to each core:

import psutil

# 每 1 秒采样一次,连续 5 次
for i in range(5):
    # percpu=True 获取每个核心的使用率
    core_percents = psutil.cpu_percent(interval=1, percpu=True)
    print(f"第{i+1}次采样 | 各核心: {[f'{p}%' for p in core_percents]}")

    # 总体使用率
    total_percent = psutil.cpu_percent(interval=1)
    print(f"         | 总体: {total_percent}%\n")

Tip:cpu_percent()The first call of will return a meaningless value of 0 because it needs to be compared with the previous one. In actual use, you would use a loop as above and setintervalto get accurate results.


3.2 Memory Monitoring

psutilYou can view physical memory and swap area (Swap) information at the same time. All values ​​are in bytes by default, and we usually convert them to more readable GB.

mem = psutil.virtual_memory()
print("=== 物理内存 ===")
print(f"总量: {mem.total / (1024**3):.2f} GB")
print(f"可用: {mem.available / (1024**3):.2f} GB")
print(f"使用率: {mem.percent}%")

swap = psutil.swap_memory()
print("\n=== 交换区 ===")
print(f"总量: {swap.total / (1024**3):.2f} GB")
print(f"使用率: {swap.percent}%")

availableThis field is very considerate: it indicates the memory that the system thinks is currently available for allocation to the new process, which is better than simplyfreeMore practical.


3.3 Disk monitoring

Partitions and mount points

The following code iterates through all disk partitions, printing mount points, file system types, and usage. Some special file systems (such astmpfssnapetc.) We have done filtering to make the output clearer.

print("=== 磁盘分区 ===")
for part in psutil.disk_partitions():
    # 过滤掉不常用的虚拟文件系统
    if not part.fstype or part.mountpoint.startswith(("/sys", "/snap")):
        continue
    print(f"设备: {part.device}")
    print(f"挂载点: {part.mountpoint}")
    print(f"文件系统: {part.fstype}")

    # 分区使用情况
    usage = psutil.disk_usage(part.mountpoint)
    print(f"已用/总量: {usage.used / (1024**3):.2f}/{usage.total / (1024**3):.2f} GB")
    print(f"使用率: {usage.percent}%")
    print("-" * 40)

Global disk I/O

disk_io = psutil.disk_io_counters()
print("=== 全局磁盘 I/O ===")
print(f"读取次数: {disk_io.read_count}")
print(f"写入次数: {disk_io.write_count}")
print(f"读取总量: {disk_io.read_bytes / (1024**3):.2f} GB")
print(f"写入总量: {disk_io.write_bytes / (1024**3):.2f} GB")

If you need to monitor a specific disk device, you can use the parameterperdisk=TrueGet I/O statistics for each disk.


3.4 Network Monitoring

Global traffic and interface status

# 全局网络 I/O
net_io = psutil.net_io_counters()
print("=== 全局网络 I/O ===")
print(f"已发送: {net_io.bytes_sent / (1024**2):.2f} MB")
print(f"已接收: {net_io.bytes_recv / (1024**2):.2f} MB")

# 所有网络接口
print("\n=== 网络接口状态 ===")
for name, stats in psutil.net_if_stats().items():
    # 跳过回环接口和已禁用的接口
    if name == "lo" or not stats.isup:
        continue
    print(f"接口名: {name}")
    print(f"状态: {'启用' if stats.isup else '禁用'}")
    print(f"MTU: {stats.mtu}")
    print("-" * 30)

In this way, you can quickly locate which network card is working and what the current MTU is, which is very helpful for network troubleshooting.


3.5 Process Management

Process management ispsutilOne of the most powerful parts. You can list all processes, get details about a process, and even kill a process.

First, let’s take a look at the total number of processes in the system and information about the current Python process:

print(f"系统当前进程总数: {len(psutil.pids())}")

current_proc = psutil.Process()
print("\n=== 当前 Python 进程 ===")
print(f"PID: {current_proc.pid}")
print(f"进程名: {current_proc.name()}")
print(f"启动时间: {current_proc.create_time()}")  # 时间戳

In actual operation and maintenance, we often need to find the process with the highest CPU or memory usage. The following function encapsulates a method to safely obtain process information and prints the top 5 CPU killers:

def get_safe_proc_info(pid):
    """安全获取进程信息,捕获常见异常"""
    try:
        p = psutil.Process(pid)
        return {
            "pid": pid,
            "name": p.name(),
            "status": p.status(),
            "cpu_pct": p.cpu_percent(interval=0.1),
            "mem_pct": p.memory_percent()
        }
    except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
        return None

# 获取所有进程信息并排序
top_procs = sorted(
    [get_safe_proc_info(pid) for pid in psutil.pids() if get_safe_proc_info(pid)],
    key=lambda x: x["cpu_pct"],
    reverse=True
)[:5]

print("=== 前 5 个 CPU 占用最高进程 ===")
for proc in top_procs:
    print(f"PID {proc['pid']}\t{proc['name']}\tCPU: {proc['cpu_pct']:.1f}%\tMem: {proc['mem_pct']:.1f}%")

Performance reminder: If you need to traverse a large number of processes, please use it firstpsutil.process_iter(), which is better thanpids() + Process()way performance is over 30% higher.


4. Practical tips and precautions

4.1 Practical tips

  • Efficient process traversal: usepsutil.process_iter()Get all process objects at once to avoid repeated construction.
  • Cross-platform compatibility check: Some functions (such as battery status, temperature sensor) only exist on specific platforms. usehasattr(psutil, "sensors_battery")To determine whether the function is available and avoid the code reporting errors on other systems.
  • Timestamp conversion:create_time()What is returned is the Unix timestamp, paired withdatetime.datetime.fromtimestamp()That is converted to human readable time.
  • Sampling interval for long-term monitoring:cpu_percent(interval=…)orProcess.cpu_percent(interval=…)It is recommended that the interval be set to 1~5 seconds to avoid excessive CPU overhead caused by too short an interval.

4.2 Notes

  1. Permission issues Operations such as viewing other users' processes and obtaining network connection details usually require root or administrator privileges, otherwise it will throwAccessDeniedabnormal.
  2. exception-handling three brothers When processing a process, you must overrideNoSuchProcess(The process has exited),AccessDenied(insufficient permissions) andZombieProcess(Linux zombie process), otherwise the program may crash unexpectedly.
  3. Performance overhead psutilIt is lightweight in itself, but frequent calls to underlying system interfaces still consume CPU. When designing monitoring services, pay attention to controlling the sampling frequency and caching information that does not change frequently.

5. Summary

psutilUsing a set of concise and unified APIs to solve the pain points of cross-platform system monitoring can be said to be the "ceiling" of system tools in the Python ecosystem. Whether you are writing a random script to check the server status, or weaving monitoring indicators into the web service of the production environment, it can help you get twice the result with half the effort.

What is shown above is just the tip of the iceberg. More advanced functions (such as process termination, network connection list, hardware sensor data) can be found in psutil 官方文档. take you fromtopnetstatfreeliberated from the shackles of apip install psutilLet’s get started!