up = empty & down = empty: never go up (or down) [and link down counter must be oviously 0]
up = date & down = empty: go up only one time (no down) [and link down counter must be oviously 0] and is up from that datetime to current datetime
up = date & down = date
if up > down, the link is estabilished and go down for "link down" times, the differencies between the dates is how many time the interface is up from last down event.
on the up > down case, the differencie between actual datetime and "up" give how many time the interface is up.
if up = down, is one border case, because milliseconds are not readables, is considered like up > down, but other ckecks must be done
if up < down, the link is estabilished on the past, but go down [for "link down" times] and never go up again
on the up < down case, the differencie between actual datetime and "down" give how many time the interface is down.
This function convert datetime to epoch, and is present one example for the ethernet link up/down compare times.
The function do not check if is provided a valid date+time or not
:global datetime2epoch do={
:local dtime [:tostr $1]
/system clock
:local cyear [get date] ; :if ($cyear ~ "....-..-..") do={:set cyear [:pick $cyear 0 4]} else={:set cyear [:pick $cyear 7 11]}
:if (([:len $dtime] = 10) or ([:len $dtime] = 11)) do={:set dtime "$dtime 00:00:00"}
:if ([:len $dtime] = 15) do={:set dtime "$[:pick $dtime 0 6]/$cyear $[:pick $dtime 7 15]"}
:if ([:len $dtime] = 14) do={:set dtime "$cyear-$[:pick $dtime 0 5] $[:pick $dtime 6 14]"}
:if ([:len $dtime] = 8) do={:set dtime "$[get date] $dtime"}
:if ([:tostr $1] = "") do={:set dtime ("$[get date] $[get time]")}
:local vdate [:pick $dtime 0 [:find $dtime " " -1]]
:local vtime [:pick $dtime ([:find $dtime " " -1] + 1) [:len $dtime]]
:local vgmt [get gmt-offset]; :if ($vgmt > 0x7FFFFFFF) do={:set vgmt ($vgmt - 0x100000000)}
:if ($vgmt < 0) do={:set vgmt ($vgmt * -1)}
:local arrm [:toarray "0,0,31,59,90,120,151,181,212,243,273,304,334"]
:local vdoff [:toarray "0,4,5,7,8,10"]
:local MM [:pick $vdate ($vdoff->2) ($vdoff->3)]
:local M [:tonum $MM]
:if ($vdate ~ ".../../....") do={
:set vdoff [:toarray "7,11,1,3,4,6"]
:set M ([:find "xxanebarprayunulugepctovecANEBARPRAYUNULUGEPCTOVEC" [:pick $vdate ($vdoff->2) ($vdoff->3)] -1] / 2)
:if ($M>12) do={:set M ($M - 12)}
}
:local yyyy [:pick $vdate ($vdoff->0) ($vdoff->1)] ; :if ((($yyyy - 1968) % 4) = 0) do={:set ($arrm->1) -1; :set ($arrm->2) 30}
:local totd ((($yyyy - 1970) * 365) + (($yyyy - 1968) / 4) + ($arrm->$M) + ([:pick $vdate ($vdoff->4) ($vdoff->5)] - 1))
:return (((((($totd * 24) + [:pick $vtime 0 2]) * 60) + [:pick $vtime 3 5]) * 60) + [:pick $vtime 6 8] - $vgmt)
}
/interface
:foreach iface in=[find] do={
:local if [get $iface name]
:local dwn [get $if link-downs]
:local lup [get $if last-link-up-time]
:local ldn [get $if last-link-down-time]
:local sup [$datetime2epoch $lup]
:local sdn [$datetime2epoch $ldn]
:local snw [$datetime2epoch]
:local sdf ($sup - $sdn)
:if ( ([:typeof $lup] != "nil") and ([:typeof $ldn] != "nil")) do={
:if ($sdf < 0) do={
:put "\r\n*** WARNING ***: Interface $if go down $dwn times, is never go up again after >$ldn<, $($snw-$sdn) seconds ago!!!\r\n"
} else={
:put "The interface $if go down $dwn times. Last up is >$lup< and is up for $($snw-$sup) seconds"
}
} else={
:if ([:typeof $lup] = "nil") do={
:put "The interface $if is never go up from the start of the device."
} else={
:put "The interface $if is never go down, last up is >$lup< and is up for $($snw-$sup) seconds"
}
}
}