Académique Documents
Professionnel Documents
Culture Documents
AutomatedBilling
From MikroTik Wiki
Contents
■ 1 Introduction
■ 2 Requirements
■ 3 The Four Scripts Involved
■ 3.1 Oversight
■ 3.2 Month End
■ 3.3 Usage Warnings
■ 3.4 Billing
■ 4 What You Need To Know Before Starting
■ 4.1 Copying the Script
■ 4.2 User Manager Field Requirements
■ 4.3 The Pool Name Field
■ 4.4 The Comment Field
■ 5 Known Issues
■ 5.1 RouterOS Versions
■ 5.2 Active Sessions Interim Update
Introduction
This script was first written as a small device to send users a bill for their usage at the end of every
month. It has since developed into a far more complex system that is now used to almost fully automate
Billing, and also send out Usage Warnings to users.
If you find this script helpful, or found a bug, please let me know. Comments or suggestions are
welcome as well - Send to admin@qnect.co.za
Requirements
■ Mikrotik RouterOS version 3.0rc5 or above
■ User Manager
■ Some free resources to run the script
■ A Basic Understanding of Scripting
http://wiki.mikrotik.com/wiki/AutomatedBilling 8/26/2009
AutomatedBilling - MikroTik Wiki Page 2 of 4
Oversight
The first of the scripts, I've called Oversight. This is a very simple script that simply takes a single
global variable, and if it's value is True, it'll trigger another script. If it's value is False, it will trigger a
completely different script. Take note, this script is the one that will be entered into the Scheduler, which
facilitates setting the script in motion at the end of every day, or at whatever time or interval you wish.
In our setup, we've set the scheduler to run every 24 hours at 23:50.
Month End
The Global variable that I speak of, is determined by our second script, which I call MonthEnd. This
script, quite simply, checks if Today (or, more accurately, the date/time at which it is run) is the last day
of the month, or not. This script compensates for months with 30 or 31 days, and also compensates for
Leapyears in February. This script then returns True or False on the Global variable called "found".
Usage Warnings
The Third script is the UsageWarnings script. The name should say it all. We'll get into the inner
workings of this script shortly.
This script basically works by comparing the user's download-used and upload-used counters' sum to the
Cap, and extrapolates a Percentage from that. If the percentage is at a certain point, it issues a warning to
the user by sending their usage information to their E-mail address. The "usage warning level" is then
updated onto the user's profile in User Manager, which tells the script to not issue a warning of the same
level again, until they reach a 'higher' usage warning level.
A skeleton version of this is also sent to Accounts E-mail, just for reference. It is particularly handy for
100% Usage warnings, so one can contact the client for possible cap-increase options, or whatever the
case may be. The script can also be adapted to send a warning to the Accounts E-mail only if the user
has reached 100%, and not all the other warnings.
Billing
The Fourth and final script is Billing. This one is also quite complex, and we'll explain as we go along.
In short, this script uses a loop to pull each user's information individually. For each user, and E-mail is
compiled stating their usage, how much they owe us, how much discount they are getting, if any, and
what our banking details are for payment.
http://wiki.mikrotik.com/wiki/AutomatedBilling 8/26/2009
AutomatedBilling - MikroTik Wiki Page 3 of 4
It sends a second E-mail, which is a skeleton version of the previous one to the Accounts E-mail, stating
only the necessary information. This is kept for our records and to keep track of who has paid and who
has not.
It requires access to an outgoing E-mail server, either locally or remotely. Currently, it is written for a
Remote SMTP server. Your outgoing server also should not require Authorization to relay E-mails.
■ First Name
■ Last Name
■ E-mail Address
■ Pool Name
■ Comment Field
http://wiki.mikrotik.com/wiki/AutomatedBilling 8/26/2009
AutomatedBilling - MikroTik Wiki Page 4 of 4
Example: 1000230007509
Broken Up: 10002 - 3000 - 75 - 09
Although there is a Usage-Limit function in User Manager and RouterOS, for the purposes of this script,
we have chosen to not use it. It is in the planning to implement it.
Known Issues
RouterOS Versions
This script has given problems under RouterOS 2.9.46 and earlier. The problems that we have
experienced were no longer there once we upgraded to 3.0rc5. We are also successfully running the
script on Version 3.6.
In this event, it is necessary to either suspend the accounting session, or terminate the active session and
therefore force the accounting session to close.
Category: Scripting
http://wiki.mikrotik.com/wiki/AutomatedBilling 8/26/2009