Skip to content

Instantly share code, notes, and snippets.

@andiwand
Created August 22, 2017 07:20
Show Gist options
  • Save andiwand/1cf3c3a7aba85319d40eb548e7ed38e6 to your computer and use it in GitHub Desktop.
Save andiwand/1cf3c3a7aba85319d40eb548e7ed38e6 to your computer and use it in GitHub Desktop.
nagios check script - openwrt mwan3 status
#/bin/ash
. /lib/functions.sh
. /lib/functions/network.sh
. /lib/mwan3/mwan3.sh
mwan3_iface_status()
{
local device result track_ips tracking IP IPT
mwan3_get_iface_id id $1
network_get_device device $1
config_get enabled "$1" enabled 0
config_get family "$1" family ipv4
if [ "$family" == "ipv4" ]; then
IP="$IP4"
IPT="$IPT4"
fi
if [ "$family" == "ipv6" ]; then
IP="$IP6"
IPT="$IPT6"
fi
if [ -z "$id" -o -z "$device" ]; then
result="unknown"
istate=0
elif [ -n "$($IP rule | awk '$1 == "'$(($id+1000)):'"')"i -a -n "$($IP rule | awk '$1 == "'$(($id+2000)):'"')" -a -n "$($IPT -S mwan3_iface_in_$1 2> /dev/null)" -a -n "$($IPT -S mwan3_iface_out_$1 2> /dev/null)" -a -n "$($IP route list table $id default dev $device 2> /dev/null)" ]; then
result="online"
istate=1
elif [ -n "$($IP rule | awk '$1 == "'$(($id+1000)):'"')" -o -n "$($IP rule | awk '$1 == "'$(($id+2000)):'"')" -o -n "$($IPT -S mwan3_iface_in_$1 2> /dev/null)" -o -n "$($IPT -S mwan3_iface_out_$1 2> /dev/null)" -o -n "$($IP route list table $id default dev $device 2> /dev/null)" ]; then
result="error"
istate=0
else
if [ "$enabled" == "1" ]; then
result="offline"
istate=0
else
result="disabled"
istate=0
fi
fi
mwan3_list_track_ips()
{
track_ips="$1 $track_ips"
}
config_list_foreach $1 track_ip mwan3_list_track_ips
if [ -n "$track_ips" ]; then
if [ -n "$(ps -w | grep mwan3track | grep -v grep | sed '/.*\/usr\/sbin\/mwan3track \([^ ]*\) .*$/!d;s//\1/' | awk '$1 == "'$1'"')" ]; then
tracking="active"
tstate=1
else
tracking="down"
tstate=0
fi
else
tracking="not enabled"
fi
out="$out$1 $result tracking $tracking, "
allup=$((allup && istate))
alldown=$((alldown && istate^1))
}
allup=1
alldown=1
out=""
config_load mwan3
interfaces=`config_foreach echo interface`
for interface in $interfaces; do
mwan3_iface_status $interface
done
if [ $allup -eq 1 ]; then
echo "OK - $out"
result=0
elif [ $alldown -eq 1 ]; then
echo "ERROR - $out"
result=2
else
echo "WARN - $out"
result=1
fi
exit $result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment