Community discussions

MikroTik App
 
kd2pm2
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 63
Joined: Wed Nov 14, 2012 7:14 am
Location: New Jersey, USA

Script to email total bytes and reset counters

Wed May 05, 2021 3:42 pm

Anyone come up with a script that can be scheduled to go off on the 1st of the month and email me the total bytes uploaded and downloaded on the WAN port and then reset the counters?

I want to see how much data I am using with my Comcast ISP and make sure that I am staying within the limits before they start to throttle or charge me extra.

I can get the reset counters part scripted pretty easily, just not sure on how to capture the bytes and pack them into an email, if thats even possible.
Ed in NJ-USA
RB2011 / RB4011 / RB260GS / RB750
 
jonah1810
Frequent Visitor
Frequent Visitor
Posts: 69
Joined: Tue Jul 30, 2019 10:19 pm

Re: Script to email total bytes and reset counters

Wed May 12, 2021 8:45 pm

EDIT: my script will work only on places where bytes returns "tx/rx" value. a much simpler script was shared by rextended below.

As luck would have it i made a similar script for pulling total bytes from simple queue im sure you can adapt it.
{
	:local bite
	:local ubyte
	:lotal dbyte
	:local tot
	/queue simple
	:set bite [get [find target=("10.100." . $x . "." . $i . "/32")] bytes]
	:set ubyte [:pick "$bite" 0 [find "$bite" "/" -1]]
	  :set dbyte [:pick "$bite" ([find "$bite" "/" -1]+1) 40]
	  :set tot [:tonum ($ubyte + $dbyte)]
	  }
you would need to change the first 2 lines to be for a ethernet port instead of a simple queue target. but the principle is the same. afterwards create an empty file and then set the file contents as your $tot variable. you should then be able to email that
Last edited by jonah1810 on Thu May 13, 2021 1:32 am, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 4611
Joined: Tue Feb 25, 2014 12:49 pm
Location: Capalbio, Tuscany, Italy

Re: Script to email total bytes and reset counters

Wed May 12, 2021 11:19 pm

Do not lost time with previous script.

The result is not like bytes on queue where you obtain single value like "654321/123456",
but you can obtain single value for tx and rx distinctly on format "4 567 890" bytes

assuming ether1 is your WAN:
:global totalTX [/interface ethernet get ether1 tx-bytes]
:global totalRX [/interface ethernet get ether1 rx-bytes]
to read the values

and the forum is full of example to how send mail.
 
jonah1810
Frequent Visitor
Frequent Visitor
Posts: 69
Joined: Tue Jul 30, 2019 10:19 pm

Re: Script to email total bytes and reset counters

Thu May 13, 2021 1:30 am

Good catch extended! I wasn't by a computer where i could check if it worked. i just assumed it was the same as other places where "bytes" is a stat. My apologies for the misinformation.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 4611
Joined: Tue Feb 25, 2014 12:49 pm
Location: Capalbio, Tuscany, Italy

Re: Script to email total bytes and reset counters

Thu May 13, 2021 3:09 am

Good catch extended! I wasn't by a computer where i could check if it worked. i just assumed it was the same as other places where "bytes" is a stat. My apologies for the misinformation.
no no, not apologize, thanks to try to help!

Who is online

Users browsing this forum: No registered users and 16 guests