Complete Project: Build a Server Health Monitoring Toolkit
Summary: Use all learned skills to build a full CLI project.
Monitoring the health of servers is a crucial task for any system administrator or DevOps engineer. In this comprehensive walkthrough, we will leverage different programming concepts and modules to create a command-line server health monitoring toolkit from scratch. This toolkit will give you real-time insights into system performance metrics and help you spot issues before they escalate.
Table of Contents
- Project Overview
- Requirements
- Project Structure
- Step-by-Step Implementation
- Running the Toolkit
- Final Touches & Packaging
- Conclusion
Project Overview
This project focuses on building a Python-based CLI tool to monitor your server's key health metrics: CPU, memory, disk, and network usage. You'll integrate popular Python libraries and best coding practices to deliver a reliable and extensible solution.
Requirements
To get started, ensure you have the following:
- Python (3.7+)
- Libraries:
psutil
(for hardware & usage metrics)click
(for CLI commands)tabulate
orprettytable
(for neat table outputs)- Optionally:
colorama
(for colored terminal text)
Install them using:
pip install psutil click tabulate colorama
Project Structure
Our simple directory layout:
server_health_toolkit/
├── monitor.py
├── README.md
├── requirements.txt
All core logic lives in monitor.py
.
Step-by-Step Implementation
Setting up the CLI
We'll use click
for our command-line interface.
# monitor.py
import click
import psutil
from tabulate import tabulate
from colorama import Fore, Style, init
init(autoreset=True)
@click.group()
def cli():
"""Server Health Monitoring Toolkit"""
pass
Fetching CPU Usage
Let's add a cpu
command to check usage:
@cli.command()
def cpu():
"""Display CPU usage per core and overall."""
cpu_percent = psutil.cpu_percent(interval=1, percpu=True)
overall = psutil.cpu_percent(interval=1)
click.echo(Fore.YELLOW + "\nCPU Usage:")
table = [
["Core " + str(i+1), f"{v}%"] for i, v in enumerate(cpu_percent)
]
table.append(["Overall", f"{overall}%"])
click.echo(tabulate(table, headers=["Core", "Usage"], tablefmt="pretty"))
Checking Memory Usage
Now, let's check memory stats:
@cli.command()
def memory():
"""Display system memory usage."""
mem = psutil.virtual_memory()
swap = psutil.swap_memory()
click.echo(Fore.CYAN + "\nMemory Usage:")
table = [
["Total", f"{mem.total // (1024**2)} MB"],
["Available", f"{mem.available // (1024**2)} MB"],
["Used", f"{mem.used // (1024**2)} MB ({mem.percent}%)"],
["Swap Used", f"{swap.used // (1024**2)} MB ({swap.percent}%)"],
]
click.echo(tabulate(table, headers=["Type", "Usage"], tablefmt="pretty"))
Monitoring Disk Space
Let's assess all partitions:
@cli.command()
def disk():
"""Display disk usage for all partitions."""
click.echo(Fore.GREEN + "\nDisk Usage:")
table = []
for p in psutil.disk_partitions():
usage = psutil.disk_usage(p.mountpoint)
table.append([
p.device,
p.mountpoint,
f"{usage.used // (1024**3)} GB / {usage.total // (1024**3)} GB",
f"{usage.percent}%",
])
click.echo(tabulate(table, headers=["Device", "Mountpoint", "Used/Total", "Percent"], tablefmt="pretty"))
Tracking Network Activity
We'll display sent/received bytes and packets:
@cli.command()
def network():
"""Display network I/O statistics."""
net = psutil.net_io_counters()
table = [
["Bytes Sent", f"{net.bytes_sent // (1024**2)} MB"],
["Bytes Received", f"{net.bytes_recv // (1024**2)} MB"],
["Packets Sent", net.packets_sent],
["Packets Received", net.packets_recv],
]
click.echo(Fore.MAGENTA + "\nNetwork Usage:")
click.echo(tabulate(table, headers=["Metric", "Value"], tablefmt="pretty"))
Configurable Alerts
Add simple alerting for high resource usage:
@cli.command()
@click.option("--cpu-threshold", default=90, help="CPU usage percent alert threshold.")
@click.option("--mem-threshold", default=90, help="Memory percent alert threshold.")
@click.option("--disk-threshold", default=90, help="Disk percent alert threshold.")
def alerts(cpu_threshold, mem_threshold, disk_threshold):
"""Check key metrics and alert if usage exceeds thresholds."""
# CPU
cpu_percent = psutil.cpu_percent(interval=1)
if cpu_percent > cpu_threshold:
click.echo(Fore.RED + f"High CPU usage: {cpu_percent}% (> {cpu_threshold}%)")
else:
click.echo(f"CPU usage OK: {cpu_percent}%")
# Memory
mem = psutil.virtual_memory()
if mem.percent > mem_threshold:
click.echo(Fore.RED + f"High Memory usage: {mem.percent}% (> {mem_threshold}%)")
else:
click.echo(f"Memory usage OK: {mem.percent}%")
# Disk
flagged = False
for p in psutil.disk_partitions():
usage = psutil.disk_usage(p.mountpoint)
if usage.percent > disk_threshold:
click.echo(Fore.RED + f"High Disk Usage on {p.device}: {usage.percent}% (> {disk_threshold}%)")
flagged = True
if not flagged:
click.echo("Disk usages OK.")
Displaying Results
You can run each component individually, or add a summary
to show everything:
@cli.command()
def summary():
"""Display summary of all system metrics."""
click.echo(Style.BRIGHT + Fore.WHITE + "==== Server Health Summary ====")
cpu()
memory()
disk()
network()
Running the Toolkit
To use your toolkit, execute commands like:
python monitor.py cpu
python monitor.py memory
python monitor.py disk
python monitor.py network
python monitor.py alerts
python monitor.py summary
You can tweak thresholds and use the toolkit in shell scripts or as part of a cron job.
Final Touches & Packaging
-
Add a
requirements.txt
:psutil click tabulate colorama
-
Update your README with instructions and examples.
-
Optionally, use
setuptools
to make it pip-installable.
Conclusion
Building your own server health monitoring CLI toolkit is a fantastic way to combine modular Python programming, third-party libraries, and real-world systems knowledge. This project can be easily extended: add logging, remote monitoring, email alerts, or a web dashboard.
Give it a try, and keep your servers running optimally—one metric at a time!
Happy monitoring! 🚀