Python System Monitoring Script for DevOps

🚀 Python for DevOps: Real-Time System Monitoring Script (CPU + Memory + Disk) One of the most practical skills in DevOps is automating system monitoring. Instead of manually checking servers, I built a simple Python script that: ✅ Monitors CPU usage ✅ Tracks Memory consumption ✅ Checks Disk utilization ✅ Triggers alerts when thresholds are exceeded 💻 Full Script import psutil import shutil # Thresholds CPU_THRESHOLD = 80 MEM_THRESHOLD = 80 DISK_THRESHOLD = 80 def check_cpu(): cpu = psutil.cpu_percent(interval=1) if cpu > CPU_THRESHOLD: print(f"ALERT: High CPU usage: {cpu}%") else: print(f"OK: CPU usage: {cpu}%") def check_memory(): mem = psutil.virtual_memory() usage = mem.percent if usage > MEM_THRESHOLD: print(f"ALERT: High Memory usage: {usage}%") else: print(f"OK: Memory usage: {usage}%") def check_disk(): disk = shutil.disk_usage("/") usage = (disk.used / disk.total) * 100 if usage > DISK_THRESHOLD: print(f"ALERT: High Disk usage: {usage:.2f}%") else: print(f"OK: Disk usage: {usage:.2f}%") def main(): print("===== System Monitoring =====") check_cpu() check_memory() check_disk() if __name__ == "__main__": main() Output: ubuntu@satheesha:~/python$ python3 full-monitor-script.py ===== System Monitoring ===== OK: CPU usage: 1.0% OK: Memory usage: 30.5% OK: Disk usage: 2.74% ⚙️ How I Used It Installed dependency using: sudo apt install python3-psutil Ran the script to get real-time system health Can be scheduled using cron for continuous monitoring 🔥 Why This Matters in DevOps 👉 Helps detect issues before outages 👉 Reduces manual effort 👉 Can be extended to send alerts (Email / Slack / SNS) 👉 Foundation for tools like monitoring agents 🎯 Key Learning "Don’t just run commands like top or df -h — automate them using Python and build intelligent monitoring." 🚀 Next Steps I’m planning to: Integrate this with Jenkins pipeline Send alerts to Slack Push metrics to monitoring tools 💬 How do you monitor your servers in real-time? #DevOps #Python #Automation #Monitoring #SRE #Cloud #Linux #Jenkins #Learning #100DaysOfCode

To view or add a comment, sign in

Explore content categories