Community discussions

MikroTik App
 
gutekpl
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 92
Joined: Wed Feb 20, 2019 6:31 pm

Basic netwatch via ping with saving error timestamp to textfile

Tue Jan 14, 2020 2:40 pm

Hi guys.
Out of curiosity I want to test my ISP stability/uptime. Easiest way I think of is to run scheduler with ping 8.8.8.8 every few seconds, and report if there is output other than normal ping reply.
This is what I came up with:
:local file
:local time
:local date

:set time [/system clock get time]
:set date [/system clock get date]
:local file ([:pick $date 7 11]."-".[:pick $date 0 3]."-".[:pick $date 4 6])

:if ([/ping 8.8.8.8 count=5] = 0) do={
:log error "Ping error"
/file print file="$file"
:delay 1s
/file set [find name~"$file"] contents=$time
}
:log error "Ping error." <- this I need to see it on speed in log file, marked with red color.
FIle is created but every time when ping fails it overwrite file with fresh time.
I need it to work like >> output.txt in cmd so it will write new line at the end instead of overwriting whole file. Is that doable? I want to have separate txt file for every day.
Last edited by gutekpl on Tue Jan 14, 2020 3:41 pm, edited 1 time in total.
 
gutekpl
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 92
Joined: Wed Feb 20, 2019 6:31 pm

Re: Basic netwatch via ping with saving error output to textfile

Tue Jan 14, 2020 3:35 pm

Ok, I think I managed to get it working :D
:local file
:local time
:local date

:set time [/system clock get time]
:set date [/system clock get date]
:local file ([:pick $date 7 11]."-".[:pick $date 0 3]."-".[:pick $date 4 6])

:if ([/ping 8.8.8.8 count=5] = 0) do={
:log error "Ping error"

:local contents [/file get $file contents]
:set contents ($contents . "\n" . $time)
/file set $file contents=$contents
}
If You have any comments or improvement please say.
 
User avatar
macsrwe
Forum Guru
Forum Guru
Posts: 1007
Joined: Mon Apr 02, 2007 5:43 am
Location: Arizona, USA
Contact:

Re: Basic netwatch via ping with saving error timestamp to textfile

Wed Jan 15, 2020 5:32 am

This will work until the file gets to a certain size, then it will fail with all new data falling down the bitbucket. String variables in scripting are limited in size.
 
gutekpl
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 92
Joined: Wed Feb 20, 2019 6:31 pm

Re: Basic netwatch via ping with saving error timestamp to textfile

Fri Jan 17, 2020 3:40 pm

Are You reffering to this part?
:set contents ($contents . "\n" . $time)
How to avoid it? Is there any other way to save new data at the end of the file?
 
User avatar
macsrwe
Forum Guru
Forum Guru
Posts: 1007
Joined: Mon Apr 02, 2007 5:43 am
Location: Arizona, USA
Contact:

Re: Basic netwatch via ping with saving error timestamp to textfile

Fri Jan 17, 2020 10:49 pm

After 13 years and multiple courses on the MikroTik, I know of none. The routerOS file system is insanely primitive, and the scripting language seems purposely written to ignore it. The /file edit command can't handle a file significantly larger than a system note -- not even an average export file for a more than trivial configuration. You cannot even create a folder without resorting to awkward tricks in FTP. At best, a script might be able to use a file to store a semaphore or two.
 
sopyan0807
newbie
Posts: 39
Joined: Wed Jan 22, 2020 5:21 pm
Location: Indonesia
Contact:

Re: Basic netwatch via ping with saving error timestamp to textfile

Sun Jan 26, 2020 5:41 am

Why not just use a telegram bot to receive ping notifications?
 
gutekpl
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 92
Joined: Wed Feb 20, 2019 6:31 pm

Re: Basic netwatch via ping with saving error timestamp to textfile

Sun Jan 26, 2020 9:49 pm

Why not just use a telegram bot to receive ping notifications?
That was my first idea and I did it that way as I already have few telegram informations implemented. It was working pretty ok on test IP 8.8.8.88. All "unreachable" messages arrived to me via telegram.

But when I moved it from DEV to PROD it stopped to work. At begining I did not know why. Turns out You need an internet connection for sending telegram messages... You get the point now, right?

Who is online

Users browsing this forum: Bing [Bot] and 25 guests