Sharing a script I wrote for tracking monthly and daily total WAN traffic statistics. The script reads traffic counters from eth1, stores them in a JSON file, and presents the totals through the file names. These files are empty, it’s their names that provide the values.
If you need per-IP traffic statistics, take a look at another script I wrote: http://forum.mikrotik.com/t/per-ip-wan-traffic-statistics/157975/1
This is how it looks: Key features
Everything is local on the router. You don’t need any external servers or software to collect or view the data. Just open WinBox and look at the Files window.
Best if used with a USB flash drive to avoid any worries about write cycles. If your device doesn’t support external storage, you could schedule the script to run less frequently at the expense of more statistics loss when unexpected reboots take place. The amount of data written is very small, a few hundred bytes.
Detects manual traffic counters resets as long as Rx counters are less than previously stored Rx value. If you intend to reset the counters, just run the script manually right before and after the reset to retain maximum accuracy.
Limitations
Tested with RouterOS 7.18.2+. This is important because the script uses a some newer scripting features.
RouterOS doesn’t support floating point, therefore reporting accurracy is +/-1GiB. Calculation and saving is done in bytes, so there are no repeated rounding losses. The rounding (or rather truncating) happens only at the last step when the report file is produced.
Detects reboots by placing a marker file in the root directory which is a RAM/tmpfs disk on many devices; for devices with no RAM disk by default, set up your own and specify the path for the marker file.
To retain maximum accuracy, run the script just before a manual reboot to collect the data.
How to use
Install and format USB drive.
Upload the provided rsc script file to the USB drive (e.g. via WinBox - Files).
Schedule the script to run every hour (or however often you like) at x:59:59.
IS possible this to generate a nice HTML report ? This is still OK, but in general most of devices have USB port, so there is more possibility to work with.
EDIT: Tested this on RoS 7.18.2 and got this error into log after Scheduller is executed:
executing script from scheduler (traffic) failed, please check it manually
(scheduler:traffic) syntax error (line 1 column 5)
i checked the file paths are are fine. I just changed this path into your script to fit to my USB setup.
RouterOS doesn’t allow executing scripts just by referencing the file name like Linux shell. You need to use “/import usb1-part1/traffic.rsc” as shown in the first post.
To produce this data in an HTML format would require writing another script pretty much from scratch.
That makes sense. Now works as should be. Testing this month. Will share impressions at end of month. Thanks
EDIT: Found some weird stats when script is executed.. How is possible ether1 to show 4.X GB downloaded, but after executing the script it shows 9GB downloaded?
Have you reset traffic counters or rebooted the router since executing the script for the first time? The counters on the interface itself are in-memory only (4.8GiB in your example).
If you don’t mind losing some statistics, try deleting traffic.json and then running the script multiple times. The values should match the interface counters until a reboot or a reset.
If there is a bug, I would want to know about it for sure.
No i dont have reset the router! On first executing the script shows 4GB download and on second iteration it showed 9GB. From where it get that info i dont know!
Upon looking closer at your first screenshots, I conclude that you made some changes to the script. I see two issues. First, the timestamp on x_traffic_marker is exactly the same as on traffic.json (and other files), 20:05:00. This can only be like that on the first ever run or after a reboot. In all other cases the timestamps should be different.
Second, the marker file is supposed to be in the root top-level directory, which is RAM. By placing it on USB drive, you are defeating the purpose of the marker file, which is to disappear on reboots.
Speaking of which, does your router have a directory (actually, disk) called “flash”? If yes, then you don’t need to (and shouldn’t) do anything with the “marker” variable.
I suspect the changes you made cause the script to think that router was rebooted upon every execution. Because that’s when it adds interface counters as is, without doing any calculations with the previously stored counters.
The numbers will continue to make no sense. You would need to fix the script, remove traffic.json and the x_traffic_marker, and start over again. The only change you need to make to the original script is the “savedir” variable, that’s it.
So assumed on your asnwer will revert xmarker file as default, and will start over again. Disk you noted is HDD attached to my router. One strange thing more.. Why script calculates 6+0=7 like is shown in image bellow:
If script “rounds” to full number and dont count MB, they count as 6GB total, because 6+0 is not 7 right?
Ignore Total= i was added to script to be more readable to me. Nothing more is changed.
The calculations are consistent, it’s just about the prefix. You are correct, the powers of two should be named “mebi”, “gibi” and so on. Although the industry is not entirely consistent here. Windows OS shows file sizes as GB while using the binary meaning. RAM sizes are always GB. I work with VMware daily, and they use MB/GB/TB everywhere. I also work with Linux daily, and it did adopt the EIC notation, but many tools just use “K/M/G” with binary values (good approach).
To be honest, in my whole life and professional career I’ve never had a need to mean anything other than binary and never saw anyone saying “gigabyte” while meaning 1,000,000,000 bytes. Other than hard drive manufacturers who definitely used this language loophole to their advantage and probably started the whole thing…
But once again, this is the formally correct approach, and RouterOS uses EIC binary prefixes, so it would make sense to be consistent. I updated the script.
You shouldn’t do that if you want the script to work correctly. The whole purpose of the marker file is to be in non-persistent memory and to get removed during a reboot.
The script doesn’t do any rounding, it just divides bytes by 1073741824 to get gibibytes. RouterOS doesn’t support floating point nor does it do rounding properly. It simply truncates everything after the point, so even 0.99 ends up as 0. But it’s not a big deal for this use case, 0.99 GiB of traffic is nothing for a whole month or even a day.
The reason why you see 7 = 6+0 is because each digit is divided independently, total, download, upload. If I calculated download, then upload, then summed up them, you would get correct math but less accurate data because of double truncating.