Auto update mwan3 track IP with hotplug

Hello,
I was trying to create a hotplug script to auto update the track ip I have on the lte interface in mwan3.
Does anyone more experienced than me with hotplug and scripts find anything wrong or that can be improved?

/root/bin/change_track_ip.sh

#!/bin/sh
# 
. /lib/functions/network.sh
network_flush_cache
NET_IF="lte_4"
#network_get_ipaddr NET_ADDR "${NET_IF}"
network_get_gateway NET_GW "${NET_IF}"
#echo "${NET_GW}"
#echo "${NET_ADDR}"
uci delete mwan3.lte.track_ip
uci add_list mwan3.lte.track_ip="${NET_GW}"
uci commit mwan3
/etc/init.d/mwan3 restart

/etc/hotplug.d/iface/97-change-lte-track-ip-mwan3

CHANGE_TRACK_IP="/root/bin/change_track_ip.sh"
if [ "${ACTION}" = "ifup" ]; then
        if [ "${INTERFACE}" = "lte_4" ]; then
                ${CHANGE_TRACK_IP}
        fi
fi
if [ "${ACTION}" = "ifupdate" ]; then
        if [ "${INTERFACE}" = "lte_4" ]; then
                ${CHANGE_TRACK_IP}
        fi
fi

thank you!

1 Like

While I don't have deeper knowledge about the network.sh internals and I'm far far away from being a pro. The scripts are looking good to me. So I only have one thing you could improve.

For /root/bin/change_track_ip.sh I would suggest to build in a check if the interface is actually up already to avoid an empty value for NET_GW/NET_ADDR. You could check before with something like:

if test -z "$NET_GW" # just checking if empty or not
then
    sleep 10  # dev not yet up
else
    **your script lines**
fi

--- or ---

while true; do
    ethtool lte_4 | grep no | cut -d " " -f 3 
    if [ $? -eq 0 ]; then
        sleep 10  # dev not yet up
    else
    **your script lines**
    fi
done

That what just came in mind. I think there are functions available in network.sh or uci to check if an interface is up to make things nicer then this ugly way I've written down. But I don't have deeper knowledge about the network functions. Sorry. At least a small bump.

For /etc/hotplug.d/iface/97-change-lte-track-ip-mwan3 you could shorten things a bit to make it look nicer.

if [ "${ACTION}" = "ifup" ] && [ "${INTERFACE}" = "lte_4" ];
then
	${CHANGE_TRACK_IP}
fi
if [ "${ACTION}" = "ifupdate" ] && [ "${INTERFACE}" = "lte_4" ];
then
	${CHANGE_TRACK_IP}
fi
2 Likes

@trendy, change single quotes to double quotes to make variable expansion work properly.
In addition, a single if statement should be enough:

if [ "${ACTION}" = "ifup" -o "${ACTION}" = "ifupdate" ] \
&& [ "${INTERFACE}" = "lte_4" ]; then ...; fi

@pwned, the ifup and ifupdate events imply that the connection is established successfully, thus you don't need to check the interface status.

2 Likes

mwan3 is definately a tricky one... i'm surprised they dont have a built in uci-conf flag for use gw ip as track ip...

looking at it's internals... seems there are also some custom hooks points... so the mwan3 dev/s would know the best point to hook into the hotplug logix...

15-mwan3
16-mwan3-user > /etc/mwan3.user

( connected and disconnected internal track events are another complexity / consideration ) my gut tells me to run your logic pre 15-mwan3... but i'm probably wrong... the key concern being avoiding unnecessary service reload/restart aka chicken egg behavior... on that point... you could also likely only set/apply the uci-conf-val if it is different )

3 Likes

That would be nice and maybe @aaronjg would consider it as a future feature.
I thought about adding the script in /etc/mwan3.user but then it would be triggered on every interface event, no?

That sounds interesting, it might make sense to remove the last line from the script (to restart the mwan3 service) so by the time the 15-mwan3 is called, the tracking ip is already there.
@pwned and @vgaetera I have squeezed down the hotplug rule according to your suggestions :slight_smile:

2 Likes

I believe @ptpt52 had a solution for this at some point: https://github.com/openwrt/packages/commit/57cfe04c035838d8a3f6cd5358f405e7e96bab52

Using network.sh functions as you do is a bit nicer of a solution than grepping the routing table, however it really should be done in the mwan3track script. Updating the uci script will cause a flash write every time a network is connected/disconnected, which is not ideal.

4 Likes

Yes, I didn't consider that much to be honest.
Then I'll let it be and wait for the commit from ptpt52 to be merged. Feel free to use my script if it is a more elegant way to identify the gateway.
Thank you all for your help! :slight_smile:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.