Operations 11 min read

Build Python Scripts for Real-Time Linux Server Monitoring

This article explains how to create Python scripts that monitor Linux server CPU, load, memory, and network usage by reading data from the /proc virtual filesystem, providing step‑by‑step code examples and illustrating each script’s output with screenshots.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Build Python Scripts for Real-Time Linux Server Monitoring

Linux administrators can write simple Python scripts to monitor server CPU, memory, network, and load, complementing tools like inotify-sync and glances.

Python version description

Python is a free, high‑level interpreted language developed by Guido van Rossum. The article uses Ubuntu 12.10 with Python 2.7 (CPython). Python 3 has syntax differences, and other implementations such as Jython, IronPython, and PyPy exist.

Working principle: based on /proc filesystem

The /proc virtual filesystem allows interaction with kernel data structures without rebooting, providing information about processes, system status, CPU, load, and memory.

Process information: each process has a directory with files like cmdline, mem, stat, etc.

System information: /proc/stat includes CPU usage, disk space, memory swap, interrupts, etc.

CPU information: /proc/cpuinfo provides detailed processor data.

Load information: /proc/loadavg contains system load averages.

Memory information: /proc/meminfo shows total, free, and swap memory.

/proc directory main files description

Using Python scripts to monitor Linux server

CPU monitoring

Script CPU1.py reads /proc/cpuinfo and returns a list of dictionaries, one per core.

Listing 1. Get CPU information

# Python CPU1.py
Intel(R) Celeron(R) CPU E3200  @ 2.40GHz

Figure 1. Running Listing 1

You can make the script executable with #chmod +x CPU1.py and run it directly:

#chmod +x CPU1.py
# ./CPU1.py

System load monitoring

Script CPU2.py reads /proc/loadavg to obtain load averages, using the os module for cross‑platform compatibility.

Listing 2. Get system load information

Figure 2. Running Listing 2

Memory information retrieval

Script mem.py reads /proc/meminfo, using string methods like split and strip to extract total and free memory.

Listing 3. Get memory usage

Figure 3. Running Listing 3

Network interface monitoring

Script net.py reads /proc/net/dev to report input and output statistics for each network interface, using Python file operations.

Listing 4. Get network interface I/O

Figure 4. Running Listing 4

Python script to monitor Apache server processes

Script crtrl.py checks whether the Apache process has exited unexpectedly and restarts it, using Python's time module and can be added to /etc/rc.local for automatic execution.

Listing 5. crtrl.py

Conclusion

Linux system administrators can write simple, practical Python scripts to monitor CPU, system load, memory, and network usage on Linux servers, tailoring them to the specific characteristics of their environments.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

monitoringPythonLinuxscriptsprocfs
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.