Server Sağlamlıq Yoxlaması

Server resurslarını, xidmətləri və kritik metrikaları yoxlayaraq hərtərəfli sağlamlıq hesabatı yaradan script.

Yayımlanma: 10.03.2024

Kod

#!/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"

İstifadə

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

Teqlər

health check monitoring server monitoring system check