Talk:Automated Usage Script without usermanager
From MikroTik Wiki
Email address to mail on update:
--
Original Notes:
I wrote this script to be a temporary solution to monitoring corporate customers while we changed from one monitoring system to another, as I didn't want the hastle of having to setup something as complicated as the user manager.
This now allows me to deploy a simple monitoring system at any location and takes less than 10 minutes to get up and running.
---
Newer, yet unformated version. Features:
- on peak / off peak accounting - rate limiting when over limit - adjustable on and off peak
---
# aug/15/2009 13:27:32 by RouterOS 3.27
# software id = HX8Q-6KBW
#
/system script
add name=overseer policy=\
ftp,reboot,read,write,policy,test,winbox,password,sniff source=":global fo\
und\r\
\n\r\
\n/system script run monthend\r\
\n\r\
\n:if ([\$found] = \"true\") do={/system script run monthlyreport; :log in\
fo \"--Completed Monthly Report--\"} else={ /system script run usagereport\
; :log info \"--Completed Usage Report--\"}"
add name=monthend policy=\
ftp,reboot,read,write,policy,test,winbox,password,sniff source=":local dat\
e\r\
\n:local time\r\
\n:local day\r\
\n:local month\r\
\n:local year\r\
\n:local hour\r\
\n:global found \"\"\r\
\n \r\
\n:set date [/system clock get date]\r\
\n:set time [/system clock get time]\r\
\n:set day [:pick \$date 4 6]\r\
\n:set hour [:pick \$time 0 2]\r\
\n\r\
\n:if ([\$day] = \"26\" ) do={ :if ([\$hour] = \"00\" ) do={:set found \"t\
rue\" } else={ :set found \"false\" } } else={ :set found \"false\" }"
add name=usagereport policy=\
ftp,reboot,read,write,policy,test,winbox,password,sniff source=":local dat\
e\r\
\n:local time\r\
\n:local day\r\
\n:local month\r\
\n:local year\r\
\n:local hour\r\
\n \r\
\n:set date [/system clock get date]\r\
\n:set time [/system clock get time]\r\
\n:set day [:pick \$date 4 6]\r\
\n:set hour [:pick \$time 0 2]\r\
\n\r\
\n:if ([\$hour] = \"00\") do={/system script run usagereportoffpeak; :log \
info \"--Completed off Peak Report--\"}\r\
\n:if ([\$hour] = \"01\") do={/system script run usagereportoffpeak; :log \
info \"--Completed off Peak Report--\"}\r\
\n:if ([\$hour] = \"02\") do={/system script run usagereportoffpeak; :log \
info \"--Completed off Peak Report--\"}\r\
\n:if ([\$hour] = \"03\") do={/system script run usagereportoffpeak; :log \
info \"--Completed off Peak Report--\"}\r\
\n:if ([\$hour] = \"04\") do={/system script run usagereportoffpeak; :log \
info \"--Completed off Peak Report--\"}\r\
\n:if ([\$hour] = \"05\") do={/system script run usagereportoffpeak; :log \
info \"--Completed off Peak Report--\"}\r\
\n:if ([\$hour] = \"06\") do={/system script run usagereportoffpeak; :log \
info \"--Completed off Peak Report--\"}\r\
\n:if ([\$hour] = \"07\") do={/system script run usagereportoffpeak; :log \
info \"--Completed off Peak Report--\"}\r\
\n:if ([\$hour] = \"08\") do={/system script run usagereportonpeak; :log i\
nfo \"--Completed On Peak Report--\"}\r\
\n:if ([\$hour] = \"09\") do={/system script run usagereportonpeak; :log i\
nfo \"--Completed On Peak Report--\"}\r\
\n:if ([\$hour] = \"10\") do={/system script run usagereportonpeak; :log i\
nfo \"--Completed On Peak Report--\"}\r\
\n:if ([\$hour] = \"11\") do={/system script run usagereportonpeak; :log i\
nfo \"--Completed On Peak Report--\"}\r\
\n:if ([\$hour] = \"12\") do={/system script run usagereportonpeak; :log i\
nfo \"--Completed On Peak Report--\"}\r\
\n:if ([\$hour] = \"13\") do={/system script run usagereportonpeak; :log i\
nfo \"--Completed On Peak Report--\"}\r\
\n:if ([\$hour] = \"14\") do={/system script run usagereportonpeak; :log i\
nfo \"--Completed On Peak Report--\"}\r\
\n:if ([\$hour] = \"15\") do={/system script run usagereportonpeak; :log i\
nfo \"--Completed On Peak Report--\"}\r\
\n:if ([\$hour] = \"16\") do={/system script run usagereportonpeak; :log i\
nfo \"--Completed On Peak Report--\"}\r\
\n:if ([\$hour] = \"17\") do={/system script run usagereportonpeak; :log i\
nfo \"--Completed On Peak Report--\"}\r\
\n:if ([\$hour] = \"18\") do={/system script run usagereportonpeak; :log i\
nfo \"--Completed On Peak Report--\"}\r\
\n:if ([\$hour] = \"19\") do={/system script run usagereportonpeak; :log i\
nfo \"--Completed On Peak Report--\"}\r\
\n:if ([\$hour] = \"20\") do={/system script run usagereportonpeak; :log i\
nfo \"--Completed On Peak Report--\"}\r\
\n:if ([\$hour] = \"21\") do={/system script run usagereportonpeak; :log i\
nfo \"--Completed On Peak Report--\"}\r\
\n:if ([\$hour] = \"22\") do={/system script run usagereportonpeak; :log i\
nfo \"--Completed On Peak Report--\"}\r\
\n:if ([\$hour] = \"23\") do={/system script run usagereportonpeak; :log i\
nfo \"--Completed On Peak Report--\"}"
add name=monthlyreport policy=\
ftp,reboot,read,write,policy,test,winbox,password,sniff source=":local dat\
e\r\
\n:local time\r\
\n:local day\r\
\n:local month\r\
\n:local year\r\
\n:local hour\r\
\n \r\
\n:set date [/system clock get date]\r\
\n:set time [/system clock get time]\r\
\n:set day [:pick \$date 4 6]\r\
\n:set hour [:pick \$time 0 2]\r\
\n\r\
\n:if ([\$hour] = \"00\") do={/system script run monthlyreportoffpeak; :lo\
g info \"--Completed off peak monthly total--\"}\r\
\n:if ([\$hour] = \"01\") do={/system script run monthlyreportoffpeak; :lo\
g info \"--Completed off peak monthly total--\"}\r\
\n:if ([\$hour] = \"02\") do={/system script run monthlyreportoffpeak; :lo\
g info \"--Completed off peak monthly total--\"}\r\
\n:if ([\$hour] = \"03\") do={/system script run monthlyreportoffpeak; :lo\
g info \"--Completed off peak monthly total--\"}\r\
\n:if ([\$hour] = \"04\") do={/system script run monthlyreportoffpeak; :lo\
g info \"--Completed off peak monthly total--\"}\r\
\n:if ([\$hour] = \"05\") do={/system script run monthlyreportoffpeak; :lo\
g info \"--Completed off peak monthly total--\"}\r\
\n:if ([\$hour] = \"06\") do={/system script run monthlyreportoffpeak; :lo\
g info \"--Completed off peak monthly total--\"}\r\
\n:if ([\$hour] = \"07\") do={/system script run monthlyreportoffpeak; :lo\
g info \"--Completed off peak monthly total--\"}\r\
\n:if ([\$hour] = \"08\") do={/system script run monthlyreportonpeak; :log\
\_info \"--Completed On peak monthly total--\"}\r\
\n:if ([\$hour] = \"09\") do={/system script run monthlyreportonpeak; :log\
\_info \"--Completed On peak monthly total--\"}\r\
\n:if ([\$hour] = \"10\") do={/system script run monthlyreportonpeak; :log\
\_info \"--Completed On peak monthly total--\"}\r\
\n:if ([\$hour] = \"11\") do={/system script run monthlyreportonpeak; :log\
\_info \"--Completed On peak monthly total--\"}\r\
\n:if ([\$hour] = \"12\") do={/system script run monthlyreportonpeak; :log\
\_info \"--Completed On peak monthly total--\"}\r\
\n:if ([\$hour] = \"13\") do={/system script run monthlyreportonpeak; :log\
\_info \"--Completed On peak monthly total--\"}\r\
\n:if ([\$hour] = \"14\") do={/system script run monthlyreportonpeak; :log\
\_info \"--Completed On peak monthly total--\"}\r\
\n:if ([\$hour] = \"15\") do={/system script run monthlyreportonpeak; :log\
\_info \"--Completed On peak monthly total--\"}\r\
\n:if ([\$hour] = \"16\") do={/system script run monthlyreportonpeak; :log\
\_info \"--Completed On peak monthly total--\"}\r\
\n:if ([\$hour] = \"17\") do={/system script run monthlyreportonpeak; :log\
\_info \"--Completed On peak monthly total--\"}\r\
\n:if ([\$hour] = \"18\") do={/system script run monthlyreportonpeak; :log\
\_info \"--Completed On peak monthly total--\"}\r\
\n:if ([\$hour] = \"19\") do={/system script run monthlyreportonpeak; :log\
\_info \"--Completed On peak monthly total--\"}\r\
\n:if ([\$hour] = \"20\") do={/system script run monthlyreportonpeak; :log\
\_info \"--Completed On peak monthly total--\"}\r\
\n:if ([\$hour] = \"21\") do={/system script run monthlyreportonpeak; :log\
\_info \"--Completed On peak monthly total--\"}\r\
\n:if ([\$hour] = \"22\") do={/system script run monthlyreportonpeak; :log\
\_info \"--Completed On peak monthly total--\"}\r\
\n:if ([\$hour] = \"23\") do={/system script run monthlyreportonpeak; :log\
\_info \"--Completed On peak monthly total--\"}"
add name=manualreport policy=\
ftp,reboot,read,write,policy,test,winbox,password,sniff source="/system sc\
ript run usagereport\r\
\n:local pos1\r\
\n:local pos2\r\
\n:local pos3\r\
\n:local pos4\r\
\n:local pos5\r\
\n:local pos6\r\
\n:local pos7\r\
\n:local pos8\r\
\n:local content\r\
\n:local i\r\
\n:local sitename\r\
\n:local onpeakgigs\r\
\n:local offpeakgigs\r\
\n:local email\r\
\n:local megstotal\r\
\n:local totalcurrent\r\
\n:local bytesdowncurrent\r\
\n:local bytesupcurrent\r\
\n:local megsdownonpeakcurrent\r\
\n:local megsdownoffpeakcurrent\r\
\n:local megsupcurrent\r\
\n:local onpeakpercentage\r\
\n:local offpeakpercentage\r\
\n:local lastwarning\r\
\n:local warninglevel\r\
\n:local warn\r\
\n:local update\r\
\n:local newwarning\r\
\n:local bytesdownonpeaksaved\r\
\n:local bytesdownoffpeaksaved\r\
\n:local text\r\
\n:local combinedtext\r\
\n:set combinedtext \"\"\r\
\n\r\
\n#Scripts based on Automated billing script at http://wiki.mikrotik.com/w\
iki/AutomatedBilling\r\
\n# This version created by Andrew Cox - www.accessplus.com.au\r\
\n:log info \"------ Begining Manual Usage Reports -------\"\r\
\n\r\
\n#For each queue in the list\r\
\n:foreach i in=[/queue simple find comment !=\"\"] do={\r\
\n\r\
\n#Pull name out of queue and divide up accordingly\r\
\n:set content [/queue simple get \$i comment] \r\
\n\r\
\n#Determine variables from comment\r\
\n#Format is: sitename ! gigsallowed on peak / gigsallowed off peak # who-\
to-email ! last warning level(0-50-75-90-99) # stored onpeak / stored offp\
eak\r\
\n:if ([:find \$content \"!\"] != \"\") do={\r\
\n :set pos1 [:find \$content \"!\"]\r\
\n :set pos4 [:len \$content]\r\
\n :set pos2 ([:find [:pick \$content (\$pos1+1) \$pos4] \"#\"]+\$pos1+1)\
\r\
\n :set pos3 ([:find [:pick \$content (\$pos2+1) \$pos4] \"!\"]+\$pos2)\r\
\n :set sitename [:pick \$content 0 (\$pos1)]\r\
\n :set pos7 ([:find [:pick \$content (\$pos1+1) \$pos2] \"/\"]+\$pos1+1)\
\r\
\n :set onpeakgigs [:pick \$content (\$pos1+1) \$pos7]\r\
\n :set offpeakgigs [:pick \$content (\$pos7+1) \$pos2]\r\
\n :set email [:pick \$content (\$pos2+1) (\$pos3+1)]\r\
\n :set totalcurrent [/queue simple get \$i bytes]\r\
\n :set lastwarning [:pick \$content (\$pos3+2) (\$pos3+4)]\r\
\n :set pos5 [:find \$totalcurrent \"/\"]\r\
\n :set pos6 [:len \$totalcurrent]\r\
\n :set pos8 ([:find [:pick \$content (\$pos3+5) \$pos4] \"/\"]+\$pos3+5)\
\r\
\n :set bytesdownonpeaksaved ([:pick \$content (\$pos3+8) \$pos8])\r\
\n :set bytesdownoffpeaksaved ([:pick \$content (\$pos8+1) \$pos4])\r\
\n :set megsdownonpeakcurrent (\$bytesdownonpeaksaved / 1048576)\r\
\n :set megsdownoffpeakcurrent (\$bytesdownoffpeaksaved / 1048576)\r\
\n\r\
\n#Begin calculating usage percentage\r\
\n :set onpeakpercentage ( ( \$bytesdownonpeaksaved * 100) / (\$onpeakgig\
s * 1073741824 ) )\r\
\n :set offpeakpercentage ( ( \$bytesdownoffpeaksaved * 100) / (\$offpeak\
gigs * 1073741824 ) )\r\
\n :log info \"\$sitename: \$megsdownonpeakcurrent MB / \$megsdownoffpeak\
current MB of \$onpeakgigs GB / \$offpeakgigs GB Download Allowance\"\r\
\n :set text \"\$combinedtext \\n \$sitename: \$megsdownonpeakcurrent MB \
Peak / \$megsdownoffpeakcurrent MB Off-Peak of \$onpeakgigs GB Peak / \$of\
fpeakgigs GB Off-Peak Download Allowance\"\r\
\n :set combinedtext \"\$text\"\r\
\n }\r\
\n}\r\
\n:log info \"------ Ending Manual Usage Reports -------\"\r\
\n:local time [/system clock get time]\r\
\n/tool e-mail send server=\"1.3.3.7\" from=\"report@example.com\" t\
o=\"accounts@example.com\" subject=\"Manual Data Usage Report\
\_- Pirate ADSL\" body=\"Pirate Internet ADSL Download Usage Report - Runt\
ime: \$time\r\
\n\$text\r\
\n\r\
\nMikrotik Traffic Monitor System\r\
\n- This is a manually run report, regular reports will come at their own \
standard percentage intervals -\r\
\nNote: Peak Data is defined as 08:00am to 11:59pm, Off Peak Data is defin\
ed as 12:00am to 07:59am.\""
add name=Quick-Generate-Queues policy=\
ftp,reboot,read,write,policy,test,winbox,password,sniff source="#change th\
e target address range to match what you want to generate.\r\
\n:local x\r\
\n:for x from 2 to 254 do={/queue simple add target-address=\"1.1.1.\
\$x\" queue=\"default/default\"}\r\
\n"
add name=usagereportonpeak policy=\
ftp,reboot,read,write,policy,test,winbox,password,sniff source=":local pos\
1\r\
\n:local pos2\r\
\n:local pos3\r\
\n:local pos4\r\
\n:local pos5\r\
\n:local pos6\r\
\n:local pos7\r\
\n:local pos8\r\
\n:local pos9\r\
\n:local content\r\
\n:local i\r\
\n:local sitename\r\
\n:local onpeakgigs\r\
\n:local offpeakgigs\r\
\n:local email\r\
\n:local megstotal\r\
\n:local totalcurrent\r\
\n:local bytesdowncurrent\r\
\n:local bytesupcurrent\r\
\n:local megsdowncurrent\r\
\n:local megsupcurrent\r\
\n:local percentage\r\
\n:local lastonpeakwarning\r\
\n:local lastoffpeakwarning\r\
\n:local warninglevel\r\
\n:local warn\r\
\n:local update\r\
\n:local newwarning\r\
\n:local bytesdownonpeaksaved\r\
\n:local bytesdownoffpeaksaved\r\
\n:local bytestotal\r\
\n\r\
\n#Script based on Automated billing script at http://wiki.mikrotik.com/wi\
ki/AutomatedBilling\r\
\n#Details:\r\
\n#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\r\
\n#The details stored in the queue are broken down and used to determine w\
hen a user should be sent a usage warning at 50 75 90 and 100%\r\
\n#You can create/modify a new entry as long as you follow this format\r\
\n#1) If you just want to name an entry you may do so like \"XYZ Router\" \
in the queue name\r\
\n#2) If you want to determine a data limit, enter it like \"sitename!giga\
bytelimit#emailaddress!00#0000\" into the comment.\r\
\n#3) If you just want to enter a comment on its own, you can do this so l\
ong as you don't have an \"!\" in the field\r\
\n#Breaking it down: sitename is a descriptive name, gigabyte limit is the\
\_monthy limit, email address is the address you want notified \r\
\n#00 is the last percentage warning level for this user, 0000 is the defa\
ult value for a new data store\r\
\n#This version created by Andrew Cox - www.accessplus.com.au\r\
\n#v1.4 Updated 15/June/2009\r\
\n:log info \"------ Begining Daily Usage Reports -------\"\r\
\n\r\
\n#For each queue in the list\r\
\n:foreach i in=[/queue simple find comment !=\"\"] do={\r\
\n\r\
\n#Pull comment out of queue and divide up accordingly\r\
\n:set content [/queue simple get \$i comment]\r\
\n\r\
\n#Determine variables from comment\r\
\n#Format is: sitename ! gigsallowed on peak / gigsallowed off peak # who-\
to-email ! last warning level(0-50-75-90-99) # stored onpeak / stored offp\
eak\r\
\n:if ([:find \$content \"!\"] != \"\") do={\r\
\n :set pos1 [:find \$content \"!\"]\r\
\n :set pos4 [:len \$content]\r\
\n :set pos2 ([:find [:pick \$content (\$pos1+1) \$pos4] \"#\"]+\$pos1+1)\
\r\
\n :set pos3 ([:find [:pick \$content (\$pos2+1) \$pos4] \"!\"]+\$pos2+1)\
\r\
\n :set sitename [:pick \$content 0 (\$pos1)]\r\
\n :set pos7 ([:find [:pick \$content (\$pos1+1) \$pos2] \"/\"]+\$pos1+1)\
\r\
\n :set onpeakgigs [:pick \$content (\$pos1+1) \$pos7]\r\
\n :set offpeakgigs [:pick \$content (\$pos7+1) \$pos2]\r\
\n :set email [:pick \$content (\$pos2+1) (\$pos3)]\r\
\n :set totalcurrent [/queue simple get \$i bytes]\r\
\n :set lastonpeakwarning [:pick \$content (\$pos3+1) (\$pos3+3)]\r\
\n :set lastoffpeakwarning [:pick \$content (\$pos3+4) (\$pos3+6)]\r\
\n :set pos5 [:find \$totalcurrent \"/\"]\r\
\n :set pos6 [:len \$totalcurrent]\r\
\n :set bytesupcurrent ([:pick \$totalcurrent 0 (\$pos5)])\r\
\n :set bytesdowncurrent ([:pick \$totalcurrent (\$pos5+1) \$pos6])\r\
\n :set megsupcurrent (\$bytesupcurrent / 1048576)\r\
\n :set pos8 ([:find [:pick \$content (\$pos3+7) \$pos4] \"/\"]+\$pos3+7)\
\r\
\n :set bytesdownonpeaksaved ([:pick \$content (\$pos3+7) \$pos8])\r\
\n :set bytesdownoffpeaksaved ([:pick \$content (\$pos8+1) \$pos4])\r\
\n :set bytestotal (\$bytesdowncurrent + \$bytesdownonpeaksaved)\r\
\n :set megsdowncurrent (\$bytestotal / 1048576)\r\
\n\r\
\n#Begin calculating usage percentage\r\
\n :set percentage ( ( \$bytestotal * 100) / (\$onpeakgigs * 1073741824 )\
\_)\r\
\n :log info \"\$sitename: \$percentage%\"\r\
\n :if ([\$percentage] < 50) do={ :set warninglevel \"00\"; /queue simple\
\_set \$i max-limit=0/0}\r\
\n :if ([\$percentage] > 50) do={\r\
\n :if ([\$percentage] < 75) do={ :set warninglevel \"50\"; /queue simp\
le set \$i max-limit=0/0}\r\
\n :if ([\$percentage] > 75) do={ :set warninglevel \"75\"; /queue simp\
le set \$i max-limit=0/0}\r\
\n }\r\
\n :if ([\$percentage] > 75) do={\r\
\n :if ([\$percentage] < 90) do={ :set warninglevel \"75\"; /queue simp\
le set \$i max-limit=0/0}\r\
\n :if ([\$percentage] > 90) do={ :set warninglevel \"90\"; /queue simp\
le set \$i max-limit=0/0}\r\
\n }\r\
\n :if ([\$percentage] > 90) do={\r\
\n :if ([\$percentage] < 100) do={ :set warninglevel \"90\"; /queue sim\
ple set \$i max-limit=0/0}\r\
\n :if ([\$percentage] > 100) do={ :set warninglevel \"99\"; /queue sim\
ple set \$i max-limit=256k/256k ; :log info \"\$sitename restricted to 256\
k - onpeak\" }\r\
\n }\r\
\n\r\
\n# Parse warning necessity\r\
\n :if ([\$warninglevel] > \$lastonpeakwarning ) do={ :set warn \"true\" ;\
\_:set update \"true\" }\r\
\n :if ([\$warninglevel] = \$lastonpeakwarning ) do={ :set warn \"false\" \
; :set update \"false\" }\r\
\n :if ([\$warninglevel] < \$lastonpeakwarning ) do={ :set warn \"false\" \
; :set update \"true\" }\r\
\n\r\
\n#Update Warning Levels\r\
\n :if ([\$update] = true ) do={ :set newwarning \$warninglevel } else={ :\
set newwarning \$lastonpeakwarning }\r\
\n :if ([\$warn] = true ) do={\r\
\n#Check for email address in queue comment\r\
\n :if ([\$email] != \"\" ) do={\r\
\n /tool e-mail send to=\"\$email\" from=\"report@example.com\" server=\"\
1.3.3.7\" subject=\"\$sitename: Usage at \$percentage\" body=\"This \
message is to inform you of the current on peak usage for \$sitename\r\
\nThe current warning trigger is \$warninglevel%.\r\
\n\r\
\nThis site has downloaded \$megsdowncurrent MB of its onpeak allowance, w\
hich is over \$percentage% of the \$onpeakgigs GB onpeak \$offpeakgigs GB \
offpeak monthly download allowance.\r\
\n\r\
\nThis is an Automatically generated E-mail that is sent out when users re\
ach 50%, 75%, 90% and 100% of their onpeak/offpeak cap.\r\
\n\r\
\nTraffic Monitor System,\r\
\naccounts@example.com\"\r\
\n}\r\
\n#Send email to support/accounts also\r\
\n /tool e-mail send to=\"accounts@example.com\" from=\"accounts@\
example.com\" server=\"1.3.3.7\" subject=\"\$sitename: Usage at \
\$percentage\" body=\"Current usage for \$sitename - trigger is \$warningl\
evel%.\r\
\n\$megsdowncurrent MB, which is over \$percentage% of the \$onpeakgigs GB\
\_monthly download allowance.\r\
\nTraffic Monitor System\"\r\
\n:log info \"Sent Warning Level \$warninglevel% to \$email\"\r\
\n}\r\
\n#Set new warning level on queue comment\r\
\n/queue simple set \$i comment=\"\$sitename!\$onpeakgigs/\$offpeakgigs#\$\
email!\$newwarning/\$lastoffpeakwarning#\$bytestotal/\$bytesdownoffpeaksav\
ed\"\r\
\n/queue simple reset-counters \$i\r\
\n}\r\
\n}"
add name=usagereportoffpeak policy=\
ftp,reboot,read,write,policy,test,winbox,password,sniff source=":local pos\
1\r\
\n:local pos2\r\
\n:local pos3\r\
\n:local pos4\r\
\n:local pos5\r\
\n:local pos6\r\
\n:local pos7\r\
\n:local pos8\r\
\n:local content\r\
\n:local i\r\
\n:local sitename\r\
\n:local onpeakgigs\r\
\n:local offpeakgigs\r\
\n:local email\r\
\n:local megstotal\r\
\n:local totalcurrent\r\
\n:local bytesdowncurrent\r\
\n:local bytesupcurrent\r\
\n:local megsdowncurrent\r\
\n:local megsupcurrent\r\
\n:local percentage\r\
\n:local lastonpeakwarning\r\
\n:local lastoffpeakwarning\r\
\n:local warninglevel\r\
\n:local warn\r\
\n:local update\r\
\n:local newwarning\r\
\n:local bytesdownonpeaksaved\r\
\n:local bytesdownoffpeaksaved\r\
\n:local bytestotal\r\
\n\r\
\n#Script based on Automated billing script at http://wiki.mikrotik.com/wi\
ki/AutomatedBilling\r\
\n#Details:\r\
\n#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\r\
\n#The details stored in the queue are broken down and used to determine w\
hen a user should be sent a usage warning at 50 75 90 and 100%\r\
\n#You can create/modify a new entry as long as you follow this format\r\
\n#1) If you just want to name an entry you may do so like \"XYZ Router\" \
in the queue name\r\
\n#2) If you want to determine a data limit, enter it like \"sitename!giga\
bytelimit#emailaddress!00#0000\" into the comment.\r\
\n#3) If you just want to enter a comment on its own, you can do this so l\
ong as you don't have an \"!\" in the field\r\
\n#Breaking it down: sitename is a descriptive name, gigabyte limit is the\
\_monthy limit, email address is the address you want notified \r\
\n#00 is the last percentage warning level for this user, 0000 is the defa\
ult value for a new data store\r\
\n#This version created by Andrew Cox - www.accessplus.com.au\r\
\n#v1.4 Updated 15/June/2009\r\
\n:log info \"------ Begining Daily Usage Reports -------\"\r\
\n\r\
\n#For each queue in the list\r\
\n:foreach i in=[/queue simple find comment !=\"\"] do={\r\
\n\r\
\n#Pull comment out of queue and divide up accordingly\r\
\n:set content [/queue simple get \$i comment]\r\
\n\r\
\n#Determine variables from comment\r\
\n#Format is: sitename ! gigsallowed on peak / gigsallowed off peak # who-\
to-email ! last warning level on peak(0-50-75-90-99) / last warning level \
off peak # stored onpeak / stored offpeak\r\
\n:if ([:find \$content \"!\"] != \"\") do={\r\
\n :set pos1 [:find \$content \"!\"]\r\
\n :set pos4 [:len \$content]\r\
\n :set pos2 ([:find [:pick \$content (\$pos1+1) \$pos4] \"#\"]+\$pos1+1)\
\r\
\n :set pos3 ([:find [:pick \$content (\$pos2+1) \$pos4] \"!\"]+\$pos2+1)\
\r\
\n :set sitename [:pick \$content 0 (\$pos1)]\r\
\n :set pos7 ([:find [:pick \$content (\$pos1+1) \$pos2] \"/\"]+\$pos1+1)\
\r\
\n :set onpeakgigs [:pick \$content (\$pos1+1) \$pos7]\r\
\n :set offpeakgigs [:pick \$content (\$pos7+1) \$pos2]\r\
\n :set email [:pick \$content (\$pos2+1) (\$pos3)]\r\
\n :set totalcurrent [/queue simple get \$i bytes]\r\
\n :set lastonpeakwarning [:pick \$content (\$pos3+1) (\$pos3+3)]\r\
\n :set lastoffpeakwarning [:pick \$content (\$pos3+4) (\$pos3+6)]\r\
\n :set pos5 [:find \$totalcurrent \"/\"]\r\
\n :set pos6 [:len \$totalcurrent]\r\
\n :set bytesupcurrent ([:pick \$totalcurrent 0 (\$pos5)])\r\
\n :set bytesdowncurrent ([:pick \$totalcurrent (\$pos5+1) \$pos6])\r\
\n :set megsupcurrent (\$bytesupcurrent / 1048576)\r\
\n :set pos8 ([:find [:pick \$content (\$pos3+7) \$pos4] \"/\"]+\$pos3+7)\
\r\
\n :set bytesdownonpeaksaved ([:pick \$content (\$pos3+7) \$pos8])\r\
\n :set bytesdownoffpeaksaved ([:pick \$content (\$pos8+1) \$pos4])\r\
\n :set bytestotal (\$bytesdowncurrent + \$bytesdownoffpeaksaved)\r\
\n :set megsdowncurrent (\$bytestotal / 1048576)\r\
\n\r\
\n#Begin calculating usage percentage\r\
\n :set percentage ( ( \$bytestotal * 100) / (\$offpeakgigs * 1073741824 \
) )\r\
\n :log info \"\$sitename: \$percentage%\"\r\
\n :if ([\$percentage] < 50) do={ :set warninglevel \"00\"; /queue simple\
\_set \$i max-limit=0/0}\r\
\n :if ([\$percentage] > 50) do={\r\
\n :if ([\$percentage] < 75) do={ :set warninglevel \"50\"; /queue simp\
le set \$i max-limit=0/0}\r\
\n :if ([\$percentage] > 75) do={ :set warninglevel \"75\"; /queue simp\
le set \$i max-limit=0/0}\r\
\n }\r\
\n :if ([\$percentage] > 75) do={\r\
\n :if ([\$percentage] < 90) do={ :set warninglevel \"75\"; /queue simp\
le set \$i max-limit=0/0}\r\
\n :if ([\$percentage] > 90) do={ :set warninglevel \"90\"; /queue simp\
le set \$i max-limit=0/0}\r\
\n }\r\
\n :if ([\$percentage] > 90) do={\r\
\n :if ([\$percentage] < 100) do={ :set warninglevel \"90\"; /queue sim\
ple set \$i max-limit=0/0}\r\
\n :if ([\$percentage] > 100) do={ :set warninglevel \"99\"; /queue sim\
ple set \$i max-limit=256k/256k ; :log info \"\$sitename restricted to 128\
k - offpeak\" }\r\
\n }\r\
\n\r\
\n# Parse warning necessity\r\
\n :if ([\$warninglevel] > \$lastoffpeakwarning ) do={ :set warn \"true\" \
; :set update \"true\" }\r\
\n :if ([\$warninglevel] = \$lastoffpeakwarning ) do={ :set warn \"false\"\
\_; :set update \"false\" }\r\
\n :if ([\$warninglevel] < \$lastoffpeakwarning ) do={ :set warn \"false\"\
\_; :set update \"true\" }\r\
\n\r\
\n#Update Warning Levels\r\
\n :if ([\$update] = true ) do={ :set newwarning \$warninglevel } else={ :\
set newwarning \$lastoffpeakwarning}\r\
\n :if ([\$warn] = true ) do={\r\
\n#Check for email address in queue comment\r\
\n :if ([\$email] != \"\" ) do={\r\
\n /tool e-mail send to=\"\$email\" from=\"report@example.com\" server=\"\
1.3.3.7\" subject=\"\$sitename: Usage at \$percentage\" body=\"This \
message is to inform you of the current off peak usage for \$sitename\r\
\nThe current warning trigger is \$warninglevel%.\r\
\n\r\
\nThis site has downloaded \$megsdowncurrent MB of its offpeak allowance, \
which is over \$percentage% of the \$offpeakgigs GB offpeak \$onpeakgigs G\
B onpeak monthly download allowance.\r\
\n\r\
\nThis is an Automatically generated E-mail that is sent out when users re\
ach 50%, 75%, 90% and 100% of their onpeak/offpeak cap.\r\
\n\r\
\nTraffic Monitor System,\r\
\naccounts@example.com\"\r\
\n}\r\
\n#Send email to support/accounts also\r\
\n /tool e-mail send to=\"accounts@example.com\" from=\"accounts@\
example.com\" server=\"1.3.3.7\" subject=\"\$sitename: Usage at \
\$percentage\" body=\"Current usage for \$sitename - trigger is \$warningl\
evel%.\r\
\n\$megsdowncurrent MB, which is over \$percentage% of the \$offpeakgigs G\
B monthly download allowance.\r\
\nTraffic Monitor System\"\r\
\n:log info \"Sent Warning Level \$warninglevel% to \$email\"\r\
\n}\r\
\n#Set new warning level on queue comment\r\
\n/queue simple set \$i comment=\"\$sitename!\$onpeakgigs/\$offpeakgigs#\$\
email!\$lastonpeakwarning/\$newwarning#\$bytesdownonpeaksaved/\$bytestotal\
\"\r\
\n/queue simple reset-counters \$i\r\
\n}\r\
\n}\r\
\n"
add name=monthlyreportonpeak policy=\
ftp,reboot,read,write,policy,test,winbox,password,sniff source=":local pos\
1\r\
\n:local pos2\r\
\n:local pos3\r\
\n:local pos4\r\
\n:local pos5\r\
\n:local pos6\r\
\n:local pos7\r\
\n:local pos8\r\
\n:local pos9\r\
\n:local content\r\
\n:local i\r\
\n:local sitename\r\
\n:local onpeakgigs\r\
\n:local offpeakgigs\r\
\n:local email\r\
\n:local megstotal\r\
\n:local totalcurrent\r\
\n:local bytesdowncurrent\r\
\n:local bytesupcurrent\r\
\n:local megsdownonpeak\r\
\n:local megsdownoffpeak\r\
\n:local megsupcurrent\r\
\n:local onpeakpercentage\r\
\n:local offpeakpercentage\r\
\n:local warninglevel\r\
\n:local warn\r\
\n:local update\r\
\n:local newwarning\r\
\n:local bytesdownonpeaksaved\r\
\n:local bytesdownoffpeaksaved\r\
\n:local bytestotalonpeak\r\
\n:local bytestotaloffpeak\r\
\n\r\
\n#This version created by Andrew Cox - www.accessplus.com.au\r\
\n#v2.0 Updated 24/June/2009\r\
\n:log info \"------ Begining Monthly Usage Reports -------\"\r\
\n\r\
\n#For each queue in the list\r\
\n:foreach i in=[/queue simple find comment !=\"\"] do={\r\
\n\r\
\n#Pull comment out of queue and divide up accordingly\r\
\n:set content [/queue simple get \$i comment]\r\
\n\r\
\n#Determine variables from comment\r\
\n#Format is: sitename ! gigsallowed on peak / gigsallowed off peak # who-\
to-email ! last warning level(0-50-75-90-99) # stored onpeak / stored offp\
eak\r\
\n:if ([:find \$content \"!\"] != \"\") do={\r\
\n :set pos1 [:find \$content \"!\"]\r\
\n :set pos4 [:len \$content]\r\
\n :set pos2 ([:find [:pick \$content (\$pos1+1) \$pos4] \"#\"]+\$pos1+1)\
\r\
\n :set pos3 ([:find [:pick \$content (\$pos2+1) \$pos4] \"!\"]+\$pos2+1)\
\r\
\n :set sitename [:pick \$content 0 (\$pos1)]\r\
\n :set pos7 ([:find [:pick \$content (\$pos1+1) \$pos2] \"/\"]+\$pos1+1)\
\r\
\n :set onpeakgigs [:pick \$content (\$pos1+1) \$pos7]\r\
\n :set offpeakgigs [:pick \$content (\$pos7+1) \$pos2]\r\
\n :set email [:pick \$content (\$pos2+1) (\$pos3)]\r\
\n :set totalcurrent [/queue simple get \$i bytes]\r\
\n :set pos5 [:find \$totalcurrent \"/\"]\r\
\n :set pos6 [:len \$totalcurrent]\r\
\n :set bytesupcurrent ([:pick \$totalcurrent 0 (\$pos5)])\r\
\n :set bytesdowncurrent ([:pick \$totalcurrent (\$pos5+1) \$pos6])\r\
\n :set megsupcurrent (\$bytesupcurrent / 1048576)\r\
\n :set pos8 ([:find [:pick \$content (\$pos3+7) \$pos4] \"/\"]+\$pos3+7)\
\r\
\n :set bytesdownonpeaksaved ([:pick \$content (\$pos3+7) \$pos8])\r\
\n :set bytesdownoffpeaksaved ([:pick \$content (\$pos8+1) \$pos4])\r\
\n :set bytestotalonpeak (\$bytesdowncurrent + \$bytesdownonpeaksaved)\r\
\n :set bytestotaloffpeak (\$bytesdownoffpeaksaved)\r\
\n :set megsdownonpeak (\$bytestotalonpeak / 1048576)\r\
\n :set megsdownoffpeak (\$bytestotaloffpeak / 1048576)\r\
\n\r\
\n#Begin calculating usage percentage\r\
\n :set onpeakpercentage ((\$bytestotalonpeak*100)/( \$onpeakgigs*1073741\
824))\r\
\n :set offpeakpercentage ((\$bytestotaloffpeak*100)/(\$offpeakgigs*10737\
41824))\r\
\n :log info \"\$sitename: OnPeak \$onpeakpercentage% OffPeak \$offpeakpe\
rcentage%\"\r\
\n\r\
\n:if ([\$email] != \"\" ) do={\r\
\n/tool e-mail send to=\$email from=\"report@example.com\" server=\"1.3.\
3.7\" subject=\"\$sitename: Monthly Report\" body=\"This message is to\
\_inform you of the full monthly usage for \$sitename\r\
\n\r\
\nIn this month this site has downloaded \$megsdownonpeak MB on peak, whic\
h is \$onpeakpercentage% of the \$onpeakgigs GB monthly download allowance\
.\r\
\nIn this month this site has downloaded \$megsdownoffpeak MB off peak, wh\
ich is \$offpeakpercentage% of the \$offpeakgigs GB monthly download allow\
ance.\r\
\n\r\
\nThis is an Automatically generated E-mail that is sent out at the end of\
\_each month.\r\
\n\r\
\nTraffic Monitor System,\r\
\naccounts@example.com\"\r\
\n:log info \"Sent monthly report for \$sitename to \$email\"\r\
\n}\r\
\n#Send email to support/accounts also\r\
\n /tool e-mail send to=\"accounts@example.com\" from=\"accounts@\
example.com\" server=\"1.3.3.7\" subject=\"\$sitename: Monthly Report\
\" body=\"Full monthly usage for \$sitename\r\
\nIn this month this site has downloaded \$megsdownonpeak MB on peak, whic\
h is \$onpeakpercentage% of the \$onpeakgigs GB monthly download allowance\
.\r\
\nIn this month this site has downloaded \$megsdownoffpeak MB off peak, wh\
ich is \$offpeakpercentage% of the \$offpeakgigs GB monthly download allow\
ance.\r\
\n\r\
\nTraffic Monitor System,\r\
\nPlease report any errors in this message to admin\"\r\
\n#Set warning level on queue name back to 00 and reset counters\r\
\n/queue simple set \$i comment=\"\$sitename!\$onpeakgigs/\$offpeakgigs#\$\
email!00/00#0000/0000\"\r\
\n/queue simple reset-counters \$i\r\
\n}\r\
\n}\r\
\n\r\
\n"
add name=monthlyreportoffpeak policy=\
ftp,reboot,read,write,policy,test,winbox,password,sniff source=":local pos\
1\r\
\n:local pos2\r\
\n:local pos3\r\
\n:local pos4\r\
\n:local pos5\r\
\n:local pos6\r\
\n:local pos7\r\
\n:local pos8\r\
\n:local pos9\r\
\n:local content\r\
\n:local i\r\
\n:local sitename\r\
\n:local onpeakgigs\r\
\n:local offpeakgigs\r\
\n:local email\r\
\n:local megstotal\r\
\n:local totalcurrent\r\
\n:local bytesdowncurrent\r\
\n:local bytesupcurrent\r\
\n:local megsdownonpeak\r\
\n:local megsdownoffpeak\r\
\n:local megsupcurrent\r\
\n:local onpeakpercentage\r\
\n:local offpeakpercentage\r\
\n:local warninglevel\r\
\n:local warn\r\
\n:local update\r\
\n:local newwarning\r\
\n:local bytesdownonpeaksaved\r\
\n:local bytesdownoffpeaksaved\r\
\n:local bytestotalonpeak\r\
\n:local bytestotaloffpeak\r\
\n\r\
\n#This version created by Andrew Cox - www.accessplus.com.au\r\
\n#v2.0 Updated 24/June/2009\r\
\n:log info \"------ Begining Monthly Usage Reports -------\"\r\
\n\r\
\n#For each queue in the list\r\
\n:foreach i in=[/queue simple find comment !=\"\"] do={\r\
\n\r\
\n#Pull comment out of queue and divide up accordingly\r\
\n:set content [/queue simple get \$i comment]\r\
\n\r\
\n#Determine variables from comment\r\
\n#Format is: sitename ! gigsallowed on peak / gigsallowed off peak # who-\
to-email ! last warning level(0-50-75-90-99) # stored onpeak / stored offp\
eak\r\
\n:if ([:find \$content \"!\"] != \"\") do={\r\
\n :set pos1 [:find \$content \"!\"]\r\
\n :set pos4 [:len \$content]\r\
\n :set pos2 ([:find [:pick \$content (\$pos1+1) \$pos4] \"#\"]+\$pos1+1)\
\r\
\n :set pos3 ([:find [:pick \$content (\$pos2+1) \$pos4] \"!\"]+\$pos2+1)\
\r\
\n :set sitename [:pick \$content 0 (\$pos1)]\r\
\n :set pos7 ([:find [:pick \$content (\$pos1+1) \$pos2] \"/\"]+\$pos1+1)\
\r\
\n :set onpeakgigs [:pick \$content (\$pos1+1) \$pos7]\r\
\n :set offpeakgigs [:pick \$content (\$pos7+1) \$pos2]\r\
\n :set email [:pick \$content (\$pos2+1) (\$pos3)]\r\
\n :set totalcurrent [/queue simple get \$i bytes]\r\
\n :set pos5 [:find \$totalcurrent \"/\"]\r\
\n :set pos6 [:len \$totalcurrent]\r\
\n :set bytesupcurrent ([:pick \$totalcurrent 0 (\$pos5)])\r\
\n :set bytesdowncurrent ([:pick \$totalcurrent (\$pos5+1) \$pos6])\r\
\n :set megsupcurrent (\$bytesupcurrent / 1048576)\r\
\n :set pos8 ([:find [:pick \$content (\$pos3+7) \$pos4] \"/\"]+\$pos3+7)\
\r\
\n :set bytesdownonpeaksaved ([:pick \$content (\$pos3+7) \$pos8])\r\
\n :set bytesdownoffpeaksaved ([:pick \$content (\$pos8+1) \$pos4])\r\
\n :set bytestotaloffpeak (\$bytesdowncurrent + \$bytesdownoffpeaksaved)\
\r\
\n :set bytestotalonpeak (\$bytesdownonpeaksaved)\r\
\n :set megsdownonpeak (\$bytestotalonpeak / 1048576)\r\
\n :set megsdownoffpeak (\$bytestotaloffpeak / 1048576)\r\
\n\r\
\n\r\
\n#Begin calculating usage percentage\r\
\n :set onpeakpercentage ((\$bytestotalonpeak*100)/( \$onpeakgigs*1073741\
824))\r\
\n :set offpeakpercentage ((\$bytestotaloffpeak*100)/(\$offpeakgigs*10737\
41824))\r\
\n :log info \"\$sitename: OnPeak \$onpeakpercentage% OffPeak \$offpeakpe\
rcentage%\"\r\
\n\r\
\n:if ([\$email] != \"\" ) do={\r\
\n/tool e-mail send to=\$email from=\"report@example.com\" server=\"1.3.\
3.7\" subject=\"\$sitename: Monthly Report\" body=\"This message is to\
\_inform you of the full monthly usage for \$sitename\r\
\n\r\
\nIn this month this site has downloaded \$megsdownonpeak MB on peak, whic\
h is \$onpeakpercentage% of the \$onpeakgigs GB monthly download allowance\
.\r\
\nIn this month this site has downloaded \$megsdownoffpeak MB off peak, wh\
ich is \$offpeakpercentage% of the \$offpeakgigs GB monthly download allow\
ance.\r\
\n\r\
\nThis is an Automatically generated E-mail that is sent out at the end of\
\_each month.\r\
\n\r\
\nTraffic Monitor System,\r\
\naccounts@example.com\"\r\
\n:log info \"Sent monthly report for \$sitename to \$email\"\r\
\n}\r\
\n#Send email to support/accounts also\r\
\n /tool e-mail send to=\"accounts@example.com\" from=\"accounts@\
example.com\" server=\"1.3.3.7\" subject=\"\$sitename: Monthly Report\
\" body=\"Full monthly usage for \$sitename\r\
\nIn this month this site has downloaded \$megsdownonpeak MB on peak, whic\
h is \$onpeakpercentage% of the \$onpeakgigs GB monthly download allowance\
.\r\
\nIn this month this site has downloaded \$megsdownoffpeak MB off peak, wh\
ich is \$offpeakpercentage% of the \$offpeakgigs GB monthly download allow\
ance.\r\
\n\r\
\nTraffic Monitor System,\r\
\naccounts@example.com\r\
\nPlease report any errors in this message to admin\"\r\
\n#Set warning level on queue name back to 00 and reset counters\r\
\n/queue simple set \$i comment=\"\$sitename!\$onpeakgigs/\$offpeakgigs#\$\
email!00/00#0000/0000\"\r\
\n/queue simple reset-counters \$i\r\
\n}\r\
\n}\r\
\n\r\
\n"