diff options
author | Christian Poessinger <christian@poessinger.com> | 2019-05-30 23:30:01 +0200 |
---|---|---|
committer | John Estabrook <jestabro@sentrium.io> | 2020-01-17 14:40:39 -0600 |
commit | 06133a4162b45dc30391a9cbfa4df56c6e658867 (patch) | |
tree | be2f342526e32ffd230a298c989d7b6156c44b36 /src | |
parent | caec7a022a5760cac1bf67499e77c3028571d510 (diff) | |
download | vyos-1x-06133a4162b45dc30391a9cbfa4df56c6e658867.tar.gz vyos-1x-06133a4162b45dc30391a9cbfa4df56c6e658867.zip |
[dhcp] T1416: fix DHCP server status view
(cherry picked from commit 7a27f6f93b58abd2fabc9e80e429e14a70ebd6aa)
Diffstat (limited to 'src')
-rwxr-xr-x | src/op_mode/show_dhcp.py | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/src/op_mode/show_dhcp.py b/src/op_mode/show_dhcp.py index 4c4ee6355..652173dc1 100755 --- a/src/op_mode/show_dhcp.py +++ b/src/op_mode/show_dhcp.py @@ -55,15 +55,28 @@ def get_lease_data(lease): return data def get_leases(leases, state=None, pool=None): + # define variable for leases + leases_dict = {} + + # get leases from file leases = IscDhcpLeases(lease_file).get() - if state is not None: - leases = list(filter(lambda x: x.binding_state == 'active', leases)) + # convert leases list to dictionary to avoid records duplication - it's the fastest and easiest way to do this + for lease in leases: + leases_dict[lease.ip] = lease + + # filter leases by state + if state is 'active': + leases = list(filter(lambda x: x.active and x.valid, leases_dict.values())) + if state is 'free': + leases = list(filter(lambda x: x.binding_state == 'free', leases_dict.values())) + # filter lease by pool name if pool is not None: leases = list(filter(lambda x: in_pool(x, pool), leases)) - return list(map(get_lease_data, leases)) + # return sorted leases list + return sorted(list(map(get_lease_data, leases)), key = lambda k: k['ip']) def show_leases(leases): headers = ["IP address", "Hardware address", "Lease expiration", "Pool", "Client Name"] @@ -73,7 +86,7 @@ def show_leases(leases): lease_list.append([l["ip"], l["hardware_address"], l["expires"], l["pool"], l["hostname"]]) output = tabulate.tabulate(lease_list, headers) - + print(output) def get_pool_size(config, pool): @@ -146,7 +159,7 @@ if __name__ == '__main__': leases = len(get_leases(lease_file, state='active', pool=args.pool)) if size != 0: - use_percentage = round(leases / size) * 100 + use_percentage = round(leases / size * 100) else: use_percentage = 0 |