You know that feeling when you haven’t checked on a server in ages, and then—boom!—it’s out of disk space? Yeah, I do too. Turns out, servers don’t clean up after themselves (rude), and if you don’t keep an eye on them, they’ll just keep hoarding logs, backups, and who-knows-what until they grind to a halt.
Since I’m not a fan of randomly logging in to check if my servers are breathing, I decided to automate the whole thing. Enter my handy Python script: a disk space monitor that emails me before disaster strikes.
How It Works
The script uses Python’s psutil
library to check disk usage, filters out unimportant partitions (because who cares about /dev/loop
?), and fires off an email if any partition is dangerously full. Oh, and to avoid spammy notifications, it creates a tiny lock file, so it only alerts me once per crisis.
The Magic Behind It
- Threshold-Based Alerts: It only sends a warning when disk usage crosses 90%.
- Exclusions: Filters out
tmpfs
,cdrom
, and other partitions that don’t matter. - Email Notifications: Uses an external SMTP server with authentication, so I get a proper “Hey, fix this!” message in my inbox.
- Lock files: Prevents repetitive emails by making sure it doesn’t alert me every minute for the same issue.
The Script
#!/usr/bin/env python3
import os
import smtplib
import socket
import psutil
from email.mime.text import MIMEText
# Configuration
ALERT = 90 # Alert level (percentage)
ADMIN = "kim@schulz.dk" # Recipient email
SMTP_SERVER = "smtp.example.com" # SMTP server
SMTP_PORT = 587 # SMTP port
SMTP_USER = "your_smtp_username" # SMTP username
SMTP_PASS = "your_smtp_password" # SMTP password
EXCLUDED_PARTITIONS = ("/dev/loop", "udev", "tmpfs", "cdrom", "qemu") # Excluded partitions
# Function to send an email
def send_email(subject, message):
msg = MIMEText(message)
msg["Subject"] = subject
msg["From"] = SMTP_USER
msg["To"] = ADMIN
try:
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
server.starttls()
server.login(SMTP_USER, SMTP_PASS)
server.sendmail(SMTP_USER, ADMIN, msg.as_string())
except Exception as e:
print(f"Failed to send email: {e}")
# Get disk usage information
for partition in psutil.disk_partitions():
if not partition.mountpoint or any(ex in partition.device for ex in EXCLUDED_PARTITIONS):
continue
usage = psutil.disk_usage(partition.mountpoint)
usep = usage.percent # Extract percentage
lockname = partition.device.replace('/', '_') + ".lock"
if usep >= ALERT:
if os.path.exists(lockname):
print(f"Found lockfile for {partition.device} - Skipping email")
else:
with open(lockname, 'w') as lockfile:
lockfile.write('Locked')
hostname = socket.gethostname()
message = f"Running out of space \"{partition.device} ({usep}%)\" on {hostname}"
send_email(f"Alert: Almost out of disk space {usep}%", message)
else:
if os.path.exists(lockname):
os.remove(lockname)
PythonAutomating with a Cron Job
To make this truly hands-off, you can set up a cron job to run the script every 10 minutes. Just add this line to your crontab:
*/10 * * * * /usr/bin/python3 /path/to/disk_space_alert.py
This ensures the script runs automatically, keeping your servers in check without any manual intervention.
Why This Matters
I love automation, and I hate finding out about server problems when it’s already too late (Always when I am out traveling). This script means I get notified before things explode, so I can clean up old logs, expand storage, or at least panic in an orderly fashion.
Feel free to steal this idea, tweak it, and let me know if you make any cool improvements. Cheers to stress-free server maintenance!
PS: yes I know there are lots of monitoring systems, scripts etc, but what’s the fun in that? I had more fun figuring out how to make this in Python instead.