Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I've install and configure DHCP server on CentOS 6.5. I've also added a subnet to the dhcpd.conf file as follows:

subnet 192.168.1.0 netmask 255.255.255.0 {
  option domain-name-servers 192.168.1.2, 8.8.8.8;
  default-lease-time 600;
  max-lease-time 7200;
  range dynamic-bootp 192.168.1.10 192.168.1.30;
  option broadcast-address 192.168.1.255;
  option routers 192.168.1.1;
  option ip-forwarding off;
}

As you can see the DHCP server can assign only 20 IP Addresses. Is it possible to make the DHCP server send an alert to the system administrator after it has assigned all the 20 addresses using a shell script?

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

An option for you is counting number of lease declaration in dhcpd.leases:

dhcpd.leases(5) - Linux man page

Name

dhcpd.leases - DHCP client lease database

....

the Lease Declaration

lease ip-address { statements... }

Each lease declaration includes the single IP address that has been leased to the
client. The statements within the braces define the duration of the lease and to
whom it is assigned.

So you just count number of line starting with lease to know number of ip address have been assigned:

COUNT=$(grep -c '^lease' /var/lib/dhcpd/dhcpd.leases)

if [[ $COUNT eq 20 ]]
then
    #do something here
fi
share|improve this answer
    
Thanks it worked I will mark the question as answered now –  Networker yesterday
add comment

This is not a direct solution but it would seem you could make use of the on commit facility within your DHCP configuration file. Here's an example from this articled titled: Execute a script when ISC DHCP hands out a new lease.

In the dhcpd.conf file you can create actions on various events such as when a lease is given out.

subnet 192.168.1.0 netmask 255.255.255.0 {
    option routers  192.168.1.2;

    on commit {
        set clip = binary-to-ascii(10, 8, ".", leased-address);
        set clhw = binary-to-ascii(16, 8, ":", substring(hardware, 1, 6));
        execute("/usr/local/sbin/dhcpevent", "commit", clip, clhw, host-decl-name);
    }
    ...

When the above script, dhcpevent, runs it's passed 4 arguments.

execute_statement argv[0] = /usr/local/sbin/dhcpevent
execute_statement argv[1] = commit
execute_statement argv[2] = 192.168.1.40
execute_statement argv[3] = 11:aa:bb:cc:dd:ee
execute_statement argv[4] = d1.jp

clipw & clhw are variables where, in this example, portions of other meta data has been parsed and stored prior to running the script. These variables are then passed along with other items to the event script.

You could parley this approach into a script where you could keep track of the numbers of IPs that have been leased out or perhaps you could interrogate the actual lease status file that the DHCP server keeps track of this info (/var/lib/dhcpd/dhcpd.leases), and then report if that files has a number of leases that exceeds your quota.

References

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.