#!/bin/bash

# List of servers (replace these with your actual server hostnames or IPs)
servers=("thevault.sshjunkie.com" "zcluster.technodrome1.sshjunkie.com" "zcluster.technodrome2.sshjunkie.com" "shredder.sshjunkie.com" "root@chatwithus.live")  # Add as many servers as needed

# Define log file
log_file="/home/doc/system_health_report_$(date +%F).log"

# Telegram Bot API Token and Chat ID
BOT_TOKEN="8178867489:AAH0VjN7VnZSCIWasSz_y97iBLLjPJA751k"
CHAT_ID="1559582356"
TELEGRAM_API="https://api.telegram.org/bot$BOT_TOKEN/sendMessage"

# Function to escape Markdown special characters
escape_markdown() {
    echo "$1" | sed 's/\([_*[]\)/\\\1/g'  # Escape _, *, [, and ]
}

# Function to send the report to Telegram
send_to_telegram() {
    local message=$1
    local chunk_size=4096
    local start=0
    local end=$chunk_size

    while [ ${#message} -gt $start ]; do
        chunk="${message:$start:$chunk_size}"
        curl -s -X POST $TELEGRAM_API \
            -d chat_id=$CHAT_ID \
            -d text="$chunk" \
            -d parse_mode="Markdown"
        start=$end
        end=$((start + chunk_size))
    done
}

# Function to run the checks on each server
check_health() {
    server=$1
    server_report="=== Health Check for $server ===\n"

    # Memory Usage (summary)
    memory_output=$(ssh $server "free -h")
    free_memory=$(echo "$memory_output" | awk 'NR==2 {print $4}')
    if [[ "$free_memory" < "1.0Gi" ]]; then
        server_report+="Memory: Low memory available! Only $free_memory free, consider adding more RAM.\n"
    else
        server_report+="Memory: Sufficient memory available ($free_memory free).\n"
    fi

    # Swap Usage (summary)
    swap_output=$(ssh $server "swapon -s")
    swap_used=$(echo "$swap_output" | awk '{if(NR>1) print $3}')
    if [[ "$swap_used" > "1Gi" ]]; then
        server_report+="Swap: High swap usage ($swap_used used). This may indicate memory pressure.\n"
    else
        server_report+="Swap: Minimal swap usage ($swap_used used).\n"
    fi

    # CPU Load (summary)
    cpu_output=$(ssh $server "uptime")
    load_avg=$(echo "$cpu_output" | awk '{print $10}' | sed 's/,//')
    if (( $(echo "$load_avg > 2.0" | bc -l) )); then
        server_report+="CPU Load: High load average ($load_avg). Check if any processes are consuming too much CPU.\n"
    else
        server_report+="CPU Load: Normal load average ($load_avg).\n"
    fi

    # Disk Space (summary)
    disk_output=$(ssh $server "df -h")
    disk_free=$(echo "$disk_output" | grep -v 'tmpfs' | grep -v 'Filesystem' | awk '{print $4}' | sed 's/[A-Za-z]*//g' | head -n 1)
    if [[ "$disk_free" < "10G" ]]; then
        server_report+="Disk Space: Low disk space! Only $disk_free free. Consider cleaning up or adding storage.\n"
    else
        server_report+="Disk Space: Sufficient disk space ($disk_free free).\n"
    fi

    # Service Check (example: check if Apache is running)
    apache_status=$(ssh $server "systemctl is-active apache2")
    if [[ "$apache_status" == "active" ]]; then
        server_report+="Apache: Running normally.\n"
    else
        server_report+="Apache: Not running. Check service logs for issues.\n"
    fi

    # Additional Checks (summary: disk I/O, uptime)
    iostat_output=$(ssh $server "iostat | head -n 2")  # Get summary of disk I/O
    uptime_output=$(ssh $server "uptime")
    server_report+="Disk I/O: $iostat_output\n"
    server_report+="Uptime: $uptime_output\n"

    # Escape Markdown special characters
    escaped_report=$(escape_markdown "$server_report")

    # Send the server-specific summary to Telegram
    send_to_telegram "$escaped_report"

    # Separator for readability in log file
    echo -e "\n=====================\n" >> $log_file
}

# Main loop to go through each server
for server in "${servers[@]}"; do
    check_health $server
done

echo "Health check completed. Reports sent to Telegram and saved to $log_file."
