Changed folder structure; added authelia config files
This commit is contained in:
86
homelab/vps/wg_monitor.sh
Normal file
86
homelab/vps/wg_monitor.sh
Normal file
@ -0,0 +1,86 @@
|
||||
#!/bin/bash
|
||||
|
||||
# IP address to ping
|
||||
IP="10.10.10.6"
|
||||
|
||||
# Number of ping attempts
|
||||
COUNT=5
|
||||
|
||||
# Log file
|
||||
LOG_FILE="/var/log/wg_monitor.log"
|
||||
|
||||
# Function to log messages with timestamp
|
||||
log_message() {
|
||||
# Create log file if it doesn't exist and set proper permissions
|
||||
if [ ! -f "$LOG_FILE" ]; then
|
||||
touch "$LOG_FILE"
|
||||
chmod 644 "$LOG_FILE"
|
||||
fi
|
||||
local message="$(date '+%Y-%m-%d %H:%M:%S') - $1"
|
||||
echo "$message" >> "$LOG_FILE"
|
||||
echo "$message" # Also print to console for debugging
|
||||
}
|
||||
|
||||
# Always log script start
|
||||
log_message "Script started - pinging $IP"
|
||||
|
||||
# Ping the IP address
|
||||
echo "Executing ping command..."
|
||||
ping_result=$(ping -c $COUNT -W 5 $IP 2>&1)
|
||||
exit_code=$?
|
||||
|
||||
echo "Ping exit code: $exit_code"
|
||||
echo "Ping result:"
|
||||
echo "$ping_result"
|
||||
|
||||
# Check if ping failed completely
|
||||
if [ $exit_code -eq 1 ]; then
|
||||
log_message "Ping failed with exit code 1"
|
||||
|
||||
# Extract packet loss percentage
|
||||
packet_loss=$(echo "$ping_result" | grep -o '[0-9]*% packet loss' | grep -o '[0-9]*')
|
||||
|
||||
echo "Packet loss: $packet_loss%"
|
||||
log_message "Packet loss detected: $packet_loss%"
|
||||
|
||||
# If 100% packet loss, restart WireGuard
|
||||
if [ "$packet_loss" = "100" ]; then
|
||||
log_message "100% packet loss detected for $IP. Restarting WireGuard interface wg0..."
|
||||
|
||||
# Stop WireGuard interface
|
||||
echo "Stopping WireGuard..."
|
||||
/usr/bin/wg-quick down wg0
|
||||
down_result=$?
|
||||
|
||||
if [ $down_result -eq 0 ]; then
|
||||
log_message "WireGuard interface wg0 stopped successfully"
|
||||
else
|
||||
log_message "Failed to stop WireGuard interface wg0 (exit code: $down_result)"
|
||||
fi
|
||||
|
||||
sleep 2
|
||||
|
||||
# Start WireGuard interface
|
||||
echo "Starting WireGuard..."
|
||||
/usr/bin/wg-quick up wg0
|
||||
up_result=$?
|
||||
|
||||
if [ $up_result -eq 0 ]; then
|
||||
log_message "WireGuard interface wg0 started successfully"
|
||||
else
|
||||
log_message "Failed to start WireGuard interface wg0 (exit code: $up_result)"
|
||||
fi
|
||||
else
|
||||
log_message "Ping failed but not 100% packet loss ($packet_loss%)"
|
||||
fi
|
||||
elif [ $exit_code -eq 0 ]; then
|
||||
# Ping successful
|
||||
log_message "Ping to $IP successful"
|
||||
echo "Ping successful"
|
||||
else
|
||||
log_message "Ping command failed with exit code $exit_code"
|
||||
echo "Ping failed with exit code $exit_code"
|
||||
fi
|
||||
|
||||
log_message "Script completed"
|
||||
echo "Script completed"
|
||||
Reference in New Issue
Block a user