AutomatedBilling - MikroTik Wiki
Page 1 of 4
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
[email protected]
Requirements ■ ■ ■ ■
Mikrotik RouterOS version 3.0rc5 or above User Manager Some free resources to run the script A Basic Understanding of Scripting
The Four Scripts Involved
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. You can find the script Oversight here.
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". You can find the script MonthEnd here.
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. You can find the script Usage Warnings here. You can find examples of the Usage Warning E-mails here.
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. You can find the Billing Script here. You can find examples of the Billing E-mails here.
What You Need To Know Before Starting As mentioned, this script works on User Manager. It must be installed/running on the same router that is running User Manager. 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.
Copying the Script When you copy the script, I recommend you first copy it into a plain text editor. You'll notice every single line starts with a space. Even the empty lines. All these spaces need to be manually removed in order for the script to work properly. This is just a small little snag inherent in copying text from Wiki Code frames. Also, make sure no line ends in a space either. This also seems to cause problems when running the script.
User Manager Field Requirements This script also requires each and every user to have the following user fields filled in: /tool user-manager user print
■ ■ ■ ■ ■
First Name Last Name E-mail Address Pool Name Comment Field
The Pool Name Field In this setup, we've used the Pool-name field to specify which one of our wireless packages the user has opted for. This directly translates to a certain amount that the user owes us every month. More about this later.
http://wiki.mikrotik.com/wiki/AutomatedBilling
8/26/2009
AutomatedBilling - MikroTik Wiki
Page 4 of 4
The Comment Field Because of the limitations in User Manager's database structure, we were forced to enter more than one piece of information into one field. Example: 1000230007509 Broken Up: 10002 - 3000 - 75 - 09
What this means: ■ ■ ■ ■
The first 5 Characters is the next Statement/Invoice Number to be issued. The following 4 character is the User's data Cap, in Megabytes. The next 2 characters is the last "Usage Warning Level" that was issued for this user The last 2 characters is the Month in which the last Statement/Invoice was issued.
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. Please review the Scripts for additional information.
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.
Active Sessions Interim Update In the event that the Month-End lapses, and the Billing script is run, the counters in the User Manager are reset. However, if the user is still connected, and has an active session, the interim update of that session will simply logged it's current usage against the User Manager again, regardless of the fact that User Manager's counters were reset. 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. This functionality will be implemented in the next version of this script. Retrieved from "http://wiki.mikrotik.com/wiki/AutomatedBilling" Category: Scripting ■ This page was last modified 11:14, 22 April 2008.
http://wiki.mikrotik.com/wiki/AutomatedBilling
8/26/2009