about summary refs log tree commit diff
path: root/tools/dnsmasq-leases-html/dnsmasq-leases-html.py
diff options
context:
space:
mode:
authorFranck Cuny <franck@fcuny.net>2023-04-06 17:37:27 -0700
committerFranck Cuny <franck@fcuny.net>2023-04-06 19:21:17 -0700
commit9516b633fa651875503bb61b5194bfb7e5f409e3 (patch)
treebc63b50a4f78ca72976d27a6cee6f4ca2301e213 /tools/dnsmasq-leases-html/dnsmasq-leases-html.py
parentmonitoring: small fixes (diff)
downloadworld-9516b633fa651875503bb61b5194bfb7e5f409e3.tar.gz
tools/dnsmasq-leases-html: create a HTML page with leases from dnsmasq
Parse the file that contains all the leases assigned by dnsmasq, and
create a static HTML page from it. This can be served by nginx to make
it easy to see what IP is assigned to a machine, and which machines are
currently on the network.
Diffstat (limited to 'tools/dnsmasq-leases-html/dnsmasq-leases-html.py')
-rwxr-xr-xtools/dnsmasq-leases-html/dnsmasq-leases-html.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/tools/dnsmasq-leases-html/dnsmasq-leases-html.py b/tools/dnsmasq-leases-html/dnsmasq-leases-html.py
new file mode 100755
index 0000000..c1f03db
--- /dev/null
+++ b/tools/dnsmasq-leases-html/dnsmasq-leases-html.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python3
+
+import datetime
+import ipaddress
+import os
+
+from jinja2 import Environment, FileSystemLoader
+
+
+outfile = os.getenv("DNSMASQ_LEASES_OUT", "/var/lib/dnsmasq/leases.html")
+leases_file = os.getenv("DNSMASQ_LEASES", "/var/lib/dnsmasq/dnsmasq.leases")
+
+leases = []
+
+with open(leases_file, "r") as f:
+    for line in f:
+        content = line.rstrip("\n").split(" ")
+        lease = dict()
+        if int(content[0]) == 0:
+            lease["expire"] = "never"
+        else:
+            lease["expire"] = datetime.datetime.fromtimestamp(int(content[0]))
+        lease["MAC"] = content[1]
+        lease["IP"] = ipaddress.ip_address(content[2])
+        lease["hostname"] = content[3]
+        leases.append(lease)
+
+leases = sorted(leases, key=lambda d: d["IP"])
+
+dir_path = os.path.dirname(os.path.realpath(__file__))
+templates_dir = os.path.join(dir_path, "templates")
+environment = Environment(loader=FileSystemLoader(templates_dir))
+template = environment.get_template("index.html")
+
+content = template.render(leases=leases)
+with open(outfile, "w") as fh:
+    print(content, file=fh)