Instance Filter Script with Net.InterfaceNames list

Started by Tursiops, July 04, 2016, 01:52:06 AM

Previous topic - Next topic

Tursiops

Hi,

I am trying to filter out interfaces which are adminState down as well as expectedState down via instance filter scripting.
I'm using the Net.InterfaceNames Agent List to get a proper name for the DCI itself and am having issues retrieving those interface states in the filter script.
As $1 is only the interface name, I used the FindObject function hoping to get the interface object to then allow me to check for adminState and expectedState.
Unfortunately that didn't work out, so I tried GetInterfaceObject, but that requires an interface index - which I don't get with the Net.InterfaceNames list.
Switching to Net.InterfaceList didn't help me either.

Does anyone have a code snippet to point me in the right direction?

Thanks

Andreas@rc

Hi,

this is a part of my ConfigPoll hook, which is looking for expectedState of an interface. Maybe this will help you.

interfaces = GetNodeInterfaces($node);
foreach(i : interfaces) {
if(i->expectedState == 0) { // 0 = UP, 1 = DOWN, 2 = IGNORE
...
}
else {
...
}
}

Tursiops

Thanks for the snippet.

Not sure how well this would work inside of an instance filter script.
Instance Discovery will loop through the list of all interfaces and return the names, one by one. It's inside that loop that the instance filter script runs (for all I understand).
I guess I could loop through all node interfaces for each interface on the node discovered by Instance Discovery, then compare names and on a match check the expectedState. Seems a bit inefficient though. If I could directly retrieve the correct interface object from within the instance filter script, it would be a fairly quick check.

From what I read about FindObject, that should have worked using the interface name provided by Instance Discovery.  But I could not use any of the object's properties afterwards.  ???
I might give it another go later today and provide the code I used. Maybe I've had some typo or logic error in there - wouldn't be the first time.  :-[

Andreas@rc

Well,
i found this little trick along some other stuff. This should provide everything you need.

i = GetInterfaceObject($node, $1);
if ((i != null) && (i->expectedState == 0))
{
return %(true, $1, i->description);
}
return false;

Tursiops

Sorry, but that does not work for me either.
If I use Net.InterfaceNames as list for InstanceDiscovery, I receive an error that I am not using a whole number in GetInterfaceObject(). That's expected, as it requires the interface index, not the name.
If I use Net.InterfaceList instead, I get the same error and a whole number of DCIs with names like "1 127.0.0.1/8 24(2147483647) 000000000000 Loopback Pseudo-Interface 1" (that's just the {instance} part of it, which appears to mix index, IP addresses and MAC address).

If I use FindObject, for all I can tell I should be getting an interface object.
After going through some older forum posts I thought I'd test that as follows:
i = FindObject($1,$node);
trace (0,i->name);
trace (0,classof(i));


The logs then give me the interface name and "NetObj" in the logs.
That also explains why none of the interface attributes (short of the NetObj ones) were accessible - I got the wrong object type back. Based on the documentation I would have thought this should be "Interface"?

Victor Kirhenshtein

Hi,

yes, it is bug in FindObject (fixed in 2.0.5). Anyway, it may not be a good idea to use FindObject - because it finds objects globally, while you need interfaces from that specific node. You can add the following function to your script to get interface by name:


sub FindInterfaceByName(node, ifName)
{
foreach(i : GetNodeInterfaces(node))
{
   if (i->name == ifName)
      return i;
}
return null;
}


and use it like this:


iface = FindInterfaceByName($node, "Se1/0");


Best regards,
Victor

Tursiops

Hi Victor,

If I use FindInterfaceByName, I would have to loop through all interfaces on the node for each individual interface I want to instance (as per your code). If I have a switch stack of 96 ports or more, that would cost some resources every time an instance discovery is running. I was hoping that if I can use FindObject instead and provide the $node as per documentation, i.e. FindObject($1,$node), that should not be a global search and avoid the loops?
Guess I'll wait for 2.0.5 and see if I can get it to work with that. Otherwise I'll be looping. :)

Cheers

Victor Kirhenshtein

Hi,

node object in FindObject call is not for limiting search scope, but for access control (it will only find objects where this node set as trusted if trusted nodes check is on). If you have trusted nodes check off, you can pass null as node parameter.

Best regards,
Victor

Tursiops

As the node on FindObject does not limit the search scope, I implemented this using the suggested GetNodeInterfaces loop. Works fine. :)