summaryrefslogtreecommitdiff
path: root/src/op_mode/show_dhcp.py
blob: 652173dc16a00b1c7530e6e6987d5a82c7b1037f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/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 sys

from vyos.config import Config
from isc_dhcp_leases import Lease, IscDhcpLeases

lease_file = "/config/dhcpd.leases"
pool_key = "shared-networkname"

def in_pool(lease, pool):
    if pool_key in lease.sets:
        if lease.sets[pool_key] == pool:
            return True

    return False

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["hardware_address"] = lease.ethernet
    data["hostname"] = lease.hostname
    data["ip"] = lease.ip

    try:
        data["pool"] = lease.sets[pool_key]
    except:
        data["pool"] = ""

    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()

    # 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 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"]

    lease_list = []
    for l in 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):
    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()

    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("-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()

    # Do nothing if service is not configured
    config = Config()
    if not config.exists_effective('service dhcp-server'):
        print("DHCP service is not configured")
        sys.exit(0)

    if args.leases:
        if args.expired:
            if args.pool:
                leases = get_leases(lease_file, state='free', pool=args.pool)
            else:
                leases = get_leases(lease_file, state='free')
        else:
            if args.pool:
                leases = get_leases(lease_file, state='active', pool=args.pool)
            else:
                leases = get_leases(lease_file, state='active')

        if args.json:
            print(json.dumps(leases, indent=4))
        else:
            show_leases(leases)
    elif args.statistics:
        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))

            if size != 0:
                use_percentage = round(leases / size * 100)
            else:
                use_percentage = 0

            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")