Skip to main content

 

Update Device Description

14 months ago
OfflineBrittany Ross
Brittany Ross
Description:

[Originally posted by Stanley Johnson]

Updates a device's description with the SNMP description, contact and location information that resides on the device.  

We use a syslog passive monitor to trigger this whenever we receive a message that says "configured from console".

Language: VB Script/JScript
Code:
// Name: Update device description
// Description: Update device description based on SNMP sysName variable
// Language: JScript
// SNMP MIB-2 Usefull System OIDS
//1.3.6.1.2.1.1.1 - sysDescr
//1.3.6.1.2.1.1.2 - sysObjectID
//1.3.6.1.2.1.1.3 - sysUpTime
//1.3.6.1.2.1.1.4 - sysContact
//1.3.6.1.2.1.1.5 - sysName
//1.3.6.1.2.1.1.6 - sysLocation

var oSnmpRqst =  new ActiveXObject("CoreAsp.SnmpRqst") ;
var nDeviceID = Context.GetProperty("DeviceID") ;
var oResult = oSnmpRqst.Initialize(nDeviceID) ;
var sSuccess=0;

 Context.Logmessage("Starting Device Description Update");



if (oResult.Failed) {
  Context.SetResult(1, "Initialization error " ) ;
}
else {

//-------Do the Description Attribute SNMP Get and DB updates.
  var sysDesc = GetSNMPAttrib(nDeviceID,"1.3.6.1.2.1.1.1.0","Description");
  Context.LogMessage("Getting Description");
  if (sysDesc != 1) {
     Context.LogMessage("Description = " + sysDesc);
     if (SetDBAttrib(nDeviceID,"Description",sysDesc)==1) sSuccess=1;
  }
  else {
    sSuccess=1;
  }

//-------Do the Contact Attribute SNMP Get and DB updates.
  var sysContact = GetSNMPAttrib(nDeviceID,"1.3.6.1.2.1.1.4.0","Contact");
  if (sysContact != 1) {
     if (SetDBAttrib(nDeviceID,"Contact",sysContact)==1) sSuccess=1;
  }
  else {
    sSuccess=1;
  }


//-------Do the Location Attribute SNMP Get and DB updates.
  var sysLocation = GetSNMPAttrib(nDeviceID,"1.3.6.1.2.1.1.6.0","Location");
  if (sysLocation != 1) {
     if (SetDBAttrib(nDeviceID,"Location",sysLocation)==1) sSuccess=1;
  }
  else {
    sSuccess=1;
  }

 
  if (sSuccess != 0){
    Context.SetResult(1,"Update Failed");
  }
  else {
    Context.SetResult(0,"Update Successful");
  }
}

//--------This function Gets the payload for an SNMP OID from a given Device
function GetSNMPAttrib(nDeviceID,sOID) {
  var oResult = oSnmpRqst.Get(sOID);
  if (oResult.Failed) {
    return 1;
  }
  else {
    return oResult.GetPayload;
  }
}


//--------This Function updates the DeviceAttribute table for a given device/attribute combination.
function SetDBAttrib(nDeviceID,sAttrib,sPayload) {
    var oDb = Context.GetDB ;
    if (oDb == null) {
     return 1;
    }
    else {
     var oRs = new ActiveXObject("ADODB.Recordset");
     var sSql = "UPDATE DeviceAttribute SET sValue='" + sPayload + "' WHERE sName='" +sAttrib + "' AND nDeviceID = " + nDeviceID;
     oDb.Execute (sSql) ;
    return 0;
    }
}
-----
You liked this too4 people like this discussion.
 
Concluded

0 Conclusions:

10 Replies

OfflineNicolas Fontanini Nicolas Fontanini said 13 months ago

Merci !!

thanks !!


OfflineCharlie Wise Charlie Wise said 5 months ago

Can anyone currently using this confirm that it still works?  It appears to execute properly, but does not actually update any attributes.


OfflineNicolas Fontanini Nicolas Fontanini said 5 months ago

Hello,

Yes it still works, the attributes are updated .

The script must be added as active monitor


OfflineRichard Helton Richard Helton said 3 months ago

If we set it up as an active monitor what is used to trigger it?


OfflineStanley Johnson Stanley Johnson said 3 months ago

As noted above, we use it as an action on a passive monitor so that we update the device details whenever somebody makes a change to the server.   This seems to make the most sense as the "configured from console" syslog message only shows up when there has been a change to the actual device.


OfflineRichard Helton Richard Helton said 3 months ago

Thanks Stanley, I go that part, but when I tested it I get an initialization error.

If I set it up as an active monitor, as Nicolas stated, the test works but I do not know what would activate it under the active monitor.


OfflineNicolas Fontanini Nicolas Fontanini said 3 months ago

Hello,

If you want to set it up as an active monitor, you need to add it to the active monitor library (Configure => Active Monitor Library => New => select Active Script Monitor => then you paste this code and select JSscript

then you can add this active monitor to all devices at the same time and 'use a different poll frequency' for this monitor (86400 sec for ex.)

or you activate it once for all devices and then deactivate it for all devices..

Nicolas.


OfflineRichard Helton Richard Helton said 3 months ago

Thanks everyone, we got it working last night on the passive "up" monitor.

I think I will include both so I can activate as needed when necessary.


OfflineMark Wallace Mark Wallace said 3 months ago

Thanks heaps!


OfflineDhruvesh Patel Dhruvesh Patel said 6 weeks ago

Thanks to Stanley for the script and Brittnay for posting it here. I do have one question about the attributes and if there is a possibility to add the attributes on the device automatically.

We needed to list the serial numbers of all our Cisco Switches (3560G's & a few 3500's). I was able to get the OID & Instance for the serial number from one of the 3560G's and below is what I've added to the original script to get this information:

-------------------------------------------------------------------------------------------------------------

//-------Do the Serial Number Attribute SNMP Get and DB updates.

  var entPhysicalSerialNum = GetSNMPAttrib(nDeviceID,"1.3.6.1.2.1.47.1.1.1.1.11.1001","SerialNumber");

  if (entPhysicalSerialNum != 1) {

     if (SetDBAttrib(nDeviceID,"SerialNumber",entPhysicalSerialNum)==1) sSuccess=1;

  }

  else {

    sSuccess=1;

  }

-------------------------------------------------------------------------------------------------------------

The only issue I had with this was that when the script ran as an active monitor, I did not add the "SerialNumber" attribute on the device attributes and I was not able to see the device S/N. I had to manually add the SerialNumber attribute on the device and then apply the above script as an active monitor which then populated the SerialNumber Value.

Has anyone been able to use this script and at the same time create the attribute defined in the jscript if it was not already on the device.

I do know that I can select multiple devices and add the attribute I wanted using the buld field change; but I'm curious to see if anyone was able to get this working so that the attirbute gets created and populated with its respective value when the script pools for the OID's defined in it.


Would you like to comment?

You must be a member. Sign In if you are already a member.

  • 1,364 views
  • $obj.VersionIndex versions
  • 10 replies
  • 5 followers
     
Post Date:
February 23, 2011
Posted By:
Brittany Ross

About this forum

  • 21,991 views
  • 19 topics
  • 4 followers
     

Viewed 1,364 times