Page 1 of 1

Netwatch logging and send log daily by email

Posted: Thu Jun 29, 2017 3:00 pm
by Technetium
I'm a beginner of RouterOS ;)
I would like to set up a lan monitoring tool that send me the log daily.
Email is already configured.

Using netwatch i want log only if a host is down.
But how can i write a specific log only dedicated to netwatch and send it only daily by email ?
How can i "reset" the file after successful sending it ?

Re: Netwatch logging and send log daily by email

Posted: Thu Jun 29, 2017 3:11 pm
by Shadeofspirit
I'm a beginner of RouterOS ;)
I would like to set up a lan monitoring tool that send me the log daily.
Email is already configured.

Using netwatch i want log only if a host is down.
But how can i write a specific log only dedicated to netwatch and send it only daily by email ?
How can i "reset" the file after successful sending it ?
what do you want to have in log?
create new "action" and "rule" in system logging (save to file)
make schedule to send this file and delete after sending

Re: Netwatch logging and send log daily by email

Posted: Sat Jul 01, 2017 2:56 pm
by Technetium
In log i want write a string like "PC1 is down".

I don't understand how to use "action" and "rule" of logging system to obtain a file only dedicated to netwatch message.

I don't understand how to log only netwatch log because script can write only on 4 topics.. "debug, error, info and warning".. so, how can i create a rule to filter only netwatch message ?

Re: Netwatch logging and send log daily by email

Posted: Sun Jul 02, 2017 11:34 am
by Shadeofspirit
In log i want write a string like "PC1 is down".

I don't understand how to use "action" and "rule" of logging system to obtain a file only dedicated to netwatch message.

I don't understand how to log only netwatch log because script can write only on 4 topics.. "debug, error, info and warning".. so, how can i create a rule to filter only netwatch message ?
try smth like this:
create new action in logging (type - disk, file count 1, file name - any name you want)
create new rule (topics: script, warning action: your new action, prefix - any you want )
in netwach in down action add command (or add to the script what you run on "down"event this line: /log warning "PC is down at $[/system clock get time]" )

after that add to the scheduler script to send e-mail with file (file will have name you had written in "action" + .0.txt)
to send file you can use smth like:
/tool e-mail send to=yourmail@gmail.com subject="Netwach lof file" file="netwachlog.0.txt"

to clear log file after sending just delete file
/file remove netwachlog.0.txt
also you can add logging for "up" event

Re: Netwatch logging and send log daily by email

Posted: Sun Jul 02, 2017 12:15 pm
by Technetium
Thanks very clear and it works ;)

I'm a beginner of RouterOs and after many research i found that from script i have to save any messages about topics we select (es. warning or info or..) and after (download, email..) filter it to select the messages that interest us.
In my mind, working on linux system, i would like to create a log file dedicated to each action that i want log, but in routerOs isn't possible.
Now i'm in the hell of those who study a new system xD

Some other question:
I haven't found how to check a filesize of a file.. or how to check if the mail was really sent without error.
In case of lack of connection, error on authentication etc.. a simple script (example i've found on internet, also in this forum) will not send the email and after delete in any case the log file. That's terribly wrong in my mind.

Re: Netwatch logging and send log daily by email

Posted: Sun Jul 02, 2017 12:38 pm
by Shadeofspirit
Thanks very clear and it works ;)

I'm a beginner of RouterOs and after many research i found that from script i have to save any messages about topics we select (es. warning or info or..) and after (download, email..) filter it to select the messages that interest us.
In my mind, working on linux system, i would like to create a log file dedicated to each action that i want log, but in routerOs isn't possible.
Now i'm in the hell of those who study a new system xD

Some other question:
I haven't found how to check a filesize of a file.. or how to check if the mail was really sent without error.
In case of lack of connection, error on authentication etc.. a simple script (example i've found on internet, also in this forum) will not send the email and after delete in any case the log file. That's terribly wrong in my mind.
i can't help with checking send email (i didn't have to setup this). in my cases i usually setup logging to remote servers and for critical systems have telegram alerts

Re: Netwatch logging and send log daily by email

Posted: Sun Jul 02, 2017 2:26 pm
by Technetium
i can't help with checking send email (i didn't have to setup this). in my cases i usually setup logging to remote servers and for critical systems have telegram alerts
This setup work if the device is supposed to be constantly connected. If the connection stops, the message to syslog server are lost.
On server i use both, local log and remote syslog server. If the connection fail, after i can recover the local log on request or wait until the automatic send of the log by mail.
I think you use the same approach.

Re: Netwatch logging and send log daily by email

Posted: Sun Jul 02, 2017 2:57 pm
by Shadeofspirit
i can't help with checking send email (i didn't have to setup this). in my cases i usually setup logging to remote servers and for critical systems have telegram alerts
This setup work if the device is supposed to be constantly connected. If the connection stops, the message to syslog server are lost.
On server i use both, local log and remote syslog server. If the connection fail, after i can recover the local log on request or wait until the automatic send of the log by mail.
I think you use the same approach.
in my case log from netwatch comes to remote syslog server, so all the information about connection state is on it. and as i had said - the state of critical services comes also by telegram bot

Re: Netwatch logging and send log daily by email

Posted: Fri Aug 11, 2017 10:17 pm
by ovdeathiam
I have something simmilar already configured.
Zrzut ekranu z 2017-08-11 21.19.07.png
Logs are saved to _GoogleDNS.txt untill it's full then it is renamed to have a timestamp and a new clear file is created. You could change it to what you need.

  • Netwatch settings:
    Host: 8.8.8.8
    Interval: 00:00:02
    Timeout: 1000
  • Script up:
    :local workdir "log/google/"
    :local name "GoogleDNS"
    
    :local scl [/system clock get];
    :local months {jan="01";feb="02";mar="03";apr="04";may="05";jun="06";jul="07";aug="08";sep="09";oct="10";nov="11";dec="12"};
    :set ($scl->"y") [:pick ($scl->"date") 7 11];
    :set ($scl->"mn") [:pick ($scl->"date") 0 3];
    :set ($scl->"m") ($months->($scl->"mn"));
    :set ($scl->"d") [:pick ($scl->"date") 4 6];
    :set ($scl->"H") [:pick ($scl->"time") 0 2];
    :set ($scl->"M") [:pick ($scl->"time") 3 5];
    :set ($scl->"S") [:pick ($scl->"time") 6 8];
    :set ($scl->"timestamp") (($scl->"y").".".($scl->"m").".".($scl->"d")."_".($scl->"H").".".($scl->"M").".".($scl->"S"));
    
    :local tempfile ($workdir."_".$name.".txt");
    :local output (" - UP ".($scl->"timestamp")."\r\n");
    
    :local newcontent ([/file get "$tempfile" contents].$output); :delay 1;
    /file set [/file find name="$tempfile"] contents=$newcontent;
    :log debug message=($name.": UP");
    
  • Down:
    :local workdir "log/google/"
    :local name "GoogleDNS"
    
    :local scl [/system clock get];
    :local months {jan="01";feb="02";mar="03";apr="04";may="05";jun="06";jul="07";aug="08";sep="09";oct="10";nov="11";dec="12"};
    :set ($scl->"y") [:pick ($scl->"date") 7 11];
    :set ($scl->"mn") [:pick ($scl->"date") 0 3];
    :set ($scl->"m") ($months->($scl->"mn"));
    :set ($scl->"d") [:pick ($scl->"date") 4 6];
    :set ($scl->"H") [:pick ($scl->"time") 0 2];
    :set ($scl->"M") [:pick ($scl->"time") 3 5];
    :set ($scl->"S") [:pick ($scl->"time") 6 8];
    :set ($scl->"timestamp") (($scl->"y").".".($scl->"m").".".($scl->"d")."_".($scl->"H").".".($scl->"M").".".($scl->"S"));
    
    :local tempfile ($workdir."_".$name.".txt");
    :local logfile ($workdir.$name."_".($scl->"timestamp").".txt");
    :local output ($name." - DOWN ".($scl->"timestamp"));
    
    :if ([:len [/file find where size > "3072" name~$tempfile;]] > 0) do={
        :local tempcontent ([file get "$tempfile" contents]);
        /file print file="$logfile"; :delay 1;/file set [/file find name="$logfile"] contents=$tempcontent;
        /file set [/file find name="$tempfile"] contents="";
        }
    
    :if ([:len [/file find where name~$tempfile;]] > 0) do={} else={
        /file print file="$tempfile"; :delay 1; /file set "$tempfile" contents="";
        }
    
    :local newcontent ([/file get "$tempfile" contents].$output);
    /file set [/file find name="$tempfile"] contents=$newcontent;
    
    :log debug message=($name.": DOWN")
    

Re: Netwatch logging and send log daily by email

Posted: Fri Aug 11, 2017 11:12 pm
by JohnTRIVOLTA
I'm a beginner of RouterOS ;)
I would like to set up a lan monitoring tool that send me the log daily.
Email is already configured.

Using netwatch i want log only if a host is down.
But how can i write a specific log only dedicated to netwatch and send it only daily by email ?
How can i "reset" the file after successful sending it ?
I use this script https://rickfreyconsulting.com/send-logs-via-e-mail/ by Rick Frey , and i just add a schedule and a script to clear the log аbout 15 seconds after being sent.
/system scheduler
add interval=1d name="clear logs" policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive start-date=jan/21/2017 start-time=20:00:15
/system script
add name="clear logs" owner=ico policy=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source="/\
    system logging action set disk disk-lines-per-file=1; :delay 5;\r\
    \n/system logging action set disk disk-lines-per-file=10000"