summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2019-05-30 23:30:01 +0200
committerChristian Poessinger <christian@poessinger.com>2019-05-30 23:30:01 +0200
commit7a27f6f93b58abd2fabc9e80e429e14a70ebd6aa (patch)
tree06ac8a4e45e3e51613bbb62d753aeddc95faa17e
parent0a16c85a8b6aa728e142156e0e985ac21916db20 (diff)
downloadvyos-1x-7a27f6f93b58abd2fabc9e80e429e14a70ebd6aa.tar.gz
vyos-1x-7a27f6f93b58abd2fabc9e80e429e14a70ebd6aa.zip
[dhcp] T1416: fix DHCP server status view
-rw-r--r--op-mode-definitions/dhcp.xml4
-rwxr-xr-xsrc/op_mode/show_dhcp.py23
2 files changed, 20 insertions, 7 deletions
diff --git a/op-mode-definitions/dhcp.xml b/op-mode-definitions/dhcp.xml
index a7d09304e..85403af6f 100644
--- a/op-mode-definitions/dhcp.xml
+++ b/op-mode-definitions/dhcp.xml
@@ -22,7 +22,7 @@
<properties>
<help>Show DHCP leases for a specific pool</help>
</properties>
- <command>sudo ${vyos_op_scripts_dir}/show_dhcp.py --leases --pool $4</command>
+ <command>sudo ${vyos_op_scripts_dir}/show_dhcp.py --leases --pool $6</command>
</tagNode>
</children>
</node>
@@ -36,7 +36,7 @@
<properties>
<help>Show DHCP server statistics for a specific pool</help>
</properties>
- <command>sudo ${vyos_op_scripts_dir}/show_dhcp.py --statistics --pool $4</command>
+ <command>sudo ${vyos_op_scripts_dir}/show_dhcp.py --statistics --pool $6</command>
</tagNode>
</children>
</node>
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