Back to articles
Linux Project: A Cool System Monitoring Tool

Linux Project: A Cool System Monitoring Tool

Raymond Nwambuonwo / October 8, 2024

Linux Project: A Cool System Monitoring Tool

Table of Contents

  1. What is this tool?
  2. What does it do?
  3. How I made it
  4. How to use it
  5. Understanding the code

What is this tool?

Imagine you have a special pair of glasses that let you see inside your computer. That's kind of what this tool does! It's like a friendly robot that peeks inside your computer and tells you what's going on. And as always, I like explaining things in a digestible way for easier understanding.

What does it do?

Our tool does four main things:

  1. It checks how hard your computer's brain (CPU) is working.
  2. It sees how much of your computer's memory is being used.
  3. It looks at how full your computer's storage is.
  4. It makes a list of all the programs that are running right now.

Then, it shows you all this information and updates it every 5 seconds.

How I made it

To make this tool, I followed these steps:

  1. First, I learned about a special helper called "psutil". It's a component that can easily look inside the computer.
  2. I wrote a set of instructions (referred to as a "script") using a language called Python. Python is like giving instructions to a friendly dragon - it's powerful but easy to talk to!
  3. I told the dragon (Python) to use the superhero (psutil) to look at different parts of the computer.
  4. I made the dragon repeat these steps over and over, so we always have new information.

How to use it

Using this tool is pretty easy! Just follow these steps:

  1. Make sure you have Python on your computer.
  2. Install the helper. Type this in your computer's command center:
    pip install psutil
  3. Save our instructions in a file called system_monitor.py.
  4. Tell the computer it's okay to run our file:
    chmod +x system_monitor.py
  5. Run the file by typing:
    python3 system_monitor.py
  6. Watch as the information appears and updates every 5 seconds!

Understanding the code

Let's look at our instructions and see what each part does:

import psutil
import time

These lines are like telling our dragon to bring two helpers: psutil (our superhero) and time (which helps us wait between updates).

def get_cpu_usage():
    return psutil.cpu_percent(interval=1)

This part asks our superhero to check how hard the computer's brain is working.

def get_memory_usage():
    memory = psutil.virtual_memory()
    return memory.percent

Here, we're asking about the computer's memory - like how many things it's trying to remember at once.

def get_disk_usage():
    disk = psutil.disk_usage('/')
    return disk.percent

This checks how full the computer's storage box is.

def get_running_processes():
    processes = []
    for proc in psutil.process_iter(['pid', 'name', 'status']):
        if proc.info['status'] == psutil.STATUS_RUNNING:
            processes.append(proc.info)
    return processes

This part makes a list of all the programs that are awake and doing things right now.

def main():
    while True:
        print("\n--- System Monitor ---")
        print(f"CPU Usage: {get_cpu_usage()}%")
        print(f"Memory Usage: {get_memory_usage()}%")
        print(f"Disk Usage: {get_disk_usage()}%")

        print("\nRunning Processes:")
        for proc in get_running_processes():
            print(f"PID: {proc['pid']}, Name: {proc['name']}")

        time.sleep(5)  # Wait for 5 seconds before the next update

This is the main part of our tool. It:

  1. Prints a title
  2. Shows how busy the CPU, memory, and storage are
  3. Lists all the running programs
  4. Waits for 5 seconds
  5. Then does it all over again!
if __name__ == "__main__":
    main()

This last part is like saying "Okay, dragon, start doing all these things now!"

The line if __name__ == "__main__": is a common Python idiom. Here's what it means:

  1. __name__ is a special variable that Python creates for each script. It's like a name tag for the script.

  2. When you run a Python script directly, Python sets __name__ to the string "__main__" for that script.

  3. If the script is imported as a module into another script, __name__ is set to the name of the script/module instead.

So, this line is basically asking: "Is this script being run directly (not being imported)?"

  • If yes (i.e., __name__ is "__main__"), then run the main() function.
  • If no (i.e., the script is being imported into another script), don't run main().

This is useful because:

  1. It allows you to write code that can be either run directly or imported as a module.
  2. When you import this script into another script, the main() function won't automatically run.

Here's a simple analogy:

Imagine you have a toy robot. The robot has a special button that makes it dance. The line if __name__ == "__main__": is like asking, "Is someone playing with me directly?" If yes, the robot dances (runs the main() function). If the robot is just sitting on a shelf (being imported but not used directly), it doesn't dance.

In our system monitoring tool, this line ensures that the monitoring only starts when you run the script directly, not if you were to import its functions into another script.

And that's it! Now you know how our cool system monitoring tool works. It's like having X-ray vision for your computer!