1
0
Fork 0
mirror of https://github.com/Ysurac/openmptcprouter-feeds.git synced 2025-03-09 15:40:03 +00:00
openmptcprouter-feeds/omr-quota/files/bin/omr-quota
bdaylik bb483535fa
update omr-quota to up the interface when its usage is below threshold (#309)
Existing condition for the upping the interface asks for all 3 checks to have positive values (RX, TX and TT). However this is not necessary as some can be set to 0 to disable the check.

This modification makes sure that if the above checks for RX, TX and TT values have failed, thus the usage is still below the set quota, the interface is set to be up.

Thanks to this modification its possible to set only one check positive (e.g. TT) and let the script automatically down and up the interface as expected.
2024-03-25 12:14:55 +01:00

39 lines
1.7 KiB
Bash
Executable file

#!/bin/sh
[ -n "$1" ] || exit
. /lib/functions.sh
# retrieve args
OMR_QUOTA_INTERFACE="$1"
shift
# main loop
while true; do
OMR_QUOTA_REAL_INTERFACE="$(ifstatus $OMR_QUOTA_INTERFACE | jsonfilter -e '@.l3_device')"
rx_bits=`vnstat -i $OMR_QUOTA_REAL_INTERFACE --json | jsonfilter -q -e '@.interfaces[0].traffic.month[-1].rx' | tr -d "\n"`
tx_bits=`vnstat -i $OMR_QUOTA_REAL_INTERFACE --json | jsonfilter -q -e '@.interfaces[0].traffic.month[-1].tx' | tr -d "\n"`
rx=$((rx_bits/1024))
tx=$((tx_bits/1024))
tt=$((rx + tx))
if [ -n "$OMR_QUOTA_RX" ] && [ "$OMR_QUOTA_RX" -gt 0 ] && [ -n "$rx" ] && [ "$OMR_QUOTA_RX" -le "$rx" ]; then
if [ "$(ifstatus $OMR_QUOTA_INTERFACE | jsonfilter -e '@.up')" = "true" ]; then
logger -t "OMR-QUOTA" "Set interface $OMR_QUOTA_INTERFACE down, RX quota reached"
ifdown $OMR_QUOTA_INTERFACE
fi
elif [ -n "$OMR_QUOTA_TX" ] && [ "$OMR_QUOTA_TX" -gt 0 ] && [ -n "$tx" ] && [ "$OMR_QUOTA_TX" -le "$tx" ]; then
if [ "$(ifstatus $OMR_QUOTA_INTERFACE | jsonfilter -e '@.up')" = "true" ]; then
logger -t "OMR-QUOTA" "Set interface $OMR_QUOTA_INTERFACE down, TX quota reached"
ifdown $OMR_QUOTA_INTERFACE
fi
elif [ -n "$OMR_QUOTA_TT" ] && [ "$OMR_QUOTA_TT" -gt 0 ] && [ -n "$tt" ] && [ "$OMR_QUOTA_TT" -le "$tt" ]; then
if [ "$(ifstatus $OMR_QUOTA_INTERFACE | jsonfilter -e '@.up')" = "true" ]; then
logger -t "OMR-QUOTA" "Set interface $OMR_QUOTA_INTERFACE down, RX+TX quota reached"
ifdown $OMR_QUOTA_INTERFACE
fi
elif [ "$(ifstatus $OMR_QUOTA_INTERFACE | jsonfilter -e '@.up')" = "false" ]; then
logger -t "OMR-QUOTA" "Set interface $OMR_QUOTA_INTERFACE up"
ifup $OMR_QUOTA_INTERFACE
fi
sleep "$OMR_QUOTA_INTERVAL"
done