usage warning script

Hi i am trying tho get the usage warning script to work but with no luck. Please look at the script and let me know if im doing something wrong. I only need the usage warning script and not the other automated billing scripts. I cant see anything happening in the logs when i manually run the script.

:local name
:local surname
:local usage
:local bytesin
:local bytesout
:local bytestotal
:local megstotal
:local cap
:local comment
:local newcomment
:local invoice
:local newinvoice
:local lastmonth
:local monthname
:local lastwarning
:local newmonth
:local newwarning
:local percentage
:local percentile
:local warninglevel
:local warn
:local update
:local email
:local server

:set server 192.168.0.230
/tool user-manager user
:foreach i in=[/tool user-manager user find subscriber=admin] do={

:set name [get $i first-name]
:set surname [get $i last-name]
:set comment [get $i comment]
:set bytesin [get $i download-used]
:set bytesout [get $i upload-used]
:set bytestotal ($bytesin + $bytesout)
:set email [get $i email]

#Test Comment:  10001 3000 50 09

:set megstotal ($bytestotal / 1000000)
:set cap [:pick $comment 5 9]
:set invoice [:pick $comment 0 5]
:set newinvoice ($invoice)
:set lastwarning [:pick $comment 9 11]
:set lastmonth [:pick $comment 11 13]
:set percentage (($megstotal * 100) / $cap)

#classifying your current cap percentile
#I ended up not using the Percentile variable
#The Percentile variable just describes where, more or less,
#the user's current usage percentage is.

:if ([$percentage] < 50) do={ :set percentile 1050 ; :set warninglevel 10 }

:if ([$percentage] > 50) do={
:if ([$percentage] < 75) do={ :set percentile 5075 ; :set warninglevel 50 }
:if ([$percentage] > 75) do={ :set percentile 7590 ; :set warninglevel 75 }
}

:if ([$percentage] > 75) do={
:if ([$percentage] < 90) do={ :set percentile 7590 ; :set warninglevel 75 }
:if ([$percentage] > 90) do={ :set percentile 9010 ; :set warninglevel 90 }
}

:if ([$percentage] > 90) do={
:if ([$percentage] < 100) do={ :set percentile 9010 ; :set warninglevel 90 }
:if ([$percentage] > 100) do={ :set percentile 1000 ; :set warninglevel 99 }
}

# Parse warning necessity

: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 }

:if ([$update] = true) do={ :set newwarning
$warninglevel ; :set newinvoice $invoice ; :set newmonth $lastmonth } else={ :set newwarning $lastwarning ; :set
newinvoice $invoice ; :set  newmonth $lastmonth }
:if ([$warn] = true) do={

/tool e-mail send to=$email from=accounts@megamax.za.net server=$server subject="Usage Warning: Usage at $percentage" body="Dear $name $surname

This message is to inform you of your current usage.
The current warning trigger is $warninglevel%.

You have downloaded $megstotal MB, which is $percentage%
of your $cap MB cap.

This is an Automatically generated E-mail
that is sent out when users reach 50%, 75%, 90% and 100% of their cap.

If any information contained in this E-mail is incorrect,
please inform us immediately.
If you have received this E-mail in error,
please reply to this E-mail stating that you are not the intended recipient,
and delete this message.

Thank You

Accounts
MEGAMAX
accounts@megamax.za.net"

:log info "Sent Warning Level $warninglevel% to $email"

/tool e-mail send to=agent1@megamax.za.net
from=NoReply@YourWISP.com server=$server  subject="Usage Warning for $name $surname" body="Usage Notification for $name $surname
Usage: $megstotal
Cap: $cap
Percentage: $percentage%"

:log info "Sent Notification for $name $surname to Accounts" }

:set newcomment "$newinvoice$cap$newwarning$newmonth"
/tool user-manager user set $i comment=$newcomment
}

try to replace following line

:foreach i in=[/tool user-manager user find subscriber=admin]

with this one

:foreach i in=[/tool user-manager user find subscriber="admin"]

Still no result in the log. created a test account of 50 megs and ran script manually on 50% 75% and 100% with no results being emailed to agent1.

I thought it might be smtp server and tried something small like script below and all worked hundred percent. i noticed however there is the "" after the /tool e-mail send \ but none in the warning script.
Could this have any effect?
Could it be my version?3.30 .
Am i correct in putting the smtp server in here?

:set server 192.168.0.230



/tool e-mail send \
to=agent1@megamax.za.net \

subject=("test") \
body=("blablabla")

i seem to be getting errors when i run the script in terminal

  MikroTik RouterOS 3.30 (c) 1999-2009       http://www.mikrotik.com/





[ashraf@happydaddy] > :local name
[ashraf@happydaddy] > :local surname
[ashraf@happydaddy] > :local usage
[ashraf@happydaddy] > :local bytesin
[ashraf@happydaddy] > :local bytesout
[ashraf@happydaddy] > :local bytestotal
[ashraf@happydaddy] > :local megstotal
[ashraf@happydaddy] > :local cap
[ashraf@happydaddy] > :local comment
[ashraf@happydaddy] > :local newcomment
[ashraf@happydaddy] > :local invoice
[ashraf@happydaddy] > :local newinvoice
[ashraf@happydaddy] > :local lastmonth
[ashraf@happydaddy] > :local monthname
[ashraf@happydaddy] > :local lastwarning
[ashraf@happydaddy] > :local newmonth
[ashraf@happydaddy] > :local newwarning
[ashraf@happydaddy] > :local percentage
[ashraf@happydaddy] > :local percentile
[ashraf@happydaddy] > :local warninglevel
[ashraf@happydaddy] > :local warn
[ashraf@happydaddy] > :local update
[ashraf@happydaddy] > :local email
[ashraf@happydaddy] > :local server
[ashraf@happydaddy] > 
[ashraf@happydaddy] > :set server 192.168.0.230
expected variable name (line 1 column 6)
[ashraf@happydaddy] > /tool user-manager user
[ashraf@happydaddy] /tool user-manager user> :foreach i in=[/tool user-manager user find subscriber="admin"
] do={
{...                                                                                                     
{... :set name [get $i first-name]
expected variable name (line 3 column 6)
[ashraf@happydaddy] /tool user-manager user> :set surname [get $i last-name]
expected variable name (line 1 column 6)
[ashraf@happydaddy] /tool user-manager user> :set comment [get $i comment]
expected variable name (line 1 column 6)
[ashraf@happydaddy] /tool user-manager user> :set bytesin [get $i download-used]
expected variable name (line 1 column 6)
[ashraf@happydaddy] /tool user-manager user> :set bytesout [get $i upload-used]
expected variable name (line 1 column 6)
[ashraf@happydaddy] /tool user-manager user> :set bytestotal ($bytesin + $bytesout)
expected variable name (line 1 column 6)
[ashraf@happydaddy] /tool user-manager user> :set email [get $i email]
expected variable name (line 1 column 6)
[ashraf@happydaddy] /tool user-manager user> 
[ashraf@happydaddy] /tool user-manager user> #Test Comment:  10001 3000 50 09

You cannot run scripts in terminal like that. Each terminal line is its own implied scope, so variables are falling out of scope on each new line.
Wrap the entire script in a {} brace pair to maintain scope.

oops thanks fewi…still learning. Is there a way to test the script and get a result in the log.

Ok… i think its working now. Just need to let it run and see. Below is my script.


:local date
:local cap
:local clientname
:local name
:local surname
:local usage
:local bytesin
:local bytesout
:local bytestotal
:local megstotal
:local comment
:local newcomment
:local invoice
:local newinvoice
:local lastmonth
:local monthname
:local lastwarning
:local newmonth
:local newwarning
:local percentage
:local percentile
:local warninglevel
:local warn
:local update
:local email
:local server
/tool user-manager user
:foreach i in=[/tool user-manager user find subscriber="admin"] do={
:set server 192.168.0.230
:set comment [get $i comment]
:set clientname [get $i last-name]
:set name [get $i name]
:set email [get $i email]
:set bytesin [get $i download-used]
:set bytesout [get $i upload-used]
:set bytestotal ($bytesin + $bytesout)
:set megstotal ($bytestotal / 1000000)
:set cap [:pick $comment 5 9]
:set invoice [:pick $comment 0 5]
:set newinvoice ($invoice)
:set lastwarning [:pick $comment 9 11]
:set lastmonth [:pick $comment 11 13]
:set percentage (($megstotal * 100) / $cap)

:if ([$percentage] < 50) do={ :set percentile 1050 ; :set warninglevel 10 }

:if ([$percentage] > 50) do={ 
     :if ([$percentage] < 75) do={ :set percentile 5075 ; :set warninglevel 50 } 
     :if ([$percentage] > 75) do={ :set percentile 7590 ; :set warninglevel 75 }
}

:if ([$percentage] > 75) do={ 
     :if ([$percentage] < 90) do={ :set percentile 7590 ; :set warninglevel 75 } 
     :if ([$percentage] > 90) do={ :set percentile 9010 ; :set warninglevel 90 }
}

:if ([$percentage] > 90) do={ 
     :if ([$percentage] < 100) do={ :set percentile 9010 ; :set warninglevel 90 } 
     :if ([$percentage] > 100) do={ :set percentile 1000 ; :set warninglevel 99 }
}

: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 }

:if ([$update] = true) do={ :set newwarning $warninglevel ; :set newinvoice $invoice ; :set newmonth $lastmonth } else={ :set newwarning $lastwarning ; :set newinvoice $invoice ; :set newmonth $lastmonth }

:if ([$warn] = true) do={

/tool e-mail send to=$email from=NoReply@YourWISP.com server=$server subject="Usage Warning: Usage at $percentage" body="Dear $name $surname

 The current warning trigger is $warninglevel %.
 You have downloaded $megstotal MB, which is $percentage %
 of your $cap MB cap.

 Thank You

 Megamax"
}



:log info "Sent Warning Level $warninglevel% to $email"

/tool e-mail send to=nawaal@megamax.za.net from=NoReply@YourWISP.com server=$server subject="Usage Warning for $name $surname" body="Usage Notification for $name $surname
 Usage: $megstotal
 Cap: $cap
 Percentage: $percentage%"


:set newcomment "$newinvoice$cap$newwarning$newmonth"
/tool user-manager user set $i comment=$newcomment
}

So is it working now…?

Yes and no…

After breaking up the script in peices and runing it in parts i found a few problems

1st one Which admin helped with.

:foreach i in=[/tool user-manager user find subscriber=admin]

with this one
Code:
:foreach i in=[/tool user-manager user find subscriber=“admin”]

2nd one i found this } was stopping the script from running

 :log info "Sent Notification for $name $surname to Accounts" }
 
 :set newcomment "$newinvoice$cap$newwarning$newmonth"
 /tool user-manager user set $i comment=$newcomment
 }

I just removed the first } and the it started up

 :log info "Sent Notification for $name $surname to Accounts" 

 :set newcomment "$newinvoice$cap$newwarning$newmonth"
 /tool user-manager user set $i comment=$newcomment
 }

Another problem i saw is that if there is no user email address in the usermanager it crashes. Is there a way to bypass the ones with no emails? i noticed that the moment i run the script manually it sends the emails but what i cant understand is if i run it again it sends the same users a email.Is it not supposed to remember that it sent a email to that person and not send a warning again?

Below is the script am am using currently Hope this helps others

:local name
:local surname
:local usage
:local bytesin
:local bytesout
:local bytestotal
:local megstotal
:local cap
:local comment
:local newcomment
:local invoice
:local newinvoice
:local lastmonth
:local monthname
:local lastwarning
:local newmonth
:local newwarning
:local percentage
:local percentile
:local warninglevel
:local warn
:local update
:local email
:local server

:set server 192.168.0.230
/tool user-manager user
:foreach i in=[/tool user-manager user find subscriber="admin"] do={

:set name [get $i first-name]
:set surname [get $i last-name]
:set comment [get $i comment]
:set bytesin [get $i download-used]
:set bytesout [get $i upload-used]
:set bytestotal ($bytesin + $bytesout)
:set email [get $i email]
:set megstotal ($bytestotal / 1000000)

#Test Comment:  10001 3000 50 09

:set cap [:pick $comment 5 9]
:set invoice [:pick $comment 0 5]
:set newinvoice ($invoice)
:set lastwarning [:pick $comment 9 11]
:set lastmonth [:pick $comment 11 13]
:set percentage (($megstotal * 100) / $cap)

#classifying your current cap percentile
#I ended up not using the Percentile variable
#The Percentile variable just describes where, more or less,
#the user's current usage percentage is.

:if ([$percentage] < 50) do={ :set percentile 1050 ; :set warninglevel 10 }

:if ([$percentage] > 50) do={
     :if ([$percentage] < 75) do={ :set percentile 5075 ; :set warninglevel 50 } 
     :if ([$percentage] > 75) do={ :set percentile 7590 ; :set warninglevel 75 }
}

:if ([$percentage] > 75) do={
     :if ([$percentage] < 90) do={ :set percentile 7590 ; :set warninglevel 75 } 
     :if ([$percentage] > 90) do={ :set percentile 9010 ; :set warninglevel 90 }
}

:if ([$percentage] > 90) do={
     :if ([$percentage] < 100) do={ :set percentile 9010 ; :set warninglevel 90 } 
     :if ([$percentage] > 100) do={ :set percentile 1000 ; :set warninglevel 99 }
}

# Parse warning necessity

: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 }

:if ([$update] = true) do={ :set newwarning $warninglevel ; :set newinvoice $invoice ; :set newmonth $lastmonth } else={ :set newwarning $lastwarning ; :set newinvoice $invoice ; :set newmonth $lastmonth }

/tool e-mail send to=$email from=NoReply@YourWISP.com server=$server subject="Usage Warning: Usage at $percentage" body="Dear $name $surname
 
This message is to inform you of your current usage.
The current warning trigger is $warninglevel%.
 
You have downloaded $megstotal MB, which is $percentage%
of your $cap MB cap.
 
This is an Automatically generated E-mail
that is sent out when users reach 50%, 75%, 90% and 100% of their cap.

If any information contained in this E-mail is incorrect,
please inform us immediately.
If you have received this E-mail in error,
please reply to this E-mail stating that you are not the intended recipient,
and delete this message.

Thank You
 
Accounts
MegaMax
accounts@megamx.za.net"

:log info "Sent Warning Level $warninglevel% to $email"

/tool e-mail send to=nawaal@maxwell.za.net from=NoReply@YourWISP.com server=$server subject="Usage Warning for $name $surname" body="Usage Notification for $name $surname
Usage: $megstotal
Cap: $cap
Percentage: $percentage%"

:log info "Sent Usage Warning copy for $name $surname to Accounts" 


:set newcomment "$newinvoice$cap$newwarning$newmonth"
/tool user-manager user set $i comment=$newcomment
}

Yes it was, but you dropped the line that handled that (which also explains the ‘ekstra’ } you had to remove, to make it run)

:if ([$warn] = true) do={



change above missing line with

:if ([$warn] = true && [:len $email] > 0) do={

Also be aware that if your costumer hit 100, warninglevel will not be set!
Personally I would have written it like this, but granted, it is a matter of taste.

:if ($percentage < 50) do={
    :set warninglevel 10
} else={
     :if ($percentage < 75) do={
        :set warninglevel 50
     } else={
        :if ($percentage < 90) do={
            :set warninglevel 75
        } else={
            :if ($percentage < 100) do={
                :set warninglevel 90
            } else={
                :set warninglevel 99
            }
        }
    }
}

Sweet…will try it later and post results. Thanks for the help.

Hi

I am testing this script and it is working for me, but one question - does rebooting/clearing queue data affect the running totals? I’m hoping not since this is why the queue comment field is used?

TIA

Ok find below my script please check to see if all ok. Also how does it handle people who has uncapped example 0 transfer limit?


:local name
:local surname
:local usage
:local bytesin
:local bytesout
:local bytestotal
:local megstotal
:local cap
:local comment
:local newcomment
:local invoice
:local newinvoice
:local lastmonth
:local monthname
:local lastwarning
:local newmonth
:local newwarning
:local percentage
:local percentile
:local warninglevel
:local warn
:local update
:local email
:local server

:set server 192.168.0.230
/tool user-manager user
:foreach i in=[/tool user-manager user find subscriber="admin"] do={

:set name [get $i first-name]
:set surname [get $i last-name]
:set comment [get $i comment]
:set bytesin [get $i download-used]
:set bytesout [get $i upload-used]
:set bytestotal ($bytesin + $bytesout)
:set email [get $i email]
:set megstotal ($bytestotal / 1000000)

#Test Comment:  10001 3000 50 09

:set cap [:pick $comment 5 9]
:set invoice [:pick $comment 0 5]
:set newinvoice ($invoice)
:set lastwarning [:pick $comment 9 11]
:set lastmonth [:pick $comment 11 13]
:set percentage (($megstotal * 100) / $cap)

#classifying your current cap percentile
#I ended up not using the Percentile variable
#The Percentile variable just describes where, more or less,
#the user's current usage percentage is.

:if ($percentage < 50) do={
    :set warninglevel 10
} else={
     :if ($percentage < 75) do={
        :set warninglevel 50
     } else={
        :if ($percentage < 90) do={
            :set warninglevel 75
        } else={
            :if ($percentage < 100) do={
                :set warninglevel 90
            } else={
                :set warninglevel 99
            }
        }
    }
}

# Parse warning necessity

: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 }

:if ([$update] = true) do={ :set newwarning $warninglevel ; :set newinvoice $invoice ; :set newmonth $lastmonth } else={ :set newwarning $lastwarning ; :set newinvoice $invoice ; :set newmonth $lastmonth }


:if ([$warn] = true && [:len $email] > 0) do={

/tool e-mail send to=$email from=NoReply@YourWISP.com server=$server subject="Usage Warning: Usage at $percentage" body="Dear $name $surname
 
This message is to inform you of your current usage.
The current warning trigger is $warninglevel%.
 
You have downloaded $megstotal MB, which is $percentage%
of your $cap MB cap.
 
This is an Automatically generated E-mail
that is sent out when users reach 50%, 75%, 90% and 100% of their cap.

If any information contained in this E-mail is incorrect,
please inform us immediately.
If you have received this E-mail in error,
please reply to this E-mail stating that you are not the intended recipient,
and delete this message.

Thank You
 
Accounts
MegaMax
accounts@megamx.za.net"

:log info "Sent Warning Level $warninglevel% to $email" }



/tool e-mail send to=nawaal@maxwell.za.net from=NoReply@YourWISP.com server=$server subject="Usage Warning for $name $surname" body="Usage Notification for $name $surname
Usage: $megstotal
Cap: $cap
Percentage: $percentage%"

:log info "Sent Usage Warning copy for $name $surname to Accounts" 


:set newcomment "$newinvoice$cap$newwarning$newmonth"
/tool user-manager user set $i comment=$newcomment
}

Hmm my memory keeps filling up and i have to reboot system to free it again. Anything to do with the script?

Does anybody have this script running ok ?

try global instead local variables