Page 1 of 1

Issues getting/resetting tx-bytes/rx-bytes

Posted: Tue Nov 22, 2022 11:52 pm
by jokakilla
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

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

Posted: Wed Nov 23, 2022 12:09 am
by jokakilla
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 :(

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

Posted: Wed Nov 23, 2022 8:41 am
by Jotne
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

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

Posted: Wed Nov 23, 2022 9:57 am
by rextended
.

# 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

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

Posted: Wed Nov 23, 2022 8:36 pm
by jokakilla
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!