Community discussions

MikroTik App
 
jokakilla
just joined
Topic Author
Posts: 20
Joined: Sat Oct 30, 2021 11:09 pm

Issues getting/resetting tx-bytes/rx-bytes

Tue Nov 22, 2022 11:52 pm

Hi everyone,
on my Mikrotik I'm trying to write a script that pushs rx/tx bytes of my WAN interface via MQTT to my Node-Red installation that should forward it to Influx to get some Grafana diagrams in the end.
The following code has two issues:
1. The rx-bytes/tx-bytes should contain the values since the script ran the last time. So I'm trying to reset the counter at the end. But the values are not reset for some reason.
2. The variables totalTX and totalRX are each separated into 3 digit blocks. This avoids creating a valid json message. I need the digits without spaces. E.g. instead of 133 519 996 it needs to be 133519996.

Any help is welcome! :)

    # Required packages: iot
    :local broker "node_red_system"
    :local topic "mikrotik/wanVolume"

    :local totalTX [/interface ethernet get ether1 tx-bytes]
    :local totalRX [/interface ethernet get ether1 rx-bytes]
    :local message \
        "{  \"rx\":$"totalRX",\
                    \"tx\":$"totalTX"}"
    :log info "$message"
    /iot mqtt publish broker=$broker topic=$topic message=$message
    /interface reset-counters ether1
 
jokakilla
just joined
Topic Author
Posts: 20
Joined: Sat Oct 30, 2021 11:09 pm

Re: Issues getting/resetting tx-bytes/rx-bytes

Wed Nov 23, 2022 12:09 am

Found the solution for issue two. But seriously what the heck: why are the rx/tx bytes an array with one element of type string?
# Required packages: iot
:local broker "node_red_system"
:local topic "mikrotik/wanVolume"

:local replaceChar do={
  :log info "replace"
  :for i from=0 to=([:len $1] - 1) do={
    :local char [:pick $1 $i]
    :if ($char = $2) do={
      :set $char $3
    }
    :set $output ($output . $char)
  }
  :return $output
}

:local etherInfo [/interface ethernet get ether1]
:local rawRx ($etherInfo->"rx-bytes"->0)
:local rawTx ($etherInfo->"tx-bytes"->0)
:local totalRX [ $replaceChar $rawRx " " "" ]
:local totalTX [ $replaceChar $rawTx " " "" ]
:local message \
    "{  \"rx\":$"totalRX",\
                \"tx\":$"totalTX"}"
:log info "$message"
/iot mqtt publish broker=$broker topic=$topic message=$message
/interface reset-counters ether1
The first issue is unfortunately still there :(
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3279
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Issues getting/resetting tx-bytes/rx-bytes

Wed Nov 23, 2022 8:41 am

Here is how I get all the interface traffic. (try cut past this to terminal)
:foreach id in=[/interface find] do={
	:local output "$[/interface print stats as-value where .id=$id]"
	:put "$output"
}
For more information see my script that I do use to get all kind of data inn to Splunk:
viewtopic.php?t=179960

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

Re: Issues getting/resetting tx-bytes/rx-bytes  [SOLVED]

Wed Nov 23, 2022 9:57 am

.

# Required packages: iot
:local broker "node_red_system"
:local topic  "mikrotik/wanVolume"
:local if     "ether1"
/interface
:local tRX [get $if rx-byte]
:local tTX [get $if tx-byte]
:local message "{\"rx\":\"$tRX\",\"tx\":\"$tTX\"}"
:log info $message
reset-counters $if
/iot mqtt publish broker=$broker topic=$topic message=$message
Last edited by rextended on Thu Nov 24, 2022 2:49 am, edited 1 time in total.
 
jokakilla
just joined
Topic Author
Posts: 20
Joined: Sat Oct 30, 2021 11:09 pm

Re: Issues getting/resetting tx-bytes/rx-bytes

Wed Nov 23, 2022 8:36 pm

Wow that's so much shorter than my version...and working :D
Still don't know why my reset variant isn't working but I can live with copying your solution ;) Thanks!

Who is online

Users browsing this forum: Google [Bot], loloski, massinia, UkRainUa and 13 guests