Community discussions

MikroTik App
 
unridaz
newbie
Topic Author
Posts: 34
Joined: Tue Mar 19, 2013 11:48 pm

Netwatch Email Alerts and Retrying Failed Emails

Fri Apr 18, 2014 1:31 am

Hello,

I have kinda pieced together a solution to monitor a host using Netwatch, and fire emails, and to check if the email failed and try again... What I would like is to improve the solution.

What I have so far is:

Netwatch:

/tool netwatch
add down-script="/system script run HOSTDOWN" host=10.3.3.2 interval=10s \
up-script="/system script run HOSTUP"

Scripts:

HOSTDOWN:

delay 10;
:local plogmessage;
:local plogtime;
:foreach i in=[/log\
find topics="script,warning"] do={:set\
plogmessage [/log get $i message]};
:foreach i in=[/log\
find topics="script,warning"] do={:set\
plogtime [/log get $i time]};
:local identity;
:set identity [/system identity get name];
/tool e-mail send\
to="email@example.com"\
from="$identity@example.com"\
server=1.1.1.1 port=25\
subject="HOST is DOWN at $identity" \
body="$plogtime, $plogmessage";

HOSTUP:

delay 10;
:local plogmessage;
:local plogtime;
:foreach i in=[/log\
find topics="script,warning"] do={:set\
plogmessage [/log get $i message]};
:foreach i in=[/log\
find topics="script,warning"] do={:set\
plogtime [/log get $i time]};
:local identity;
:set identity [/system identity get name];
/tool e-mail send\
to="email@example.com"\
from="$identity@example.com"\
server=1.1.1.1 port=25\
subject="PBX is UP at $identity" \
body="$plogtime, $plogmessage";


So that is fairly basic, netwatch and up and down scripts that send emails.. What I don't like, or what I would want is to improve the variable plogmessage. Currently it goes through each log message and sets the variable to the message. Ok. But in the end I only get the last log message, and not a list of all the log messages which :foreach finds.

Now, if for some reason the email fails to send, I have setup a scheduled task to try and find this:

SCHEDULER:
/system scheduler add interval=30s name=EMAILCHECKER

here is the "on-event" code, I separated it for readability:

delay 10;
:global emailerrorcount;
:global prevemailerrorcount;
:set emailerrorcount [/log print count-only where topics="system,e-mail,error"];
:if ($emailerrorcount != $prevemailerrorcount) do={\
/log print file=emaillog where topics="system,e-mail,error";
/system script run emailretry; };
:set prevemailerrorcount $emailerrorcount;

You may have noticed this code also calls another script which sends emails, "emailretry"

EMAILRETRY:

delay 10;
:local identity;
:set identity [/system identity get name];
/tool e-mail send\
to="email@example.com"\
from="$identity@example.com"\
server=1.1.1.1 port=25\
subject="Email Failed to Send From \ $identity" file=emaillog \
body="Number of failed attempts: $emailerrorcount \n\rThe logged messages are attached(unformatted).";

Here I am saving the logged messages to a file, and emailing the file. If possible I would like to include this list of messages in the email body, instead of an attachment.

I would welcome any suggestions or feedback. This is not perfect and would like to make it better.. Thank you!


EDIT** I forgot that you must also go into /System -> Logging and add a new logging rule for the 'Email' topic, I have the action set to 'memory'.

Because I am using 'memory', another kindof undesirable trait of this solution is that if you have some failed emails, and then time passes, as the log gets full of other messages, eventually the old failed messages will be purged. When this happens $emailerrorcount and $prevemailerrorcount will no longer be the same, and it will fire the EMAILRETRY until there are no more log messages.
 
plisken
Forum Guru
Forum Guru
Posts: 2511
Joined: Sun May 15, 2011 12:24 am
Location: Belgium
Contact:

Re: Netwatch Email Alerts and Retrying Failed Emails

Wed Apr 23, 2014 12:07 am

Hello, look at this website in dutch but look to the printscreens a made

http://wirelessinfo.be/index.php/mikrot ... s/netwatch

See that you have configured the email tool
http://wirelessinfo.be/index.php/mikrotik/pages/email

Goodluck