From 1a44d8607f715934f2c03f28a9bf547321b26ed8 Mon Sep 17 00:00:00 2001 From: Christian Breunig Date: Sun, 30 Jul 2023 22:03:53 +0200 Subject: T3355: always work with UNIX timestamps for dhcp client lease display ISC dhcp client contains least_update timestamp in human readable format this makes less sense for an API and also the expiry timestamp is provided in UNIX time. Convert string (e.g. Sun Jul 30 18:13:44 CEST 2023) to UNIX time (1690733624) vyos@vyos:~$ ${vyos_op_scripts_dir}/dhcp.py show_client_leases --family inet --interface eth0.10 Interface eth0.10 IP address 172.16.33.123 [Active] Subnet Mask 255.255.255.0 Domain Name vyos.net Router 172.16.33.254 Name Server 172.16.254.30 DHCP Server 172.16.33.254 DHCP Server 86400 Last Update Sun Jul 30 18:13:44 CEST 2023 Expiry Mon Jul 31 18:13:43 CEST 2023 vyos@vyos:~$ ${vyos_op_scripts_dir}/dhcp.py show_client_leases --family inet --interface eth0.10 --raw [ { "last_update": 1690733624, "reason": "RENEW", "interface": "eth0.10", "new_expiry": "1690820023", "new_dhcp_lease_time": "86400", "medium": "", "alias_ip_address": "", "new_ip_address": "172.16.33.123", "new_broadcast_address": "172.16.33.255", "new_subnet_mask": "255.255.255.0", "new_domain_name": "vyos.net", "new_network_number": "172.16.33.0", "new_domain_name_servers": "172.16.254.30", "new_routers": "172.16.33.254", "new_static_routes": "", "new_dhcp_server_identifier": "172.16.33.254", "new_dhcp_message_type": "5", "old_ip_address": "172.16.33.123", "old_subnet_mask": "255.255.255.0", "old_domain_name": "vyos.net", "old_domain_name_servers": "172.16.254.30", "old_routers": "172.16.33.254", "old_static_routes": "" } ] --- src/op_mode/dhcp.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/op_mode/dhcp.py b/src/op_mode/dhcp.py index 653e670c5..20ef698bd 100755 --- a/src/op_mode/dhcp.py +++ b/src/op_mode/dhcp.py @@ -34,6 +34,8 @@ from vyos.utils.file import read_file from vyos.utils.process import cmd from vyos.utils.process import is_systemd_service_running +time_string = "%a %b %d %H:%M:%S %Z %Y" + config = ConfigTreeQuery() lease_valid_states = ['all', 'active', 'free', 'expired', 'released', 'abandoned', 'reset', 'backup'] sort_valid_inet = ['end', 'mac', 'hostname', 'ip', 'pool', 'remaining', 'start', 'state'] @@ -291,6 +293,9 @@ def show_server_leases(raw: bool, family: ArgFamily, pool: typing.Optional[str], def _get_raw_client_leases(family='inet', interface=None): + from time import mktime + from datetime import datetime + lease_dir = '/var/lib/dhcp' lease_files = [] lease_data = [] @@ -309,7 +314,11 @@ def _get_raw_client_leases(family='inet', interface=None): for line in f.readlines(): line = line.rstrip() if 'last_update' not in tmp: - tmp.update({'last_update' : line}) + # ISC dhcp client contains least_update timestamp in human readable + # format this makes less sense for an API and also the expiry + # timestamp is provided in UNIX time. Convert string (e.g. Sun Jul + # 30 18:13:44 CEST 2023) to UNIX time (1690733624) + tmp.update({'last_update' : int(mktime(datetime.strptime(line, time_string).timetuple()))}) continue k, v = line.split('=') @@ -344,9 +353,10 @@ def _get_formatted_client_leases(lease_data, family): if 'new_dhcp_lease_time' in lease: data_entries.append(["DHCP Server", lease['new_dhcp_lease_time']]) if 'last_update' in lease: - data_entries.append(["Last Update", lease['last_update']]) + tmp = strftime(time_string, localtime(int(lease['last_update']))) + data_entries.append(["Last Update", tmp]) if 'new_expiry' in lease: - tmp = strftime('%a %b %d %H:%M:%S %Z %Y', localtime(int(lease['new_expiry']))) + tmp = strftime(time_string, localtime(int(lease['new_expiry']))) data_entries.append(["Expiry", tmp]) # Add empty marker -- cgit v1.2.3