#!/bin/bash # System Health Monitoring Script # Generated by Axiom - Autonomous System Administrator TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S') REPORT_FILE="/opt/axiom/logs/health-$(date '+%Y-%m-%d').log" DATA_DIR="/opt/axiom/data" # Create log header echo "=== System Health Report ===" >> "$REPORT_FILE" echo "Timestamp: $TIMESTAMP" >> "$REPORT_FILE" echo "" >> "$REPORT_FILE" # CPU Information echo "--- CPU Usage ---" >> "$REPORT_FILE" CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}') echo "CPU Usage: ${CPU_USAGE}%" >> "$REPORT_FILE" LOAD_AVG=$(uptime | awk -F'load average:' '{print $2}') echo "Load Average:${LOAD_AVG}" >> "$REPORT_FILE" echo "" >> "$REPORT_FILE" # Memory Information echo "--- Memory Usage ---" >> "$REPORT_FILE" free -h >> "$REPORT_FILE" MEM_PERCENT=$(free | grep Mem | awk '{printf("%.1f"), $3/$2 * 100.0}') echo "Memory Usage: ${MEM_PERCENT}%" >> "$REPORT_FILE" echo "" >> "$REPORT_FILE" # Disk Information echo "--- Disk Usage ---" >> "$REPORT_FILE" df -h / /boot >> "$REPORT_FILE" DISK_PERCENT=$(df / | tail -1 | awk '{print $5}' | sed 's/%//') echo "" >> "$REPORT_FILE" # Network Statistics echo "--- Network Statistics ---" >> "$REPORT_FILE" ss -s >> "$REPORT_FILE" echo "" >> "$REPORT_FILE" # Active Services echo "--- Critical Services Status ---" >> "$REPORT_FILE" for service in nginx sshd firewalld chronyd; do STATUS=$(systemctl is-active $service) echo "$service: $STATUS" >> "$REPORT_FILE" done echo "" >> "$REPORT_FILE" # Failed Services echo "--- Failed Services ---" >> "$REPORT_FILE" FAILED=$(systemctl list-units --state=failed --no-pager --no-legend | wc -l) if [ "$FAILED" -gt 0 ]; then systemctl list-units --state=failed --no-pager >> "$REPORT_FILE" else echo "No failed services" >> "$REPORT_FILE" fi echo "" >> "$REPORT_FILE" # Recent Authentication Attempts echo "--- Recent Auth Attempts (Last 10) ---" >> "$REPORT_FILE" journalctl _SYSTEMD_UNIT=sshd.service -n 10 --no-pager | grep -i "accepted\|failed" >> "$REPORT_FILE" 2>&1 || echo "No recent auth events" >> "$REPORT_FILE" echo "" >> "$REPORT_FILE" # Security: Check for security updates echo "--- Security Updates ---" >> "$REPORT_FILE" dnf check-update --security -q | tail -10 >> "$REPORT_FILE" 2>&1 || echo "System up to date" >> "$REPORT_FILE" echo "" >> "$REPORT_FILE" # Alert thresholds ALERTS="" if (( $(echo "$CPU_USAGE > 80" | bc -l) )); then ALERTS="${ALERTS}[ALERT] High CPU usage: ${CPU_USAGE}%\n" fi if (( $(echo "$MEM_PERCENT > 85" | bc -l) )); then ALERTS="${ALERTS}[ALERT] High memory usage: ${MEM_PERCENT}%\n" fi if [ "$DISK_PERCENT" -gt 85 ]; then ALERTS="${ALERTS}[ALERT] High disk usage: ${DISK_PERCENT}%\n" fi if [ "$FAILED" -gt 0 ]; then ALERTS="${ALERTS}[ALERT] Failed services detected: ${FAILED}\n" fi # Write alerts to separate file if any if [ -n "$ALERTS" ]; then echo "=== SYSTEM ALERTS ===" >> "$REPORT_FILE" echo -e "$ALERTS" >> "$REPORT_FILE" echo -e "$ALERTS" > "$DATA_DIR/latest-alerts.txt" else echo "=== No alerts - System healthy ===" >> "$REPORT_FILE" echo "No alerts" > "$DATA_DIR/latest-alerts.txt" fi echo "========================================" >> "$REPORT_FILE" echo "" >> "$REPORT_FILE" # Keep summary for quick reference cat > "$DATA_DIR/latest-health.json" <