
Ks Tan (Asia Vad) asked a question.
Currently, the interface status comment field can only be automatically added from one of the 3 of ifAlias, ifname, or ifDesc according to General settings, not all or any combination of 2. In some project, the details are needed from both ifAlias + ifName, or different combination of 2 or all 3.
For example both ifAlias : Connection to Singtel ISP, ifName : Gi 1/0/1, are needed in the alert details
The script below retrieves both ifAlias + ifName record of the test Device, from another db table stored by Discovery Scan asset details, and update it to comment field.
Note: It's to be test run from Active Monitor > Test button, to update the interface comment for selected Device, but not to be assigned to actual device as monitor.
- // Script developed by Kstan, Asia Vad.
- // To add both ifAlias AND ifName to the interface status montior Comment field
- // For more complete network interface comment, based on ifAlias, ifDesc, ifName captured
- // during Discovery Scanning of network devices, and stored at AssetToken table
-
- var oDb = Context.GetDB;
- if (null == oDb)
- {
- Context.SetResult( 1, " Problem creating the DB object");
- }
- else
- {
-
- // Get the device ID
- var sDeviceID = Context.GetProperty("DeviceID");
-
- // Retrieve interface status monitor
-
- var sSql = "SELECT nPivotActiveMonitorTypeToDeviceID, Device.nDeviceID, sArgument, sComment, sNetworkAddress FROM PivotActiveMonitorTypeToDevice JOIN Device ON Device.nDeviceID = PivotActiveMonitorTypeToDevice.nDeviceID JOIN ActiveMonitorType ON ActiveMonitorType.nActiveMonitorTypeID = PivotActiveMonitorTypeToDevice.nActiveMonitorTypeID JOIN NetworkInterface ON NetworkInterface.nNetworkInterfaceID = Device.nDefaultNetworkInterfaceID WHERE PivotActiveMonitorTypeToDevice.bRemoved = 0 AND Device.bRemoved = 0 AND ActiveMonitorType.bRemoved = 0 AND ActiveMonitorType.sMonitorTypeName = 'Interface' AND Device.nDeviceID = " + sDeviceID;
- var oRs = oDb.Execute(sSql);
-
- while ( !oRs.EOF )
- {
- var nPivotActiveMonitorTypeToDeviceID = oRs("nPivotActiveMonitorTypeToDeviceID");
- var nDeviceID = oRs("nDeviceID");
- var ifIndex = oRs("sArgument");
- var ifComment = oRs("sComment");
- var networkAddress = oRs("sNetworkAddress");
- // Context.LogMessage("Inf Monitor:" + nDeviceID + "," + ifIndex + "," + ifComment + "," + networkAddress);
-
- // Retrieve ifName and ifDescription based on nDeviceID and ifIndex
- // The script will fail, if there is apostrophe in the text returned.
- var sSql2 = "SELECT nDeviceID ,IfIndex ,IfNameID ,(select Value from AssetToken where AssetToken.TokenID = AssetDeviceInterface.IfNameID) as ifName ,IfDescriptionID ,(select Value from AssetToken where AssetToken.TokenID = AssetDeviceInterface.IfDescriptionID) as IfDesc ,IfAliasID ,(select Value from AssetToken where AssetToken.TokenID = AssetDeviceInterface.IfAliasID) as IfAlias ,IfOperStatus FROM AssetDeviceInterface where nDeviceID =" + nDeviceID + " AND ifIndex = " + ifIndex;
- var oRs2 = oDb.Execute(sSql2);
- if ( !oRs2.EOF )
- {
- var nDeviceID2 = oRs2("nDeviceID");
- var IfIndex2 = oRs2("IfIndex");
- var ifName = oRs2("ifName");
- var IfAlias = oRs2("IfAlias");
- var newComment = IfAlias + " (" + ifName + ")";
- var sSql3 = "UPDATE PivotActiveMonitorTypeToDevice SET sComment = '" + newComment + "' where nPivotActiveMonitorTypeToDeviceID = " + nPivotActiveMonitorTypeToDeviceID;
-
- // Context.LogMessage("Token Result:" + nDeviceID2 + "," + IfIndex2 + "," + ifName + "," + IfAlias);
- Context.LogMessage("SQL Cmd Executed:" + sSql3);
- var oRs3 = oDb.Execute(sSql3);
- } else
- {
- Context.LogMessage("No result, no change");
- }
-
- oRs.moveNext();
- }
-
- }
-
- oDb.Close(); //Close database connection
-
- Context.SetResult(0, "Result OK");
Thanks for the script!!
I have solved the "fail if there is apostrophe in the text returned" and changed "ifName" with "ifDesc" if the first is blank (it should't happen, but I found this in a few old devices!)
Here you are my version