Community discussions

MikroTik App
 
lucasgg
just joined
Topic Author
Posts: 1
Joined: Thu Sep 27, 2018 7:22 pm

IP Pool Usage Script Inaccurate Reports

Thu Sep 27, 2018 9:37 pm

Hi all, I have a functioning script that reports IP Pool usage. However, it does not behave correctly because 2 pools are "connected". Basically, pppoe1 spills over into pppoe2. this is causing IP Pool usage to be reported incorrectly.


I really appreciate your help.


LegacyDHCP [32/253] 12 % used
vpn-pool [0/13] 0 % used
Personal DHCP Pool [0/1] 0 % used
SERVERS [0/231] 0 % used
pppoe-pool-1 [283/253] 111 % used
pppoe-pool-2 [0/510] 0 % used
local-diag [0/5] 0 % used


Here 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

  # 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={
           :log error ("IP Pool " . $poolname . " is " . $poolpercent . "% full")
           :put ([:terminal style varname] . $line)
        } else={
           :if ( [:tonum $poolpercent] > $warnthreshold ) do={
              :log warning ("IP Pool " . $poolname . " is " . $poolpercent . "% full")
              :put ([:terminal style syntax-meta] . $line)
           } else={
              :put ([:terminal style none] . $line)
           }
        }

  # foreach p
     }
  # /ip pool
  }
}

Who is online

Users browsing this forum: No registered users and 23 guests