Skip to content

Instantly share code, notes, and snippets.

@alexdelprete
Last active December 16, 2024 00:14
Show Gist options
  • Save alexdelprete/b56b901099b2502f9a8a3014f075413f to your computer and use it in GitHub Desktop.
Save alexdelprete/b56b901099b2502f9a8a3014f075413f to your computer and use it in GitHub Desktop.
OPNsense Monit HA Webhook Script
#!/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