Python DevOps Automation with subprocess for Disk Monitoring

🚀 Python for DevOps – Automating Disk Monitoring with subprocess As part of my DevOps learning, I worked on automating system monitoring using Python’s subprocess module. Instead of manually checking disk usage, I built a simple script to monitor it and trigger alerts. 🔧 Here’s the code: import subprocess Run disk usage command result = subprocess.run(["df", "-h", "/"], capture_output=True, text=True) Print full output print(result.stdout) Parse and check usage lines = result.stdout.split("\n") for line in lines[1:]: if line: usage = line.split()[4] # Extract usage % print(f"Disk Usage: {usage}") if int(usage.replace("%","")) > 80: print("⚠️ ALERT: Disk usage is above 80%!") Output: ubuntu@satheesha:~/python$ python3 import-subprocess.py Disk Usage: 3% 💡 Key Learnings: ✔️ subprocess helps automate Linux commands using Python ✔️ Useful for real-time monitoring and automation ✔️ Can be extended to trigger alerts, emails, or restart services 🚀 Real DevOps Use Cases: System health monitoring Auto-alerting when resources are high Integrating with cron jobs Automating routine checks Small automation like this can save a lot of manual effort in production environments. #Python #DevOps #Automation #Linux #Scripting #Cloud #Learning #Monitoring

To view or add a comment, sign in

Explore content categories