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

Simon Hobson dhcp1 at thehobsons.co.uk
Fri Jun 30 08:34:09 UTC 2006

Sébastien CRAMATTE wrote:

>Well  I would like to run a  "tail -q -n+0 --follow=name --retry 
>/var/lib/dhcpd3.leases"   background process
>And get leases from this stream. By this mean I don't need to parse all
>file every times (I've got more than 2000 ips, leases are renewed every
>24H )

That makes sense, I haven't tried it, but I think 
you'll find that you get the whole leases file 
spittng out every hour as well as all the 
intermediate updates.

>But with your code how can know if a lease was parsed before ?  I don't
>want to store 2 times the same lease ? I  should use  "lease,starts,
>end, mac, state"as database unique keys ????

Well that's part of your database design !

We can offer suggestions, but you will have to 
decide what will work for you - there is a 
tradeoff between ease of data input, ease of data 
extraction, and database size.

For ease of data entry, extract the lease data 
(IP, Mac, UID, Start, End, State, anything else 
you want to log). Then test if that same record 
exists in the database, and if not, insert it 
(probably with a timestamp for the record 
insertion). This will get you a database in log 
format with each lease that is ever issued - to 
determine a lease state at any point in time you 
will have to select the latest record that 
matches the lease parameters you are looking for 
as there are likely to be multiple overlapping 

Alternatively, you can do more work at input 
time. Extract the lease parameters as before, but 
you work out if this is an extension of an 
existing lease (IP, MAC, UID, etc match, but 
start & end times are different but overlap with 
existing database entry). In this case you update 
the database record with the new expiry time.

Now you have a database that effectively contains 
one record for each unique lease - so you can see 
in one record the extent of the lease (start & 
end times) but you don't have all the 
intermediate data (all the lease renewals etc).


More information about the dhcp-users mailing list