parsing dhcpd.leases file

Daniel Duarte dan.r.duarte at
Wed Apr 15 17:22:12 UTC 2009


I know this topic has been discussed in the past, and there's lot of
implementations on the internet, but I would like to explain how I'm
thinking to parse the dhcpd.leases. I believe there's many with
experience on this area, and I would like to ear your thoughts about
this solution.

- parse periodically the lease file (?every hour? every 10 minutes?),
in order to load all of the client transactions on a DB;
- change as little as possible on the DHCP server. All the work should
be done on another server.
- avoid duplicate data, and re-parsing the same events more than once;

Main Problems:
- dhcpd.leases rotates to dhcpd.leases~ at uncontrolled moments;
- the new file contains information already existent on the previous file;

Possible solutions, I'm thinking to tackle the problems on the following way:
- Both files (leases and leases~) are copied from the DHCP server to
another machine;

- To solve the problem of the rotating files, the date and the size of
the old file dhcp.leases~ is stored on file. My parser, would
determine if the lease file has rotated by checking the date of the ~
file. If it has changed from the previous time it run, then it must
parse both files, otherwise it will just parse the dhcpd.leases file.
The size of the ~ file would allow the program to determine if the
file has rotated more than once since the last time it run;

- To avoid re-parsing duplicates, the parser would have a bookmark
file where, every time it runs, saves the line number where it stops
parsing. The next time it runs, it would start parsing from that line.

On a more structured way:

if ~filetime.present == ~filetime.last {
  # lease file didn't rotate
  if bookmarkfile exists, start parsing leasefile from line x;
  else parse entire leasefile;
  store lastparsedlinenumber x on bookmarkfile;
else {
  # lease file has rotated
  if bookmarkfile exists && (~filesize.present == ~filesize.last) {
     # the file has rotated only once since the last time it run
     start parsing ~leasefile from line x;
  } else {
     # no bookmark info, or file has rotated more than once
     parse entire ~leasefile;
  parse entire leasefile && query DB to check if it's a new lease;
  store lastparsedlinenumber x on bookmarkfile;

- When the file rotates, will always have to process some leases that
have already been processed on the ~ file; On this situation, I'll
probably query the DB to know if each event has already been saved, or
if it's a new lease.
- The parser should run at least once before the lease file rotates.
Otherwise we'll lose data;

Main Advantage:
Completely independent of the DHCP Server. I'll only scp the files
from there, and read it's parameters (date & time).

Any ideas?

Thanks in advance,
Daniel Duarte

More information about the dhcp-users mailing list