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.