Community discussions

MikroTik App
 
danielmd
just joined
Topic Author
Posts: 2
Joined: Sat May 06, 2023 7:50 pm

analysis traffcic by interface

Wed May 24, 2023 5:47 pm

Hello,

who is it possible to send ma an email, every x time from an interface with rx and tx bytes ?

Regards Daniel
You do not have the required permissions to view the files attached to this post.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: analysis traffcic by interface

Wed May 24, 2023 7:17 pm

 
optio
Long time Member
Long time Member
Posts: 655
Joined: Mon Dec 26, 2022 2:57 pm

Re: analysis traffcic by interface

Wed May 24, 2023 7:34 pm

You can create scheduler script which will trigger in some interval:

Here is a snippet to get interface tx/rx bytes :
:local intf <interface_name>
/interface
:local tx [get $intf tx-byte]
:local rx [get $intf rx-byte]
Setup configuration for sending email in Tools->Email if not set, and in scheduler script use
/tool/e-mail/send
command to send variables from snippet, ex:
/tool/e-mail/send to="<to_email_address>" subject="<some subject>" body="Interface: $intf\nTx: $tx\nRx: $rx"
P.S.
Since your post viewtopic.php?p=1000423#p1000644 gives more context, you will need to persist byte counters with other short interval scheduler script in case of reboot, you can use interface comment for that storage. Also reset interface counters and comment after sending email if you don't need cumulative traffic report.

Here is snippet to persist tx/rx into interface comment which can be used in persistence scheduler script:
:local intf <interface_name>
/interface
:local cmt [:toarray [get $intf comment]]
:local tx ([:tonum [:pick $cmt 0]] + [get $intf tx-byte])
:local rx ([:tonum [:pick $cmt 1]] + [get $intf rx-byte])
reset-counters $intf
set $intf comment="$tx,$rx"
and to extract tx/rx values from that comment (instead using counters from 1st snippet) for sending over email:
:local intf <interface_name>
/interface
:local cmt [:toarray [get $intf comment]]
:local tx [:pick $cmt 0]
:local rx [:pick $cmt 1]
If you need human readable format of bytes you can use this before sending:
:local bytes2human do={
  :local b [:tonum $1]
  :local n
  :local d
  :local h
  :if ($b < 1024) do={
    :set h "$b B"
  } else={
    :if ($b < 1048576) do={
      :set n ($b / 1024)
      :set d (($b - $n * 1024) / 102)
      :set h "$n.$d KiB"
    } else={
      :if ($b < 1073741824) do={
        :set n ($b / 1048576)
        :set d (($b - $n * 1048576) / 104858)
        :set h "$n.$d MiB"
      } else={
        :set n ($b / 1073741824)
        :set d (($b - $n * 1073741824) / 107374182)
        :set h "$n.$d GiB"
      }
    }
  }
  :return [$h]
}
:set tx [$bytes2human $tx]
:set rx [$bytes2human $rx]

Who is online

Users browsing this forum: astelsrl and 17 guests