Server Health Check

Creates comprehensive health report by checking server resources, services and critical metrics.

Published: March 10, 2024

Detailed Information

This script checks your server's overall health status and creates a comprehensive HTML report. It checks CPU, memory, disk usage, service statuses, and network connectivity.

What Does This Script Do?

This script performs server health check to:

  • Check CPU usage and show status
  • Analyze memory (RAM) usage
  • Check disk usage
  • Check status of critical services (nginx, apache2, mysql, etc.)
  • Test internet connectivity
  • Check DNS resolution
  • List recent system errors
  • Create visual report in HTML format

Why Should You Use It?

Server health check is critical for proactive system management:

  • Early Warning: Detect problems before they occur
  • Visual Report: Easy-to-read report in HTML format
  • Automation: Regular check with cron job
  • Documentation: Track changes over time

How to Use

Step-by-Step Usage Guide

1. Create Script File

nano health_check.sh

2. Make Executable

chmod +x health_check.sh

3. Run Script

./health_check.sh

4. Open HTML Report

firefox health_report_*.html
# or
xdg-open health_report_*.html

Automation

# Run every 6 hours with crontab
crontab -e
# Add: 0 */6 * * * /path/to/health_check.sh

Requirements

Requirements

  • systemctl: For service control (systemd systems)
  • bc: For mathematical calculations
  • journalctl: For system logs
  • ping, nslookup: For network tests

Use Cases

Use Cases

1. Regular Health Check

Regularly check your server's health status.

2. Troubleshooting

Quickly get status report when experiencing system problems.

3. Performance Monitoring

Track performance changes over time.

Examples

Usage Examples

Example 1: Basic Usage

./health_check.sh

Example 2: Automated Reporting

# Run daily at 02:00
0 2 * * * /path/to/health_check.sh

Code

#!/bin/bash

# Server Health Check Script

REPORT_FILE="health_report_$(date +%Y%m%d_%H%M%S).html"

generate_report() {
    cat > "$REPORT_FILE" << EOF
<!DOCTYPE html>
<html>
<head>
    <title>Server Health Report</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .ok { color: green; }
        .warning { color: orange; }
        .critical { color: red; }
        table { border-collapse: collapse; width: 100%; margin: 20px 0; }
        th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
        th { background-color: #4CAF50; color: white; }
    </style>
</head>
<body>
    <h1>Server Health Report</h1>
    <p><strong>Server:</strong> $(hostname)</p>
    <p><strong>Date:</strong> $(date)</p>
    
    <h2>System Resources</h2>
    <table>
        <tr><th>Metric</th><th>Value</th><th>Status</th></tr>
EOF

    CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk "{print 100 - \$1}")
    CPU_STATUS="ok"
    [ $(echo "$CPU_USAGE > 80" | bc) -eq 1 ] && CPU_STATUS="warning"
    [ $(echo "$CPU_USAGE > 90" | bc) -eq 1 ] && CPU_STATUS="critical"
    
    cat >> "$REPORT_FILE" << EOF
        <tr>
            <td>CPU Usage</td>
            <td>${CPU_USAGE}%</td>
            <td class="$CPU_STATUS">$CPU_STATUS</td>
        </tr>
EOF

    MEM_USAGE=$(free | grep Mem | awk "{print (\$3/\$2) * 100.0}")
    MEM_STATUS="ok"
    [ $(echo "$MEM_USAGE > 80" | bc) -eq 1 ] && MEM_STATUS="warning"
    [ $(echo "$MEM_USAGE > 90" | bc) -eq 1 ] && MEM_STATUS="critical"
    
    cat >> "$REPORT_FILE" << EOF
        <tr>
            <td>Memory Usage</td>
            <td>$(printf "%.2f" $MEM_USAGE)%</td>
            <td class="$MEM_STATUS">$MEM_STATUS</td>
        </tr>
EOF

    DISK_USAGE=$(df -h / | awk "NR==2 {print \$5}" | sed "s/%//")
    DISK_STATUS="ok"
    [ $DISK_USAGE -gt 80 ] && DISK_STATUS="warning"
    [ $DISK_USAGE -gt 90 ] && DISK_STATUS="critical"
    
    cat >> "$REPORT_FILE" << EOF
        <tr>
            <td>Disk Usage (/)</td>
            <td>${DISK_USAGE}%</td>
            <td class="$DISK_STATUS">$DISK_STATUS</td>
        </tr>
    </table>
    
    <h2>Services Status</h2>
    <table>
        <tr><th>Service</th><th>Status</th></tr>
EOF

    SERVICES=("nginx" "apache2" "mysql" "postgresql" "docker" "ssh")
    for service in "${SERVICES[@]}"; do
        if systemctl list-units --all | grep -q "$service.service"; then
            if systemctl is-active --quiet "$service"; then
                STATUS="<span class=\"ok\">Running</span>"
            else
                STATUS="<span class=\"critical\">Stopped</span>"
            fi
            echo "        <tr><td>$service</td><td>$STATUS</td></tr>" >> "$REPORT_FILE"
        fi
    done

    cat >> "$REPORT_FILE" << EOF
    </table>
    
    <h2>Network Connectivity</h2>
    <table>
        <tr><th>Test</th><th>Result</th></tr>
EOF

    if ping -c 1 8.8.8.8 &> /dev/null; then
        echo "        <tr><td>Internet</td><td class=\"ok\">Connected</td></tr>" >> "$REPORT_FILE"
    else
        echo "        <tr><td>Internet</td><td class=\"critical\">Disconnected</td></tr>" >> "$REPORT_FILE"
    fi

    if nslookup google.com &> /dev/null; then
        echo "        <tr><td>DNS Resolution</td><td class=\"ok\">Working</td></tr>" >> "$REPORT_FILE"
    else
        echo "        <tr><td>DNS Resolution</td><td class=\"critical\">Failed</td></tr>" >> "$REPORT_FILE"
    fi

    cat >> "$REPORT_FILE" << EOF
    </table>
    
    <h2>Last System Errors</h2>
    <pre>
$(journalctl -p 3 -n 20 --no-pager 2>/dev/null || echo "No recent errors")
    </pre>
    
</body>
</html>
EOF
}

echo "Running server health check..."
generate_report
echo "✓ Health report generated: $REPORT_FILE"
echo ""
echo "Open with: firefox $REPORT_FILE"

Usage

chmod +x health_check.sh
./health_check.sh

# Open HTML report
firefox health_report_*.html

# Automate with crontab
crontab -e
# Add: 0 */6 * * * /path/to/health_check.sh

Troubleshooting

Troubleshooting

Problem: "bc: command not found"

Solution: Install bc package:

sudo apt-get install bc

Tags

health check monitoring server monitoring system check