| 1 | package Net::ISC::DHCPd::Leases; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | Net::ISC::DHCPd::Leases - Parse ISC DHCPd leases | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | my $leases = Net::ISC::DHCPd::Leases->new( | ||||||||||||||||||||
| 10 | file => '/var/lib/dhcp3/dhcpd.leases', | ||||||||||||||||||||
| 11 | ); | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | # parse the leases file | ||||||||||||||||||||
| 14 | $leases->parse; | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | for my $lease ($leases->leases) { | ||||||||||||||||||||
| 17 | say "lease has ended" if($lease->ends < time); | ||||||||||||||||||||
| 18 | } | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | if(my $n = $leases->find_leases({ ends => time }) { | ||||||||||||||||||||
| 21 | say "$n lease(s) has expired now"; | ||||||||||||||||||||
| 22 | } | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | An object constructed from this class represents a leases file for | ||||||||||||||||||||
| 27 | the dhcpd server. It is read-only, so changes to the leases file | ||||||||||||||||||||
| 28 | must be done through a running server, using L<Net::ISC::DHCPd::OMAPI>. | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | The object has one important attribute, which is L</leases>. This | ||||||||||||||||||||
| 31 | attribute holds a list of L<Net::ISC::DHCPd::Leases::Lease> objects | ||||||||||||||||||||
| 32 | constructed from all the leases found in the leases file. | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | =cut | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 7 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 37 | 7 | ▶ | use Net::ISC::DHCPd::Leases::Lease; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 38 | 7 | ▶ | use MooseX::Types::Path::Class 0.05 qw(File); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 39 | 7 | ▶ | use Time::Local; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | =head1 ATTRIBUTES | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | =head2 leases | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | Holds a list of all the leases found after reading the leases file. | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | =cut | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | has leases => ( | ||||||||||||||||||||
| 50 | is => 'ro', | ||||||||||||||||||||
| 51 | isa => 'ArrayRef', | ||||||||||||||||||||
| 52 | auto_deref => 1, | ||||||||||||||||||||
| 53 | 5 | ▶ | default => sub { [] }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 54 | 7 | ); | |||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | =head2 file | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | This attribute holds a L<Path::Class::File> object to the leases file. | ||||||||||||||||||||
| 59 | It is read-write and the default value is "/var/lib/dhcp3/dhcpd.leases". | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | =cut | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | has file => ( | ||||||||||||||||||||
| 64 | is => 'rw', | ||||||||||||||||||||
| 65 | isa => File, | ||||||||||||||||||||
| 66 | coerce => 1, | ||||||||||||||||||||
| 67 | 0 | ▶ | default => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 68 | 0 | Path::Class::File->new('', 'var', 'lib', 'dhcp3', 'dhcpd.leases'); | |||||||||||||||||||
| 69 | }, | ||||||||||||||||||||
| 70 | 7 | ); | |||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 7 | has fh => ( | |||||||||||||||||||
| 73 | is => 'rw', | ||||||||||||||||||||
| 74 | isa => 'FileHandle', | ||||||||||||||||||||
| 75 | required => 0, | ||||||||||||||||||||
| 76 | ); | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 7 | has _filehandle => ( | |||||||||||||||||||
| 79 | is => 'ro', | ||||||||||||||||||||
| 80 | lazy_build => 1, | ||||||||||||||||||||
| 81 | ); | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 1 | ▶ | sub _build__filehandle { | ||||||||||||||||||
|
Subroutine
_build__filehandle: called CC=2 SCAR=7.6
| |||||||||||||||||||||
| 84 | 1 | my $self = shift; | |||||||||||||||||||
| 85 | 1 | ▶ | if ($self->fh) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | 0 | return $self->fh; | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 1 | $self->file->openr; | |||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 0 | ▶ | __PACKAGE__->meta->add_method(filehandle => sub { | ||||||||||||||||||
|
Subroutine
filehandle: not called CC=1 SCAR=6.9
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 93 | 0 | Carp::cluck('->filehandle is replaced with private attribute _filehandle'); | |||||||||||||||||||
| 94 | 0 | shift->_filehandle; | |||||||||||||||||||
| 95 | 7 | }); | |||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | =head1 METHODS | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | =head2 parse | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | Read lines from L</file>, and parses every lease it can find. | ||||||||||||||||||||
| 102 | Returns the number of leases found. Will add each found lease to | ||||||||||||||||||||
| 103 | L</leases>. | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | =cut | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 7 | our $DATE = qr# (\d{4})/(\d\d)/(\d\d) \s (\d\d):(\d\d):(\d\d) #mxo; | |||||||||||||||||||
| 108 | 7 | our $START = qr#^ lease \s ([\d\.]+) \s \{ #mxo; | |||||||||||||||||||
| 109 | 7 | our $END = qr# } [\n\r]+ #mxo; | |||||||||||||||||||
| 110 | 7 | our $PARSER = qr / (?| (starts) \s\d+\s (.+?) | |||||||||||||||||||
| 111 | | (ends) \s\d+\s (.+?) | ||||||||||||||||||||
| 112 | | ^\s*binding \s (state) \s (\S+) | ||||||||||||||||||||
| 113 | | ^\s*(next) \s binding \s state \s (\S+) | ||||||||||||||||||||
| 114 | | hardware \s (ethernet) \s (\S+) | ||||||||||||||||||||
| 115 | | option \s agent.(remote-id) \s (.+?) | ||||||||||||||||||||
| 116 | | option \s agent.(circuit-id) \s (.+?) | ||||||||||||||||||||
| 117 | | client-(hostname) \s "([^"]+)" | ||||||||||||||||||||
| 118 | ) /mxo; | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 10 | ▶ | sub _done { | ||||||||||||||||||
|
Subroutine
_done: called CC=6 SCAR=18.0
| |||||||||||||||||||||
| 122 | 10 | my ($self, $lease) = @_; | |||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 10 | for my $k (qw/starts ends/) { | |||||||||||||||||||
| 125 | 20 | ▶ | next unless($lease->{$k}); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 126 | 20 | ▶ | if(my @values = $lease->{$k} =~ $DATE) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 127 | 20 | $values[1]--; # decrease month | |||||||||||||||||||
| 128 | 20 | $lease->{$k} = timelocal(reverse @values); | |||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | # rather than doing this map, we need to see if we can make Moose use | ||||||||||||||||||||
| 133 | # aliases and accept the alternate names on instantiation. We also should | ||||||||||||||||||||
| 134 | # be able to do the mac address cleanup and validation in the | ||||||||||||||||||||
| 135 | # Net::ISC::DHCPd::Leases::Lease->new | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 10 | my %map = ( | |||||||||||||||||||
| 138 | 'circuit-id' => 'circuit_id', | ||||||||||||||||||||
| 139 | 'remote-id' => 'remote_id', | ||||||||||||||||||||
| 140 | ip => 'ip_address', | ||||||||||||||||||||
| 141 | hostname => 'client_hostname', | ||||||||||||||||||||
| 142 | ethernet => 'hardware_address', | ||||||||||||||||||||
| 143 | ); | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 10 | for my $key (keys %map) { | |||||||||||||||||||
| 146 | 50 | ▶ | if(defined $lease->{$key}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 147 | 20 | $lease->{ $map{$key} } = delete $lease->{$key}; | |||||||||||||||||||
| 148 | } | ||||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | 10 | return $lease; | |||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 1 | ▶ | sub parse { | ||||||||||||||||||
|
Subroutine
parse: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||
| 155 | 1 | my $self = shift; | |||||||||||||||||||
| 156 | 1 | my $fh = $self->_filehandle; | |||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | 1 | ▶ | sysread $fh, my $buffer, -s $fh or die "Couldn't read file: $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 159 | 1 | my $lines = 0; | |||||||||||||||||||
| 160 | 1 | my $lease; | |||||||||||||||||||
| 161 | 1 | while(1) { | |||||||||||||||||||
| 162 | 105 | my $string; | |||||||||||||||||||
| 163 | # look for lines with \r\n endings | ||||||||||||||||||||
| 164 | 105 | ▶ | if($buffer =~ /^(.*?\x0d?\x0a)/s) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 165 | 104 | my $length = length $1; | |||||||||||||||||||
| 166 | 104 | $string = substr($buffer,0,$length,''); | |||||||||||||||||||
| 167 | } | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | 105 | ▶ | last unless $string; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 170 | 104 | ++$lines; | |||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | 104 | ▶ | if ($lease) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 173 | 91 | ▶ | if ($string =~ /$PARSER;/) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 174 | 40 | $lease->{$1} = $2; | |||||||||||||||||||
| 175 | } elsif($string =~ /.*?$END/) { | ||||||||||||||||||||
| 176 | # just removing this class object cuts our time in half.. | ||||||||||||||||||||
| 177 | # the coercion checks and things slow us down a bunch. | ||||||||||||||||||||
| 178 | 10 | push @{$self->leases}, Net::ISC::DHCPd::Leases::Lease->new($self->_done($lease)); | |||||||||||||||||||
| 179 | #push @{$self->leases}, $self->_done($lease); | ||||||||||||||||||||
| 180 | 10 | $lease = undef; | |||||||||||||||||||
| 181 | 10 | next; | |||||||||||||||||||
| 182 | } | ||||||||||||||||||||
| 183 | ▶ | } elsif(!$lease and $string =~ /$START/) { | |||||||||||||||||||
|
Condition66%2/3
not $lease and $string =~ /$Net::ISC::DHCPd::Leases::START/
MC/DC50%1/2
not $lease and $string =~ /$Net::ISC::DHCPd::Leases::START/
not $lease
$string =~ /$Net::ISC::DHCPd::Leases::START/
Truth table66%2/3
not(A) and B
A= not $leaseB= $string =~ /$Net::ISC::DHCPd::Leases::START/
| |||||||||||||||||||||
| 184 | 10 | $lease = { ip => $1 }; | |||||||||||||||||||
| 185 | } | ||||||||||||||||||||
| 186 | } | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 1 | return $lines; | |||||||||||||||||||
| 189 | } | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | =head2 find_leases | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | This method will return zero or more L<Net::ISC::DHCPd::Leases::Lease> | ||||||||||||||||||||
| 194 | objects as a list. It takes a hash-ref which will be matched against | ||||||||||||||||||||
| 195 | the attributes of the child leases. | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | =cut | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | 1 | ▶ | sub find_leases { | ||||||||||||||||||
|
Subroutine
find_leases: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||
| 200 | 1 | my $self = shift; | |||||||||||||||||||
| 201 | 1 | ▶ | my $query = shift or return; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 202 | 1 | my @leases; | |||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | LEASE: | ||||||||||||||||||||
| 205 | 1 | for my $lease ($self->leases) { | |||||||||||||||||||
| 206 | 10 | for my $key (keys %$query) { | |||||||||||||||||||
| 207 | 10 | ▶ | next LEASE unless($lease->$key eq $query->{$key}); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 208 | } | ||||||||||||||||||||
| 209 | 1 | push @leases, $lease; | |||||||||||||||||||
| 210 | } | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | 1 | return @leases; | |||||||||||||||||||
| 213 | } | ||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | =head1 AUTHOR | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | See L<Net::ISC::DHCPd>. | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | =cut | ||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | 7 | 1; | |||||||||||||||||||