Community discussions

MikroTik App
 
nbctcp
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 77
Joined: Tue Sep 16, 2014 7:32 pm

Script DNSHealth Can't send email

Mon Nov 22, 2021 2:38 am

PROBLEM
OS 7.1rc6
1. when I run the script nothing coming to my email.
mikrotik log didn't show any mail log
Where my mistake?
tq

STEPS TAKEN
1. test manual email
/tool e-mail set address=[:resolve smtp.gmail.com] from=user@gmail.com password
="-user" port=587 user=user@gmail.com tls=starttls
/tool e-mail send to=user@gmail.com subject="test" body="test"
I can receive the email

SCRIPT
https://github.com/arkashiks/dns_health ... hcheck.rsc
:local sysname [/system identity get name];
:local EMAIL "user@gmail.com";
:local PrimaryDNS "192.168.88.250";
:local BackupDNS "192.168.100.1";
:local TestDomain "mikrotik.com";
:local ConfiguredDNS [/ip dns get servers];
###### when router is in its primary configuration
:if ($PrimaryDNS = $ConfiguredDNS) do={
    :do { 
        ###### test resolution
        :put [:resolve $TestDomain server $ConfiguredDNS];
        ###### generate syslog messages
        /log info "Primary DNS $PrimaryDNS healthcheck completed, no issues";
    } on-error={ 
        :put "resolver failed"; 
        ###### generate syslog messages
        /log info "name resolution using primary DNS $PrimaryDNS failed";
        /log info "temporary setting backup DNS $BackupDNS as primary";
        ###### update DNS with backup DNS
        /ip dns set servers=$BackupDNS; 
        ###### send notification email
        /tool e-mail send to="$EMAIL" subject="$sysname script notification: Primary DNS $PrimaryDNS down" body="Primary DNS $PrimaryDNS is down.\r\nDNS configuration changed to backup DNS $BackupDNS."
        /log info "notification email to $EMAIL sent";
    }
}
###### when router is in its backup configuration
:if ($BackupDNS = $ConfiguredDNS) do={
    :do { 
        ###### test resolution
        :put [:resolve $TestDomain server $PrimaryDNS]
        ###### generate syslog messages
        /log info "name resolution using primary DNS $PrimaryDNS working now";
        /log info "restoring original DNS configuration";
        ###### revert back DNS configuration to original
        /ip dns set servers=$PrimaryDNS;
        ###### send notification email
        /tool e-mail send to="$EMAIL" subject="$sysname script notification: Primary DNS $PrimaryDNS up" body="Primary DNS $PrimaryDNS is up.\r\nOriginal DNS configuration restored."
        /log info "notification email to $EMAIL sent";        
    } on-error={ 
        :put "resolver failed";
        ###### generate syslog messages
        /log info "system is configured with backup DNS $BackupDNS";
        /log info "Primary DNS $PrimaryDNS is still down, next check in 300 seconds";
    }
}
Last edited by nbctcp on Mon Nov 22, 2021 9:06 am, edited 2 times in total.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3297
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Script DNSHealth Can't send email

Mon Nov 22, 2021 8:56 am

Do always use code tag button above post for posting codes.
[size=200]</>[/size]
This script will only send email when DNS has changed, so if no change, no DNS.

Try change:
 /tool e-mail send to="$Email" subject="$sysname script notification: Primary DNS $PrimaryDNS down" body="Primary DNS $PrimaryDNS is down.\r\nDNS configuration changed to backup DNS $BackupDNS."
to:
:put "Primary DNS $PrimaryDNS is down."
Change this:
:local TestDomain "mikrotik.com"
to some that does not work (to force change), eks:
:local TestDomain "xxxyyy333.com"
Wrap the whole script in extra curly brackets. {<script>}, then cut and past it to terminal.
If script then show message "Primary DNS $PrimaryDNS is down.", you know script does work.
Change DNS back to original, replay put line with /tools e-mail and test again.

Who is online

Users browsing this forum: LunaticRv and 10 guests