Community discussions

 
terintamel
newbie
Topic Author
Posts: 47
Joined: Sun Sep 05, 2010 3:09 am

DHCP Usage Script not working in 6.27

Thu Apr 02, 2015 3:49 pm

I have a DHCP usage script that I got from another website (can't remember) that goes through my DHCP pools and alerts me if the usage is over a defined threshold. It then creates a file and emails me the file. Below is the script.
# List stats for IP -> Pool
#
# criticalthreshold = output pool display in red if pool used is above this %
# warnthreshold = output pool display in gold if pool used is above this %
 
:local criticalthreshold 85
:local warnthreshold 70
:local flag 0
:local filecontent
/file print file="dhcp_tracker.txt";

# Internal processing below...
# ----------------------------------
/ip pool {
   
   :local poolname
   :local pooladdresses
   :local poolused
   :local poolpercent
   :local minaddress
   :local maxaddress
   :local findindex
   :local tmpint
   :local maxindex
   :local line
 
   :put ("IP Pool Statistics")
   :put ("------------------")

 
# Iterate through IP Pools
   :foreach p in=[find] do={
 
      :set poolname [get $p name]
      :set pooladdresses 0
      :set poolused 0
      :set line ""
 
      :set line ("     " . $poolname)
 
#   Iterate through current pool's IP ranges
      :foreach r in=[:toarray [get $p range]] do={
 
#      Get min and max addresses
         :set findindex [:find [:tostr $r] "-"]
         :if ([:len $findindex] > 0) do={
            :set minaddress [:pick [:tostr $r] 0 $findindex]
            :set maxaddress [:pick [:tostr $r] ($findindex + 1) [:len [:tostr $r]]]
         } else={
            :set minaddress [:tostr $r]
            :set maxaddress [:tostr $r]
         }
 
#       Convert to array of octets (replace '.' with ',')
         :for x from=0 to=([:len [:tostr $minaddress]] - 1) do={
            :if ([:pick [:tostr $minaddress] $x ($x + 1)] = ".") do={
               :set minaddress ([:pick [:tostr $minaddress] 0 $x] . "," . \
                                       [:pick [:tostr $minaddress] ($x + 1) [:len [:tostr $minaddress]]]) }
         }
         :for x from=0 to=([:len [:tostr $maxaddress]] - 1) do={
            :if ([:pick [:tostr $maxaddress] $x ($x + 1)] = ".") do={
               :set maxaddress ([:pick [:tostr $maxaddress] 0 $x] . "," . \
                                       [:pick [:tostr $maxaddress] ($x + 1) [:len [:tostr $maxaddress]]]) }
         }
 
#      Calculate available addresses for current range
         :if ([:len [:toarray $minaddress]] = [:len [:toarray $maxaddress]]) do={
            :set maxindex ([:len [:toarray $minaddress]] - 1)
            :for x from=$maxindex to=0 step=-1 do={
#             Calculate 256^($maxindex - $x)
               :set tmpint 1
               :if (($maxindex - $x) > 0) do={
                  :for y from=1 to=($maxindex - $x) do={ :set tmpint (256 * $tmpint) }
               }
               :set tmpint ($tmpint * ([:tonum [:pick [:toarray $maxaddress] $x]] - \
                                                    [:tonum [:pick [:toarray $minaddress] $x]]) )
               :set pooladdresses ($pooladdresses + $tmpint)
#         for x
            }
 
#      if len array $minaddress = $maxaddress
         }
 
#      Add current range to total pool's available addresses
         :set pooladdresses ($pooladdresses + 1)
 
#   foreach r
      }
 
#   Now, we have the available address for all ranges in this pool
#   Get the number of used addresses for this pool
      :set poolused [:len [used find pool=[:tostr $poolname]]]
      :set poolpercent (($poolused * 100) / $pooladdresses)
 
#   Output information
      :set line ([:tostr $line] . "  [" . $poolused . "/" . $pooladdresses . "]")
      :set line ([:tostr $line] . "  " . $poolpercent . " % used")
 
#   Set colored display for used thresholds

      :if ( [:tonum $poolpercent] > $criticalthreshold ) do={
         :set flag 1
         :set filecontent ($filecontent . "IP Pool " . $poolname . " is " ." [" . $poolused . "/" . $pooladdresses . "] ". $poolpercent . "% full". "\r\n")
         :put ([:terminal style varname] . $line)
      } else={
         :if ( [:tonum $poolpercent] > $warnthreshold ) do={
            :set flag 1
            :set filecontent ($filecontent . "IP Pool " . $poolname . " is " ." [" . $poolused . "/" . $pooladdresses . "] ". $poolpercent . "% full". "\r\n")
            :put ([:terminal style syntax-meta] . $line)
         } else={
           :put ([:terminal style none] . $line)
         }
      }
 
# foreach p
   }
# /ip pool
}
:if (flag=1) do={
/file set "dhcp_tracker.txt" contents=$filecontent;
/tool e-mail send file=dhcp_tracker.txt to="user@domain.com" body="See attached file" subject="$[/system identity get name] $[/system clock get time] $[/system clock get date] DHCP Warning"
} else={}
Before on 4.17 I would get a file 'dhcp_tracker.txt emailed to me with the following contents.

IP Pool Jamestown_AP1 is [26/29] 89% full
IP Pool Advance_Extention is [72/61] 118% full

Now on 6.27 the file 'dhcp_tracker.txt' is created but not emailed and the contents are wrong. Below are the contents on 6.27.

# apr/ 2/2015 12:43:13 by RouterOS 6.27
# software id = 5DU2-RAD2
#
# NAME TYPE SIZE CREATION-TIME
0 skins directory jan/01/1970 00:00:08
1 advance.backup backup 176.1KiB apr/02/2015 09:41:20
2 lease.rsc script 33.6KiB jan/03/1970 17:10:38
3 supout.rif .rif file 605.7KiB apr/01/2015 13:28:16
4 pub directory apr/01/2015 13:27:59


What do I need to change to have this work in 6.27?
 
terintamel
newbie
Topic Author
Posts: 47
Joined: Sun Sep 05, 2010 3:09 am

Re: DHCP Usage Script not working in 6.27

Thu Apr 02, 2015 3:53 pm

Sorry. False alarm. The script works fine, but for some reason when I migrated to 6.27 my DHCP servers did not have a pool attached so pool usage was at zero. Would have found this out the hard way when clients wanted to renew their leases and could not get an address. Now that I have corrected the pool issue the script is fine.

Who is online

Users browsing this forum: No registered users and 10 guests