I need to parse dhcpd.leases to store data in mysql

Simon Hobson dhcp1 at thehobsons.co.uk
Fri Jun 30 07:03:15 UTC 2006


Sébastien CRAMATTE wrote:

>But how do you parse the lease ?

In basic terms, you read the file and parse 
what's in it ! You've already been given an 
example in Perl that will do it. I doubt if 
anyone has something they can share with you, 
because not many people do it, and it's probably 
that no-one has done exactly what you are after.


There's a bit more to it than that though. 
Because of the way that dhcp works, if you just 
parse the file every hour or so, there is a 
chance that you could miss some intermediate log 
records. If you parse it every few minutes, then 
you vastly reduce that opportunity to miss 
things, but you will spend a lot of time just 
re-parsing what you've already done. Either way 
you will need to handle the case where records 
already exist, so the database operations become 
non-trivial.

Which is right for you depends on what you want 
to achieve. Do you want to capture every detail 
with minimum latency, or are you happy to just 
get an overview that could be an hour or two old ?

For minimum latency and maximum 'resolution', I'd 
be considering a loop that reads the leases file 
and then waits when it gets to the end so that it 
can read further records as they are added (like 
"tail -f" does). You will need to watch for when 
the file moves and becomes the backup file, at 
which point you'll need to start over.

For minimum overhead whilst still capturing 
everything, watch for the backup file changing 
and then process it. This will give you 
everything the server did in the previous hour.

If you don't care about processing overhead, just 
parse the leases file (or the backup file) every 
so often. Run the program from cron, and it's the 
simplest way to do it.


You basic process will need to be something like :

Repeat
   read a record and parse it
   is it a record type we're interested in
     - if not then skip
   does it already exist in the database
     - if yes, then update the record if required
     - else add the record
until you run out of data


Simon


More information about the dhcp-users mailing list