A dhcp-script
for Dnsmasq
that makes it manage DHCP lease information in an SQLite database.
Dnsmasq has a dhcp-script
configuration parameter.
When set, Dnsmasq invokes the script whenever DHCP lease information changes.
When combined with leasefile-ro
it fully replaces the leases file.
The script tracks leases in an SQLite database. It also accumulates client information and requests.
The leases file that Dnsmasq uses by default is a point-in-time snapshot.
Dnsmasq adds and removes from the file.
No long-term record is kept.
Using log-dhcp
helps, but the log files do not make the data easy to use.
However, by storing the requests in a database and associating them with client and lease information,
we can keep a complete history of DHCP usage.
On the host running Dnsmasq:
- Install SQLite and use it to create a database using the schema below.
- Save the script to a bin directory, e.g., /usr/local/sbin.
- Configure Dnsmasq to use the script on the database.
Or download and run the install.sh
(below) on your DHCP server and run it:
curl -sLo - https://gist.githubusercontent.com/amigus/6a9e4151d175d04bf05337b815f2213e/raw/install.sh
| sh
The schema stores requests, leases, and client information. The script adds to and deletes from the leases table as required to maintain it, as it does the leases file by default. Nothing is deleted from requests so that it can grow significantly over time. Nothing is deleted from clients either; however, the primary key is the ethernet address, so the table never grows big enough to cause an issue.
sqlite3 /var/lib/misc/dnsmasq.leases.db /path/to/database.sql
chown dnsmasq:dnsmasq /var/lib/misc/dnsmasq.leases.db
chgrp dnsmasq /var/lib/misc
chmod g+w /var/lib/misc
Dnsmasq passes information to the script via command-line arguments and environment variables.
The script converts the information into SQL statements compatible with the schema.
It uses the sqlite3
command to execute the statements as a single transaction on the SQLite database.
The script supports the init
argument using the dnsmasq
VIEW.
Dnsmasq invokes the script with init
when it starts instead of reading the leases file.
install -m 550 -o root -g dnsmasq /path/to/dnsmasq-dhcp.sh /usr/local/sbin
Dnsmasq invokes the script in dhcp-script
.
The script-on-renewal
parameter causes the script to store renewal requests in the database.
The leasefile-ro
option disables the built-in leases file.
See the Dnsmasq man page for the details.
dhcp-script=/usr/local/sbin/dnsmasq-dhcp.sh
script-on-renewal
leasefile-ro
Dnsmasq runs the script as root by default but the script does not need root privilege.
Add the dhcp-scriptuser
configuration parameter to run the script as a non-root user.
dhcp-scriptuser=dnsmasq
Note, however, that the user must have write access to the SQLite database and the parent directory.
dhcp:~# ls -ahl /var/lib/misc
total 29M
drwxrwxr-x 2 root dnsmasq 4.0K Sep 3 10:07 .
drwxr-xr-x 6 root root 4.0K Apr 2 14:58 ..
-rw-r--r-- 1 dnsmasq dnsmasq 0 Apr 30 20:31 dnsmasq.leases
-rw-rw---- 1 dnsmasq dnsmasq 28.6M Sep 3 10:07 dnsmasq.leases.db
dhcp:~#