Community discussions

MikroTik App
 
Chime
just joined
Topic Author
Posts: 2
Joined: Sun Aug 24, 2014 8:21 pm

Trouble with custom probe

Sun Aug 24, 2014 8:41 pm

Hi all, I'm trying to make a custom probe to detect whether or not a laptop is plugged into a power source or running on battery. I've created a VBScript that does a WMI query and returns a value of 2 if the laptops power supply is plugged in, and a value of 1 if it is not. I've also made a custom function to access this script and make the info available to The Dude. This appears to be working, as I can call the function in a label and have the values show up as expected. However, when I try to create a probe to monitor this information I can't seem to get the logic right.

VBScript:
Dim objWMIService, objItem, colItems, strComputer, strDomain, strUser, strPassword

strComputer = wscript.arguments(0)
strDomain = wscript.arguments(1)
strUser = wscript.arguments(2)
strPassword = wscript.arguments(3)

Set objWMIService = GetObject("winmgmts:\\" _
& strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery(_
"Select * from Win32_Battery")

For Each objItem in colItems

If ObjItem.BatteryStatus = 2 then
	Wscript.Echo "2"
Else
	Wscript.Echo "1"
End If
Next
Function:
array_element(execute("cscript.exe",concatenate("//NoLogo C:\dudescr\BatteryStatus.vbs ",User_Pass()),"C:\WINDOWS\system32\"),1)
Probe error line:
if(Battery_Status()=2, "", "Battery power!!!")
Thanks!
 
lebowski
Forum Guru
Forum Guru
Posts: 1619
Joined: Wed Aug 27, 2008 5:17 pm

Re: Trouble with custom probe

Mon Aug 25, 2014 6:47 pm

I have seen some misleading results from logic in the dude so you will have to verify every single step in the logic.
Note "negative 1" (-1) is logically true so you might need to take extra steps to deal with it. Since the execute function returns -1 if an error occurs.

Is the function actually getting called and what result is being sent to your probe? In the function you can add an if to get better detail of what is going on.
Here is how I would modify the function for testing "if the result is two, return two, otherwise return false".
if(array_element(execute("cscript.exe",concatenate("//NoLogo C:\dudescr\BatteryStatus.vbs ",User_Pass()),"C:\WINDOWS\system32\"),1)=2, 2, "False")

This way you can determine if the function is working or the result is being "incorrectly" evaluated as true in the error line of the probe.

In the probe line make sure Battery_Status is not False.
if(Battery_Status()<>"False",if(Battery_Status()=2, "", "Battery Power!!!"), "False battery status")

Since we deliberately sent "2" from the function, if it returned 2, then if you get anything else you know the function is not working correctly or the device is unreachable.

Once the testing is done...
If the logic is correct you could then execute the script "if" the script executes (not the greatest but it works fine).
if(array_element(execute("cscript.exe",concatenate("//NoLogo C:\dudescr\BatteryStatus.vbs ",User_Pass()),"C:\WINDOWS\system32\"),1), array_element(execute("cscript.exe",concatenate("//NoLogo C:\dudescr\BatteryStatus.vbs ",User_Pass()),"C:\WINDOWS\system32\"),1), "False")

OR
If the logic is correct and you want to handle negative 1 you could then execute the script "if" the script executes and is not -1 (still not the greatest but it should work fine).
if(array_element(execute("cscript.exe",concatenate("//NoLogo C:\dudescr\BatteryStatus.vbs ",User_Pass()),"C:\WINDOWS\system32\"),1)<>-1, array_element(execute("cscript.exe",concatenate("//NoLogo C:\dudescr\BatteryStatus.vbs ",User_Pass()),"C:\WINDOWS\system32\"),1), "False")

OR
If the logic is correct and you want to handle negative 1 you could add 1 to your result (another; it should work fine).
if(array_element(execute("cscript.exe",concatenate("//NoLogo C:\dudescr\BatteryStatus.vbs ",User_Pass()),"C:\WINDOWS\system32\"),1)<>+1, array_element(execute("cscript.exe",concatenate("//NoLogo C:\dudescr\BatteryStatus.vbs ",User_Pass()),"C:\WINDOWS\system32\"),1), "False")

Then your probe line could be
if(Battery_Status()<>"False",if(Battery_Status()>2, "", "Battery Power!!!"), "False battery status")

Note: In my own testing of functions the return values are not consistently easy to evaluate in the probe error line.
For example if(execute,2,0) works better than if(execute,2,"unknown") With the latter "unknown" would not give correct logic when used in a probe where "0" would BUT "False" works fine, YMMV.

ALSO 4.3 requires double backslash for execute c:\\test.exe

HTH
Lebowski

edit - typo, clarity...
 
Chime
just joined
Topic Author
Posts: 2
Joined: Sun Aug 24, 2014 8:21 pm

Re: Trouble with custom probe

Thu Sep 04, 2014 9:53 pm

Thanks Lebowski! The probe works great now, and I have a better understanding of how the dude operates. Cheers 8)

Who is online

Users browsing this forum: No registered users and 24 guests