AW Dev Rethought

Programs must be written for people to read, and only incidentally for machines to execute - Harold Abelson

🧩 Python Automation Recipes – 🖥️ System Health Alert


Description:

📌 Introduction

High CPU usage. Memory spikes. Disk pressure.

These issues often start small — and go unnoticed until something crashes.

A simple health monitoring script can detect abnormal resource usage early and trigger alerts.

This automation recipe shows how to monitor CPU and RAM usage and send alerts when thresholds are exceeded, using Python.


🔎 Explanation

  • The script uses psutil to read system metrics:
    • CPU usage percentage
    • Memory usage percentage
  • Thresholds are defined for CPU and RAM.
  • If usage exceeds the configured limit:
    • an alert is logged
    • (optionally) an email alert is sent
  • The script runs continuously at fixed intervals.

This is a common pattern in:

  • production monitoring
  • personal servers
  • automation pipelines
  • DevOps tooling

✅ Key Takeaways

  • 🖥️ Monitor system resource usage automatically.
  • 🚨 Detect and react to threshold breaches.
  • ⚙️ Easy to extend with Slack, email, or logging systems.

Code Snippet:

import psutil
import time
from datetime import datetime
from pathlib import Path

# --- Step 1: Configuration ---

CPU_THRESHOLD = 80        # Alert if CPU usage > 80%
RAM_THRESHOLD = 80        # Alert if RAM usage > 80%
CHECK_INTERVAL = 10       # Seconds between checks

LOG_FILE = Path("system_health_log.txt")

print("🖥️ System Health Monitor Started...\n")

# --- Step 2: Monitoring loop ---
while True:

    cpu_usage = psutil.cpu_percent(interval=1)
    memory_usage = psutil.virtual_memory().percent

    timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

    if cpu_usage > CPU_THRESHOLD or memory_usage > RAM_THRESHOLD:

        alert_message = (
            f"[{timestamp}] 🚨 ALERT! "
            f"CPU: {cpu_usage}% | RAM: {memory_usage}%"
        )

        print(alert_message)

        # Log alert
        with LOG_FILE.open("a") as f:
            f.write(alert_message + "\n")

        # 👉 Extend here to send email or Slack alert

    else:
        print(f"[{timestamp}] ✅ CPU: {cpu_usage}% | RAM: {memory_usage}%")

    time.sleep(CHECK_INTERVAL)

Link copied!

Comments

Add Your Comment

Comment Added!