Community discussions

MikroTik App
 
kelarlee
newbie
Topic Author
Posts: 29
Joined: Thu Dec 27, 2018 5:48 pm

Ping then put for SNMP

Fri May 20, 2022 3:25 pm

Hi there, i'm trying to make a script for SNMP protocol to monitor one of my resources but had no luck, maybe someone can help me ? So the idea - use ping command, count recieved packets and if success - return integer number. The idea was took from this guide https://github.com/gitpel/routeros-ipsec-snmp
So my script:
:local HOST "8.8.8.8"
:local PINGCOUNT "5"
:if ([/ping $HOST interval=1 count=$PINGCOUNT] = 0) do={
:put 0
} else {
:put 1
}

I'm using SNMP tester, tried to test it and got snmp value not "0" or "1" but string of ping result such as
 
"20.05.2022 15:08:10 (4133 ms) : Value:   SEQ HOST                                     SIZE TTL TIME  STATUS                0 8.8.8.8                                    56 119 21ms      1 8.8.8.8                                    56 119 21ms      2 8.8.8.8                                    56 119 21ms      3 8.8.8.8                                    56 119 21ms      4 8.8.8.8                                    56 119 21ms      sent=5 received=5 packet-loss=0% min-rtt=21ms avg-rtt=21ms max-rtt=21ms    1" 
Using 1.3.6.1.4.1.14988.1.1.18.1.1.2.X OID from guide what i mantioned before. I need exactly "0" or "1" to integrate this script to my Zabbix. Thank you.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Ping then put for SNMP

Fri May 20, 2022 3:36 pm

It is the usual problem of taking something done to do something very specific, and thinking of upsetting it.

I already have the solution for you, just answer to my questions.
If possible I want you to deduce the soluction instead to spoon feeding you.
Is more constructive, from my point of view.

First question: What tool inside RouterOS can already "ping" the IP,
and already can execute something when does it come back online or when does it go offline?
 
kelarlee
newbie
Topic Author
Posts: 29
Joined: Thu Dec 27, 2018 5:48 pm

Re: Ping then put for SNMP

Fri May 20, 2022 3:56 pm

It is the usual problem of taking something done to do something very specific, and thinking of upsetting it.

I already have the solution for you, just answer to my questions.
If possible I want you to deduce the soluction instead to spoon feeding you.
Is more constructive, from my point of view.

First question: What tool inside RouterOS can already "ping" the IP,
and already can execute something when does it come back online or when does it go offline?
I understand what you mean, but Netwatch does not fit for my task. The idea - every 5 minutes Zabbix through SNMP protocol sending request to mikrotik to execute this script. This script must return "0" or "1" after execution and send this value to Zabbix server to operate this value in triggers. I need exactly this schema because i need ping specific resource via IPSEC vpn tunnel from mikrotik side. As i mantioned before about this guide https://github.com/gitpel/routeros-ipsec-snmp i tested this script and it works fine. But i cant get it how to make same thing work with ping check command.
 
afuchs
Frequent Visitor
Frequent Visitor
Posts: 81
Joined: Wed Jul 03, 2019 11:10 am

Re: Ping then put for SNMP

Fri May 20, 2022 4:15 pm

Is the SNMP-Check a must?
Quick google search sows that Zabbx can Perform SSH checks ([/url]), and Routeros supports ssh too.
 
kelarlee
newbie
Topic Author
Posts: 29
Joined: Thu Dec 27, 2018 5:48 pm

Re: Ping then put for SNMP

Fri May 20, 2022 4:23 pm

Is the SNMP-Check a must?
Quick google search sows that Zabbx can Perform SSH checks ([/url]), and Routeros supports ssh too.
Yes SNMP is preferable, but that's not the point. Script working not as should be, it must return just integer number "0" or "1" but instead it return ping result as string like this "sent=5 received=5 packet-loss=0% min-rtt=21ms avg-rtt=21ms max-rtt=21ms"
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Ping then put for SNMP

Fri May 20, 2022 5:01 pm

The link script do not return integer number "0" or "1", but any can appear on terminal like you run the command, like ping or what you "put".

simply :put "0" or "1" make appear only 0 or 1 on terminal,
instead with ping appear everything.

With netwatch you can set a :global variable with 0 or 1 and on called script from SNMP you can write only
:put $globalvarname
and you remotely know how netwatch set that variable.
 
kelarlee
newbie
Topic Author
Posts: 29
Joined: Thu Dec 27, 2018 5:48 pm

Re: Ping then put for SNMP

Fri May 20, 2022 6:56 pm

The link script do not return integer number "0" or "1", but any can appear on terminal like you run the command, like ping or what you "put".

simply :put "0" or "1" make appear only 0 or 1 on terminal,
instead with ping appear everything.

With netwatch you can set a :global variable with 0 or 1 and on called script from SNMP you can write only
:put $globalvarname
and you remotely know how netwatch set that variable.
Thank you for tip. Tried several options with global var and netwatch but nothing worked for me :( I used in netwatch this lines with UP status:
:global test
set $test "0"
And DOWN status
:global test
set $test "1"
Script contain only one line:
:put $test
Return was empty string. I found another solution what worked for me, there's script:
:local nwstatus  [/tool netwatch get [find comment="test"] status]
if ($nwstatus = "up") do={
    :put 1
} else= {
    :put 0
}
With this i return "1" with up status and "0" with down.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Ping then put for SNMP

Sat May 21, 2022 1:56 am

About syntax, I see 3 errors here
:global test
set $test "0"
1) simply one line do all
:global "0"

2) missing : before set
:set ...

3) on :set do not put $ before variable name (instead on "set" commands is needed, like "/interface set $variable name=test")
:set test "0"
 
kelarlee
newbie
Topic Author
Posts: 29
Joined: Thu Dec 27, 2018 5:48 pm

Re: Ping then put for SNMP

Sat May 21, 2022 8:45 am

About syntax, I see 3 errors here
:global test
set $test "0"
1) simply one line do all
:global "0"

2) missing : before set
:set ...
:global test "1"
3) on :set do not put $ before variable name (instead on "set" commands is needed, like "/interface set $variable name=test")
:set test "0"
Thank you again for explanation. But still not working, for some reason when i declare global var in netwatch
:global test "1"
Mikrotik cant see this var at all. When i'm trying to return the value of this var in terminal, mikrotik even do not know this var and return empty value. When i declare this var directly from terminal and used :put $test - it returns "1" as should be. Also found this thread viewtopic.php?t=63274 where support guy said "For netwatch, scheduler, etc current user is sys*. As I mentioned it is not a bug it is designed to work so.". In continue of this in mikrotik wiki says "global - accessible from all scripts created by current user, defined by global keyword;". So maybe my script just cant see this global var because it was created from netwatch with sys* user but script was created from admin user for example ?
 
vsdias
just joined
Posts: 1
Joined: Tue Oct 18, 2022 12:50 am

Re: Ping then put for SNMP

Tue Oct 18, 2022 12:58 am

Hi,

I did some digging and ended up finding out a solution for this based on another post at the forum:

Try this:
:global pingResult -1
{
    :local jobID [:execute ":set pingResult [:ping interval=20ms count=10 99.99.99.99]"]
    :while ([:len [/system script job find where .id=$jobID]] > 0) do={
        :delay 1s
    }
}

if ($pingResult > 1) do={ 
     :put 1 
} else={
    :put 0
}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Ping then put for SNMP

Tue Oct 18, 2022 1:35 am

And what solution would you have found, copying the post, to what?
viewtopic.php?p=861988#p861988
Last edited by rextended on Fri Oct 21, 2022 3:23 pm, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Ping then put for SNMP

Tue Oct 18, 2022 5:40 am

Hi,

I did some digging and ended up finding out a solution for this based on another post at the forum:

Try this:
[...]
The "solution" on your post is not relevant to the discussion or to the solution of this problem.

In fact, you don't provide any solution, description or procedure, neither to the main problem, nor to the netwatch problem,
you just copied and pasted one script from my other post.

Who is online

Users browsing this forum: No registered users and 15 guests