Automated Usage Script without usermanager

From MikroTik Wiki
Revision as of 05:49, 28 July 2008 by Normis (talk | contribs)
Jump to navigation Jump to search

Summary

I wrote a usage tracking script based on the original Automated Billing Script The way I run it is using a Mikrotik box setup to pass everything through transparently then I generated a Queue per IP address to keep track of the usage. On my test box (A PowerRouter 732) I currently have 3 class C blocks passing through with only 3% cpu (Multi CPU enabled).

==Todo==: I plan on giving this script the ability to backup to a file on the mikrotik so it will count both data in the queue currently + data usage saved in the file (in case you lose power to the device or have to restart it at any time)

I also plan on modifying the monthend script further so that it can only be triggered on the last hour of the last day of the month. This means you could then run the usage script hourly to get more frequent updates.

See below the script for details on how it works.

The Scripts

Overseer

Like the automated billing, this also has a calling script "Overseer"

 :global found
 
 /system script run monthend
 
 :if ([$found] = "true") do={ /system script run monthlyreport; :log info "--Completed Monthly Report--"}
 :if ([$found] = "false") do={ /system script run usagewarning; :log info "--Completed Usage Report--"}

Monthend

If it is the end of the month, the Monthly report must be run and counters reset, rather than the usage report. This finds our if it is end of month or not.

 :local date
 :local time
 :local day
 :local month
 :local year
 :local hour
 :local minute
 :local yeardiv
 :local yearmult
 :local leapyear
 :local lastday "0"
 :global found ""
 
 :set date [/system clock get date]
 :set time [/system clock get time]
 :set month [:pick $date 0 3]
 :set day [:pick $date 4 6]
 :set year [:pick $date 7 11]
 :set hour [:pick $time 0 2]
 :set minute [:pick $time 3 5] 
 
 :set yeardiv ($year / 4)
 :set yearmult ($yeardiv * 4) 
 
 :if ([$yearmult] = $year) do={ :set leapyear true } else={ :set leapyear false }
 :if ([$month] = "jan") do={ :set lastday "31" }
 :if ([$month] = "feb") do={ 
        :if ($leapyear = true) do={ :set lastday 29 }
        :if ($leapyear = false) do={ :set lastday 28 } }
 :if ([$month] = "mar") do={ :set lastday 31 } 
 :if ([$month] = "apr") do={ :set lastday 30 }
 :if ([$month] = "may") do={ :set lastday 31 }
 :if ([$month] = "jun") do={ :set lastday 30 }
 :if ([$month] = "jul") do={ :set lastday 31 }
 :if ([$month] = "aug") do={ :set lastday 31 }
 :if ([$month] = "sep") do={ :set lastday 30 }
 :if ([$month] = "oct") do={ :set lastday 31 }
 :if ([$month] = "nov") do={ :set lastday 30 }
 :if ([$month] = "dec") do={ :set lastday 31 }
 :if ([$lastday] = $day) do={ :set found true } else={ :set found false }

Usage Report

The main usage report, takes the name of each queue and looks firstly for a hint that any data is contained in this. I have used ! to seperate the initial data block so If it finds a ! it will treat this name as data.

The data is then broken up from a name like this: Any Site name!50#info@yourcompany.com!00 this then gives us $sitename (descriptive name) $gigs (gigabyte download limit) $email (address to email report to) $lastwarning (previous usage warning level)

If you just name a script without any ! in it, this will be ignored in the processing (useful for naming devices without reporting)


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.