Community discussions

MikroTik App
 
novelty22
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 64
Joined: Fri Feb 12, 2010 1:57 pm

if / elseif in Dude device labels or functions

Wed Nov 30, 2016 5:43 pm

Hi,

I'm trying to figure out how to do a function / device label that would have if / elseif type logic.

Basically, If a given OID (x == 2), display "Sync OK", elsif (x ==3), display "Detailed Error1", elseif (x==4), display "Detailed Error2"

I've figured out how to display the "OK" part, but how would I expand on this by adding the other "elseif" conditions?
if(oid("1.3.6.1.4.1.17713.21.1.3.1.0","2","Sync OK")


Is there a section of a doc or post anyone could point me to? I've tried looking through the "Function/Probe" thread and also on the wiki, but can't seem to find anything that would provide basic if / elsif, I might have skimmed right by it though....

Thanks
 
lebowski
Forum Guru
Forum Guru
Posts: 1619
Joined: Wed Aug 27, 2008 5:17 pm

Re: if / elseif in Dude device labels or functions

Wed Nov 30, 2016 7:05 pm

The if statement in the dude has the following description
first parameter - condition, second - returned if condition yields logical true, third - returned otherwise
if(x,return this value , else return this value)
but you can put an if inside an if...
if(x,return this value , else if(y,return this value , else if(z,return this value , false)))

In your example the if is not correct... if(oid("1.3.6.1.4.1.17713.21.1.3.1.0","2","Sync OK")

There is a factor that complicates things in probes. In a probe for it to be UP it will have to return an empty string.
So the error line of a probe is formatted like this if(x,"",error). If x is 1 the return of "" would cause the probe to be "up".
But on the appearance of a device you would want if(x,"Sync OK","Sync failed").
Now you want
if(oid("1.3.6.1.4.1.17713.21.1.3.1.0")=2,"Sync OK","Sync Failed")
or maybe
if(oid("1.3.6.1.4.1.17713.21.1.3.1")=2,"Sync OK","Sync Failed")

if (5, "sync 5", if (6, "sync 6", if (7, "sync 7", "sync failed")))

Be careful to look closely at the commas since those are the field separators. Start off like this if(x,1,2) and then you can make another if and replace 1 or 2 with the whole if.

Another good test is to use your oid directly just place [oid("1.3.6.1.4.1.17713.21.1.3.1.0")] directly on a device label to make sure you are getting the value you want. Sometimes you have to use oid_raw or remove the last 0 from your oid...[oid("1.3.6.1.4.1.17713.21.1.3.1")]

HTH,
Lebowski
 
lebowski
Forum Guru
Forum Guru
Posts: 1619
Joined: Wed Aug 27, 2008 5:17 pm

Re: if / elseif in Dude device labels or functions

Wed Nov 30, 2016 7:59 pm

Heck here is the OID table in question, so 0 is a value (don't bother since 0=false) if you really need to test for 0, add some other value to it first.
Object ID: 1.3.6.1.4.1.17713.21.1.3.1
GPS Current SYNC State:
0 - The Initialization State
1 - The No Synchronization State
2 - The Synchronization State
3 - The Hold Off State
4 - The Regaining Sync State
5 - The Free Run State

This should work for the device appearance...
if(oid("1.3.6.1.4.1.17713.21.1.3.1,10,5")=2,"Sync OK",if(oid("1.3.6.1.4.1.17713.21.1.3.1,10,5")=3,"Hold off",if(oid("1.3.6.1.4.1.17713.21.1.3.1,10,5")=4,"Retraining","failed to read sync")))

I always try to get people to use functions to better handle the issue.
Always make a function that returns the value stored in the oid or false then if the device is unreachable or the oid doesn't exist you get "false" then the probe would perform more predictably I also try to get people to put the cache time in their function so that the negative cache time doesn't break things. (negative cache time is 300 seconds) that means when a read of an oid fails that failure is cached for 300 seconds. A device that has an error will not come back up until after the negative cache time expires (5 minutes) and a successful probe happens.

Function
Name: syncstate
Code: if(string_size(oid("1.3.6.1.4.1.17713.21.1.3.1",10,5)) , oid("1.3.6.1.4.1.9.2.1.57.0", 10 ,5) ,"False")

For the appearance
[syncstate()] or
[if(syncstate()=2,"Sync OK", if(syncstate()=3,"Sync Hold", if(syncstate()=4,"Sync training", "Sync Unknown")]

for the available line of the probe
syncstate() <> "False"

For the error line of the probe
if(syncstate()<>"False", if(syncstate()=2,"", if(syncstate()=3,"Sync Hold", if(syncstate()=4,"Sync training", Concatenate("Sync Unknown = ", syncstate()) ))) ," Sync read failed ")

Notice the check for false so we can make sure we have a value before we start since the device is not responding.
So now if syncstate =2 the probe will be up, all others states create some other message and if the syncstate function can't read any value you should get "Sync read failed".

Sorry if there is errors in the syntax... I don't have the gear to test it.
Lebowski
 
novelty22
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 64
Joined: Fri Feb 12, 2010 1:57 pm

Re: if / elseif in Dude device labels or functions

Wed Nov 30, 2016 9:54 pm

This is most likely the info I was looking for.

I won't be back into the office until tomorrow morning to test any of this though

Thank you so much for the reply
 
novelty22
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 64
Joined: Fri Feb 12, 2010 1:57 pm

Re: if / elseif in Dude device labels or functions

Thu Dec 01, 2016 7:58 pm

This should work for the device appearance...
if(oid("1.3.6.1.4.1.17713.21.1.3.1,10,5")=2,"Sync OK",if(oid("1.3.6.1.4.1.17713.21.1.3.1,10,5")=3,"Hold off",if(oid("1.3.6.1.4.1.17713.21.1.3.1,10,5")=4,"Retraining","failed to read sync")))
This is exactly what i was looking for.


if(oid("1.3.6.1.4.1.17713.21.1.3.1.0")=2,"Sync OK","Sync Failed")
That works fine

But this one:
if(oid("1.3.6.1.4.1.17713.21.1.3.1.0,10,5")=2,"Sync OK",if(oid("1.3.6.1.4.1.17713.21.1.3.1.0,10,5")=3,"Hold off",if(oid("1.3.6.1.4.1.17713.21.1.3.1.0,10,5")=4,"Retraining","failed to read sync")))
Is always returning "failed to read sync state"

If I take the arguments for cache, and negative cache out (the ,10,5) it returns the proper status relative to the SNMP value. (Sync ok for 2, Hold off for 3, etc)
Not sure I'm understanding the explanation on cache and negative cache time anyways.

Thank you so much for your help
 
lebowski
Forum Guru
Forum Guru
Posts: 1619
Joined: Wed Aug 27, 2008 5:17 pm

Re: if / elseif in Dude device labels or functions

Mon Dec 05, 2016 5:25 pm

This is the fix... sorry for the syntax error.
if(oid("1.3.6.1.4.1.17713.21.1.3.1.0",10,5)=2,"Sync OK",if(oid("1.3.6.1.4.1.17713.21.1.3.1.0",10,5)=3,"Hold off",if(oid("1.3.6.1.4.1.17713.21.1.3.1.0",10,5)=4,"Retraining","failed to read sync")))
I am running 4.0b3. They need to change it in newer versions in my opinion. I know the programmers had a good reason for keeping track of failed probes (negative cache time). But it is/was buggy and they didn't send a new probe on the next polling interval to find out if the device came back up. So any time you have a device miss a ping (or any probe) that probe would be down/broke for 5 minutes. So I built most my probes with them setup like the example except for putting the correct syntax.

As you are learning the dude go into the functions and look at the description for the functions to learn them...
In 4.0b3 the description for OID is;
returns value of given snmp OID. Only first parameter mandatory. First parameter - oid string, second - cache time - default 5 seconds (5.0), third - negative cache time - default 5 minutes (300.0), forth - ip address (overrides context device), fifth - snmp profile (overrides context device)
 
lebowski
Forum Guru
Forum Guru
Posts: 1619
Joined: Wed Aug 27, 2008 5:17 pm

Re: if / elseif in Dude device labels or functions

Mon Dec 05, 2016 6:05 pm

I really like to use functions. Since you can differentiate when a device is in an error state or completely down.

Click on functions, click +, put syncstate as the name, paste the code from below and click ok. Of course be wary of the syntax errors if
Name: syncstate
if(string_size(oid("1.3.6.1.4.1.17713.21.1.3.1",10,5)) , oid("1.3.6.1.4.1.17713.21.1.3.1.0", 10 ,5) ,"False")
Now on the the appearance of the device you can use syncstate() directly...
[if(syncstate()=2,"Sync OK", if(syncstate()=3,"Sync Hold", if(syncstate()=4,"Sync training", "Sync Unknown")]
or the error line of a probe it must return "" to be up so when it is in state 2 you want to return nothing.
if(syncstate<>"False",if(syncstate()=2,"",if(syncstate()=3,"Hold off",if(syncstate()=4,"Retraining","failed to read sync"))),"State unknown device down or unsupported")
Of course you could go crazy and fix it even more! So now you want to return the correct result for every state but lets not try to include the 0 state, it is a huge problem since 0 = false = null.
[if(syncstate()=1,"Not Sync",if(syncstate()=2,"Sync OK", if(syncstate()=3,"Sync Hold", if(syncstate()=4,"Sync training",if(syncstate()=5,"Sync Free run","Sync Unknown")))))]
Had a few minutes to up the design...
Lebowski

Who is online

Users browsing this forum: deejay2 and 40 guests