Function FindObject returns wrog object class when node as argument

Started by maredcz, January 25, 2016, 03:06:57 PM

Previous topic - Next topic

maredcz

According to source codes, the function should return generic object class or null
/**
* Find object
* First argument: object id or name
* Second argument (optional): current node object or null
* Returns generic object or null if requested object was not found or access to it was denied
*/

If I call the function with a node as argument
o=FindObject(nodeid);
the 
o->type
returns: ERROR: Script finished with error: Error 15 in line XX: Unknown object's attribute


With container ID as parameter is not problem.

So I think the function is returning Node pointer when the argument is the node.

From my point of sight is it wrong behaviour. For this purpose exist FindNodeObject.

Martin



Code example
sub main() {

n=FindObject(10506);   // node class type

  println("Name: " . n->name . " ID: " . n->id);
  println("snmpSysName: " . n->snmpSysName); // WRONG - returns a snmpSysname value
  println("Type: " . n->type);  //WRONG - shoul return "2", but returns
             // - ERROR: Script finished with error: Error 15 in line XX: Unknown object's attribute
 
n=FindObject(219);   //Container class type

  println("Name: " . n->name . " ID: " . n->id);
  println("snmpSysName: " . n->snmpSysName); //CORRECT error message, snmpSysName does not exists in netobj"
                  // - ERROR: Script finished with error: Error 15 in line XX: Unknown object's attribute
  println("Type: " . n->type);  //CORRECT - returns "5"

}

tomaskir

Your example code works fine for me.

Are you sure you arent dealing with security issues as described here?
https://wiki.netxms.org/wiki/SG:Security_Issues

Here is the code I used:

// node class type
n=FindObject(507);
println("Trying for node...");
println("Name: " . n->name . " ID: " . n->id);
// this should not throw error
try {
  println("snmpSysName: " . n->snmpSysName);
} catch {
  println("Got an error for node!");
}
println("Type: " . n->type);

println("");

//Container class type
n=FindObject(851);
println("Trying for container...");
println("Name: " . n->name . " ID: " . n->id);
// this should throw error
try {
  println("snmpSysName: " . n->snmpSysName);
} catch {
  println("Got an error for container!");
}
println("Type: " . n->type);

Alex Kirhenshtein

That was actually fixed couple of days ago in ca594353.

But released version still use different classes (Node / NetObj). You can check it this way:if (instanceof(FindObject(123) == "Node") { ... }

maredcz

The function instanceof is not known im my system.
I am currently running 2.0.1 version.

In my case FindObject function returns different classes for Node and Container, but should return only the generic class as mentioned in Documentation.

println(classof(FindObject(10506)));  //10506 is Node, FindObject returns "Node"
println(classof(FindObject(219)));   //219 is Container, FindObject returns "NetObj"

It is not generally big problem, but it differs from the function description in Documentation pages and source code comments.

Martin

tomaskir

Working with 2.0.2 here, the output of this script:

// this is a Container
println(classof(FindObject(125)));

// this is a Node
println(classof(FindObject(4528)));

// this is a Container
try {
  println(classof(FindNodeObject(null, 125)));
} catch {
  println("This error should be displayed, since we are calling classoff(null)");
}

// this is a Node
println(classof(FindNodeObject(null, 4528)));



Is

NetObj
Node
This error should be displayed, since we are calling classoff(null)
Node

*** FINISHED ***


Node is just a sub-class of NetObj, so calling any attributes of the NetObj on a node-class object will of course work.
For reference: https://wiki.netxms.org/wiki/NXSL_Class_Reference.

I have also fixed the FindObject function documentation on the wiki to be more clear on what the current behavior is:
https://wiki.netxms.org/wiki/NXSL:FindObject

maredcz

Hello Tomas, to be exactly I have a problem with NetObj's "TYPE attribute" when the object is Node.
As mentioned in Documentation, the Node class should inherite all attributes of NetObj class, but the
n->type returns "ERROR: Script finished with error: Error 15 in line XX: Unknown object's attribute"

It happen when the object is node.

The Title of this post should be probably
The type attribute of NetObj cannot be determined when the object is node - Error Unknown object's attribute"



node=10506;

nodeObject=FindObject(node);

// this is a Node
println("FindObject - Class of node object: " . node . " is " . classof(nodeObject));

try {
println("Type of node object: " . nodeObject->type);
} catch {
  println("Query nodeObject->type. This error should not be displayed, since Node should have the TYPE attribute inherited from NetObj");
}


tomaskir

It should be fixed in 2.0.2 (as Alex mentioned) which will be released very soon. :)
Please check after release if it works correctly for you as well.

For me (running on 2.0.2 development build), the output of the script is correct:

FindObject - Class of node object: 4535 is Node
Type of node object: 2

*** FINISHED ***

maredcz