Automated Usage Script without usermanager

From MikroTik Wiki
Revision as of 14:02, 29 July 2008 by Omega-00 (talk | contribs)
Jump to: navigation, search
#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 name will allow you keep track of usage by each site
#The details stored in the queue are broken down and used to determine if a user should also be sent an 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"
#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 warning level-set this to 00 when creating a new site.
# This version created by Andrew Cox - www.accessplus.com.au
:log info "------ Begining Daily Usage Reports -------" 

#For each queue in the list
:foreach i in=[/queue simple find name !=""] do={

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

#Determine variables from name
#Format is: sitename ! gigsallowed # who-to-email ! last warning level(0-50-75-90-100)
: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) $pos4]
   :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 megsdowncurrent ($bytesdowncurrent / 1048576)

#Begin calculating usage percentage
   :set percentage (($bytesdowncurrent * 100) / ($gigs * 1073741824))
   :log info "$sitename: $percentage%"
   :if ([$percentage] < 50) do={ :set warninglevel "00" }
   :if ([$percentage] > 10) do={
     :if ([$percentage] < 75) do={ :set warninglevel "10" }
     :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 ([$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={

  /tool e-mail send to=$email from=something@somewhere.com server=1.3.3.7 subject="$sitename: Usage at $percentage" body="This message is to inform you of your current usage for $sitename
The current warning trigger is $warninglevel%.

This site has downloaded $megsdowncurrent MB, which is $percentage%
of the $gigs GB monthly download allowance.

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

Traffic Monitor
accounts@somewhere.com"

:log info "Sent Warning Level $warninglevel% to $email"
 }
#Set new warning level on queue name
 /queue simple set $i name="$sitename!$gigs#$email!$newwarning"
 }
}


Monthly Report

Called by the overseer at the end of the month, this generates reports for Every site and then resets counters and the warning level back to 00

:local content
:local i
:local sitename
:local gigs
:local email
:local megstotal
:local totalcurrent
:local bytesdowncurrent
:local bytesupcurrent
:local megsdowncurrent
:local megsupcurrent
:local percentage 

#Script based on Automated billing script at http://wiki.mikrotik.com/wiki/AutomatedBilling
# This version created by Andrew Cox - www.accessplus.com.au
:log info "------ Begining Monthly Reports -------"
#For each queue in the list
:foreach i in=[/queue simple find name !=""] do={

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

#Determine variables from name
#Format is: sitename ! gigsallowed # who-to-email ! last warning level(0-50-75-90-100)
 :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) $pos4]
   :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 megsdowncurrent ($bytesdowncurrent / 1048576)

#Begin calculating usage percentage
   :set percentage (($bytesdowncurrent * 100) / ($gigs * 1073741824))
   :log info "$sitename: $percentage%"

/tool e-mail send to=$email from=something@somewhere.com server=1.3.3.7 subject="$sitename: Monthly Report" body="This message is to inform you of your full monthly usage for $sitename

In this month this site has downloaded $megsdowncurrent MB, which is $percentage%
of the $gigs GB monthly download allowance.

This is an Automatically generated E-mail that is sent out at the end of each month.

Traffic Monitor
accounts@somewhere.com"
:log info "Sent end of month email for $sitename to $email"

#Set warning level on queue name back to 00 and reset counters
 /queue simple set $i name="$sitename!$gigs#$email!00"
 /queue simple reset-counters $i
}

Final Notes

For those new to scripts, you can copy and paste these into winbox, but make sure that you at least remove the starting spacing from the comment lines (ones with #) if you don't they aren't treated as comments and will cause the script to error.