shanelord

v1.0 Webhook script for Internet connectivity loss on specific interface

Aug 3rd, 2023 (edited)
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.83 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. WEBHOOK_URLS="YOUR_WEBHOOK_URL"  # Default webhook URL(s) (comma-separated)
  8. ENABLE_RESTORE_NOTIFICATION=true   # Set this to false if you want to disable the notification when connectivity is restored
  9. LOCK_FILE="/tmp/internet_monitor.lock" # Define the lock file path
  10.  
  11. # Function to acquire the lock
  12. acquire_lock() {
  13.     if [[ -e "$LOCK_FILE" ]]; then
  14.         echo "Lock file exists. Script is already running or a previous run didn't finish."
  15.         exit 1
  16.     fi
  17.     touch "$LOCK_FILE"
  18. }
  19.  
  20. # Function to release the lock
  21. release_lock() {
  22.     rm -f "$LOCK_FILE"
  23. }
  24.  
  25. # Function to send a webhook notification
  26. send_webhook_notification() {
  27.     local message="$1"
  28.     local urls=($(echo "$WEBHOOK_URLS" | tr ',' ' '))  # Convert comma-separated URLs to an array
  29.     for url in "${urls[@]}"; do
  30.         curl -X POST -H "Content-Type: application/json" -d "{\"text\":\"$message\"}" "$url"
  31.     done
  32. }
  33.  
  34. # Function to check internet connectivity
  35. check_internet_connectivity() {
  36.     local ip_address="$1"
  37.     if ping -I "$INTERFACE" -c 1 "$ip_address" &> /dev/null; then
  38.         return 0  # Internet connectivity is available
  39.     else
  40.         return 1  # Internet connectivity is not available
  41.     fi
  42. }
  43.  
  44. # Function to check DNS resolution
  45. check_dns_resolution() {
  46.     local dns_address="$1"
  47.     if ping -I "$INTERFACE" -c 1 "$dns_address" &> /dev/null; then
  48.         return 0  # DNS resolution is successful
  49.     else
  50.         return 1  # DNS resolution failed
  51.     fi
  52. }
  53.  
  54. # Main function to monitor internet connectivity and DNS resolution
  55. main() {
  56.     if ! check_internet_connectivity "$PING_IP"; then
  57.         # Internet connectivity is not available, send webhook notification with a specific payload
  58.         message="Internet connectivity is down on $INTERFACE at $(date)"
  59.         send_webhook_notification "$message" "connectivity_down"
  60.     elif ! check_dns_resolution "$DNS_CHECK_ADDRESS"; then
  61.         # DNS resolution failed, send webhook notification
  62.         message="DNS resolution failed on $INTERFACE at $(date)"
  63.         send_webhook_notification "$message" "dns_failure"
  64.     else
  65.         # Internet connectivity and DNS resolution are both successful
  66.         if $ENABLE_RESTORE_NOTIFICATION; then
  67.             # Internet connectivity is restored, send webhook notification with a specific payload
  68.             message="Internet connectivity restored on $INTERFACE at $(date)"
  69.             send_webhook_notification "$message" "connectivity_restored"
  70.         fi
  71.     fi
  72. }
  73.  
  74.  
  75. # Function to display usage information
  76. usage() {
  77.     echo "Usage: $0 [-p ping_ip_address] [-d dns_check_address] [-w webhook_urls]"
  78.     echo "Options:"
  79.     echo "  -p ping_ip_address       Specify the IP address for internet connectivity check (default: 8.8.8.8)"
  80.     echo "  -d dns_check_address     Specify the DNS address for DNS resolution check (default: github.com)"
  81.     echo "  -w webhook_urls          Specify one or multiple webhook URLs as a comma-separated list"
  82.     exit 1
  83. }
  84.  
  85. # Parse command-line options using getopts
  86. while getopts ":p:d:w:" opt; do
  87.     case "$opt" in
  88.         p) PING_IP=$OPTARG ;;
  89.         d) DNS_CHECK_ADDRESS=$OPTARG ;;
  90.         w) WEBHOOK_URLS=$OPTARG ;;
  91.         \?) echo "Invalid option: -$OPTARG" >&2 ; usage ;;
  92.         :) echo "Option -$OPTARG requires an argument." >&2 ; usage ;;
  93.     esac
  94. done
  95.  
  96. # Acquire the lock before starting the main loop
  97. acquire_lock
  98.  
  99. # Run the main loop
  100. while true; do
  101.     main
  102.     sleep 1m  # Adjust the time interval as needed for internet connectivity and DNS resolution checks
  103. done
  104.  
  105. # Release the lock when the script exits
  106. release_lock
  107.  
Add Comment
Please, Sign In to add comment