Set Primary Host Name to SNMP sysName

Started by m1975Michael, September 22, 2014, 03:33:56 PM

Previous topic - Next topic

m1975Michael

I am trying to set the primary host name to the SNMP sysName.  I have found a couple of scripts on the forum but I can not get either of them to work.

if (($node->snmpSysName != null) && ($node->snmpSysName != ""))
{
   RenameObject($node, $node->snmpSysName);
}


I receive the following error when I run this script in the server console.  ERROR: Script finished with error: Error 14 in line 1: Function or operation argument is not an object.

//rename node after configuration poll to snmp sysLocation
//if sysLocation is null then rename to node ip address
//also 3 custom attributes will be setted
// you can use this attributes in events with %{attribute_name} macros
//uncomment trace line and run netxmsd with -D1 param for logging

sub main()
{
  if ($1->isSNMP)
   {
  //trace(1,"hook executed for ".$1->ipAddr);
   transport = CreateSNMPTransport($1);
    if (transport == null)
    {
    //trace(1,"snmp transport error on ".$1->ipAddr);
    return -1;
    }
   
    location = SNMPGetValue(transport, ".1.3.6.1.2.1.1.6.0"); // sysLocation
    sysname = SNMPGetValue(transport, ".1.3.6.1.2.1.1.5.0");  // sysname
    uptime = SNMPGetValue(transport, ".1.3.6.1.2.1.1.3.0");   // uptime
   
    //trace(1,"[".location."], [".sysname."], [".uptime."]");
   
    if (location != null && location !="" && location!=" ")
     { 
         //trace(1,"attr location changed to ".location);
         RenameObject($1, location);
     }
    else
    {
         //trace(1,"error in location block");
         RenameObject($1, $1->ipAddr); //change name to node ip address
    }   
    if (sysname != null && sysname !="" && sysname !=" ")
    {
         SetCustomAttribute($1, "sysname", sysname); //set custom attribute for node snmp sysName
         //trace(1,"attr sysname changed to ".location);
    }
    else
    {
         //trace(1,"error in sysname block");
    }

    if (uptime != null)
    {
         SetCustomAttribute($1, "uptime", SecondsToUptime(uptime/100)); //set custom attribute uptime for node
         //trace(1,"attr uptime changed to ".SecondsToUptime(uptime/100));
    }
    else
    {
        //trace(1,"error in uptime block");
    }
   
    //trace(1,"end execution");
    SetCustomAttribute($1, "last_update",localtime(time())->mday.".".localtime(time())->mon.".".localtime(time())->year.", ".localtime(time())->hour.":".localtime(time())->min.":".localtime(time())->sec);       
  }
}



I receive the following error when I run this script in the server console.  ERROR: Script finished with error: Error 14 in line 9: Function or operation argument is not an object.

I tried to look at examples on the wiki but I really don't know how to script.  Any assistance would be greatly appreciated.  Thank you.

Victor Kirhenshtein

Hi,

script started from console do not have $node global variable set. Examples you find was intended for working as configuration poll hooks or other automated stuff. First script is correct, if you put it into script library under name Hook::ConfigurationPoll all nodes with SNMP sysname will be renamed after configuration poll.

Best regards,
Victor

Victor Kirhenshtein

Sorry, maybe I misunderstood you - you want to change object's name (rename it), or you want to change primary host name (which is used for communications)?

Best regards,
Victor

m1975Michael

I have it working now.  Somehow the $ from all of node (s) were removed.