Comprehensive Health Check Script
#!/bin/bash
# /usr/local/bin/health-check.sh
HOSTNAME=$(hostname)
ALERT_EMAIL="admin@example.com"
ISSUES=""
# CPU Load
LOAD=$(awk '{print $1}' /proc/loadavg)
CPUS=$(nproc)
THRESHOLD=$(echo "$CPUS * 2" | bc)
if (( $(echo "$LOAD > $THRESHOLD" | bc -l) )); then
ISSUES+="HIGH LOAD: $LOAD (threshold: $THRESHOLD)\n"
fi
# Memory
MEM_USED=$(free | awk '/Mem:/ {printf "%.0f", $3/$2 * 100}')
if [ "$MEM_USED" -gt 90 ]; then
ISSUES+="HIGH MEMORY: ${MEM_USED}% used\n"
fi
# Disk
DISK_USED=$(df / | tail -1 | awk '{print $5}' | tr -d '%')
if [ "$DISK_USED" -gt 85 ]; then
ISSUES+="HIGH DISK: ${DISK_USED}% used\n"
fi
# Services
for SERVICE in nginx php8.2-fpm mariadb; do
if ! systemctl is-active --quiet "$SERVICE" 2>/dev/null; then
ISSUES+="SERVICE DOWN: $SERVICE\n"
fi
done
# HTTP Check
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost/ 2>/dev/null)
if [ "$HTTP_CODE" != "200" ]; then
ISSUES+="HTTP CHECK FAILED: status $HTTP_CODE\n"
fi
# Send alert if issues found
if [ -n "$ISSUES" ]; then
echo -e "Server: $HOSTNAME\nTime: $(date)\n\n$ISSUES" | mail -s "ALERT: $HOSTNAME health check failed" "$ALERT_EMAIL"
fi
chmod +x /usr/local/bin/health-check.sh
# Run every 5 minutes
echo "*/5 * * * * root /usr/local/bin/health-check.sh" >> /etc/crontab
Customization
- Adjust thresholds for your specific workload
- Add application-specific checks (database connectivity, queue size)
- Send alerts to Slack or Discord via webhook instead of email
- Add SSL certificate expiry check