Overview
A comprehensive health check script that monitors system resources, critical services, and network connectivity. Outputs a formatted report suitable for logging or email.
The Script
#!/bin/bash
# System Health Check Script
# Generates a comprehensive health report
set -euo pipefail
# Configuration
SERVICES=("nginx" "sshd" "docker")
CHECK_URLS=("https://google.com" "https://cloudflare.com")
LOAD_THRESHOLD=4.0
MEM_THRESHOLD=90
DISK_THRESHOLD=85
# Colors for terminal output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Start Report
echo ""
echo "=============================================="
echo " SYSTEM HEALTH CHECK REPORT"
echo " Host: $(hostname)"
echo " Date: $(date '+%Y-%m-%d %H:%M:%S')"
echo "=============================================="
# 1. System Uptime & Load
UPTIME=$(uptime -p)
LOAD=$(cat /proc/loadavg | awk '{print $1}')
echo "Uptime: $UPTIME"
echo "Load Average: $(cat /proc/loadavg | awk '{print $1, $2, $3}')"
# 2. Memory Usage
MEM_TOTAL=$(free -m | awk '/^Mem:/ {print $2}')
MEM_USED=$(free -m | awk '/^Mem:/ {print $3}')
MEM_PERCENT=$((MEM_USED * 100 / MEM_TOTAL))
SWAP_USED=$(free -m | awk '/^Swap:/ {print $3}')
echo "Memory: \${MEM_USED}MB / \${MEM_TOTAL}MB (\${MEM_PERCENT}%)"
echo "Swap Used: \${SWAP_USED}MB"
echo "Health check completed at $(date '+%H:%M:%S')"Usage
# Make executable
chmod +x health-check.sh
# Run manually
./health-check.sh
# Save to file
./health-check.sh > /var/log/health-check-$(date +%Y%m%d).log
# Email report
./health-check.sh | mail -s "Health Report: $(hostname)" [email protected] - health check
- monitoring
- cpu
- memory
- services
- uptime
- load average