Check Traffic Script

Hi everyone,

I’m looking for a script to send interface traffic data to an email address. However, I’m encountering an issue with my current script. Could anyone help me troubleshoot this problem?

Thanks in advance!



# Declare the list of interfaces
:local interfaces {"ether1"}

# Initialize email body with header
:local emailBody "Traffic Statistics Report\n\n"

# Get current date and time
:local currentDate [/system clock get date]
:local currentTime [/system clock get time]

# Get device uptime
:local deviceUptime [/system resource get uptime]

# Add date, time, and uptime to email body
:set emailBody ($emailBody . "Date: $currentDate\n")
:set emailBody ($emailBody . "Time: $currentTime\n")
:set emailBody ($emailBody . "Uptime: $deviceUptime\n\n")

:foreach interfaceName in=$interfaces do={
    # Check if the interface exists
    :local interfaceId [ /interface find where name=$interfaceName ]
    :if ([:len $interfaceId] > 0) do={
        :local trafficData [/interface get $interfaceId name]
        :local rxBytes [/interface get $interfaceId rx-byte]
        :local txBytes [/interface get $interfaceId tx-byte]

        # Convert bytes to gigabytes (1 GB = 1073741824 bytes)
        :local rxGigabytes ($rxBytes / 1073741824)
        :local txGigabytes ($txBytes / 1073741824)

        :local rxPackets [/interface get $interfaceId rx-packet]
        :local txPackets [/interface get $interfaceId tx-packet]

        :local logMessage ("Date: $currentDate, Time: $currentTime, Uptime: $deviceUptime, Interface: $trafficData, RX Gigabytes: $rxGigabytes, RX Bytes: $rxBytes, TX Gigabytes: $txGigabytes, TX Bytes: $txBytes, RX Packets: $rxPackets, TX Packets: $txPackets")

        :log info $logMessage

        # Append interface data to email body
        :set emailBody ($emailBody . "Interface: $trafficData\n"
            . "-----------------------------------\n"
            . "RX Gigabytes: $rxGigabytes\n"
            . "RX Bytes: $rxBytes\n"
            . "TX Gigabytes: $txGigabytes\n"
            . "TX Bytes: $txBytes\n"
            . "RX Packets: $rxPackets\n"
            . "TX Packets: $txPackets\n"
            . "-----------------------------------\n\n")
    } else={
        :log warning "Interface $interfaceName not found"
    }
}

# Add footer to email body
:set emailBody ($emailBody . "Generated by Center Router")

:local emailTo "notify@domain.com"
:local emailSubject "Traffic Statistics Report"

/tool e-mail send to=$emailTo subject=$emailSubject body=$emailBody

Don’t you think it’s time to list the problems you’ve encountered,
instead of throwing out “your” script and out of nowhere expecting someone to fix GPT result?


Convert bytes to gigabytes (1 GB = 1073741824 bytes)

FALSE

1 Gigabyte (GB) is 1000000000 Bytes

what is 1073741824 Bytes is 1 Gibibyte (GiB).

Dear friend, if you can help, what difference does it make how this code reached this stage? I am beginner and I got some help from GPT.
This scrip does not work at all; it gives no output.
Thank you for helping me

First, create this script/function so that it is always available as a global variable.


:global human do={
    :if ([:typeof $1]="nothing") do={
        :error "must provide a byte value to humanize"
    }
    :local input [:tonum $1]
    :if ([:typeof $input]!="num") do={
        :error "cannot convert $1 to number"
    }
    :local q
    :local r
    :local output
    
    :if ($input<1024) do={
        :set $output "$input bytes"
    } else={
        :if ($input<1048576) do={
            :set q ($input/1024)
            :set r ($input-$q*1024)
            :set r ($r/102)
            :set output "$q.$r KiB"
        } else={
            :if ($input<1073741824) do={
                :set q ($input/1048576)
                :set r ($input-$q*1048576)
                :set r ($r/104858)
                :set output "$q.$r MiB"
            } else={
                :set q ($input/1073741824)
                :set r ($input-$q*1073741824)
                :set r ($r/107374182)
                :set output "$q.$r GiB"
            }
        }
    }
	:return $output
}

With this test script you can see how to use this variable “human”.


{
# Change the interface name to the one you
:local iFace "pppoe-out1"
/interface
:local txtotal [$human [get [find name=$iFace] tx-byte]]
:local rxtotal [$human [get [find name=$iFace] rx-byte]]
:local sumtotal [$human ([get $iFace tx-byte]+[get $iFace rx-byte])]
:put "TX: $txtotal"
:put "RX: $rxtotal"
:put "Total: $sumtotal"
}

BR.