What's new

[wanuptime] WAN uptime estimation script

  • 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!

Maverickcdn

Senior Member
Just another useless script from yours truly ;) My attempt at calculating WAN uptime from log messages, not guaranteed to be accurate at all but is my attempt to calculate WAN uptime.

About/Readme on Github

To download in a SSH terminal
Code:
curl --retry 3 "https://raw.githubusercontent.com/maverickcdn/wanuptime/master/wanuptime.sh" -o "/jffs/scripts/wanuptime.sh" && chmod a+rx "/jffs/scripts/wanuptime.sh"

wanevent.PNG

Thanks to @ColinTaylor and @Martinski
Link to Colins much more straight forward version https://www.snbforums.com/threads/best-log-message-to-detect-boot-reboot.78206/post-754771

EDIT: wrong forum.. doh... meant to be in add-ons
Enjoy, hope it works for all!
 
Last edited:
Thanks for the script its quite useful.

One thing though, if one uses Scribe "tmp/syslog.log " etc, does not exist as expected , you may need to add /opt/var/log/messages to the search, parameters to accommodate such users .
 
I use scribe and I can test the new version if you need
 
Thanks for the citation. Just to close the loop on that other post here is the alternate version of my script that uses wan-event if it's available.

Note that when using wan-event for the detection method I deliberately chose to only use wan0. Trying to predict what the user is expecting from a single "uptime" number when using a dual WAN setup is impossible. Especially when you factor in things like fail-over and fail-back. Ideally a script would display two uptime numbers, one for each possible WAN interface.

/jffs/scripts/wan-event (optional)
Code:
#!/bin/sh

if [ "$2" = "connected" ]; then
   counter=1
   while [ $counter -le 30 ] && [ "$(nvram get ntp_ready)" != "1" ]
   do
      sleep 1
      counter=$((counter+1))
   done
   if [ "$(nvram get ntp_ready)" = "1" ]; then
      date +'%s' > /tmp/wan${1}_uptime.tmp
   fi
else
   rm -f /tmp/wan${1}_uptime.tmp
fi

/jffs/scripts/wanuptime.sh
Code:
#!/bin/sh

if [ "$(nvram get wan0_state_t)" != "2" ] && [ "$(nvram get wan1_state_t)" != "2" ]; then
   echo "WAN is not up"
   exit
fi

curr_secs="$(date +%s)"

if [ -s /tmp/wan0_uptime.tmp ]; then
   wanup_secs="$(cat /tmp/wan0_uptime.tmp)"     # Created by wan-event/connected
else
   infile="/tmp/syslog.log"
   if [ -f /tmp/syslog.log-1 ]; then
      infile="/tmp/syslog.log-1 /tmp/syslog.log"
   fi
   wanup_date="$(cat $infile | awk '/Initial clock set|WAN was restored/ {a=$0} END{print substr (a,1,15)}')"
   if [ -z "$wanup_date" ]; then
      echo "No WAN events detected in syslog"
      exit
   fi
   wanup_secs="$(/bin/date --date="$wanup_date" -D '%b %e %T' +'%s')"
   if [ $wanup_secs -gt $curr_secs ]; then
      last_year="$(( $(date +%Y) - 1 ))"        # Assume last year
      wanup_secs="$(/bin/date --date="$last_year $wanup_date" -D '%Y %b %e %T' +'%s')"
   fi
fi

uptime="$((curr_secs-wanup_secs))"

#echo "$curr_secs $wanup_secs $uptime"
days="$((uptime/86400))"
hours="$((uptime/3600%24))"
minutes="$((uptime/60%60))"

echo "WAN uptime = $days days $hours hours $minutes minutes"
 
Last edited:
Not wishing to hijack @Maverickcdn's thread, but here's a simple script that doesn't use syslog at all but depends on the wan-event script in the previous post. The main difference is that it provides the WAN uptime for each interface.
Code:
#!/bin/sh

convert_uptime() {
   local days="$(($1/86400))"
   local hours="$(($1/3600%24))"
   local minutes="$(($1/60%60))"
   local seconds="$(($1%60))"
   echo $days $hours $minutes $seconds
}

curr_secs="$(date +%s)"

for iface in 0 1
do
   if [ "$(nvram get wan${iface}_state_t)" = "2" ] && [ -s /tmp/wan${iface}_uptime.tmp ]; then
      wanup_secs="$(cat /tmp/wan${iface}_uptime.tmp)"   # Created by wan-event/connected
      uptime="$((curr_secs-wanup_secs))"
      uptime_msg="$(convert_uptime $uptime | awk '{print $1 " days " $2 " hours " $3 " minutes"}')"
      echo "wan${iface} uptime: $uptime_msg"
   else
      echo "wan${iface} uptime: down"
fi
done
Code:
# ./wanuptime.sh
wan0 uptime: 4 days 2 hours 15 minutes
wan1 uptime: down
 
I was also not wishing to hijack the thread but was inspired by the topic and thought of suggesting alternates for lookup and uptime calculation.

This long one-liner will provide wlan uptime, at least on my Merlin-ized RT-AX3000 with a single WAN port. It's based upon my observation that udhcpc restarts when I reboot my cable modem, so I grab the date from '/var/run/udhcpc0.pid'.

It uses a cheat with the date command to show time from the normal UTC base of "1970-01-01 00:00:00" as the uptime with the final 'awk' command reducing the day-of-year by one to shift it to a zero-based scale. One could get more or less sophisticated with the final date format to, for example, combine hours, minutes and seconds (e.g. '+%j %T' and adjusting the final 'awk'), etc. Unfortunately, the busybox-based date command limits output length (meaning specifying the units is best left to the final 'awk' instead of within the date format). Also, using this cheat means that the command line won't work for uptimes greater than 365 days. Otherwise, enjoy:

Code:
% /bin/date -u -d"@$(/usr/bin/expr $(date '+%s') - $(/bin/date -r/var/run/udhcpc0.pid '+%s'))" '+%j %H %M %S' | /usr/bin/awk '{print $1-1" days "$2" hrs "$3" mins "$4" secs "}'
1 days 12 hrs 11 mins 55 secs
 
Last edited:
It uses a cheat with the date command to show time from the normal UTC base of "1970-01-01 00:00:00" as the uptime with the final 'awk' command reducing the day-of-year by one to shift it to a zero-based scale.
:) I had the same idea of using the date command to show the uptime but couldn't work out how to subtract 1 from the day.

I did at one point think about using udhcpc (for the syslog solution) but decided against it as I guessed that not everyone uses DHCP on their WAN.
 
Last edited:

Similar threads

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