DHCP Threshold Alert

Hello,

I’ve been working on this script. It’s “sort” of working. No matter what I set the local threshold to, 20, 40, 90, the alert comes back with the same numbers. "inside_vlan Pool is 1000% full. Can someone please let me know what I am doing wrong?


:local threshold 20

:foreach pool in=[/ip pool find] do={

:local name [/ip pool get $pool name]
:local ranges [/ip pool get $pool ranges]
:local used [/ip pool get $pool used]
:local size 0

:foreach r in=$ranges do={
:local start [:pick $r 0 [:find $r "-"]]
:local end [:pick $r ([:find $r "-"] + 1) [:len $r]]


:set size ($size + ($endInt - $startInt + 1))
}

:if ($size > 0) do={
:local percentUsed (100 * $used / $size)

:if ($percentUsed > $threshold) do={
:log warning ("IP Pool '$name' is $percentUsed% full ($used of $size)")
/tool e-mail send to="myemail@email.com" subject="DHCP Pool Alert" body="$name is $percentUsed% full."
}
}
}

the first error is to use

quote

instead of

code

that make unreadable what you post.


The second, very serious, mistake is to use the same name as the objects as the variable name.

Another error is you do not specify RouterOS version,
for example “used” exist on 7.19.1 but not on 7.16.2

My apologies, I have that corrected. Can you see the code now?

Why do most people read the “first line” and ignore everything else that is written?

Second Is #16 of the list:
http://forum.mikrotik.com/t/gp-csa-for-mikrotik-devices/182176/1

I would propose an addition:
#24: The user usually is not able to follow or understand multiple information in a single post, so either he only considers the first one or the last one ignores all the previous ones.