shanelord

v2.1 Webhook script for Internet connectivity loss on specific interface

Aug 4th, 2023 (edited)
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 8.10 KB | Software | 0 0
  1. #!/bin/bash
  2.  
  3. # Default values for options
  4. PING_IP="8.8.8.8"      # Default ping IP address (Google DNS)
  5. DNS_CHECK_ADDRESS="github.com"  # Default DNS check address
  6. INTERFACE="eth0"      # Replace with your Ethernet interface name (e.g., eth0)
  7. DISCORD_WEBHOOK_URLS=("YOUR_DISCORD_WEBHOOK_URL")  # Default Discord webhook URL(s) (comma-separated)
  8. HA_WEBHOOK_URLS=("YOUR_HOME_ASSISTANT_WEBHOOK_URL") # Default Home Assistant webhook URL(s) (comma-separated)
  9. DISCORD_SEND_NOTIFICATION=""  # Option to specify the type of Discord notification to send (connectivity_down, connectivity_restored, dns_failure, dns_restored, empty for all)
  10. 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)
  11. ENABLE_RESTORE_NOTIFICATION=true   # Set this to false if you want to disable the notification when connectivity is restored
  12. LOCK_FILE="/tmp/internet_monitor.lock" # Define the lock file path
  13. PREVIOUS_CONNECTIVITY_STATUS="connected"  # Variable to track previous connectivity status
  14. PREVIOUS_DNS_STATUS="resolved"  # Variable to track previous DNS resolution status
  15.  
  16. # Function to acquire the lock
  17. acquire_lock() {
  18.     if [[ -e "$LOCK_FILE" ]]; then
  19.         echo "Lock file exists. Script is already running or a previous run didn't finish."
  20.         exit 1
  21.     fi
  22.     touch "$LOCK_FILE"
  23. }
  24.  
  25. # Function to release the lock
  26. release_lock() {
  27.     rm -f "$LOCK_FILE"
  28. }
  29.  
  30. # Function to send a webhook notification (Discord)
  31. send_discord_notification() {
  32.     local message="$1"
  33.     local payload="{\"content\":\"$message\"}"
  34.     for url in "${DISCORD_WEBHOOK_URLS[@]}"; do
  35.         curl -X POST -H "Content-Type: application/json" -d "$payload" "$url"
  36.     done
  37. }
  38.  
  39. # Function to send a webhook notification to Home Assistant
  40. send_home_assistant_notification() {
  41.     local message="$1"
  42.     local payload="$2"
  43.     for url in "${HA_WEBHOOK_URLS[@]}"; do
  44.         curl -X POST -H "Content-Type: application/json" -d "{\"text\":\"$message\",\"payload\":\"$payload\"}" "$url"
  45.     done
  46. }
  47.  
  48. # Function to check internet connectivity
  49. check_internet_connectivity() {
  50.     local ip_address="$1"
  51.     if ping -I "$INTERFACE" -c 1 "$ip_address" &> /dev/null; then
  52.         return 0  # Internet connectivity is available
  53.     else
  54.         return 1  # Internet connectivity is not available
  55.     fi
  56. }
  57.  
  58. # Function to check DNS resolution
  59. check_dns_resolution() {
  60.     local dns_address="$1"
  61.     if ping -I "$INTERFACE" -c 1 "$dns_address" &> /dev/null; then
  62.         return 0  # DNS resolution is successful
  63.     else
  64.         return 1  # DNS resolution failed
  65.     fi
  66. }
  67.  
  68. # Main function to monitor internet connectivity and DNS resolution
  69. main() {
  70.     if ! check_internet_connectivity "$PING_IP"; then
  71.         # Internet connectivity is not available
  72.         if [[ "$DISCORD_SEND_NOTIFICATION" == "connectivity_down" || -z "$DISCORD_SEND_NOTIFICATION" ]]; then
  73.             # Send Discord webhook notification for internet connectivity down
  74.             message="Internet connectivity is down on $INTERFACE at $(date)"
  75.             send_discord_notification "$message"
  76.         fi
  77.  
  78.         if [[ "$HA_SEND_NOTIFICATION" == "connectivity_down" || -z "$HA_SEND_NOTIFICATION" || "$HA_NOTIFICATION_ONLY" == true ]]; then
  79.             # Send Home Assistant webhook notification for internet connectivity down
  80.             send_home_assistant_notification "Internet connectivity is down" "connectivity_down"
  81.         fi
  82.  
  83.         # Update previous connectivity status to "disconnected"
  84.         PREVIOUS_CONNECTIVITY_STATUS="disconnected"
  85.     else
  86.         # Internet connectivity is restored
  87.         if [[ "$PREVIOUS_CONNECTIVITY_STATUS" == "disconnected" && ( "$DISCORD_SEND_NOTIFICATION" == "connectivity_restored" || -z "$DISCORD_SEND_NOTIFICATION" ) ]]; then
  88.             # Send Discord webhook notification for internet connectivity restored
  89.             message="Internet connectivity restored on $INTERFACE at $(date)"
  90.             send_discord_notification "$message"
  91.         fi
  92.  
  93.         if [[ "$PREVIOUS_CONNECTIVITY_STATUS" == "disconnected" && ( "$HA_SEND_NOTIFICATION" == "connectivity_restored" || -z "$HA_SEND_NOTIFICATION" || "$HA_NOTIFICATION_ONLY" == true ) ]]; then
  94.             # Send Home Assistant webhook notification for internet connectivity restored
  95.             send_home_assistant_notification "Internet connectivity restored" "connectivity_restored"
  96.         fi
  97.  
  98.         # Update previous connectivity status to "connected"
  99.         PREVIOUS_CONNECTIVITY_STATUS="connected"
  100.     fi
  101.  
  102.     if ! check_dns_resolution "$DNS_CHECK_ADDRESS"; then
  103.         # DNS resolution failed
  104.         if [[ "$DISCORD_SEND_NOTIFICATION" == "dns_failure" || -z "$DISCORD_SEND_NOTIFICATION" ]]; then
  105.             # Send Discord webhook notification for DNS resolution failed
  106.             message="DNS resolution failed on $INTERFACE at $(date)"
  107.             send_discord_notification "$message"
  108.         fi
  109.  
  110.         if [[ "$HA_SEND_NOTIFICATION" == "dns_failure" || -z "$HA_SEND_NOTIFICATION" || "$HA_NOTIFICATION_ONLY" == true ]]; then
  111.             # Send Home Assistant webhook notification for DNS resolution failed
  112.             send_home_assistant_notification "DNS resolution failed" "dns_failure"
  113.         fi
  114.  
  115.         # Update previous DNS resolution status to "failed"
  116.         PREVIOUS_DNS_STATUS="failed"
  117.     else
  118.         # DNS resolution is successful
  119.         if [[ "$PREVIOUS_DNS_STATUS" == "failed" && ( "$DISCORD_SEND_NOTIFICATION" == "dns_restored" || -z "$DISCORD_SEND_NOTIFICATION" ) ]]; then
  120.             # Send Discord webhook notification for DNS resolution restored
  121.             message="DNS resolution restored on $INTERFACE at $(date)"
  122.             send_discord_notification "$message"
  123.         fi
  124.  
  125.         if [[ "$PREVIOUS_DNS_STATUS" == "failed" && ( "$HA_SEND_NOTIFICATION" == "dns_restored" || -z "$HA_SEND_NOTIFICATION" || "$HA_NOTIFICATION_ONLY" == true ) ]]; then
  126.             # Send Home Assistant webhook notification for DNS resolution restored
  127.             send_home_assistant_notification "DNS resolution restored" "dns_restored"
  128.         fi
  129.  
  130.         # Update previous DNS resolution status to "resolved"
  131.         PREVIOUS_DNS_STATUS="resolved"
  132.     fi
  133. }
  134.  
  135. # Function to display usage information
  136. usage() {
  137.     echo "Usage: $0 [-p ping_ip_address] [-d dns_check_address] [-w discord_webhook_urls] [-h ha_webhook_urls] [-n notification_type] [-o]"
  138.     echo "Options:"
  139.     echo "  -p ping_ip_address       Specify the IP address for internet connectivity check (default: 8.8.8.8)"
  140.     echo "  -d dns_check_address     Specify the DNS address for DNS resolution check (default: github.com)"
  141.     echo "  -w discord_webhook_urls  Specify one or multiple Discord webhook URLs as a comma-separated list"
  142.     echo "  -h ha_webhook_urls       Specify one or multiple Home Assistant webhook URLs as a comma-separated list"
  143.     echo "  -n notification_type     Specify the type of notification to send (connectivity_down, dns_failure, connectivity_restored)"
  144.     echo "  -o                       Send Home Assistant notification exclusively"
  145.     exit 1
  146. }
  147.  
  148. # Parse command-line options using getopts
  149. while getopts ":p:d:w:h:n:o" opt; do
  150.     case "$opt" in
  151.         p) PING_IP=$OPTARG ;;
  152.         d) DNS_CHECK_ADDRESS=$OPTARG ;;
  153.         w) IFS=',' read -ra DISCORD_WEBHOOK_URLS <<< "$OPTARG" ;;
  154.         h) IFS=',' read -ra HA_WEBHOOK_URLS <<< "$OPTARG" ;;
  155.         n)
  156.             SEND_NOTIFICATION=$OPTARG
  157.             # Separate the SEND_NOTIFICATION option for Discord and Home Assistant
  158.             DISCORD_SEND_NOTIFICATION=$SEND_NOTIFICATION
  159.             HA_SEND_NOTIFICATION=$SEND_NOTIFICATION
  160.             ;;
  161.         o) HA_NOTIFICATION_ONLY=true ;;
  162.         \?) echo "Invalid option: -$OPTARG" >&2 ; usage ;;
  163.         :) echo "Option -$OPTARG requires an argument." >&2 ; usage ;;
  164.     esac
  165. done
  166.  
  167. # Acquire the lock before starting the main loop
  168. acquire_lock
  169.  
  170. # Run the main loop
  171. while true; do
  172.     main
  173.     sleep 1m  # Adjust the time interval as needed for internet connectivity and DNS resolution checks
  174. done
  175.  
  176. # Release the lock when the script exits
  177. release_lock
Add Comment
Please, Sign In to add comment