Home Command line utilities How to monitor DHCP server usage

How to monitor DHCP server usage

By sk
246 Views

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

9 comments

Bill August 7, 2016 - 7:40 pm

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.

Reply
SK August 8, 2016 - 11:24 am

You’re welcome.

Reply
Thomas Yobe December 18, 2017 - 8:46 pm

Bill, that link is dead.

Reply
Thomas Yobe December 18, 2017 - 7:36 pm

Hi, last time I queried on the DNS issue, my post was deleted. So today I just want to report that http://ravindrayadava.blogspot.com/2016/08/ubuntu-1604-install-monitor-dhcp-server.html copied this article to his blog without crediting you guys.

Reply
SK December 18, 2017 - 8:30 pm

Thanks. I left a comment on the blog. I didn’t receive any query from you. Everyday, I receive a lot of comments. I couldn’t reply to them all. Sorry.

Reply
Thomas Yobe December 18, 2017 - 8:44 pm

No Worries, I managed. I had a SERVERFAIL on my Secondary DNS server. However I have a query. I will email you.

Reply
SK December 19, 2017 - 11:04 am

Glad you fixed the problem on your own. BTW, I did a quick glance at the blog you mentioned earlier. He not only copy/paste from my blog but also from so many other blogs. 99% of the guides posted there are complete cop/paste from many blogs and websites.

Reply
Thomas Yobe December 19, 2017 - 12:23 pm

It’s a shame really. Wanting to get credit on other peoples work is not good at all.

Alessandro February 1, 2019 - 7:41 pm

Thank you so much for this script! It’s really useful for us.
I had to correct the path of dhcpd lease file like you did and another thing i suggest to check for you.
In the script i downloaded the screen output of the script as exchanged the active and non active users and i had to change the text to match the correct ones.

Keep up the good work!
Bye!

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More