parsing dhcpd.leases file

Simon Hobson dhcp1 at thehobsons.co.uk
Wed Apr 15 19:03:02 UTC 2009


Daniel Duarte wrote:

>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;
>}
>
>Limitations:
>- 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;

Minor issue : there's scope for things to change (files rotate) under 
your nose, OK it's a very tiny window, but it's there.

I would be tempted to keep a timestamp of the last transaction 
logged, then parse both the old and new files checking for events we 
don't have (ie event timestamp > last run timestamp). Perhaps even 
open both files for reading before we check so that even if the 
server rotates log files while we are parsing, we get to read 
something that is consistent.

Also, there is the issue that if the file is rotated, there will be 
entries in the new leases file that we have already parsed - so you 
will still need to check each entry against the database. Given this 
is the case, it might be simplest to just parse everything and de-dup 
the data.

-- 
Simon Hobson

Visit http://www.magpiesnestpublishing.co.uk/ for books by acclaimed
author Gladys Hobson. Novels - poetry - short stories - ideal as
Christmas stocking fillers. Some available as e-books.



More information about the dhcp-users mailing list