Last active
December 16, 2024 00:14
-
-
Save alexdelprete/b56b901099b2502f9a8a3014f075413f to your computer and use it in GitHub Desktop.
OPNsense Monit HA Webhook Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# source by Shane Lord: https://pastebin.com/wq8gzdjB | |
# thread: https://forum.opnsense.org/index.php?topic=35202 | |
# Default values for options | |
HA_WEBHOOK_URLS=("http://hass.axel.dom:8123/api/webhook/-6-r3IjF2nhN9l44BDBdFJx3X") # Default Home Assistant webhook URL(s) (comma-separated) | |
LOCK_FILE="/tmp/monit_nw_link_ha_webhook.lock" # Define the lock file path | |
# 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 to Home Assistant | |
send_home_assistant_notification() { | |
local message="$1" | |
local eventid="$2" | |
for url in "${HA_WEBHOOK_URLS[@]}"; do | |
curl -X PUT -H "Content-Type: application/json" -d '{ \"message\":\"$message\",\"eventid\":\"$eventid\" }' "$url" | |
done | |
} | |
# Main function to monitor internet connectivity and DNS resolution | |
main() { | |
send_home_assistant_notification "Internet connectivity is down" "link_down" | |
} | |
# Acquire the lock before starting the main loop | |
acquire_lock | |
# Run the main loop | |
main | |
sleep 1s # Adjust the time interval as needed for internet connectivity and DNS resolution checks | |
# Release the lock when the script exits | |
release_lock |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# User configurable variables - modify these before first run | |
# | |
# Home Assistant configuration | |
DEFAULT_HASS_URL="http://your-hass-ip:8123" # URL of your Home Assistant instance | |
DEFAULT_WEBHOOK_ID="opnsense-monit-wan-monitor" # Webhook ID to use in Home Assistant | |
# Network interface configuration | |
DEFAULT_INTERFACE="wan" # WAN interface to monitor | |
# File paths | |
CONFIG_FILE="/usr/local/opnsense/scripts/OPNsense/Monit/hass-webhook.conf" | |
WEBHOOK_SCRIPT="/usr/local/opnsense/scripts/OPNsense/Monit/hass-webhook.sh" | |
MONIT_CONFIG="/usr/local/etc/monit.opnsense.d/wan-hass.conf" | |
# Setup colors if terminal supports it | |
if [ -t 1 ]; then | |
RED=$(tput setaf 1) | |
GREEN=$(tput setaf 2) | |
YELLOW=$(tput setaf 3) | |
BLUE=$(tput setaf 4) | |
BOLD=$(tput bold) | |
RESET=$(tput sgr0) | |
else | |
RED="" | |
GREEN="" | |
YELLOW="" | |
BLUE="" | |
BOLD="" | |
RESET="" | |
fi | |
# Function to create default config | |
create_default_config() { | |
cat > ${CONFIG_FILE} << EOF | |
# Home Assistant configuration | |
HASS_URL="${DEFAULT_HASS_URL}" | |
WEBHOOK_ID="${DEFAULT_WEBHOOK_ID}" | |
# Network interface configuration | |
INTERFACE="${DEFAULT_INTERFACE}" | |
EOF | |
echo "${YELLOW}Created new config file. Please edit ${CONFIG_FILE} with your settings${RESET}" | |
exit 1 | |
} | |
# Function to validate config | |
validate_config() { | |
if [ ! -f "${CONFIG_FILE}" ]; then | |
echo "${YELLOW}Config file not found. Creating default configuration...${RESET}" | |
create_default_config | |
fi | |
# Source the config file | |
. ${CONFIG_FILE} 2>/dev/null | |
# Check each required variable and show proper guidance | |
missing_vars=0 | |
# Check HASS_URL | |
if [ -z "${HASS_URL}" ]; then | |
echo "${RED}Error: HASS_URL is missing in ${CONFIG_FILE}${RESET}" | |
echo "${BLUE}Expected format: ${BOLD}HASS_URL=\"http://your-hass-ip:8123\"${RESET}" | |
echo "Please add this line to your config file" | |
missing_vars=1 | |
else | |
case "${HASS_URL}" in | |
http://*|https://*) | |
;; | |
*) | |
echo "${RED}Error: HASS_URL must start with http:// or https://${RESET}" | |
echo "Current value: ${HASS_URL}" | |
echo "${BLUE}Expected format: ${BOLD}HASS_URL=\"http://your-hass-ip:8123\"${RESET}" | |
missing_vars=1 | |
;; | |
esac | |
fi | |
# Check WEBHOOK_ID | |
if [ -z "${WEBHOOK_ID}" ]; then | |
echo "${RED}Error: WEBHOOK_ID is missing in ${CONFIG_FILE}${RESET}" | |
echo "${BLUE}Expected format: ${BOLD}WEBHOOK_ID=\"opnsense-monit-wan-monitor\"${RESET}" | |
echo "Please add this line to your config file" | |
missing_vars=1 | |
fi | |
# Check INTERFACE | |
if [ -z "${INTERFACE}" ]; then | |
echo "${RED}Error: INTERFACE is missing in ${CONFIG_FILE}${RESET}" | |
echo "${BLUE}Expected format: ${BOLD}INTERFACE=\"wan\"${RESET}" | |
echo "${YELLOW}Available interfaces:${RESET}" | |
ifconfig -l | |
echo "Please add this line to your config file with one of the above interfaces" | |
missing_vars=1 | |
else | |
if ! ifconfig ${INTERFACE} > /dev/null 2>&1; then | |
echo "${YELLOW}Warning: Interface ${INTERFACE} not found in system${RESET}" | |
echo "${YELLOW}Available interfaces:${RESET}" | |
ifconfig -l | |
echo "Please verify your interface setting" | |
fi | |
fi | |
if [ ${missing_vars} -eq 1 ]; then | |
echo "${RED}Please fix the above errors in ${CONFIG_FILE} and run the script again${RESET}" | |
exit 1 | |
fi | |
} | |
# Validate configuration before proceeding | |
validate_config | |
echo "${BLUE}Using configuration from ${CONFIG_FILE}:${RESET}" | |
echo "${BOLD}- Interface:${RESET} ${INTERFACE}" | |
echo "${BOLD}- Home Assistant URL:${RESET} ${HASS_URL}" | |
echo "${BOLD}- Webhook ID:${RESET} ${WEBHOOK_ID}" | |
echo "${YELLOW}Updating scripts and Monit configuration...${RESET}" | |
# Create the webhook script | |
cat > ${WEBHOOK_SCRIPT} << 'EOF' | |
#!/bin/sh | |
. /usr/local/opnsense/scripts/OPNsense/Monit/hass-webhook.conf | |
STATUS="${1}" | |
WEBHOOK_URL="${HASS_URL}/api/webhook/${WEBHOOK_ID}" | |
if [ "${STATUS}" = "down" ]; then | |
MESSAGE="WAN interface ${INTERFACE} is down" | |
else | |
MESSAGE="WAN interface ${INTERFACE} is up" | |
fi | |
curl -X POST -H "Content-Type: application/json" \ | |
-d "{\"status\": \"${STATUS}\", \"interface\": \"${INTERFACE}\", \"event_id\": \"${WEBHOOK_ID}\", \"message\": \"${MESSAGE}\"}" \ | |
${WEBHOOK_URL} | |
EOF | |
chmod +x ${WEBHOOK_SCRIPT} | |
# Create monit configuration using the interface from config | |
cat > ${MONIT_CONFIG} << EOF | |
# Monitor WAN interface | |
check network wan_monitor_down with interface ${INTERFACE} | |
if link down then exec "${WEBHOOK_SCRIPT} down" | |
every 1 cycles | |
check network wan_monitor_up with interface ${INTERFACE} | |
if link up then exec "${WEBHOOK_SCRIPT} up" | |
every 1 cycles | |
EOF | |
echo "${GREEN}Configuration updated successfully:${RESET}" | |
echo "${BOLD}- Updated webhook script${RESET}" | |
echo "${BOLD}- Updated Monit configuration for interface ${INTERFACE}${RESET}" | |
echo "${YELLOW}Don't forget to run 'monit reload' to apply changes${RESET}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment