From bf2b73a4fcc7b5457ece0504e5306eaadf43f1e7 Mon Sep 17 00:00:00 2001 From: metron2 Date: Mon, 6 Jan 2025 17:55:13 -0500 Subject: [PATCH 1/3] T6998: dhcpy.py - fix datetime to be timezone aware --- src/op_mode/dhcp.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/op_mode/dhcp.py b/src/op_mode/dhcp.py index 45de86cab7..ee60d7cd28 100755 --- a/src/op_mode/dhcp.py +++ b/src/op_mode/dhcp.py @@ -101,7 +101,7 @@ def _get_raw_server_leases(family='inet', pool=None, sorted=None, state=[], orig lifetime = lease['valid-lft'] expiry = (lease['cltt'] + lifetime) - lease['start_timestamp'] = datetime.fromtimestamp(expiry - lifetime, timezone.utc) + lease['start_timestamp'] = datetime.fromtimestamp(lease['cltt'], timezone.utc) lease['expire_timestamp'] = datetime.fromtimestamp(expiry, timezone.utc) if expiry else None data_lease = {} @@ -170,10 +170,8 @@ def _get_formatted_server_leases(raw_data, family='inet'): ipaddr = lease.get('ip') hw_addr = lease.get('mac') state = lease.get('state') - start = lease.get('start') - start = _utc_to_local(start).strftime('%Y/%m/%d %H:%M:%S') - end = lease.get('end') - end = _utc_to_local(end).strftime('%Y/%m/%d %H:%M:%S') if end else '-' + start = datetime.fromtimestamp(lease.get('start'), timezone.utc) + end = datetime.fromtimestamp(lease.get('end'), timezone.utc) if lease.get('end') else '-' remain = lease.get('remaining') pool = lease.get('pool') hostname = lease.get('hostname') @@ -187,10 +185,8 @@ def _get_formatted_server_leases(raw_data, family='inet'): for lease in raw_data: ipaddr = lease.get('ip') state = lease.get('state') - start = lease.get('last_communication') - start = _utc_to_local(start).strftime('%Y/%m/%d %H:%M:%S') - end = lease.get('end') - end = _utc_to_local(end).strftime('%Y/%m/%d %H:%M:%S') + start = datetime.fromtimestamp(lease.get('last_communication'), timezone.utc) + end = datetime.fromtimestamp(lease.get('end'), timezone.utc) if lease.get('end') else '-' remain = lease.get('remaining') lease_type = lease.get('type') pool = lease.get('pool') From d607bcb2ee57a4c107c24e5b48bcb43f00d652f4 Mon Sep 17 00:00:00 2001 From: metron2 Date: Mon, 6 Jan 2025 17:56:01 -0500 Subject: [PATCH 2/3] T6998: dhcp.py - fix for empty hostname handling # Conflicts: # src/op_mode/dhcp.py --- src/op_mode/dhcp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/op_mode/dhcp.py b/src/op_mode/dhcp.py index ee60d7cd28..72c0a2d0f0 100755 --- a/src/op_mode/dhcp.py +++ b/src/op_mode/dhcp.py @@ -113,7 +113,7 @@ def _get_raw_server_leases(family='inet', pool=None, sorted=None, state=[], orig data_lease['origin'] = 'local' # TODO: Determine remote in HA data_lease['hostname'] = lease.get('hostname', '-') # remove trailing dot to ensure consistency for `vyos-hostsd-client` - if data_lease['hostname'] and data_lease['hostname'][-1] == '.': + if len(data_lease['hostname']) > 1 and data_lease['hostname'][-1] == '.': data_lease['hostname'] = data_lease['hostname'][:-1] if family == 'inet': From 892b35d87dc0154f374f988e73315f28c3f5d81f Mon Sep 17 00:00:00 2001 From: metron2 Date: Thu, 9 Jan 2025 23:59:08 -0500 Subject: [PATCH 3/3] T6998: dhcp.py fix remaining calculating and display --- src/op_mode/dhcp.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/op_mode/dhcp.py b/src/op_mode/dhcp.py index 72c0a2d0f0..3346dca31d 100755 --- a/src/op_mode/dhcp.py +++ b/src/op_mode/dhcp.py @@ -18,7 +18,7 @@ import sys import typing -from datetime import datetime +from datetime import datetime, timedelta from datetime import timezone from glob import glob from ipaddress import ip_address @@ -132,12 +132,10 @@ def _get_raw_server_leases(family='inet', pool=None, sorted=None, state=[], orig data_lease['remaining'] = '-' if lease['valid-lft'] > 0: - data_lease['remaining'] = lease['expire_timestamp'] - datetime.now(timezone.utc) - - if data_lease['remaining'].days >= 0: + if lease['expire_timestamp'] > datetime.now(timezone.utc): # substraction gives us a timedelta object which can't be formatted with strftime # so we use str(), split gets rid of the microseconds - data_lease['remaining'] = str(data_lease['remaining']).split('.')[0] + data_lease['remaining'] = str(lease['expire_timestamp'] - datetime.now(timezone.utc)).split('.')[0] # Do not add old leases if data_lease['remaining'] != '' and data_lease['pool'] in pool and data_lease['state'] != 'free':