script to check if customer has downloaded x gigs and email

hello guys, I am having problem with a script that i modified to do what i need but for some reason it is not working exactly right. I tested it with one queue in a test enviroment and it worked excelent but when i put it on my main router with hundreds of users it runs untill it finds one user with 99% usage and applies the change to all users after that without regards to what they have used. I am sure it is something simple, but i cant seem to find the problem.

{
:global mailserver
:global adminaddress
:global fromaddress
:local content
:local i
:local sitename
:local gigs
:local email
:local megstotal
:local totalcurrent
:local bytesdowncurrent
:local bytesupcurrent
:local megsdowncurrent
:local megsupcurrent
:local percentage
:local lastwarning
:local warninglevel
:local warn
:local update
:local newwarning
:local bytesdownsaved
:local bytestotal
:local over
:local speed

#Script based on Automated billing script at http://wiki.mikrotik.com/wiki/AutomatedBilling
#Details:
#This script checks all current simple queues and using values stored in the queue comment will allow you keep track of usage by each site
#The details stored in the queue are broken down and used to determine when a user should be sent a usage warning at 50 75 90 and 100%
#You can create/modify a new entry as long as you follow this format
#1) If you just want to name an entry you may do so like “XYZ Router” in the queue name
#2) If you want to determine a data limit, enter it like “sitename!gigabytelimit#emailaddress!00#0000” into the comment.
#3) If you just want to enter a comment on its own, you can do this so long as you don’t have an “!” in the field
#Breaking it down: sitename is a descriptive name, gigabyte limit is the monthy limit, email address is the address you want notified
#00 is the last percentage warning level for this user, 0000 is the default value for a new data store
#This version created by Andrew Cox - http://www.accessplus.com.au
#v1.4 Updated 15/June/2009
:log info “------ Begining Daily Usage Reports -------”

#For each queue in the list
:foreach i in=[/queue simple find comment !=“”] do={

#Pull comment out of queue and divide up accordingly
:set content [/queue simple get $i comment]

#Determine variables from comment
#Format is: sitename ! gigsallowed # who-to-email ! last warning level(0-50-75-90-99)
:if ([:find $content “!”] != “”) do={
:local pos1 [:find $content “!”]
:local pos4 [:len $content]
:local pos2 ([:find [:pick $content ($pos1+1) $pos4] “#”]+$pos1+1)
:local pos3 ([:find [:pick $content ($pos2+1) $pos4] “!”]+$pos2)
:set sitename [:pick $content 0 ($pos1)]
:set gigs [:pick $content ($pos1+1) $pos2]
:set email [:pick $content ($pos2+1) ($pos3+1)]
:set totalcurrent [/queue simple get $i bytes]
:set lastwarning [:pick $content ($pos3+2) ($pos3+4)]
:local pos5 [:find $totalcurrent “/”]
:local pos6 [:len $totalcurrent]
:set bytesupcurrent ([:pick $totalcurrent 0 ($pos5)])
:set bytesdowncurrent ([:pick $totalcurrent ($pos5+1) $pos6])
:set megsupcurrent ($bytesupcurrent / 1048576)
:set bytesdownsaved ([:pick $content ($pos3+5) $pos4])
:set bytestotal ($bytesdowncurrent + $bytesdownsaved)
:set megsdowncurrent ($bytestotal / 1048576)
:set speed [/queue simple get $i max-limit]

#Begin calculating usage percentage
:set percentage ( ( $bytestotal * 100) / ($gigs * 1073741824 ) )
:log info “$sitename: $percentage%”
:log info “$speed”
:if ([$percentage] < 50) do={ :set warninglevel “00” }
:if ([$percentage] > 50) do={
:if ([$percentage] < 75) do={ :set warninglevel “50” }
:if ([$percentage] > 75) do={ :set warninglevel “75” }
}
:if ([$percentage] > 75) do={
:if ([$percentage] < 90) do={ :set warninglevel “75” }
:if ([$percentage] > 90) do={ :set warninglevel “90” }
}
:if ([$percentage] > 90) do={
:if ([$percentage] < 100) do={ :set warninglevel “90” }
:if ([$percentage] > 100) do={ :set warninglevel “99” }
}

Parse warning necessity

:if ([$lastwarning] = 99) do={ :set over “true” }
:if ([$warninglevel] > $lastwarning ) do={ :set warn “true” ; :set update “true” }
:if ([$warninglevel] = $lastwarning ) do={ :set warn “false” ; :set update “false” }
:if ([$warninglevel] < $lastwarning ) do={ :set warn “false” ; :set update “true” }


#Update Warning Levels
:if ([$update] = true ) do={ :set newwarning $warninglevel } else={ :set newwarning $lastwarning }
:if ([$warn] = true ) do={
#Check for email address in queue comment
:if ([$email] != “” ) do={
/tool e-mail send server=$mailserver from=$fromaddress to=“$email” subject=“$sitename: Ya uso $percentage%” body="Este mensaje es para informarte de $sitename
Su uso actual este en $warninglevel%.

Ya bajo $megsdowncurrent MB, de lo cual son mas de $percentage% de las $gigs GB mensual permitido antes de que su velocidad es limitado.

Este correo es Automatico y se manda solo cuando el cleinte llegue al 50%, 75%, 90% y 100% de su limite mensual.
Sistema de Monitoreo Samianet,
$fromaddress"
}
#Send email to support/accounts also
/tool e-mail send server=$mailserver from=$fromaddress to=$adminaddress subject=“$sitename: Usage at $percentage” body=“Current usage for $sitename - trigger is $warninglevel%.
$megsdowncurrent MB, which is over $percentage% of the $gigs GB monthly download allowance.
Traffic Monitor System”
:log info “Sent Warning Level $warninglevel% to $email”
}

:if ([$over] =true ) do={
#Check if bandwith has been changed already
:if ([$speed] =“256k/1M”) do={
#Check for email address in queue comment
:if ([$email] != “” ) do={
/tool e-mail send server=$mailserver from=$fromaddress to=“$email” subject=“$sitename: Ya uso $percentage%” body="Este mensaje es para informarte de $sitename
Su uso actual este en $warninglevel%.

Ya bajo $megsdowncurrent MB, de lo cual son mas de $percentage% de las $gigs GB mensual permitido. Su velocidad ha sido limitado 128k/128k. Si usted desea seguir navegando con alta velocidad se puede considerar pasar a otro plan. Ofrecemos planes de 2gb 10gb, 20gb, 30gb y ilimitado.

Este correo es Automatico y se manda solo cuando el cleinte llegue al 50%, 75%, 90% y 100% de su limite mensual.
Sistema de Monitoreo Samianet,
$fromaddress"
}
#Send email to support/accounts also
/tool e-mail send server=$mailserver from=$fromaddress to=$adminaddress subject=“$sitename: Usage at $percentage Cambio Velocidad” body=“Current usage for $sitename - trigger is $warninglevel% fueron limitados.
$megsdowncurrent MB, which is over $percentage% of the $gigs GB monthly download allowance.
Traffic Monitor System”
:log info “Sent Warning Level $warninglevel% to $email y bajo su ancho de banda”
/queue simple set $i max-limit=128k/128k}

}


#Set new warning level on queue comment

/queue simple set $i comment=“$sitename!$gigs#$email!$newwarning#$bytestotal”
/queue simple reset-counters $i
}
}
}
}
}
}


In red i marked what changes I made

thanks

Hi Mate,

There’s an updated version of the script with some bug fixes here: http://www.mikrotik-routeros.com/?p=24

:wink:

thanks for the reply but the link is dead. edit: site is working now. will readup on it and give it another try.

michael