This script performs a comprehensive health check of your VPS, including CPU usage, memory usage, disk space, and network status. It outputs the results to the terminal and saves a detailed report locally.


#!/bin/bash

# Configuration
TMP_REPORT="/tmp/vps_health_report.txt"

# Function to check CPU usage
check_cpu() {
    echo "CPU Usage:" >> $TMP_REPORT
    mpstat | grep -A 5 "%idle" >> $TMP_REPORT
    echo "" >> $TMP_REPORT
}

# Function to check Memory usage
check_memory() {
    echo "Memory Usage:" >> $TMP_REPORT
    free -h >> $TMP_REPORT
    echo "" >> $TMP_REPORT
}

# Function to check Disk usage
check_disk() {
    echo "Disk Usage:" >> $TMP_REPORT
    df -h | grep -E '^/dev/' >> $TMP_REPORT
    echo "" >> $TMP_REPORT
}

# Function to check Network status
check_network() {
    echo "Network Status:" >> $TMP_REPORT
    ifconfig | grep -E 'inet ' >> $TMP_REPORT
    echo "" >> $TMP_REPORT
}

# Main execution
echo "Starting VPS Health Check..." > $TMP_REPORT
echo "Report generated on: $(date)" >> $TMP_REPORT
echo "" >> $TMP_REPORT

check_cpu
check_memory
check_disk
check_network

echo "Health check completed. Report saved to $TMP_REPORT."
echo "---------------------------------------------"
cat $TMP_REPORT
echo "---------------------------------------------"

# Optionally, add this script to cron for daily health checks:
# 0 6 * * * /path/to/this_script.sh

This script checks critical aspects of your VPS, including CPU, memory, disk, and network status. The results are saved to a report file and also displayed in the terminal, allowing for immediate review and historical tracking.