pothi.mikrotik-scripts/scripts/email-log.rsc

51 lines
1.3 KiB
Text
Raw Permalink Normal View History

2022-11-16 07:25:46 +05:30
# Email the generic-log when it reaches the threshold!
2022-11-23 08:29:32 +05:30
# one-time process to be done when bootstrapping the device
# :local logTopics {"info"; "error"; "warning"; "critical"}
# :foreach topic in=$logTopics do={ :system logging add topics=$topic action=disk }
2022-11-16 07:25:46 +05:30
# Requirements: adminEmail, logTopics
:global adminEmail
2022-11-23 08:29:32 +05:30
:if ([:typeof $adminEmail] = "nothing" || $adminEmail = "") do={
:log error "adminEmail is not defined or nil."; :error "Error: Check the log"; }
2022-11-16 07:25:46 +05:30
:local emailStatus
:local logFile "log.1.txt"
2022-11-23 08:29:32 +05:30
# check for "flash" folder
:do {
/file get "flash/log.0.txt"
:set logFile "flash/log.1.txt"
:put "Flash folder found!"
} on-error={
:put "Flash folder doesn't exist!"
}
2022-11-16 07:25:46 +05:30
:do {
/file get "$logFile"
} on-error={
2022-11-23 08:29:32 +05:30
# :log info "$logFile file isn't created yet!";
:error "$logFile file isn't created yet!";
2022-11-16 07:25:46 +05:30
}
# The following gets executed only if the log file is ready!
2022-11-23 08:29:32 +05:30
:log info "Emailing the log file..."
2022-11-16 07:25:46 +05:30
/tool e-mail
:do {
send file="$logFile" subject="Log" body="See sub!" to=$adminEmail
} on-error={ :log error "The log file could not be sent by email." }
2022-11-23 08:29:32 +05:30
:do { :delay 3s; :set emailStatus [get last-status] } while=( $emailStatus = "in-progress" )
2022-11-16 07:25:46 +05:30
:if ( $emailStatus = "succeeded" ) do={
:log info "The log file is sent to $adminEmail."
} else={
:log error "Email failed!"
}
/file remove "$logFile"