diff options
author | Christian Poessinger <christian@poessinger.com> | 2019-05-30 23:30:01 +0200 |
---|---|---|
committer | Christian Poessinger <christian@poessinger.com> | 2019-05-30 23:30:01 +0200 |
commit | 7a27f6f93b58abd2fabc9e80e429e14a70ebd6aa (patch) | |
tree | 06ac8a4e45e3e51613bbb62d753aeddc95faa17e /src/op_mode/show_dhcp.py | |
parent | 0a16c85a8b6aa728e142156e0e985ac21916db20 (diff) | |
download | vyos-1x-7a27f6f93b58abd2fabc9e80e429e14a70ebd6aa.tar.gz vyos-1x-7a27f6f93b58abd2fabc9e80e429e14a70ebd6aa.zip |
[dhcp] T1416: fix DHCP server status view
Diffstat (limited to 'src/op_mode/show_dhcp.py')
-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 |