Here is a Merlin specific solution using a user created script, I hope it works for others
This requires having a wan-event file I do believe to trigger the wan-event messages in the log... Im not sure how else to capture the wan up/down in the log
I had a function for something similar, I scratched this together to take advantage of the idea from
@Jeffrey Young
Enable jffs/scripts
From SSH terminal
cd/jffs/scripts
nano wantime.sh
Copy and paste code box below, ctrl+x and y to save
dos2unix /jffs/scripts/wantime.sh
chmod a+rx wantime.sh
sh wantime.sh
Code:
#!/bin/sh
# Calc difference from last wan-event connected to now written by Maverickcdn March 20 2022
run_epoch=$(date +%s) # current epoch time
# find last wan-event connected
findlast=$(cat /tmp/syslog.log | grep "wan-event" | grep "args: * connected" | tail -n1) # search log for most recent wan-event connected
[ -z "$findlast" ] && findlast=$(cat /tmp/syslog.log-1 | grep "wan-event" | grep "args: * connected" | tail -n1) # check older logs if not found prior
[ -z "$findlast" ] && printf "Couldn't find last wan-event connected event in logs... exiting. \n" && exit 0 # nothing to calculate... exit
fnd_mth=$(echo $findlast | awk '{print $1}') # found month
fnd_day=$(echo $findlast | awk '{print $2}') # found day
fnd_time=$(echo $findlast | awk '{print $3}') # found time
# convert found month to num
month_list="JanFebMarAprMayJunJulAugSepOctNovDec"
tmp_mth=${month_list%%$fnd_mth*}
month_fnd=${#tmp_mth}
month_num=$((month_fnd/3+1))
year=$(date +%Y)
[ "$fnd_day" -gt "$(date +%d)" ] && [ "$month_num" -eq "$(date +%m)" ] && year=$((year - 1)) # month same but higher fnd_day = previous year
[ "$month_num" -gt "$(date +%m)" ] && year=$((year - 1)) # if month is higher = previous year
log_date_fnd=$(echo "${year}-${month_num}-${fnd_day} ${fnd_time}") # format found date/time for date -d
saved_wan_epoch=$(date -d "$log_date_fnd" +%s) # convert wan-event date to epoch
# calc difference function
F_calc_lease() {
wan_lease_years=0 ;wan_lease_days=0 ;wan_lease_hours=0 ;wan_lease_mins=0 ;wan_lease_secs=0 # set for output
epoch_diff=$((run_epoch - saved_wan_epoch))
if [ "$epoch_diff" -gt 31536000 ] ; then # year
wan_lease_years=$((epoch_diff / 31536000))
epoch_diff=$((epoch_diff - (31536000 * wan_lease_years)))
fi
if [ "$epoch_diff" -gt 86400 ] ; then # days
wan_lease_days=$((epoch_diff / 86400))
epoch_diff=$((epoch_diff - (86400 * wan_lease_days)))
fi
if [ "$epoch_diff" -gt 3600 ] ; then # hours
wan_lease_hours=$((epoch_diff / 3600))
epoch_diff=$((epoch_diff - (3600 * wan_lease_hours)))
fi
if [ "$epoch_diff" -gt 60 ] ; then # mins
wan_lease_mins=$((epoch_diff / 60))
epoch_diff=$((epoch_diff - (60 * wan_lease_mins)))
fi
wan_lease_secs="$epoch_diff" # secs
printf "Last wan-event connected event found : %s \n" "$findlast"
printf "WAN uptime since last connected event : "
[ "$wan_lease_years" -gt 0 ] && printf "%s yr(s) " "$wan_lease_years"
[ "$wan_lease_days" -gt 0 ] && printf "%s day(s) " "$wan_lease_days"
[ "$wan_lease_hours" -gt 0 ] && printf "%s hr(s) " "$wan_lease_hours"
[ "$wan_lease_mins" -gt 0 ] && printf "%s min(s) " "$wan_lease_mins"
printf "%s sec(s) \n" "$wan_lease_secs"
} ### calc_lease
F_calc_lease
exit 0
My results
Code:
MavMAIN|>/jffs/scripts| sh wantime.sh
Last wan-event connected event found : Mar 13 03:00:05 custom_script: Running /jffs/scripts/wan-event (args: 0 connected)
WAN uptime since last connected event : 7 day(s) 17 hr(s) 24 min(s) 12 sec(s)
Lines 5,6 and 7 might be better off with these instead if you dont use a wan-event script already.... just a guess, maybe someone else can suggest something else to search for when wan goes up
Code:
findlast=$(cat /tmp/syslog.log | grep "wan: finish adding multi routes" | tail -n1)
[ -z "$findlast" ] && findlast=$(cat /tmp/syslog.log-1 | grep "wan: finish adding multi routes" | tail -n1) # check older logs if not found prior
[ -z "$findlast" ] && printf "Couldn't find last wan event in logs... exiting. \n" && exit 0 # nothing to calculate... exit