|
#!/bin/sh |
|
# Script for OPNsense to monitor WAN facing connection |
|
# - When failing: |
|
# 1. down and up the interface, check again |
|
# 2. Try interface reconfiguration, check again |
|
# 3. When still failing, reboot OPNsense |
|
|
|
# The second part of this script will also install |
|
# an action on opnsense and copy the script to a system |
|
# location. |
|
# |
|
# Therefore, to install this script: |
|
# a. Copy it to your OPNsense instance. |
|
# b. Execute it once interactively (copies script, and adds action) |
|
# |
|
# Using the OPNsense UI, this action can be enabled as a cron |
|
# job. Cron jobs are added under System>Settings>Cron. |
|
# Add an entry that you: |
|
# 1. Enable |
|
# 2. Set minutes to "*/5" |
|
# 3. Select "ping_check" as the command |
|
# 4. Set a description such as "Ping check and recover connection" |
|
# 5. Click save. |
|
# |
|
# Based on a script that was likely adapted from |
|
# http://blog.martinshouse.com/2014/06/pfsense-auto-reboot-if-internet.html |
|
# |
|
|
|
# Configuration parameters |
|
|
|
# First IP to ping to check if connection is up |
|
IP1=8.8.8.8 # Google DNS Server 1 |
|
# Second IP to ping to check if connection is up |
|
IP2=8.8.4.4 # Google DNS Server 2 |
|
# Interface to down and up |
|
INTERFACE=igb0 |
|
# Minimum uptime |
|
MIN_UPTIME=120 |
|
|
|
# ENABLE_LOGGING=true |
|
|
|
TARGET_ACTION=/usr/local/opnsense/service/conf/actions.d/actions_ping_check.conf |
|
TARGET_LOCATION=/usr/local/sbin/ping_check.sh |
|
|
|
# Logging function |
|
log_message() { |
|
if [ "$ENABLE_LOGGING" = "true" ]; then |
|
echo "$(date +%Y-%m-%d.%H:%M:%S) - $1" >> /var/log/ping_check.log |
|
fi |
|
} |
|
|
|
# Function to ping both IP addresses |
|
ping_ips() { |
|
count=$1 |
|
ip1_count=$(ping -o -s 0 -c "$count" "$IP1" | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }') |
|
|
|
if [ "$ip1_count" -ne 0 ]; then |
|
return 1 |
|
fi |
|
|
|
ip2_count=$(ping -o -s 0 -c "$count" "$IP2" | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }') |
|
|
|
if [ "$ip2_count" -ne 0 ]; then |
|
return 1 |
|
fi |
|
|
|
return 0 |
|
} |
|
|
|
# Function to restart the interface |
|
restart_interface() { |
|
ifconfig $INTERFACE down |
|
ifconfig $INTERFACE up |
|
} |
|
|
|
# Function to reconfigure the WAN interface |
|
reconfigure_wan() { |
|
configctl interface reconfigure wan |
|
} |
|
|
|
# Function to reboot the system |
|
reboot_system() { |
|
/usr/local/etc/rc.reboot |
|
} |
|
|
|
# Main script logic |
|
main() { |
|
# Testing uptime to run script only xx seconds after boot |
|
curtime=$(date +%s) |
|
uptime=$(sysctl kern.boottime | awk -F'sec = ' '{print $2}' | awk -F',' '{print $1}') |
|
uptime=$((curtime - uptime)) |
|
|
|
if [ $uptime -gt $MIN_UPTIME ]; then |
|
log_message "Testing Connection (System uptime: $uptime seconds)" |
|
|
|
ping_ips 10 |
|
if ping_ips 10; then |
|
log_message "Restarting interface '$INTERFACE'" |
|
restart_interface |
|
ping_ips 10 |
|
fi |
|
|
|
if ping_ips 10; then |
|
log_message "Reconfiguring WAN interface" |
|
reconfigure_wan |
|
ping_ips 10 |
|
fi |
|
|
|
if ping_ips 10; then |
|
log_message "Rebooting system" |
|
reboot_system |
|
fi |
|
fi |
|
} |
|
|
|
# Optional, add opnsense action for this script, which can then be added |
|
# as a cron job in the UI: |
|
if [ ! -r "$TARGET_LOCATION" ] ; then |
|
cp "$0" "$TARGET_LOCATION" |
|
chmod +x "$TARGET_LOCATION" |
|
fi |
|
|
|
if [ ! -r "$TARGET_ACTION" ] ; then |
|
cat > "$TARGET_ACTION" <<EOACTION |
|
[load] |
|
command:$TARGET_LOCATION |
|
parameters: |
|
type:script |
|
message:starting ping check |
|
description:ping_check |
|
EOACTION |
|
# Restart configd service to have action appear in the menus |
|
service configd restart |
|
fi |
|
|
|
# Run the main script logic |
|
main |
Hello, I have this script running perfectly. Thanks! One question. I sometimes have a ISP outage (ie. 5 hours long) which won't be fixed by a reboot. Will this be rebooting every 2 minutes until it comes back up? If I change the MIN_UPTIME to 3600 then it would only reboot once a hour correct?