🔭 I'm currently working on Hands-on with DevOps tools and shaping cloud solutions on Azure and AWS.
🌱 I'm currently learning Steadily mastering DevOps tools while diving deep into [...]
A comprehensive shell script for monitoring and analyzing the health of Ubuntu virtual machines based on CPU, memory, and disk space utilization.
- Purpose: Monitor VM health with CPU, Memory, and Disk metrics
- Threshold: 70% utilization
- Status: Healthy (≤70%) or Unhealthy (>70%)
- Target OS: Ubuntu 16.04 LTS and later
- ✅ Real-time CPU, Memory, and Disk monitoring
- ✅ Configurable health thresholds (default: 70%)
- ✅ Color-coded output for easy interpretation
- ✅
explainargument for detailed metrics and recommendations - ✅ Exit code support for automation (0=healthy, 1=unhealthy)
- ✅ Cron-friendly for scheduled monitoring
- ✅ Comprehensive error handling with fallback methods
Installation:
# Clone the repository
git clone https://github.com/Affan2310/Affan2310.git
cd Affan2310
# Make script executable
chmod +x vm_health_check.sh
# Run the script
./vm_health_check.shBasic Usage:
# Quick health check
./vm_health_check.sh
# Detailed health check with recommendations
./vm_health_check.sh explainHealthy VM:
✓ VM Health Status: Healthy
Unhealthy VM with Explain:
✗ VM Health Status: Unhealthy
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
VM Health Check Details (Threshold: 70%)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
CPU Utilization: 45% (NORMAL)
Memory Utilization: 82% (CRITICAL - Above threshold)
Disk Utilization: 91% (CRITICAL - Above threshold)
Recommended Actions:
• Memory: Review memory-consuming applications...
• Disk: Clean up unnecessary files...
# Edit crontab
crontab -e
# Run every hour
0 * * * * /path/to/vm_health_check.sh >> /var/log/vm-health.log 2>&1
# Run every 30 minutes with detailed output
*/30 * * * * /path/to/vm_health_check.sh explain >> /var/log/vm-health.log 2>&1| Metric | Method | Threshold | Healthy | Unhealthy |
|---|---|---|---|---|
| CPU Usage | top -bn1 |
≤70% | ✅ | ❌ |
| Memory Usage | /proc/meminfo |
≤70% | ✅ | ❌ |
| Disk Usage | df -h / |
≤70% | ✅ | ❌ |
IF (CPU > 70%) OR (Memory > 70%) OR (Disk > 70%)
THEN Status = "Unhealthy" ⚠️
ELSE
Status = "Healthy" ✓
ENDIF
Permission Denied:
chmod +x vm_health_check.shCommand Not Found (e.g., top):
sudo apt update && sudo apt install -y procpsMemory/Disk Issues:
free -h # Check memory
df -h # Check disk space
du -sh /* # Find large directoriesEmail Alert on Unhealthy VM:
#!/bin/bash
if ! ./vm_health_check.sh; then
echo "VM is unhealthy!" | mail -s "Alert" [email protected]
fiCheck Exit Code:
./vm_health_check.sh
if [ $? -eq 0 ]; then
echo "VM is healthy"
else
echo "VM needs attention"
fi- Execution Time: ~1-1.3 seconds
- CPU Usage: <2%
- Memory Usage: ~5-10 MB
- Recommended Frequency: Every 5-60 minutes
For comprehensive documentation, including:
- Detailed setup and installation
- All command-line arguments
- Complete output examples
- Advanced troubleshooting
- Best practices for monitoring
- Integration with monitoring systems
- Performance tracking
- And much more...
👉 See VM_HEALTH_CHECK_README.md



