cli: print FRR uptime on same format as FRR

Use the same uptime representation as FRR does.

Example output:
DESTINATION            PREF NEXT-HOP   PROTO   UPTIME
>* 10.0.23.0/30        0/0 e6          direct  20:10:05
192.168.3.0/24         110/1 e2        ospfv2  5d13h05m
>* 192.168.3.0/24      0/0 e2          direct  01w1d13h

Signed-off-by: Richard Alpe <richard@bit42.se>
This commit is contained in:
Richard Alpe
2024-10-08 10:54:36 +02:00
parent 950e868eb3
commit 03245f3631
3 changed files with 37 additions and 23 deletions
+17 -3
View File
@@ -150,6 +150,9 @@ class Route:
def datetime2uptime(self):
"""Convert 'last-updated' string to uptime in AAhBBmCCs format."""
ONE_DAY_SECOND = 60 * 60 * 24
ONE_WEEK_SECOND = ONE_DAY_SECOND * 7
if not self.last_updated:
return "0h0m0s"
@@ -164,10 +167,21 @@ class Route:
current_time = datetime_now()
uptime_delta = current_time - last_updated
hours, remainder = divmod(int(uptime_delta.total_seconds()), 3600)
minutes, seconds = divmod(remainder, 60)
total_seconds = int(uptime_delta.total_seconds())
total_days = total_seconds // ONE_DAY_SECOND
total_weeks = total_days // 7
return f"{hours}h{minutes}m{seconds}s"
hours = (total_seconds % ONE_DAY_SECOND) // 3600
minutes = (total_seconds % 3600) // 60
seconds = total_seconds % 60
if total_seconds < ONE_DAY_SECOND:
return f"{hours:02}:{minutes:02}:{seconds:02}"
elif total_seconds < ONE_WEEK_SECOND:
return f"{total_days}d{hours:02}h{minutes:02}m"
else:
days_remaining = total_days % 7
return f"{total_weeks:02}w{days_remaining}d{hours:02}h"
def print(self):
PadRoute.set(self.ip)