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

Sébastien CRAMATTE s.cramatte at wanadoo.fr
Fri Jun 30 07:26:53 UTC 2006


markd a écrit :
> Sébastien CRAMATTE wrote:
>   
>>> $/ = "}\n";
>>> while (<>) {
>>>      ($starts) = /starts \d ([^;]+)/;
>>>      ($ends) = /starts \d ([^;]+)/;
>>>      ($state) = /binding state ([^;]+)/;
>>>      ($mac) = /hardware ethernet ([^;]+)/;
>>>
>>>      $insert->execute($starts, $ends, $state, $mac);
>>> }
>>>
>>>       
>> But how do you parse the lease ?
>> Could you give me a complete example / script ?
>>     
>
> I'm not sure I understand the question. The code above
> is parsing the lease.  But "parse lease" is sort of vague.
> What data do you want to get out of the lease?  What format
> do you want it in?
>
> Here's a complete script that prints IP, MAC and start time
> of active leases:
>
> #!/usr/bin/perl
>
> sub parse_lease {
>     local $_ = shift;
>     my %l;
>     ($l{ipaddr}) = /lease (\S+)/;
>     ($l{starts}) = /starts \d ([^;]+)/;
>     ($l{mac}) = /hardware ethernet ([^;]+)/;
>     ($l{state}) = /binding state ([^;]+)/;
>     return \%l;
> }
>
> open(LEASES, "dhcpd.leases");
> $/ = "}\n";
>
> while (<LEASES>) {
>     my $lease = parse_lease($_);
>     next unless $lease->{state} eq 'active';
>     printf "ip=%s mac=%s starts=%s\n",
>             $lease->{ipaddr}, $lease->{mac}, $lease->{starts};
> }
>
>
> mark
>   

Hello,

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 )

I work for cable operator and I need to make a log of all leases and 
keep datas during 1Year or more . This is why I need
to store these leases in a database.

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 ????

Thanks for your help

Regards

Sebastien













More information about the dhcp-users mailing list