Community discussions

MikroTik App
 
User avatar
Letni
Member
Member
Topic Author
Posts: 376
Joined: Tue Dec 05, 2006 5:16 am
Location: South Carolina

[CONTRIB] DHCP Pool usage

Wed Oct 24, 2007 8:54 am

I got tired of not knowing when I was running low on DHCP addresses so I wrote this little bit for our routers.
This is a basic script and my first time sharing. I have tested it on v2.9.42 and v3rc7



# WarnAt needs to be at what percentage you want to be warned you are running out of IP addresses.
:local WarnAt 10

# Set to your e-mail address.
:local EMail me@mydomain.com

# Set to the name of your DHCP pool
:local Pool dhcp_pool1

:local ip [:tostr [ip pool get $Pool ranges]]
:local Start [:pick $ip 0 [:find $ip "-"]]
:local Stop [:pick $ip ([:find $ip "-"] + 1) 31]
:local Total ($Stop - $Start)
:local Identity [/system identity get name]
:local Used [/ip dhcp-server lease print count-only]
:local Available ($Total - $Used)
:local Perc (($Available* 100) / $Total)
:local Subject ($Identity . " DHCP pool is at " . $Perc . "%")
:local Body ("Out of " . $Total . " addresses you have " . $Available . " available")
:if ($Perc <= $WarnAt) do={
/tool e-mail send to=$EMail subject=$Subject body=$Body
}
 
User avatar
hilton
Long time Member
Long time Member
Posts: 634
Joined: Thu Sep 07, 2006 5:12 pm
Location: Jozi (aka Johannesburg), South Africa

Re: [CONTRIB] DHCP Pool usage

Sun May 04, 2008 9:41 pm

This script is very useful, thanks.

Just one thing, it calculates one less IP in the range than is really defined.

I'm not that sure what this line means;

:local Stop [:pick $ip ([:find $ip "-"] + 1) 31]
 
User avatar
Letni
Member
Member
Topic Author
Posts: 376
Joined: Tue Dec 05, 2006 5:16 am
Location: South Carolina

Re: [CONTRIB] DHCP Pool usage

Mon May 05, 2008 5:34 am

Thank you for noticing that hilton.

Here is my revised script that fixes that issue as well as adds the ability to use the script when running multiple dhcp servers.

Tested on v3.7
# WarnAt needs to be at what percentage you want to be warned you are running out of IP addresses.
:local WarnAt 10
#Name of DHCP Pool you want to monitor
:local DHCPPoolName dhcp_pool1
#Your Email to notify you when running out of available ip's
:local Email my@email.com
#Your mail server ip address
:local EmailServer 0.0.0.0

#Declare Variable
:local Total

:foreach i in=[/ip pool get $DHCPPoolName ranges] do={
#Get the DHCP pool range and put into a string.
:local range [:tostr $i]
#Get the Start address of the range
:local Start [:pick $range 0 [:find $range "-"]]
#Get the End address of the range
:local Stop [:pick $range ([:find $range "-"] + 1) 31]
#Calculate how many IP's are in the Pool by subtracting the end address and the start address and add one.
:local ip ($Stop - $Start +1 )
#Save the size of the pool to Total
:set Total ($ip + $Total)
}

#Get the name of the Server that is using the Pool
:local Server [/ip dhcp-server get [/ip dhcp-server find address-pool="$DHCPPoolName"] name]
#Get the number of IP address's used in the pool
:local Used [/ip dhcp-server lease print count-only where server=[/ip dhcp-server get [/ip dhcp-server find address-pool=$DHCPPoolName] name]]
#Subtract used from total to get available
:local Available ($Total - $Used)
#Calculate percentage IP's available
:local Perc (($Available * 100) / $Total)
#Setup e-mail
:local Subject ([/system identity get name] . " DHCP pool is at $Perc")
:local Body ("Out of $Total addresses you have $Available available")

#Send out Email when available ip's percentage is below or equal to warnat
:if ($Perc <= $WarnAt) do={
  /tool e-mail send to=$Email subject=$Subject body=$Body server=$EmailServer
}
:log info $Body
 
User avatar
hilton
Long time Member
Long time Member
Posts: 634
Joined: Thu Sep 07, 2006 5:12 pm
Location: Jozi (aka Johannesburg), South Africa

Re: [CONTRIB] DHCP Pool usage

Thu May 08, 2008 5:26 pm

Thanks a lot!
 
User avatar
talktozee
just joined
Posts: 23
Joined: Thu Jun 14, 2007 2:52 am

Re: [CONTRIB] DHCP Pool usage

Wed Oct 21, 2009 5:55 am

Can someone please provide a script to send an email when an IP is given out by a DHCP server/pool? Thanks.
 
User avatar
eugenevdm
Member Candidate
Member Candidate
Posts: 208
Joined: Tue Jun 01, 2004 12:23 pm
Location: Stellenbosch, South Africa
Contact:

Re: [CONTRIB] DHCP Pool usage

Wed Jul 14, 2010 6:14 am

Can someone please provide a script to send an email when an IP is given out by a DHCP server/pool? Thanks.
I recall seeing a script like this somewhere and I also need it now. Does someone have it?
 
psamsig
Member Candidate
Member Candidate
Posts: 161
Joined: Sun Dec 06, 2009 1:36 pm
Location: Denmark

Re: [CONTRIB] DHCP Pool usage

Wed Jul 14, 2010 2:01 pm

Who is online

Users browsing this forum: adimihaix, Qalderu and 28 guests