#!/usr/local/bin/bash## uplink-status - check whether we have internet connectivity#UPLINK_NAME="MY_GW"UPLINK_STATE_FILE=/usr/local/var/run/uplink-statusLINK_UP=2LINK_DOWN=1# Cleanupquit() { EC=$1 exit $EC}checkrc() { PRC=$1; if [ $PRC -ne 0 ]; then echo "$0: Exiting due to error ($PRC)" 1>&2 exit 0 fi}GATEWAY_STATUS_JSON=`pluginctl -r return_gateways_status`UPLINK_PRESENT=`echo $GATEWAY_STATUS_JSON | grep -c $UPLINK_NAME`if [ "$UPLINK_PRESENT" -eq 0 ]; then CUR_STATUS="down"else # Check dpinger status of the uplink CUR_STATUS=`echo $GATEWAY_STATUS_JSON | python3 -c "import sys, json; print(json.load(sys.stdin)['dpinger']['${UPLINK_NAME}']['status'])"` checkrc $?fiif [ ! -f "$UPLINK_STATE_FILE" ]; then echo "up" > $UPLINK_STATE_FILE checkrc $?fiLAST_STATUS=`cat $UPLINK_STATE_FILE`checkrc $?if [ "$LAST_STATUS" = "up" ] && [ "$CUR_STATUS" = "down" ]; then echo "down" > $UPLINK_STATE_FILE echo "down" quit $LINK_DOWNelif [ "$LAST_STATUS" = "down" ] && [ "$CUR_STATUS" = "none" ]; then echo "up" > $UPLINK_STATE_FILE echo "up" quit $LINK_UPelse if [ "$CUR_STATUS" = "none" ]; then CUR_STATUS="up"; fi echo "no change ($CUR_STATUS)" quit 0fi
#!/usr/local/bin/bash## set-carp-demotion - demote/promote a link dependent on interface status# Called by monit on link status change#DEMOTION_VALUE=100LINK_STATUS=${MONIT_PROGRAM_STATUS}LINK_UP=2LINK_DOWN=1# Cleanupquit() { EC=$1 if [ $EC -ne 0 ]; then echo "$0: Exiting due to error ($EC)"; fi exit $EC}checkrc() { PRC=$1; if [ $PRC -ne 0 ]; then quit $PRC; fi}CUR_DEMOTION=`sysctl -n net.inet.carp.demotion`checkrc $?if [ "$LINK_STATUS" -eq "$LINK_UP" ]; then if [[ "$CUR_DEMOTION" -gt "0" ]]; then sysctl -q net.inet.carp.demotion=-$DEMOTION_VALUE checkrc $? fielif [ "$LINK_STATUS" = "$LINK_DOWN" ]; then if [[ "$CUR_DEMOTION" -lt "$DEMOTION_VALUE" ]]; then sysctl -q net.inet.carp.demotion=$DEMOTION_VALUE checkrc $? fielse echo "Invalid link status '$LINK_STATUS'." quit 1fiquit 0