Catch exception?

I recently logged into my router to find this at the top of my log:-

However, I didn’t get an email. Why? Because the router was still resolving items with the modem and my modem was still resolving stuff with my ISP. I basically want to have something along the lines of:-

preSendEmail(email emailObject) {
	boolean resolved = false;
	while(!resolved) {
		try {
			resolve("google.com", false);
			//If the above fails, it'll throw an exception and exit this try clause before getting here
			resolved = true;
		} catch(Exception e) {
			//I really don't care what the exception is aslong as there is one.
		}
	}
	sendEmail(email);
}

Where:-
sendEmail(email emailObject); = the function that sends the email
resolve(string domain, boolean useCache); = the function that resolves the address, note the useCache = false
try{}catch(){} = the try clause

So far here’s what’s stopping me:-
A. No way to not use the cache for the resolve, from what I can see.
B. No way to catch on an exception, the second resolve throws “failure: dns server failure” the script dies

Anyone know how to do it?

EDIT Still no dice with:-

{
:local domain "google.com"

:log info "start";
:put "start";
:local resolved false;
{do {:log info "resolving"; :put "resolving"; :resolve $domain; :log info "resolved"; put "resolved"; :set resolved true;} while (!resolved);}
:log info "done";
:put "done";
}

With google.com:-

[admin@MikroTik] /system script> run test
start
resolving
resolved
done

With g:-

start
resolving
failure: dns name does not exist

I don’t know why I thought putting it in it’s only clause with the {} would help, but, eh.

EDIT:- I even tried multiple scripts, no cigar:-
emailOne:-

#Variables
#domain to test
:global domain "smtp.gdmsdafdsafail.com";
#boolean that marks if domain has been resolved
:global resolved false;
#/Variables


#loop while google can't be resolved
:do {
#Debug
:put "Running second script";
#Run second script to resolve, no try clause in routerOS
/system script run emailTwo;
#Debug
:put "Ran second script"
} while (!$resolved);

:put "Resolve worked!";

emailTwo:-

#Assign domain, else it doesn't "exist" in this process
:global domain;
#Assign resolved, same as above
:global resolved;

#Resolve domain
:resolve $domain;
#If the resolve failed, the script will have already returned, so, we assume the resolve worked.
:set resolved true;

Output:-

[admin@MikroTik] /system script> /system script run emailOne
Running second script
failure: dns name does not exist

EDIT:-
:execute seems to work, as proved by this log (Modem was unplugged for the first half):-

[admin@MikroTik] /system script> /system script run emailOne
Running second script
Ran second script
Running second script
Ran second script
Running second script
Ran second script
Running second script
Ran second script
Running second script
Ran second script
Running second script
Ran second script
Running second script
Ran second script
Running second script
Ran second script
Done

Only issue is that it seems to run it in a different thread and doesn’t wait for it, which, means that I have to do something like this:-

#Variables
#domain to test
:global domain "smtp.gmail.com";
:global domain "google.com"
#boolean that marks if domain has been resolved
:global resolved false;
#/Variables


#loop while google can't be resolved
:do {
#Debug
:put "Running second script";
#Run second script to resolve, no try clause in routerOS
#/system script run emailTwo;
:execute emailTwo
delay 1s
#Debug
:put "Ran second script"
} while (!$resolved);

:put "Done";

Anyway to have it wait for it? Instead of delaying?

I take no credit for this since the majority of it is copying & pasting from old threads & the wiki, credits to everyone required, however, it’s 7AM and I’m not in the mood for reverse traversing throughout the night finding the correct credits.

Rules:-

Actions:-

Schedule:-

Log parser:-
http://wiki.mikrotik.com/wiki/Log_Parser_-_Event_Trigger_Script
Log parser script:-

# Script Name: Log-Parser-Script
#
# This is an EXAMPLE script.  Modify it to your requirements.
#
# This script will work with all v3.x and v4.x
# If your version >= v3.23, you can use the ~ operator to match against
# regular expressions.

# Get log entry data from global variable and store it locally
:global logParseVar
:local logTime [:pick [:toarray $logParseVar] 0]
:local logTopics [:pick [:toarray $logParseVar] 1]
:local logMessage [:pick [:toarray $logParseVar] 2]
:set logParseVar ""

:put "LogTime: $logTime"
:put "LogTopics: $logTopics"
:put "LogMessage: $logMessage"

#Wait until online
/system script run emailOne

#Send email
/tool e-mail send tls=yes to="REMOVED@gmail.com" subject=$logMessage

emailOne:-

#Variables
#domain to test
:global domain "smtp.gmail.com";
#boolean that marks if domain has been resolved
:global resolved false;
#/Variables


#loop while google can't be resolved
:do {
#Debug
:log info "running loop"
#Run second script to resolve, no try clause in routerOS
:execute emailTwo
delay 10s
} while (!$resolved);

:put "Done";
:log info "Broke loop"

emailTwo:-

#Assign domain, else it doesn't "exist" in this process
:global domain;
#Assign resolved, same as above
:global resolved;

#Resolve domain
:resolve $domain;
#If the resolve failed, the script will have already returned, so, we assume the resolve worked.
:set resolved true;

Tuh-duh!

God, all of this could have been avoided if it just had try/catch. I’m tired, however, apparently I have a full day ahead of me, being 7AM and all.

Result:-

I-
Win.
/ZzzzzzZZzZzZzz

It might be wiser to monitor the router from a separate device - one that would poll it at regular intervals and it will send you emails when there’s a problem.

If you need to run DNS tests (which I assume is what the whole “:resolve google.com” business is about), you can use the API to connect to the router and issue a :resolve command. By the time you’re able to do that, the network will be back on, so there won’t be the delays you’re experiencing currently.

Although I could use another computer, the issue will still remain that I need to wait on the router anyway because it is going to be the gateway, and, I’d prefer to keep each item separate unless truly required.

The only device that I currently have that leaches off another device is my raspberry pi, and, that’s because it’s running a WakeOnLan script, pretty useless without another PC, eh?

Also, I lack the understanding of what delays you are talking about? The thirty second polling (schedule) time would still apply if I ported it over to another computer (Just polling the API instead, assuming that the API is pull and not push (Not looked into it)), however, the ten second idle time on the dns lookup would be cut, but, that time is only active when it already has an email to send, so, I feel as though I can wait ten seconds to get my email.

It’s actually push… as in, you can keep a connection on at all times, and do something immediately when the connection goes away, including reconnect at regular intervals.

If the PC checking your router is outside of your network (i.e. it uses a different ISP…), you’ll essentially be monitoring your internet connection and be able to react when it goes away.

Also, I lack the understanding of what delays you are talking about?

You said in your first post that resolve is failing

Because the router was still resolving items with the modem and my modem was still resolving stuff with my ISP.

i.e. there’s a delay between the time the router is on, and when it’s finally able to do the resolve. That’s the same delay I’m talking about.

Fair enough, that probably would have been a better idea if I had known that, I thought it was just something like:-
http://router/api?auth=APIKey&command=status

with a return like:-

Online:- True
DNS:- 8.8.8.8
Interface status eth0:- Disabled

etc.

Oh, fair enough. I do see what you mean now, that, I won’t get the email until the router is online again, so, I probably will end up remaking this but from an exterior point of view so that I get an email upon shutdown & when it turns on.

I’m not too good with thinking ahead, god dammit. Thanks.