summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@baturin.org>2018-09-04 19:47:43 +0200
committerDaniil Baturin <daniil@baturin.org>2018-09-04 19:47:43 +0200
commit2053539a7d532941e2cdbcc37fff26a1b9c95447 (patch)
tree68338ff3b367708da1feafab6db011047b63b4f0
parent93f6b6a3b23f4169a4af4a23eb795d041be84e42 (diff)
downloadvyos-1x-2053539a7d532941e2cdbcc37fff26a1b9c95447.tar.gz
vyos-1x-2053539a7d532941e2cdbcc37fff26a1b9c95447.zip
T823: extend the new DHCP op mode.
-rw-r--r--op-mode-definitions/dhcp.xml111
-rwxr-xr-xsrc/op_mode/show_dhcp.py58
-rwxr-xr-xsrc/op_mode/show_dhcpv6.py82
3 files changed, 250 insertions, 1 deletions
diff --git a/op-mode-definitions/dhcp.xml b/op-mode-definitions/dhcp.xml
new file mode 100644
index 000000000..eb57f8f1f
--- /dev/null
+++ b/op-mode-definitions/dhcp.xml
@@ -0,0 +1,111 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interfaceDefinition>
+ <node name="show">
+ <children>
+ <node name="dhcp">
+ <properties>
+ <help>Show DHCP (Dynamic Host Configuration Protocol) information</help>
+ </properties>
+ <children>
+ <node name="server">
+ <properties>
+ <help>Show DHCP information</help>
+ </properties>
+ <children>
+ <node name="leases">
+ <properties>
+ <help>Show DHCP server leases</help>
+ </properties>
+ <command>sudo ${vyos_op_scripts_dir}/show_dhcp.py --leases</command>
+ <children>
+ <tagNode name="pool">
+ <properties>
+ <help>Show DHCP leases for a specific pool</help>
+ </properties>
+ <command>sudo ${vyos_op_scripts_dir}/show_dhcp.py --leases --pool $4</command>
+ </tagNode>
+ </children>
+ </node>
+ <node name="statistics">
+ <properties>
+ <help>Show DHCP server statistics</help>
+ </properties>
+ <command>sudo ${vyos_op_scripts_dir}/show_dhcp.py --statistics</command>
+ <children>
+ <tagNode name="pool">
+ <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>
+ </tagNode>
+ </children>
+ </node>
+ </children>
+ </node>
+ </children>
+ </node>
+ <node name="dhcpv6">
+ <properties>
+ <help>Show DHCPv6 (IPv6 Dynamic Host Configuration Protocol) information</help>
+ </properties>
+ <children>
+ <node name="server">
+ <properties>
+ <help>Show DHCPv6 server information</help>
+ </properties>
+ <children>
+ <node name="leases">
+ <properties>
+ <help>Show DHCPv6 server leases</help>
+ </properties>
+ <command>sudo ${vyos_op_scripts_dir}/show_dhcpv6.py --leases</command>
+ </node>
+ </children>
+ </node>
+ </children>
+ </node>
+ </children>
+ </node>
+ <node name="restart">
+ <children>
+ <node name="dhcp">
+ <properties>
+ <help>Restart DHCP processes</help>
+ </properties>
+ <children>
+ <node name="server">
+ <properties>
+ <help>Restart the DHCP server process</help>
+ </properties>
+ <command>sudo systemctl restart isc-dhcp-server.service</command>
+ </node>
+ <node name="relay-agent">
+ <properties>
+ <help>Restart the DHCP server process</help>
+ </properties>
+ <command>sudo /opt/vyatta/sbin/dhcrelay-starter.pl --op-mode --init='/opt/vyatta/sbin/dhcrelay.init'</command>
+ </node>
+ </children>
+ </node>
+ <node name="dhcpv6">
+ <properties>
+ <help>Restart DHCPv6 processes</help>
+ </properties>
+ <children>
+ <node name="server">
+ <properties>
+ <help>Restart the DHCPv6 server process</help>
+ </properties>
+ <command>sudo systemctl restart isc-dhcpv6-server.service</command>
+ </node>
+ <node name="relay-agent">
+ <properties>
+ <help>Restart the DHCP server process</help>
+ </properties>
+ <command>sudo /opt/vyatta/sbin/dhcv6relay-starter.pl --op_mode --config_action ACTIVE</command>
+ </node>
+ </children>
+ </node>
+ </children>
+ </node>
+</interfaceDefinition>
diff --git a/src/op_mode/show_dhcp.py b/src/op_mode/show_dhcp.py
index 2084a9509..e76fc3a14 100755
--- a/src/op_mode/show_dhcp.py
+++ b/src/op_mode/show_dhcp.py
@@ -15,7 +15,9 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
+import json
import argparse
+import ipaddress
import tabulate
@@ -76,6 +78,24 @@ def show_leases(leases):
print(output)
+def get_pool_size(config, pool):
+ size = 0
+ subnets = config.list_effective_nodes("service dhcp-server shared-network-name {0} subnet".format(pool))
+ for s in subnets:
+ ranges = config.list_effective_nodes("service dhcp-server shared-network-name {0} subnet {1} range".format(pool, s))
+ for r in ranges:
+ start = config.return_effective_value("service dhcp-server shared-network-name {0} subnet {1} range {2} start".format(pool, s, r))
+ stop = config.return_effective_value("service dhcp-server shared-network-name {0} subnet {1} range {2} stop".format(pool, s, r))
+
+ size += int(ipaddress.IPv4Address(stop)) - int(ipaddress.IPv4Address(start))
+
+ return size
+
+def show_pool_stats(stats):
+ headers = ["Pool", "Size", "Leases", "Available", "Usage"]
+ output = tabulate.tabulate(stats, headers)
+
+ print(output)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
@@ -86,6 +106,7 @@ if __name__ == '__main__':
parser.add_argument("-e", "--expired", action="store_true", help="Show expired leases")
parser.add_argument("-p", "--pool", type=str, action="store", help="Show lease for specific pool")
+ parser.add_argument("-j", "--json", action="store_true", default=False, help="Product JSON output")
args = parser.parse_args()
@@ -101,4 +122,39 @@ if __name__ == '__main__':
else:
leases = get_leases(lease_file, state='active')
- show_leases(leases)
+ if args.json:
+ print(json.dumps(leases, indent=4))
+ else:
+ show_leases(leases)
+ elif args.statistics:
+ config = vyos.config.Config()
+
+ pools = []
+
+ # Get relevant pools
+ if args.pool:
+ pools = [args.pool]
+ else:
+ pools = config.list_effective_nodes("service dhcp-server shared-network-name")
+
+ # Get pool usage stats
+ stats = []
+ for p in pools:
+ size = get_pool_size(config, p)
+ leases = len(get_leases(lease_file, state='active', pool=args.pool))
+ use_percentage = round(leases / size) * 100
+ if args.json:
+ pool_stats = {"pool": p, "size": size, "leases": leases,
+ "available": (size - leases), "percentage": use_percentage}
+ else:
+ # For tabulate
+ pool_stats = [p, size, leases, size - leases, "{0}%".format(use_percentage)]
+ stats.append(pool_stats)
+
+ # Print stats
+ if args.json:
+ print(json.dumps(stats, indent=4))
+ else:
+ show_pool_stats(stats)
+ else:
+ print("Use either --leases or --statistics option")
diff --git a/src/op_mode/show_dhcpv6.py b/src/op_mode/show_dhcpv6.py
new file mode 100755
index 000000000..8879a45c5
--- /dev/null
+++ b/src/op_mode/show_dhcpv6.py
@@ -0,0 +1,82 @@
+#!/usr/bin/env python3
+#
+# Copyright (C) 2018 VyOS maintainers and contributors
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2 or later as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+
+import json
+import argparse
+import ipaddress
+
+import tabulate
+
+import vyos.config
+
+from isc_dhcp_leases import Lease, IscDhcpLeases
+
+
+lease_file = "/config/dhcpdv6.leases"
+
+def get_lease_data(lease):
+ data = {}
+
+ # End time may not be present in backup leases
+ try:
+ data["expires"] = lease.end.strftime("%Y/%m/%d %H:%M:%S")
+ except:
+ data["expires"] = ""
+
+ data["duid"] = lease.host_identifier_string
+ data["ip"] = lease.ip
+
+ return data
+
+def get_leases(leases, state=None):
+ leases = IscDhcpLeases(lease_file).get()
+
+ if state is not None:
+ leases = list(filter(lambda x: x.binding_state == 'active', leases))
+
+ return list(map(get_lease_data, leases))
+
+def show_leases(leases):
+ headers = ["IPv6 address", "Lease expiration", "DUID"]
+
+ lease_list = []
+ for l in leases:
+ lease_list.append([l["ip"], l["expires"], l["duid"]])
+
+ output = tabulate.tabulate(lease_list, headers)
+
+ print(output)
+
+if __name__ == '__main__':
+ parser = argparse.ArgumentParser()
+
+ group = parser.add_mutually_exclusive_group()
+ group.add_argument("-l", "--leases", action="store_true", help="Show DHCP leases")
+ group.add_argument("-s", "--statistics", action="store_true", help="Show DHCP statistics")
+
+ parser.add_argument("-p", "--pool", type=str, action="store", help="Show lease for specific pool")
+ parser.add_argument("-j", "--json", action="store_true", default=False, help="Product JSON output")
+
+ args = parser.parse_args()
+
+ if args.leases:
+ leases = get_leases(lease_file, state='active')
+ show_leases(leases)
+ elif args.statistics:
+ print("DHCPv6 statistics option is not available")
+ else:
+ print("Invalid option")