Monitor Disk Usage with Python Script

🚀 Monitor Disk Usage with Python (DevOps Practical Tip) As a DevOps Engineer, keeping an eye on disk usage is critical to avoid unexpected outages in production. Here’s a simple Python script I use to check disk usage of the root filesystem: import subprocess result = subprocess.run(["df", "-h", "/"], capture_output=True, text=True) print(result.stdout) 🔍 What this does: Executes df -h / to fetch disk usage Displays output in a human-readable format 💡 Real-world use case: You can extend this script to trigger alerts when disk usage crosses a threshold (say 80%): for line in result.stdout.split("\n"): if "/" in line: usage_percent = int(line.split()[4].replace("%", "")) if usage_percent > 80: print("⚠️ Alert: Disk usage is above 80%") Output: ubuntu@satheesha:~/python$ python3 check-disk_usage.py Filesystem Size Used Avail Use% Mounted on /dev/sdd 1007G 28G 928G 3% / 🔥 Where this helps: Production server monitoring Preventing downtime due to full disk Automating health checks via cron jobs Integrating with alerting tools (Slack, Email, etc.) Simple scripts like this can save hours of troubleshooting and keep systems stable. #DevOps #Python #Automation #Monitoring #Linux #SRE

To view or add a comment, sign in

Explore content categories