What's new

Email notification when switching to secondary WAN?

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

@thelonelycoder
Just as an FYI.....on my fork it takes 60 +,- 5 sec (depending on how my ISP feels that day) for me to get to WAN up.
Let me test that on the dedicated 'john's fork' router.
My three other Merlin router come up in seconds when booting.
When swiching WAN (i pull the wire or unplug my Android for simplicity), the most time is spent by the router, figuring out what to do. Then when wan-start is run the WAN is up in seconds or less.
I always get pinc time of 0 or 1 and WAN cycle of 0.
But I have a better idea now what to do which I test after running it on your fork.
 
BTW @john9527 or @RMerlin, how is router uptime calculated? I cant figure it out from the Tools page where the data comes from, certainly not the NVRAM.
 
It looks like it phrases the data from the "uptime" command.
Argh, how stupid of me, right in front of me and never seen.
Hello you handy uptime command!
Thanks Adamm.
 
Correct.....it's just linux uptime.

On my fork, I do a separate calculation on the syslog page which is based on the WAN up message.
It's a bit vague:
22:05:38 up 9 min, load average: 0.08, 0.05, 0.04

You have the same function as Merlin:
Code:
function showbootTime(){
Days = Math.floor(boottime / (60*60*24));
Hours = Math.floor((boottime / 3600) % 24);
Minutes = Math.floor(boottime % 3600 / 60);
Seconds = Math.floor(boottime % 60);
$("boot_days").innerHTML = Days;
$("boot_hours").innerHTML = Hours;
$("boot_minutes").innerHTML = Minutes;
$("boot_seconds").innerHTML = Seconds;
boottime += 1;
setTimeout("showbootTime()", 1000);
}
In sh I could do this:
Code:
uptime | awk -F, '{sub(".*up ",x,$1);print $1}'
But all my routers and linux servers are freshly booted (heat is enormous here in CH), can anyone post the output of above command with an uptime of a few weeks or months to verify it works, thanks.
It should output minutes or hours or days or?
 
Also @thelonelycoder I was looking at how you handled ntp not running before your script started as I have a similar dilemma with my scripts calculating execution time on boot. I think you need to make the one liner slightly more complex, ntpclient is very unreliable on boot and will fail on a noticeable amount of times. Your best bet is to check the value of $(nvram get ntp_ready). When ntp successfully updates it will change from 0 > 1.

It seems @kvic looked into this around 2014
 
Also @thelonelycoder I was looking at how you handled ntp not running before your script started as I have a similar dilemma with my scripts calculating execution time on boot. I think you need to make the one liner slightly more complex, ntpclient is very unreliable on boot and will fail on a noticeable amount of times. Your best bet is to check the value of $(nvram get ntp_ready). When ntp successfully updates it will change from 0 > 1.

It seems @kvic looked into this around 2014
Good point, solves some problems nicely and removes an obstacle I thought I noticed on the main router where Skynet runs.
 
You have the same function as Merlin:
Syslog page....
Screenshot from 2017-06-22 12:28:15.png
 
Good point, solves some problems nicely and removes an obstacle I thought I noticed on the main router where Skynet runs.


Here's a POSIX compliant solution kvic's idea to save you the trouble, tested it on about 10 reboots and seems to work reliably (change the logger part ofc)

Code:
    attempts=1
    while [ "$(nvram get ntp_ready)" != "1" ] && [ "$attempts" -le "5" ]; do
        ntpclient -h "$(nvram get ntp_server0)" -s >/dev/null 2>&1
        if [ "$(nvram get ntp_ready)" = "1" ]; then
            logger -st Skynet "[INFO] NTP update succeeded after $attempts attempt(s)"
        else
            attempts=$((attempts+1))
        fi
    done
 
Here's a POSIX compliant solution kvic's idea to save you the trouble, tested it on about 10 reboots and seems to work reliably (change the logger part ofc)

Code:
    attempts=1
    while [ "$(nvram get ntp_ready)" != "1" ] && [ "$attempts" -le "5" ]; do
        ntpclient -h "$(nvram get ntp_server0)" -s >/dev/null 2>&1
        if [ "$(nvram get ntp_ready)" = "1" ]; then
            logger -st Skynet "[INFO] NTP update succeeded after $attempts attempt(s)"
        else
            attempts=$((attempts+1))
        fi
    done
Im a simple man:
Code:
NPT_UP_CYCLES=0
while [ $(nvram get ntp_ready) == 0 ];do
    sleep 2
    NPT_UP_CYCLES=$(($NPT_UP_CYCLES + 1))
done
 
Im a simple man:
Code:
NPT_UP_CYCLES=0
while [ $(nvram get ntp_ready) == 0 ];do
    sleep 2
    NPT_UP_CYCLES=$(($NPT_UP_CYCLES + 1))
done

Probably better going with the "waiting" approach, not much time is saved by forcing it (around 3s now I've timed it) from when firewall-start is called at-least in my situation.
 
Must be my error, but with /proc/uptime I always get years and years of it.
I'll have to check my breakdown code again then (years, months etc).

Check the httpd code, it doesn't just directly use the content of /proc/uptime but it does some manipulations around its value.
 
Version 1.5 of the WAN connect notification script is now available.
- changed NTP early update to verbatim @Adamm's code, thanks!
- router uptime now in Web UI format, thanks for advice and suggestions @john9527 and @RMerlin
- changed WAN up and IP detection

Download in Other Scripts section here.

Expected output:
There's been a WAN type change on the router on Fri, 23 Jun 2017 12:24:42 +0200.

Router RT-AC68U is now connected through Secondary WAN
The WAN IP is: 192.168.2.28

Router uptime: 0 days 00 hours 00 minutes 53 seconds
The LAN IP is: 172.20.0.1

wan_connect_notification script stats:
NPT update cycles: 1
Ping cycles = 1, actual wait time until WAN up = 4 seconds.
WAN IP register wait cycles: 0

---
Your friendly router.
 
Version 1.5
Now it looks much better:

Code:
  There's been a WAN type change on the router on Sat, 24 Jun 2017 17:54:08 +0300.

  Router RT-N66U is now connected through Primary WAN
  The WAN IP is: 10.13.70.222

  Router uptime: 0 days 00 hours 01 minutes 07 seconds
  The LAN IP is: 192.168.1.1

  wan_connect_notification script stats:
  NPT update cycles: 1
  Ping cycles = 1, actual wait time until WAN up = 4 seconds.
  WAN IP register wait cycles: 53
 
Now it looks much better:

Code:
  There's been a WAN type change on the router on Sat, 24 Jun 2017 17:54:08 +0300.

  Router RT-N66U is now connected through Primary WAN
  The WAN IP is: 10.13.70.222

  Router uptime: 0 days 00 hours 01 minutes 07 seconds
  The LAN IP is: 192.168.1.1

  wan_connect_notification script stats:
  NPT update cycles: 1
  Ping cycles = 1, actual wait time until WAN up = 4 seconds.
  WAN IP register wait cycles: 53

Much better, glad it works for you too now.
Have you checked the secondary type?
 
Have you checked the secondary type?
Some additional tests figured out 2 problems.
I get email with ~4 sec timeout when running terminal.
I get email with ~60 sec timeout when rebooting router on primary WAN
I get email with ~4 sec timeout when i turn off primary wan source. Email says that the router is on secondary and shows correct ip.
BUT. I get two of the emails about switching from primary to secondary. Every time two exactly same emails with the same timestamp. I had the same problem with the previous script posted here 2 years ago, so it's a common problem. May be you know how to fix it. It's not a big problem at all.
And the second problem is much bigger. I don't get emails at all when switching from secondary Wan to primary. I tested it several times.
2 same emails primary --->secondary
and no emails secondary --->primary
 
Hmmm that's really interesting.
While waiting for your answer - i decided to upgrade from 380.66.4 to 380.66.6 and router asked me for a manual power off.
After that i tried to switch from primary to secondary - and got TWO emails as usual. But when i switched back from secondary to primary - i finally started getting emails. So power cycle helped to solve one of two problems.
So i'll make some tests. The only problem left for now is:

Code:
Jun 24 18:58:06 miniupnpd[1807]: HTTP listening on port 50089
Jun 24 18:58:06 miniupnpd[1807]: Listening for NAT-PMP/PCP traffic on port 5351
Jun 24 18:58:07 admin: wan_connect_notification: WAN connection restored. Ping cycles = 4, Actual time = 8, via /jffs/scripts/wan-start
Jun 24 18:58:07 admin: wan_connect_notification: WAN connection restored. Ping cycles = 2, Actual time = 4, via /jffs/scripts/wan-start
Jun 24 18:58:09 miniupnpd[1807]: upnp_event_recv: recv(): Connection reset by peer
Jun 24 18:58:09 miniupnpd[1807]: upnp_event_recv: recv(): Connection reset by peer
Jun 24 18:58:09 admin: wan_connect_notification: WAN type change detected, notification sent to avoronkov@gmail.com via /jffs/scripts/wan-start
Jun 24 18:58:10 admin: wan_connect_notification: WAN type change detected, notification sent to avoronkov@gmail.com via /jffs/scripts/wan-start
Jun 24 18:59:29 rc_service: wanduck 274:notify_rc restart_wan_if 0
 

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top