Rename interface active monitors to ifName, ifAlias, ifDescr, or a combination of these

1

The gentleman who originally wrote this was Michael Nyland. I have slightly modified it, but all credit goes to him. Many WhatsUp Gold users find this very useful. This script allows you to update the comment on interface active monitors so the comment is one of [or a combination of] the following: ifName, ifAlias, ifDescr. If you want to run this against your entire environment, you may need to run this “external” to WhatsUp Gold, so to speak. There is an internal timeout maximum of 60 seconds for scripts run within WhatsUp Gold. If you do need to run this “external” to WhatsUp Gold, you must fill in the database username/password section near the very bottom. If you plan to run directly in WUG [smaller environments], remove the line near the top starting Dim Context. I will also provide a screenshot that shows how to run this “external” to WhatsUp Gold but having it be scheduled as a recurring action.

'This Action Script is NOT Device assigned and should be used 
' as a global reccuring Action that could be scheduled or run manually as needed.
' Original Author: petersenme
' Updated 03-16-09: mnyland
' Updated 08-18-14: jalberino
' Updated 12-10-15: jalberino
Option Explicit

Dim Context : Set Context = New clContext ' This line for running outside of WUG
'Comment out or delete the line above and the clContext class at end when running within WUG

'*****CONFIGURATION*******
'Choose which values to use, and you can enable more than one.
'**Note: The sComment column in the database is limited to 150 characters, anything over that will be trimmed!
Dim bUseIfName : bUseIfName = 1
Dim bUseIfAlias : bUseIfAlias = 1
Dim bUseIfDescr : bUseIfDescr = 0
'Do a single IP or all devices in the environment?
Dim bDoSingleIP : bDoSingleIP = 0 '0 to do the entire environment, 1 to do a single IP
Dim sSingleIP : sSingleIP = "192.168.170.55" 'If single IP, enter it here
'Set to 0 to *NOT* update the database, 1 to update the database
Dim bDoTheUpdate : bDoTheUpdate = 1
'Adjust SNMP settings
Dim nSNMPTimeout : nSNMPTimeout = 3000 'Set ms for SNMP timeouts
Dim nSNMPRetry : nSNMPRetry = 2 'Set the number of SNMP retries
'****END CONFIGURATION*****
'**DO NOT EDIT BELOW HERE**
'**************************

Dim oSnmp : Set oSnmp = CreateObject("CoreAsp.SnmpRqst")
Dim nTimeout : nTimeout = oSnmp.SetTimeoutMs(nSNMPTimeout)
Dim nRetries : nRetries = oSnmp.SetNumRetries(nSNMPRetry)
Dim oEvent : Set oEvent = CreateObject("CoreAsp.EventHelper")

' global variables
Dim indexNum, arrDevices, iRc, iNumUpdated
Dim nPivotID, nDevID, sArg, sCmt, sAddr, arrDevChange, sDevChange

Const idxPivotID   = 0
Const idxDevID     = 1
Const idxArg       = 2
Const idxCmt       = 3
Const idxAddr      = 4
Const elementRow   = 2
Const elementCol   = 1
Const sOID_ifName  = "1.3.6.1.2.1.31.1.1.1.1"
Const sOID_ifDescr = "1.3.6.1.2.1.2.2.1.2"
Const sOID_ifAlias = "1.3.6.1.2.1.31.1.1.1.18"

' Perform processing here
Dim oDb : Set oDb = Context.GetDB
indexNum = 0
iNumUpdated = 0
If gatherDevices(arrDevices) Then
   For indexNum = 0 To UBound(arrDevices, elementRow)
       nPivotID = arrDevices(idxPivotID, indexNum)
       nDevID   = arrDevices(idxDevID  , indexNum)
       sArg     = arrDevices(idxArg    , indexNum)
       sCmt     = arrDevices(idxCmt    , indexNum)
       sAddr    = arrDevices(idxAddr   , indexNum)
       Context.NotifyProgress "Checking Device=" & nDevID & " Addr=" & sAddr & " PivotID=" & nPivotID & " Arg=" & sArg & " PrevCmt=" & sCmt & vbCrLf
   
       iRc = UpdateComment(nPivotID, nDevID, sArg, sCmt, sAddr)
       If iRc = 0 Then
          Context.NotifyProgress "         No update" & vbCrLf
       Else
          iNumUpdated = iNumUpdated + 1
          Context.NotifyProgress "         Change to: " & sCmt & vbCrLf
       End If
   Next
End If
Context.NotifyProgress iNumUpdated & " interface monitor comments changed" & vbCrLf

If iNumUpdated <> 0 Then
 'Context.NotifyProgress sDevChange
 arrDevChange = ReformatString(sDevChange)
 'Context.NotifyProgress arrDevChange
 SendChangeEvent(arrDevChange)
End If

' ****************************************************************************************************
' * Function gatherDevice ** Function gatherDevice ** Function gatherDevice ** Function gatherDevice *
' ****************************************************************************************************
Function gatherDevices(arrDevices)
Dim i, j, sqlQry1, oRs1
sqlQry1 = "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' "
If bDoSingleIP Then
   sqlQry1 = sqlQry1 & "AND sNetworkAddress = '" & sSingleIP & "' "
End If
sqlQry1 = sqlQry1 & "ORDER BY 5,3"
Set oRs1 = CreateObject("ADODB.Recordset")
Set oRs1 = oDb.Execute(sqlQry1)
If oRs1.EOF Or oRs1.BOF Then
   gatherDevices = 0
   Context.NotifyProgress "No interface monitors meet criteria"
Else
   gatherDevices = 1
   arrDevices = oRs1.GetRows()
End If
oRs1.Close
Set oRs1 = Nothing
End Function

' ******************************************************************************************************** 
' * Function UpdateComment ** Function UpdateComment ** Function UpdateComment ** Function UpdateComment *
' ******************************************************************************************************** 
Function UpdateComment (nPivotID, nDevID, sArg, sCmt, sAddr)
UpdateComment = 0
Dim oRs1, rc, sValues, bUpdate
Dim sIfName, sIfAlias, sIfDesc, sNewVal, sSql, sValArray, nValLength
Dim bFail, sErrorMsg
bFail = 0
sErrorMsg = ""
bUpdate = 1

Set rc = oSnmp.Initialize(nDevID)

If Not rc.Failed Then
 'ifName
 If bUseIfName = 1 Then
  sIfName = Trim(GetSNMPVal(sOID_ifName & "." & sArg, bFail, sErrorMsg))
 End If
 'ifAlias
 If bUseIfAlias = 1 Then
  sIfAlias = Trim(GetSNMPVal(sOID_ifAlias & "." & sArg, bFail, sErrorMsg))
 End If
 'ifDesc
 If bUseIfDescr = 1 Then
  sIfDesc = Trim(GetSNMPVal(sOID_ifDescr & "." & sArg, bFail, sErrorMsg))
 End If
End If

If bFail <> 0 Then
 bUpdate = 0
 Context.NotifyProgress "There was a SNMP error: " & sErrorMsg
Else
 sValues = sIfName & "," & sIfAlias & "," & sIfDesc
End If

sValArray = Split(sValues, ",")
Dim x : For x = 0 To UBound(sValArray)
 'Context.NotifyProgress sValArray(x)
 If Len(sValArray(x)) <> 0 Then
  sNewVal = sNewVal + sValArray(x) & " "
 End If
Next

If Len(sNewVal) > 150 Then
	sNewVal = Left(sNewVal, 150)
End If

sNewVal = Trim(sNewVal)

If Len(sNewVal) = 0 Then
 bUpdate = 0
End If

If bDoTheUpdate = 1 And bUpdate = 1 Then
 sSql = "Update PivotActiveMonitorTypeToDevice Set sComment = " & StringIfy(sNewVal) & " Where nPivotActiveMonitorTypeToDeviceID = " & nPivotID
 Set oRs1 = CreateObject("ADODB.Recordset")
 Set oRs1 = oDb.Execute(sSql)
 sDevChange = sDevChange & nDevID & ","
 UpdateComment = 1
End If
'Context.NotifyProgress sDevChange
End Function

' ****************
' *  GetSNMPVal  *
' ****************
Function GetSNMPVal(sOID, ByRef bFail, ByRef sErrorMsg)
GetSNMPVal = 0
Dim rc
Set rc = oSnmp.Get(sOID)
If rc.Failed Then
	sErrorMsg = rc.GetErrorMsg
	bFail = bFail + 1
Else
GetSNMPVal = rc.GetValue
End If
End Function

Function ReformatString(sString)
Dim arrDevId, DeviceID, sTmp, DevID
arrDevId = Split(sString, ",")
sTmp = ""
For Each DevID In arrDevId
 If DevID <> "" And InStr(1, sTmp, DevID, vbTextCompare) = 0 Then
  sTmp = sTmp & DevID & ","
 End If
Next
ReformatString = sTmp
End Function

' *************
' * StringIfy *
' *************
Function StringIfy(str)
If IsNull(str) Then
   StringIfy = "NULL"
Else
   StringIfy = "'" & QuoteChars(str) & "'"
End If
End Function

' **********
' * NumIfy *
' **********
Function NumIfy(num)
If IsNull(num) Then
   NumIfy = "NULL"
Else
   NumIfy = num
End If
End Function
'
' **************
' * QuoteChars *
' **************
Function QuoteChars(ByVal szLine)
Dim rc

If Not IsNull(szLine) And Len(szLine) > 0 Then
   rc = InStr(szLine, "'")
   Do While Not IsNull(rc) And rc > 0
      szLine = Left(szLine, rc) & "'" & Right(szLine, Len(szLine) - rc)
      rc = InStr(rc + 2, szLine, "'")
   Loop
End If
QuoteChars = szLine
End Function

Sub SendChangeEvent(aDevChangeID)
Dim DevID, arrDevChange1
'Context.NotifyProgress aDevChangeID
'Variables for device change event
Const DCT_MODIFIED = 2
Const DCIT_DEVICE  = 1
arrDevChange1 = Split(aDevChangeID, ",")
For Each DevID In arrDevChange1
 If DevID <> "" And DevID <> "," And DevID <> " " Then
  oEvent.SendChangeEvent DCT_MODIFIED, DevID, DCIT_DEVICE
  Context.NotifyProgress DevID & " was sent a change event."
 End If
Next
End Sub

' ****************************************************************************
' * Class clContext ** Class clContext ** Class clContext ** Class clContext *
' ****************************************************************************
Class clContext
		Private internal_StartTime
		Private oDb
		Private bGotDB
		
		Public Function LogMessage(szMsg)
		   Debug.WriteLine szMsg
           wsh.echo szMsg		   
		   LogMessage = 1
		End Function
		
		Public Function NotifyProgress(szMsg)
		   Debug.WriteLine szMsg
           wsh.echo szMsg		   
		   NotifyProgress = 1
		End Function
		
		Public Property Get GetDB
		   If bGotDB = 1 Then
		      GetDB = oDb
		   Else
			  Set oDb = CreateObject("ADODB.Connection")
              oDb.Open "dsn=WhatsUp;uid=sa;pwd=sa;"
              If oDb.Errors.Count <> 0 Then
                 Dim i
                 For i = 0 to oDb.errors.count -1
                    LogMessage "DB Connnect Msg: " + CStr(oDb.Errors.Item(i).NativeError) + " - " + oDb.Errors.Item(i).Description
                 Next
              End If
		      Set GetDB = oDb
		      bGotDB = 1
		   End If
		End Property
End Class

Create a new program action in WhatsUp Gold with the following settings, pointing at your script you’d like to launch ‘external’. This will use WhatsUp Gold to launch the action, but it will launch as a separate process.

Post your comment