Check active DHCP leases


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check active DHCP leases
# 1  
Old 04-06-2016
Check active DHCP leases

Hi all,

I'm far from an expert so bear with me on this. I'm writing a script to check for check for active DHCP leases on each subnet configured on a server, and clear and restart DHCP if the usage is above 90%. The possible scope is /16 - /24.

/usr/share/flint/scripts/leasecheck.pl is a perl script that lists the DHCP leases across the server in a nice format, but doesn't differentiate by subnet (I didn't write this). So the bash script is to take that raw information and seperate it out. This is the easiest way I could think to do this, and it works, and does what I want, but not sure if there is a better or simpler way?

Code:
#!/bin/bash

INTFCES=/etc/flint/interfaces.conf

echo -e `ip addr | grep eth[1-9] | sed -n '1~2!p' | awk '{print $7}'` > /etc/flint/interfaces.conf

ARRAYINTFCES=($(cat $INTFCES))
for ((i=0; i<${#ARRAYINTFCES[@]}; ++i)); do

SUBNET1=`ip addr | grep ${ARRAYINTFCES[$i]} | sed -n 2p | awk '{print $2}'`
AVLBLEASES=`subnetcalc $SUBNET1 | grep Max | awk '{print $4}'`
SUBNET2=`subnetcalc $SUBNET1 | grep Network | awk '{print $3}'`

function slash24 {
	OIFS=$IFS
	IFS='.' read -a myarray <<< "$SUBNET2"
	ONE="${myarray[0]}"
	TWO="${myarray[1]}"
	THREE="${myarray[2]}"
	IFS=$OIFS
	SUBNET3=$ONE.$TWO.$THREE
	ACTVLEASES=`/usr/share/flint/scripts/leasecheck.pl | grep $SUBNET3 | grep active | awk '{print $1}' | uniq | wc -l`
	PCNTAVLB=`awk 'BEGIN{printf("%0.0f", '$ACTVLEASES' / '$AVLBLEASES' * 100)}'`
}

function slash23 {
	OIFS=$IFS
	IFS='.' read -a myarray <<< "$SUBNET2"
	ONE="${myarray[0]}"
	TWO="${myarray[1]}"
	THREE="${myarray[2]}"
	IFS=$OIFS
	LAST=$(($THREE + 1))
	RANGE=[$THREE-$LAST]
	SUBNET3=$ONE.$TWO.$RANGE
	ACTVLEASES=`/usr/share/flint/scripts/leasecheck.pl | grep $SUBNET3 | grep active | awk '{print $1}' | uniq | wc -l`
	PCNTAVLB=`awk 'BEGIN{printf("%0.0f", '$ACTVLEASES' / '$AVLBLEASES' * 100)}'`
}

function slash22 {
	OIFS=$IFS
	IFS='.' read -a myarray <<< "$SUBNET2"
	ONE="${myarray[0]}"
	TWO="${myarray[1]}"
	THREE="${myarray[2]}"
	IFS=$OIFS
	LAST=$(($THREE + 3))
	RANGE=[$THREE-$LAST]
	SUBNET3=$ONE.$TWO.$RANGE
	ACTVLEASES=`/usr/share/flint/scripts/leasecheck.pl | grep $SUBNET3 | grep active | awk '{print $1}' | uniq | wc -l`
	PCNTAVLB=`awk 'BEGIN{printf("%0.0f", '$ACTVLEASES' / '$AVLBLEASES' * 100)}'`
}

function slash21 {
	OIFS=$IFS
	IFS='.' read -a myarray <<< "$SUBNET2"
	ONE="${myarray[0]}"
	TWO="${myarray[1]}"
	THREE="${myarray[2]}"
	IFS=$OIFS
	LAST=$(($THREE + 7))
	RANGE=[$THREE-$LAST]
	SUBNET3=$ONE.$TWO.$RANGE
	ACTVLEASES=`/usr/share/flint/scripts/leasecheck.pl | grep $SUBNET3 | grep active | awk '{print $1}' | uniq | wc -l`
	PCNTAVLB=`awk 'BEGIN{printf("%0.0f", '$ACTVLEASES' / '$AVLBLEASES' * 100)}'`
}

function slash20 {
	OIFS=$IFS
	IFS='.' read -a myarray <<< "$SUBNET2"
	ONE="${myarray[0]}"
	TWO="${myarray[1]}"
	THREE="${myarray[2]}"
	IFS=$OIFS
	LAST=$(($THREE + 15))
	RANGE=[$THREE-$LAST]
	SUBNET3=$ONE.$TWO.$RANGE
	ACTVLEASES=`/usr/share/flint/scripts/leasecheck.pl | grep $SUBNET3 | grep active | awk '{print $1}' | uniq | wc -l`
	PCNTAVLB=`awk 'BEGIN{printf("%0.0f", '$ACTVLEASES' / '$AVLBLEASES' * 100)}'`
}

function slash19 {
	OIFS=$IFS
	IFS='.' read -a myarray <<< "$SUBNET2"
	ONE="${myarray[0]}"
	TWO="${myarray[1]}"
	THREE="${myarray[2]}"
	IFS=$OIFS
	LAST=$(($THREE + 31))
	RANGE=[$THREE-$LAST]
	SUBNET3=$ONE.$TWO.$RANGE
	ACTVLEASES=`/usr/share/flint/scripts/leasecheck.pl | grep $SUBNET3 | grep active | awk '{print $1}' | uniq | wc -l`
	PCNTAVLB=`awk 'BEGIN{printf("%0.0f", '$ACTVLEASES' / '$AVLBLEASES' * 100)}'`
}

function slash18 {
	OIFS=$IFS
	IFS='.' read -a myarray <<< "$SUBNET2"
	ONE="${myarray[0]}"
	TWO="${myarray[1]}"
	THREE="${myarray[2]}"
	IFS=$OIFS
	LAST=$(($THREE + 63))
	RANGE=[$THREE-$LAST]
	SUBNET3=$ONE.$TWO.$RANGE
	ACTVLEASES=`/usr/share/flint/scripts/leasecheck.pl | grep $SUBNET3 | grep active | awk '{print $1}' | uniq | wc -l`
	PCNTAVLB=`awk 'BEGIN{printf("%0.0f", '$ACTVLEASES' / '$AVLBLEASES' * 100)}'`
}

function slash17 {
	OIFS=$IFS
	IFS='.' read -a myarray <<< "$SUBNET2"
	ONE="${myarray[0]}"
	TWO="${myarray[1]}"
	THREE="${myarray[2]}"
	IFS=$OIFS
	LAST=$(($THREE + 127))
	RANGE=[$THREE-$LAST]
	SUBNET3=$ONE.$TWO.$RANGE
	ACTVLEASES=`/usr/share/flint/scripts/leasecheck.pl | grep $SUBNET3 | grep active | awk '{print $1}' | uniq | wc -l`
	PCNTAVLB=`awk 'BEGIN{printf("%0.0f", '$ACTVLEASES' / '$AVLBLEASES' * 100)}'`
}

function slash16 {
	OIFS=$IFS
	IFS='.' read -a myarray <<< "$SUBNET2"
	ONE="${myarray[0]}"
	TWO="${myarray[1]}"
	THREE="${myarray[2]}"
	IFS=$OIFS
	LAST=$(($THREE + 255))
	RANGE=[$THREE-$LAST]
	SUBNET3=$ONE.$TWO.$RANGE
	ACTVLEASES=`/usr/share/flint/scripts/leasecheck.pl | grep $SUBNET3 | grep active | awk '{print $1}' | uniq | wc -l`
	PCNTAVLB=`awk 'BEGIN{printf("%0.0f", '$ACTVLEASES' / '$AVLBLEASES' * 100)}'`
}

if [ $AVLBLEASES == 254 ]
then
	slash24
elif [ $AVLBLEASES == 510 ]
then
	slash23
elif [ $AVLBLEASES == 1022 ]
then
	slash22
elif [ $AVLBLEASES == 2046 ]
then
	slash21
elif [ $AVLBLEASES == 4094 ]
then
	slash20
elif [ $AVLBLEASES == 8190 ]
then
	slash19
elif [ $AVLBLEASES == 19382 ]
then
	slash18
elif [ $AVLBLEASES == 32766 ]
then
	slash17
elif [ $AVLBLEASES == 65534 ]
then
	slash16
else
	:
fi

if [ $PCNTAVLB -ge 90 ]
then
    logger "$0 - Clearing leases and restarting DHCP, below 10% available."
    cat /dev/null > /var/lib/dhcpd/dhcpd.leases
    systemctl restart dhcpd
    echo "Please check `hostname` because a subnets leases have dropped below 10%" | mail -s `hostname` email@email.com
else
    logger "$0 - OK"
fi

done

Thanks,
# 2  
Old 04-06-2016
Without going into detailed analysis nor a deep understanding of what you are doing, twothree comments:
- the slashX functions differ only in one summand so could be boiled down into a single one with a parameter passed.
- a pipe organ like echo | grep | sed | awk | uniq | wc could almost certainly be boiled down to a single awk call.
- if the leasecheck.pl script is already available, why not expand it to do all the tasks?
- or create an overall awk script for the same reason.
# 3  
Old 04-06-2016
Thanks for the insight RudiC. The first two suggestions sound like something I'll explore further. The perl script is available, but would require a lot more learning my side, but is something I'll definitely look in to longer term.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script to check dhcp conf file

Hi, I have to prepare a script to check the dhcp conf file. The script has to check for a specific parameter called circuit ID. If the Circuit ID is unique it should show the output that it is unique and if it is duplicate it should show that the Circuit ID is duplicate. I have prepared the... (4 Replies)
Discussion started by: Crazy_Nix
4 Replies

2. Shell Programming and Scripting

How do I calculate total number of active and non active hosts?

#!/bin/bash for digit in $(seq 1 10) do if ping -c1 -w2 192.168.1.$digit &> /dev/null then echo "192.168.1.$digit is UP" else echo "192.168.1.$digit is DOWN" fi done (3 Replies)
Discussion started by: fusetrips
3 Replies

3. Shell Programming and Scripting

Linux Script to check Active URL in Firefox

Hey guys, currently I'm struggling with a little script to check an active URL in my running Firefox. What I'm doing: I'm running a low VPS with about 768mb RAM and Ubuntu on it. I only installed Fluxbox + Firefox to it in order to keep the resource consumption as low as possible. I think i... (8 Replies)
Discussion started by: uniflow
8 Replies

4. Solaris

Link based Active Active IPMP

Hi, I need to configure 4 ip address (same subnet and mask) in one ipmp group (two interfaces) in an active active formation (link based). Can some one provide the steps or a tutorial link. Thanks (2 Replies)
Discussion started by: Mack1982
2 Replies

5. Shell Programming and Scripting

Script to check if Port is Active

is there a better way to check if a port is active on linux and sunos systems? this is currently what I'm using in my script: netstat -an | egrep -i "$PORT" i know this isn't the best way as there could be numbers in that output that has my port number in it but isn't necessarily a... (0 Replies)
Discussion started by: SkySmart
0 Replies

6. AIX

Question about HACMP for active-active mode

Hi all, I am new to HACMP. So sorry for the newie question. But I did search the forum and it seems that no one asks this before. So if a 2-node cluster runs in active-active mode (and the same application), what is the benefit of using HACMP ? If it runs in active-stanby, it is easy to... (9 Replies)
Discussion started by: qiulang
9 Replies

7. Programming

POSIX - Hot to check if detached thread is still active

Hello, I have created program that run threads one by one, maximum 100. Each thread will process one block of data, and once it`s finished, new thread is created with new block of data....etc I have array of values to control status of each thread, like this: array_thread_status=1... (11 Replies)
Discussion started by: orangem
11 Replies

8. HP-UX

How to check if DHCP is running on HP-UX box?

Hello all, Can someone help me with a command to check if DHCP is running on HP-UX box? Thanks (2 Replies)
Discussion started by: nuGuy
2 Replies

9. UNIX for Dummies Questions & Answers

How to check if DHCP is running on HP-UX box?

Hello all, Can someone help me with a command to check if DHCP is running on HP-UX box? Thanks (1 Reply)
Discussion started by: nuGuy
1 Replies
Login or Register to Ask a Question