Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Default values for options
- PING_IP="8.8.8.8" # Default ping IP address (Google DNS)
- DNS_CHECK_ADDRESS="github.com" # Default DNS check address
- INTERFACE="eth0" # Replace with your Ethernet interface name (e.g., eth0)
- DISCORD_WEBHOOK_URLS=("YOUR_DISCORD_WEBHOOK_URL") # Default Discord webhook URL(s) (comma-separated)
- HA_WEBHOOK_URLS=("YOUR_HOME_ASSISTANT_WEBHOOK_URL") # Default Home Assistant webhook URL(s) (comma-separated)
- DISCORD_SEND_NOTIFICATION="" # Option to specify the type of Discord notification to send (connectivity_down, connectivity_restored, dns_failure, dns_restored, empty for all)
- HA_SEND_NOTIFICATION="" # Option to specify the type of Home Assistant notification to send (connectivity_down, connectivity_restored, dns_failure, dns_restored, empty for all)
- ENABLE_RESTORE_NOTIFICATION=true # Set this to false if you want to disable the notification when connectivity is restored
- LOCK_FILE="/tmp/internet_monitor.lock" # Define the lock file path
- PREVIOUS_CONNECTIVITY_STATUS="connected" # Variable to track previous connectivity status
- PREVIOUS_DNS_STATUS="resolved" # Variable to track previous DNS resolution status
- # Function to acquire the lock
- acquire_lock() {
- if [[ -e "$LOCK_FILE" ]]; then
- echo "Lock file exists. Script is already running or a previous run didn't finish."
- exit 1
- fi
- touch "$LOCK_FILE"
- }
- # Function to release the lock
- release_lock() {
- rm -f "$LOCK_FILE"
- }
- # Function to send a webhook notification (Discord)
- send_discord_notification() {
- local message="$1"
- local payload="{\"content\":\"$message\"}"
- for url in "${DISCORD_WEBHOOK_URLS[@]}"; do
- curl -X POST -H "Content-Type: application/json" -d "$payload" "$url"
- done
- }
- # Function to send a webhook notification to Home Assistant
- send_home_assistant_notification() {
- local message="$1"
- local payload="$2"
- for url in "${HA_WEBHOOK_URLS[@]}"; do
- curl -X POST -H "Content-Type: application/json" -d "{\"text\":\"$message\",\"payload\":\"$payload\"}" "$url"
- done
- }
- # Function to check internet connectivity
- check_internet_connectivity() {
- local ip_address="$1"
- if ping -I "$INTERFACE" -c 1 "$ip_address" &> /dev/null; then
- return 0 # Internet connectivity is available
- else
- return 1 # Internet connectivity is not available
- fi
- }
- # Function to check DNS resolution
- check_dns_resolution() {
- local dns_address="$1"
- if ping -I "$INTERFACE" -c 1 "$dns_address" &> /dev/null; then
- return 0 # DNS resolution is successful
- else
- return 1 # DNS resolution failed
- fi
- }
- # Main function to monitor internet connectivity and DNS resolution
- main() {
- if ! check_internet_connectivity "$PING_IP"; then
- # Internet connectivity is not available
- if [[ "$DISCORD_SEND_NOTIFICATION" == "connectivity_down" || -z "$DISCORD_SEND_NOTIFICATION" ]]; then
- # Send Discord webhook notification for internet connectivity down
- message="Internet connectivity is down on $INTERFACE at $(date)"
- send_discord_notification "$message"
- fi
- if [[ "$HA_SEND_NOTIFICATION" == "connectivity_down" || -z "$HA_SEND_NOTIFICATION" || "$HA_NOTIFICATION_ONLY" == true ]]; then
- # Send Home Assistant webhook notification for internet connectivity down
- send_home_assistant_notification "Internet connectivity is down" "connectivity_down"
- fi
- # Update previous connectivity status to "disconnected"
- PREVIOUS_CONNECTIVITY_STATUS="disconnected"
- else
- # Internet connectivity is restored
- if [[ "$PREVIOUS_CONNECTIVITY_STATUS" == "disconnected" && ( "$DISCORD_SEND_NOTIFICATION" == "connectivity_restored" || -z "$DISCORD_SEND_NOTIFICATION" ) ]]; then
- # Send Discord webhook notification for internet connectivity restored
- message="Internet connectivity restored on $INTERFACE at $(date)"
- send_discord_notification "$message"
- fi
- if [[ "$PREVIOUS_CONNECTIVITY_STATUS" == "disconnected" && ( "$HA_SEND_NOTIFICATION" == "connectivity_restored" || -z "$HA_SEND_NOTIFICATION" || "$HA_NOTIFICATION_ONLY" == true ) ]]; then
- # Send Home Assistant webhook notification for internet connectivity restored
- send_home_assistant_notification "Internet connectivity restored" "connectivity_restored"
- fi
- # Update previous connectivity status to "connected"
- PREVIOUS_CONNECTIVITY_STATUS="connected"
- fi
- if ! check_dns_resolution "$DNS_CHECK_ADDRESS"; then
- # DNS resolution failed
- if [[ "$DISCORD_SEND_NOTIFICATION" == "dns_failure" || -z "$DISCORD_SEND_NOTIFICATION" ]]; then
- # Send Discord webhook notification for DNS resolution failed
- message="DNS resolution failed on $INTERFACE at $(date)"
- send_discord_notification "$message"
- fi
- if [[ "$HA_SEND_NOTIFICATION" == "dns_failure" || -z "$HA_SEND_NOTIFICATION" || "$HA_NOTIFICATION_ONLY" == true ]]; then
- # Send Home Assistant webhook notification for DNS resolution failed
- send_home_assistant_notification "DNS resolution failed" "dns_failure"
- fi
- # Update previous DNS resolution status to "failed"
- PREVIOUS_DNS_STATUS="failed"
- else
- # DNS resolution is successful
- if [[ "$PREVIOUS_DNS_STATUS" == "failed" && ( "$DISCORD_SEND_NOTIFICATION" == "dns_restored" || -z "$DISCORD_SEND_NOTIFICATION" ) ]]; then
- # Send Discord webhook notification for DNS resolution restored
- message="DNS resolution restored on $INTERFACE at $(date)"
- send_discord_notification "$message"
- fi
- if [[ "$PREVIOUS_DNS_STATUS" == "failed" && ( "$HA_SEND_NOTIFICATION" == "dns_restored" || -z "$HA_SEND_NOTIFICATION" || "$HA_NOTIFICATION_ONLY" == true ) ]]; then
- # Send Home Assistant webhook notification for DNS resolution restored
- send_home_assistant_notification "DNS resolution restored" "dns_restored"
- fi
- # Update previous DNS resolution status to "resolved"
- PREVIOUS_DNS_STATUS="resolved"
- fi
- }
- # Function to display usage information
- usage() {
- echo "Usage: $0 [-p ping_ip_address] [-d dns_check_address] [-w discord_webhook_urls] [-h ha_webhook_urls] [-n notification_type] [-o]"
- echo "Options:"
- echo " -p ping_ip_address Specify the IP address for internet connectivity check (default: 8.8.8.8)"
- echo " -d dns_check_address Specify the DNS address for DNS resolution check (default: github.com)"
- echo " -w discord_webhook_urls Specify one or multiple Discord webhook URLs as a comma-separated list"
- echo " -h ha_webhook_urls Specify one or multiple Home Assistant webhook URLs as a comma-separated list"
- echo " -n notification_type Specify the type of notification to send (connectivity_down, dns_failure, connectivity_restored)"
- echo " -o Send Home Assistant notification exclusively"
- exit 1
- }
- # Parse command-line options using getopts
- while getopts ":p:d:w:h:n:o" opt; do
- case "$opt" in
- p) PING_IP=$OPTARG ;;
- d) DNS_CHECK_ADDRESS=$OPTARG ;;
- w) IFS=',' read -ra DISCORD_WEBHOOK_URLS <<< "$OPTARG" ;;
- h) IFS=',' read -ra HA_WEBHOOK_URLS <<< "$OPTARG" ;;
- n)
- SEND_NOTIFICATION=$OPTARG
- # Separate the SEND_NOTIFICATION option for Discord and Home Assistant
- DISCORD_SEND_NOTIFICATION=$SEND_NOTIFICATION
- HA_SEND_NOTIFICATION=$SEND_NOTIFICATION
- ;;
- o) HA_NOTIFICATION_ONLY=true ;;
- \?) echo "Invalid option: -$OPTARG" >&2 ; usage ;;
- :) echo "Option -$OPTARG requires an argument." >&2 ; usage ;;
- esac
- done
- # Acquire the lock before starting the main loop
- acquire_lock
- # Run the main loop
- while true; do
- main
- sleep 1m # Adjust the time interval as needed for internet connectivity and DNS resolution checks
- done
- # Release the lock when the script exits
- release_lock
Add Comment
Please, Sign In to add comment