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