Exploring System Monitoring in Python with psutil

In today’s blog post, we’ll delve into the world of system monitoring using Python and the powerful library psutil. As a sysadmin, understanding how your systems are performing is crucial, and psutil provides you with an easy-to-use toolkit for precisely that purpose.

What is psutil?

psutil is a cross-platform library for retrieving system information and managing processes in a Pythonic way. It can help you access various system-related statistics, such as CPU usage, memory consumption, disk activity, and much more. This makes it an invaluable tool for sysadmins and developers alike.

Installing psutil

Before we dive into code examples, let’s start by installing psutil. You can use pip to do this:

Python
pip install psutil

Monitoring CPU Usage

One of the most common tasks for a sysadmin is monitoring CPU usage. With psutil, it’s incredibly easy to retrieve this information:

Python
import psutil

# Get CPU usage
cpu_usage = psutil.cpu_percent(interval=1, percpu=True)
print("CPU Usage: {}%".format(cpu_usage))

In this example, we’re using psutil.cpu_percent() to get the CPU usage as a percentage. The interval parameter specifies how many seconds to wait between consecutive calls.

Monitoring Memory Usage

Memory monitoring is just as important. You can use psutil to check memory usage like this:

Python
import psutil

# Get memory usage
memory = psutil.virtual_memory()
print("Total Memory: {} GB".format(memory.total / (1024 ** 3)))
print("Available Memory: {} GB".format(memory.available / (1024 ** 3)))

The psutil.virtual_memory() function provides detailed information about the system’s virtual memory.

Monitoring Disk Usage

To keep an eye on disk usage, psutil offers a handy method:

Python
import psutil

# Get disk usage
disk = psutil.disk_usage('/')
print("Total Disk Space: {} GB".format(disk.total / (1024 ** 3)))
print("Used Disk Space: {} GB".format(disk.used / (1024 ** 3)))

This code snippet shows how to check the total and used disk space on the root directory (‘/’). You can modify the path as needed.

Monitoring Processes

Lastly, let’s look at how to monitor processes using psutil:

Python
import psutil

# Get a list of running processes
processes = [p.info for p in psutil.process_iter(['pid', 'name', 'cpu_percent'])]

# Print process details
for process in processes:
    print("PID: {} | Name: {} | CPU Usage: {}%".format(process['pid'], process['name'], process['cpu_percent']))

This code retrieves information about running processes, including their PID, name, and CPU usage percentage.

Conclusion

Python and the psutil library provide you with a powerful set of tools to monitor your system effectively. As a sysadmin, these capabilities are invaluable for keeping your systems running smoothly. Whether you need to check CPU, memory, disk usage, or monitor processes, psutil has you covered.

Remember that system monitoring is just one application of psutil; it offers even more features to explore. So, if you’re passionate about system administration, Python, and automation, integrating psutil into your toolkit is a must.

Don’t forget to check out my YouTube channel, TempcoderTech, for video tutorials and practical demonstrations related to sysadmin tasks and Python. You can also explore more on the same topics on my blog at tempcoder.org.

Happy monitoring!

Leave a Comment

Your email address will not be published. Required fields are marked *