Python Script to Monitor and Alert on Disk Usage

Python Script to Monitor and Alert on Disk Usage

Python Script to Monitor and Alert on Disk Usage

 

System administrators and DevOps engineers often need to monitor disk space usage to ensure that servers continue operating smoothly. If disk partitions fill up, applications can crash, databases may become corrupt, and logs might fail to write. In this blog post, we’ll walk through a practical Python script that checks disk usage and sends alert emails when thresholds are breached. This script can be scheduled to run periodically via cron or a task scheduler as part of a robust monitoring setup.

1. Gathering Disk Usage with Python

To start, our script needs to gather disk usage statistics. Python’s shutil module provides a clean way to get the total, used, and free space for a specified path.

import shutil

# Function to check disk usage
def get_disk_usage(mount_point="/"):
    usage = shutil.disk_usage(mount_point)
    percent_used = (usage.used / usage.total) * 100
    return {
        "total": usage.total,
        "used": usage.used,
        "free": usage.free,
        "percent_used": round(percent_used, 2)
    }

In this function, we default to monitoring the root file system /. You can customize it to check other mount points by passing them as arguments.

2. Defining Thresholds and Alerts

Now that we can calculate usage, we should define what level of usage should trigger an alert. A common threshold is 80% or above, but you may customize this depending on your use case.

ALERT_THRESHOLD = 80  # percent

Later in the script, we’ll use this value to determine whether to send an alert email.

3. Sending Email Notifications

We’ll use Python’s built-in smtplib and email libraries to send notifications. In a real-world scenario, you might use a transactional email service (like SendGrid or SES) or use authenticated SMTP credentials.

import smtplib
from email.mime.text import MIMEText

def send_alert_email(subject, body, to_email):
    from_email = "monitor@example.com"
    smtp_server = "smtp.example.com"
    smtp_port = 587
    username = "smtp_user"
    password = "smtp_password"

    msg = MIMEText(body)
    msg["Subject"] = subject
    msg["From"] = from_email
    msg["To"] = to_email

    with smtplib.SMTP(smtp_server, smtp_port) as server:
        server.starttls()
        server.login(username, password)
        server.send_message(msg)
        print("Alert email sent successfully.")

Make sure to replace SMTP configuration values with your actual credentials (use environment variables in production). For security, avoid hardcoding sensitive information.

4. Putting It All Together

Here’s the main function that checks disk usage and sends an alert if the percentage exceeds the defined threshold.

def check_and_alert(mount_point="/", recipient="you@example.com"):
    disk_info = get_disk_usage(mount_point)
    
    print(f"Disk usage at {mount_point}: {disk_info['percent_used']}%")
    
    if disk_info["percent_used"] >= ALERT_THRESHOLD:
        subject = f"[ALERT] Disk usage at {mount_point} is {disk_info['percent_used']}%"
        body = (
            f"Warning: Disk usage exceeds threshold.\n"
            f"Mount point: {mount_point}\n"
            f"Used: {disk_info['used'] / (1024**3):.2f} GB\n"
            f"Free: {disk_info['free'] / (1024**3):.2f} GB\n"
            f"Total: {disk_info['total'] / (1024**3):.2f} GB"
        )
        send_alert_email(subject, body, recipient)

This function logs the current disk usage and only sends an alert email if the threshold is crossed. It’s advised to log output using a logging framework (like logging) in a production environment.

5. Automating with Cron (Linux)

Once you’ve saved the script (e.g., as disk_monitor.py), you can automate it using cron to run at regular intervals.

# Open cron table
crontab -e

# Run the monitor script every 15 minutes
*/15 * * * * /usr/bin/python3 /path/to/disk_monitor.py >/dev/null 2>&1

This line in your crontab will monitor disk usage every 15 minutes and suppress output unless an error occurs. You may add logging if you want persistent records of checks and alerts.

Bonus Tips and Considerations

  • Use logging: Integrate Python’s logging module to record checks and alerts for auditing purposes.
  • Check multiple disks: Loop through a list of mount points and monitor each one individually.
  • Container environments: Map host volumes properly; inside containers, disk usage may display differently.
  • Metrics & dashboards: Feed disk usage data into Prometheus or a similar monitoring system for dashboards and analytics.

With just a few lines of Python and some basic automation, you’ve created a powerful tool for maintaining system health. Monitoring disk usage proactively helps prevent downtime and avoid surprises.

 

Useful links: