Community discussions

MikroTik App
 
aea9tthompson
just joined
Topic Author
Posts: 14
Joined: Sat Jan 05, 2008 12:55 am

Function for find if a process exists

Wed Jan 14, 2009 10:14 pm

Greetings,

I am trying to write a function that takes a single input parameter(a process name) than uses the HOST MIB to find if the process exists on the current device the function is called from via SNMP.

Here is what I use for a probe to do this, but I would like to do a function
if(array_find(oid_column("1.3.6.1.2.1.25.4.2.1.2"),"wuauclt.exe")>-1, -1, 0)
So far as a probe this works pretty well except that occasionally incorrect results are returned making it seem that the probe is down almost like the Dude can't get the SNMP HOST mib from the destination.

How do I create a function that takes a parameter and how do I use the parameter within a function?

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

Re: Function for find if a process exists

Wed Jan 14, 2009 11:40 pm

Here is a probe that calls a function... I don't know if this will help but it could be a start.
probe.JPG
function.JPG
This is a probe that I was working on to determine if a port channel is working.
It could have been used but I didn't add it to anything.

As far as things not responding to every SNMP querey I seem to get plenty of them but they seem to be caused by The dude and not because the SNMP read failed.

Oh also search for the Probe Thread for some more information.
You do not have the required permissions to view the files attached to this post.
 
Toepfe
newbie
Posts: 30
Joined: Fri Oct 31, 2008 11:48 am

Re: Function for find if a process exists

Thu Jan 15, 2009 8:31 am

Hi,

As far as I know, unfortunately it is not possible to use parameters in functions or probes (but I don't know the new ver. 3.1). In some cases I use the custom fields to manage it. You get the content of custom field 1 with "device_property("CustomField1")".

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

Re: Function for find if a process exists

Thu Jan 15, 2009 7:27 pm

I suppose you are trying to dynamically add probes to devices that detect a specific process.

In that case you could allow "Network Discover" to run "Service Discover (Discover Mode: Reliable)" in the Discover scan networks dialog. And that means your running auto discover for each map you want to do this on.

You would have a probe for each service you want to discover then when you add a device and discover services on the device those services should be automatically added to the device. Just modify the device type to allow the services that you are interested in.

This would work though you would have to have Auto discovery running ...
 
pjulian
Member Candidate
Member Candidate
Posts: 267
Joined: Mon May 31, 2004 12:16 pm
Location: Sydney, Australia

Re: Function for find if a process exists

Wed Jan 21, 2009 1:18 pm

sweetdude, does that function actually work OK ?
I am trying to do the same as you but look for a part of a string, like in your example I am looking for "Po" in a field with a value of "Po1" if you know what I mean.

So I might have 5 OID's with various values, but the values change depending on the server we are checking, so I just want to look for part of the value rather than the whole thing. I can get it to match on the whole thing but not part of it, any suggestions ?

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

Re: Function for find if a process exists

Wed Jan 21, 2009 5:22 pm

Yeah it works but it is for a router.

use string_substring to return just the characters you want... here is an example.

This returns 6 characters from the oid sysname...
if (string_size(oid("iso.org.dod.internet.mgmt.mib-2.system.sysName.0")), string_substring((oid("iso.org.dod.internet.mgmt.mib-2.system.sysName.0"),0 ),6 ), "")



HTH
string.JPG
You do not have the required permissions to view the files attached to this post.
 
pjulian
Member Candidate
Member Candidate
Posts: 267
Joined: Mon May 31, 2004 12:16 pm
Location: Sydney, Australia

Re: Function for find if a process exists

Wed Jan 21, 2009 11:49 pm

Thanks, this is useful, however I still can't see how I can use it directly.
Here is what I am trying to do, exactly.

1. Identify the index of the iso.org.dod.internet.mgmt.mib-2.host.hrStorage.hrStorageTable.hrStorageEntry.hrStorageDescr value that has "C:\" in it, or just "C", doesn't really matter.
2. Concatenate this OID with that index number iso.org.dod.internet.mgmt.mib-2.host.hrStorage.hrStorageTable.hrStorageEntry.hrStorageSize
So for example it would say "iso.org.dod.internet.mgmt.mib-2.host.hrStorage.hrStorageTable.hrStorageEntry.hrStorageSize.2"
3. Do the same again for the iso.org.dod.internet.mgmt.mib-2.host.hrStorage.hrStorageTable.hrStorageEntry.hrStorageUsed OID
4. Use that information to then subtract the second value from the first to get a free disk space amount on windows servers.

I can see that your info will help, just not sure how to tie it in with the reset.
The default hdd_usage function does most of what I want, but it does a find in the array for a whole string, so I'm not sure how to modify that to tell it to look for a sub_string, I have tried just putting the characters I need in there but it doesn't work, it will only work with the entire string, I really wish there was more syntax info on these functions, it would be so much better.

Regards
Paul
 
pjulian
Member Candidate
Member Candidate
Posts: 267
Joined: Mon May 31, 2004 12:16 pm
Location: Sydney, Australia

Re: Function for find if a process exists

Mon Jan 26, 2009 12:25 pm

Bump...
Can anybody help with this ?
Surely there has to be a way of finding a string in a range of OID values ?

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

Re: Function for find if a process exists

Mon Jan 26, 2009 11:54 pm

Yeah I just test until I figure it out and use others exmples from the "Probe Thread". We really need to dive into the wiki sometime to document how to use the built in functions. They really need to add variable support IMHO to make it super powerful. If it can use variables I can't see how...

For disks I made disk1, disk2 all the way up to 5 all specific for oid then just manually attached them. They show up as graphs of % of used spance. The end result is the same they complain at a set % of utilization. This was discussed and copied directly from the probe thread. Will this not work for your situation?
disk1.JPG
if((oid("1.3.6.1.2.1.25.2.3.1.6.1")/oid("1.3.6.1.2.1.25.2.3.1.5.1"))*100>0, 1, 0)
if((oid("1.3.6.1.2.1.25.2.3.1.6.1")/oid("1.3.6.1.2.1.25.2.3.1.5.1"))*100<80, "", "Disk usage > 79%%")
(oid("1.3.6.1.2.1.25.2.3.1.6.1")/oid("1.3.6.1.2.1.25.2.3.1.5.1"))*100

Disk #2 is 6.2 and 5.2, Disk#3 is 6.3 and 5.3 and so on...
You do not have the required permissions to view the files attached to this post.
 
pjulian
Member Candidate
Member Candidate
Posts: 267
Joined: Mon May 31, 2004 12:16 pm
Location: Sydney, Australia

Re: Function for find if a process exists

Tue Jan 27, 2009 12:02 am

Yes this would work, problem is some servers have A:\ and some don't now, so you never really know which disk is the first disk, hence why I want to search for the drive letter, then make the function based on that, then I simply create a probe for C: and D: etc and add them into the standard template for windows servers.

Put simply, I don't want to have to search for the exact OID indes each time I ad a server.

Thanks for your efforts though, this would definately work and is how I am doing it now.

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

Re: Function for find if a process exists

Tue Jan 27, 2009 2:40 am

I look at the built in function hdd_size, it returns the capacity of the first occurence of "FixedDisk" There is absolutely a way to do what you want. Probably take a few hours to figure out.

What I would do is create a new function called say DiskC or what ever, Put the code from hdd_size int it, Place the function name on a device label then start adding to it slowly, Every time you apply your changes the label on the device will update. This way you can test your code quickly and know when you broke something. Putting a function on a device label is just [hdd_size()]

Also download notepad++ and paste your code in there and set the language to object C or what ever and you will get nice syntax highlighting.

I am strugling to understand it too so I feel your pain!

I have made probes that call functions by examining cpu_usage. I would work on this more but I only monitor a few servers. I am the network administrator and won't be needing to deal with large numbers of servers.

Please post what you learn in the probe thread if you keep working on it... If I figure it out on my spare time I will post back here and the probe thread.
 
pjulian
Member Candidate
Member Candidate
Posts: 267
Joined: Mon May 31, 2004 12:16 pm
Location: Sydney, Australia

Re: Function for find if a process exists

Tue Jan 27, 2009 9:57 am

That's pretty much what I have started with, and tried to modify, where it does an array_find is where I come unstuck, array_find will find the whole value of the OID whereas I just need to find the first character of the value. If I could find some way to combine your Po1 interface function and the hdd_size function I would have it, but I can't figure out how to return just the first character or the value within the array_find.

Good idea about the label, I will try some more with that tonight and keep playing, I so wish that Mikrotik would release some actual doco on these functions and the commands and their syntax with some good examples, the power is there but just out of reach as it were :-)

Regards
Paul
 
Toepfe
newbie
Posts: 30
Joined: Fri Oct 31, 2008 11:48 am

Re: Function for find if a process exists

Tue Jan 27, 2009 4:50 pm

Hi,

To return the first character of a value use the function string_substring (in the description of the function, you will get information about the parameter setting)

for example:
string_substring(oid("1.3.6.1.2.1.25.2.3.1.3.1"),0,1)
gives back the first character of the above OID value.

Of course you may also use string_substring in combination with array_find.

But do I understand you correctly: You want to have a full automatically function which checks how much HDDs are installed on a server, which OID (end)number they have and uses a spezified probe for every HDD to check it then?

Differs a little bit to the first entry of this thread ;-)

Bye
 
pjulian
Member Candidate
Member Candidate
Posts: 267
Joined: Mon May 31, 2004 12:16 pm
Location: Sydney, Australia

Re: Function for find if a process exists

Sat Jan 31, 2009 10:46 am

OK, I understand what you are saying, but how can I use a string_substring with an array_find ?

This is what works:
array_find(
oid_column("iso.org.dod.internet.mgmt.mib-2.host.hrStorage.hrStorageTable.hrStorageEntry.hrStorageDescr", 600),"C:\ Label: Serial Number 18727cdf"
)

But I need it to look for just "C" instead of the whole shebang with the serial number and stuff because that changes on every disk.

It needs to look like this:
array_find(
oid_column("iso.org.dod.internet.mgmt.mib-2.host.hrStorage.hrStorageTable.hrStorageEntry.hrStorageDescr", 600),"C"
)

Or something similar using the string_substring.

I have tried all sorts of ways to try and implement string_substring in there, but an array_find will only compare the whole value it finds, you can't compare just one or two characters which is what I need to try and use the string_substring for.

Regards
Paul
 
Toepfe
newbie
Posts: 30
Joined: Fri Oct 31, 2008 11:48 am

Re: Function for find if a process exists

Mon Feb 02, 2009 11:56 am

Hi,

I'm already not sure if I completely understand what you want to do. But if you like to search for "C:" in your OID string, maybe the following line will help you:

if(string_find(oid_column("1.3.6.1.2.1.25.2.3.1.3",600),"C:")<>4294967295, "ok", "not found")

Bye
 
pjulian
Member Candidate
Member Candidate
Posts: 267
Joined: Mon May 31, 2004 12:16 pm
Location: Sydney, Australia

Re: Function for find if a process exists

Wed Feb 04, 2009 1:50 pm

Thats fine if I am looking for a string in a normal OID value or column, but I need to find a substring in an oid column that is part of an array_find, so the syntax is all wrong.

My previous example of what works is exactly what I need to do...except it looks for the entire string that is the value of the OID, I need to just find one part of that string in the OID value.

Regards
Paul
 
Toepfe
newbie
Posts: 30
Joined: Fri Oct 31, 2008 11:48 am

Re: Function for find if a process exists

Wed Feb 04, 2009 4:41 pm

Hi,

Sorry if I wrote something stupid, but I don't see really a difference of the outcome between the lines

array_find(oid_column("1.3.6.1.2.1.25.2.3.1.3",600),"C:\ Label: Serial Number 18727cdf")

and

string_find(oid_column("1.3.6.1.2.1.25.2.3.1.3",600),"C:\ Label: Serial Number 18727cdf")

With both lines I get 0 (the position). The only difference I noticed is, that it doesn't work with array_find in combination with "C:" (or a part of "C:\ Label: Serial Number 18727cdf"). I'm also wondering why :?

With string_find I always get the right position, doesn't matter if I searched for C:, D: or something else. If the string where not found in the oid_column, the output is 4294967295.

But now I think it's time for me to give up ;-)

Bye
 
pjulian
Member Candidate
Member Candidate
Posts: 267
Joined: Mon May 31, 2004 12:16 pm
Location: Sydney, Australia

Re: Function for find if a process exists

Wed Feb 04, 2009 11:46 pm

Please don't get me wrong, I appreciate your help, very much.

You have found the same issue as me though, that I can't seem to find a way to tie in the string_substring command with the array_find command and get the correct result, however you have just used a command I was previously unaware of, which is string_find. Perhaps I can use this command with the part of the string I am looking for, I will try it and let you know.

Regards
Paul
 
pjulian
Member Candidate
Member Candidate
Posts: 267
Joined: Mon May 31, 2004 12:16 pm
Location: Sydney, Australia

Re: Function for find if a process exists

Wed Feb 04, 2009 11:55 pm

I can't get it to work with the string_find either strangely, what version of The Dude are you using, maybe I need the latest version to get this string_find to work ?

Regards
Paul
 
pjulian
Member Candidate
Member Candidate
Posts: 267
Joined: Mon May 31, 2004 12:16 pm
Location: Sydney, Australia

Re: Function for find if a process exists

Thu Feb 05, 2009 12:09 am

I have installed 3.1 and string_find works, to a degree, it returns a number higher than the actual index, is this because the value type is octet string ? Do I need to do anything else with that do you know ?
When I search for A:\ which is the first value, I get a 0 returned, when I search for C:\ which is the second value I get 5 returned, when I search for D:\ which is the third value I get 41 returned :?

Regards
Paul
 
Toepfe
newbie
Posts: 30
Joined: Fri Oct 31, 2008 11:48 am

Re: Function for find if a process exists

Thu Feb 05, 2009 9:16 am

Hi Paul,

Now I made some more investigations and I had to correct myself. There is a difference between array_find() and string_find(). Array_find() gives back the line number starting with 0, string_find() gives back the position number also starting with 0. With array_find() it seems only to be possible to search for complete lines (see following screenshot):

Snmp Walk starts with line number 1!
dude10.jpg
Another screenshot of the oid_column("1.3.6.1.2.1.25.2.3.1.3",600) output in an editor:
dude11.jpg
Some examples:
array_find "C:\ Label:System...." gives back 0
string_find "C:\"    : 0
array_find "D:\ Label:System...." : 1
string_find "D:\" : 42
array_find "E:\ Label:System...." : 2
string_find "E:\" : 82

...and so on.

Once again, I am not really sure what you like to do, but if you want to search in the sample OID for drive letters, from my point of view string_find() should be the correct function.

By the way, I use Dude 3.0

Bye
Heimo
You do not have the required permissions to view the files attached to this post.
 
pjulian
Member Candidate
Member Candidate
Posts: 267
Joined: Mon May 31, 2004 12:16 pm
Location: Sydney, Australia

Re: Function for find if a process exists

Thu Feb 05, 2009 12:50 pm

Thanks, that's very interesting, I was wondering why I was getting the weird numbers back using the string_find, it's returning the position of the string it found, not the index.

So all I need to figure out is how I can search for just a character or 2 using an array_find, or make a string_find return the index instead of the position of the character.

Thanks for your help

Regards
Paul
 
Toepfe
newbie
Posts: 30
Joined: Fri Oct 31, 2008 11:48 am

Re: Function for find if a process exists

Thu Feb 05, 2009 4:46 pm

Hi Paul,

The following function code gives back the index (line) number starting with 0

if(
string_find(oid_column("1.3.6.1.2.1.25.2.3.1.3",600),"E:")<>4294967295,
array_find(oid_column("1.3.6.1.2.1.25.2.3.1.3",600),
string_substring(oid_column("1.3.6.1.2.1.25.2.3.1.3",600),
string_find(oid_column("1.3.6.1.2.1.25.2.3.1.3",600),"E:"),
(
string_find(oid_column("1.3.6.1.2.1.25.2.3.1.3",600),",",
string_find(oid_column("1.3.6.1.2.1.25.2.3.1.3",600),"E:"))
-string_find(oid_column("1.3.6.1.2.1.25.2.3.1.3",600),"E:"
)
))),
"not found"
)

As you see, I search for drive letter "E:", if you want to search for another one replace all four "E" with f.e. "C".

If you need the index (line) number starting with 1 (maybe for some OID endnumbers) simply add +1, as in the follwing code.

if(
string_find(oid_column("1.3.6.1.2.1.25.2.3.1.3",600),"E:")<>4294967295,
array_find(oid_column("1.3.6.1.2.1.25.2.3.1.3",600),
string_substring(oid_column("1.3.6.1.2.1.25.2.3.1.3",600),
string_find(oid_column("1.3.6.1.2.1.25.2.3.1.3",600),"E:"),
(
string_find(oid_column("1.3.6.1.2.1.25.2.3.1.3",600),",",
string_find(oid_column("1.3.6.1.2.1.25.2.3.1.3",600),"E:"))
-string_find(oid_column("1.3.6.1.2.1.25.2.3.1.3",600),"E:"
)
)))+1,
"not found"
)

I am not a programmer, so sorry if there exist a simplier way to get what you want ;-)

Bye
Heimo

Who is online

Users browsing this forum: Sailwebwifi and 26 guests