How to monitor DHCP server usage

We have published an article yesterday about how to install and configure DHCP server in Ubuntu 16.04 LTS server. In this article, we will see how to monitor DHCP server’s usage with a help of a simple script written by Mr.Ashutosh, one of our friend and regular reader of OSTechNix. This script analyzes the ‘/var/lib/dhcp/dhcpd.leases’ file in the DHCP server, and displays the details, such as what leases are active, who are using them etc.

Monitor DHCP server usage

I have tested this script in Ubuntu 16.04 LTS server. It worked just fine. Please note that the location of dhcpd.leases file might be different in some Linux distributions. You need to mention correct file name in the server. In Ubuntu 16.04 LTS server, you can find correct location of dhcpd.leases file under /var/lib/dhcp/ location. The author have mentioned /var/lib/dhcpd/dhcpd.leases in this script. It doesn’t work for me. I replaced the path with correct location i.e /var/lib/dhcp/dhcpd.leases in Ubuntu 16.04 systems.

Here I have included the complete script. Just coy and paste it to a text file and save it with .sh extension. For the purpose of this tutorial, I have it as monitor_dhcp.sh.

#!/bin/bash

rm /tmp/showdhcp.log &
rm /tmp/tmp* &
tempFile=$(mktemp)

cat /var/lib/dhcp/dhcpd.leases \
  | grep \
      -e '^lease ' \
      -e '^  binding state' \
      -e hardware \
      -e client-hostname \
      -e } >$tempFile.1

sed -e "s/^lease \(.*\) {$/\1~/" \
    -e "s/^  binding state \(.*\);$/\1~/" \
    -e "s/^  hardware ethernet \(.*\);$/\1~/" \
    -e "s/  client-hostname \(.*\);$/\1~/" \
    -e "s/^}//" \
    $tempFile.1 > $tempFile.2

sed -e ":'loop';/~$/N;s/~\n/,/;t'loop'" \
    -e "s/,$//" \
    -e "s/,/\t/g" \
    $tempFile.2 | sort > $tempFile.3

sed -e'$!N;/^\(.*\)\n\1$/!P;D' $tempFile.3 > $tempFile.4

print=$(printf "IP Addr\t\tStatus\t\tMAC\t\tHost Name\n")
cat $tempFile.4 > /tmp/showdhcp.log &
sed -i "1i $print" /tmp/showdhcp.log 
#rm -rvf /tmp/Non_Active_User*
#rm -rvf /tmp/Active_User*
#cp -rvf $tempFile.4 /tmp/showdhcp.log &
free=$(grep -o -w 'free' /tmp/showdhcp.log | wc -w)
active=$(grep -o -w 'active' /tmp/showdhcp.log | wc -w)
echo -e ""
echo -e "--------- Total Count- Active & Non Active -----------"
echo -e ""
echo -e "Total Active User :"       $free
echo -e "Total Non Active User :"   $active
echo -e "------------------------------------------------------"
#sed -n '/free/p' /tmp/showdhcp.log > /tmp/Non_Active_User_$(date +"%Y-%m-%d__%H%M%S").log
#sed -n '/active/p' /tmp/showdhcp.log > /tmp/Active_User_$(date +"%Y-%m-%d__%H%M%S").log
rm $tempFile.* 
#rm -rvf /tmp/showdhcp.log
#rm -rvf /tmp/tmp* &

Or, Download the script from the following location.

Once downloaded, make it as executable using as shown below.

sudo chmod +x monitor_dhcp.sh

Now, run the following command to monitor DHCP server’s usage.

./monitor_dhcp.sh

Sample output:

--------- Total Count- Active & Non Active -----------

Total Active User : 2
Total Non Active User : 1
------------------------------------------------------

ostechnix@ubuntuserver: ~_010

As you see in the above output, there are two active users, and one non-active user.

Alternatively, you can check the DHCP client details using the log file.

cat /tmp/showdhcp.log

Sample output:

IP Addr        Status      MAC     Host Name
192.168.1.150   free    xx:xx:xx:xx:xx:xx
192.168.1.151   active  xx:xx:xx:xx:xx:xx   "sk"
192.168.1.152   free    xx:xx:xx:xx:xx:xx

ostechnix@ubuntuserver: ~_011

For any further details and questions about this script, contact Mr.Ashutosh.

That’s all for today. Hope this script might useful for you.

Happy weekend! Cheers!!

Thanks for stopping by!

How can I benefit from this blog:

Have a Good day!!

You may also like...

  • Bill

    For campus-wide DHCP server monitoring, I use dhcpd-pools (http://dhcpd-pools.sourceforge.net/). But for simple monitoring, this tool you’ve presented is cool! Thanks.

    • SK

      You’re welcome.