
Community Archive (Customer) asked a question.
We really need something on here so source code displays properly...
Option Explicit
Dim oDb : Set oDb = Context.GetDB
'***Configuration***
Dim nSNMPTimeout : nSNMPTimeout= 3000 'Set ms for SNMP timeouts
Dim nSNMPRetry : nSNMPRetry = 2 'Set the number of SNMP retries
Dim nThreshold : nThreshold = 10 'Set the time difference in seconds
Dim sControl : sControl = "192.168.170.121" 'Enter the IP address of the control device
Dim sSNMPCredID : sSNMPCredID = GetCredID(oDb, "CheckMe") 'Name of the SNMP credential to use, this must match what is in WUG
'***End Configuration -- Changes below this line can break the script***
'Get device information
Dim nDeviceID : nDeviceID = Context.GetProperty("DeviceID") 'This will be the remote deviceID
'Set default values
Dim bFail : bFail = 0
Dim bSNMPresult : bSNMPresult = 0
Dim sErrorMsg : sErrorMsg = ""
Dim nResult : nResult = 0
Dim sOIDSystemDate : sOIDSystemDate = "1.3.6.1.2.1.25.1.2.0"
Dim sOIDSysname : sOIDSysname = " 1.3.6.1.2.1.1.5.0"
Dim sMsg : sMsg = ""
'Create SNMP object
Dim oSnmp : Set oSnmp = CreateObject("CoreAsp.SnmpRqst")
Dim nTimeout : nTimeout = oSnmp.SetTimeoutMs(nSNMPTimeout)
Dim nRetries : nRetries = oSnmp.SetNumRetries(nSNMPRetry)
'************Remote Time
Dim rc
Set rc = oSnmp.Initialize(nDeviceID)
If rc.Failed Then
sErrorMsg = rc.GetErrorMsg
bSNMPresult = 1
Else
Set rc = oSnmp.Get(sOIDSysname)
If rc.Failed Then
sErrorMsg = rc.GetErrorMsg
bFail = 1
Else
Dim sSysNameRemote : sSysNameRemote = rc.GetValue
Context.LogMessage "Connected to SNMP on: " & sSysNameRemote
End If
End If
If bFail = 0 And bSNMPresult = 0 Then
Dim sTime : sTime = GetSNMPVal(sOIDSystemDate, bFail, sErrorMsg)
If bFail = 0 Then
Dim sRemoteTime : sRemoteTime = DateTimeConversion(sTime)
FormatDateTime sRemoteTime, vbLongTime
Context.LogMessage "Time: " & sRemoteTime & vbCrLf
Else
Context.LogMessage sErrorMsg
End If
End If
'*********Control Time
Dim rc2 : Set rc2 = oSnmp.Initialize2(sControl,sSNMPCredID)
If rc2.Failed Then
sErrorMsg = rc2.GetErrorMsg
bSNMPresult = 1
Else
Set rc2 = oSnmp.Get(sOIDSysname)
If rc2.Failed Then
sErrorMsg = rc2.GetErrorMsg
bFail = 1
Context.Logmessage sErrorMsg
Else
Dim sSysNameLocal : sSysNameLocal = rc2.GetValue
Context.LogMessage "Connected to SNMP on: " & sSysNameLocal
End If
End If
If bFail = 0 And bSNMPresult = 0 Then
sTime = GetSNMPVal(sOIDSystemDate, bFail, sErrorMsg)
If bFail = 0 Then
Dim sLocalTime : sLocalTime = DateTimeConversion(sTime)
FormatDateTime sLocalTime, vbLongTime
Context.LogMessage "Control Time:" & sLocalTime & vbCrLf
Else
Context.LogMessage sErrorMsg
End If
End If
'***Compare localtime to remotetime
Dim nDifference : nDifference = ABS(DateDiff("s",sRemoteTime,sLocalTime))
Context.LogMessage nDifference & " second difference!"
'***Up or Down
If nDifference > nThreshold Then
sMsg = "Local time " & sLocalTime & " is not within " & nThreshold & " seconds of " & sRemoteTime & " monitor is 'Down'!"
nResult = 1
Else
sMsg = "Local time " & sLocalTime & " is within " & nThreshold & " seconds of " & sRemoteTime & " monitor is 'Up'!"
End If
Context.SetResult nResult,sMsg
'****Clean up
Set sRemoteTime = Nothing
Set sLocalTime = Nothing
Set sTime = Nothing
' ****************
' * GetSNMPVal *
' ****************
Function GetSNMPVal(sOID, ByRef bFail, ByRef sErrorMsg)
Dim rc
GetSNMPVal = 0
Set rc = oSnmp.Get(sOID)
If rc.Failed Then
sErrorMsg = rc.GetErrorMsg
bFail = bFail + 1
Else
GetSNMPVal = rc.GetValue
End If
End Function
' *******************
' * GetCredentialID *
' *******************
Function GetCredID(oDB, sSNMPCred)
GetCredID = 0
Dim sSql, sRet, sRS
sSql = "Select max(nCredentialTypeID) AS nID from CredentialType " & _
" Where nCredentialType in (1,2,4) " & _
" and sDisplayName = " & StringIfy(sSNMPCred)
Set sRS = oDb.Execute(sSql)
If Not sRS.EOF And Not sRS.BOF Then
sRet = Trim(sRS.GetString)
If Len(sRet) > 0 Then
If Right(sRet, 1) = vbCr Then
sRet = Left(sRet, Len(sRet) - 1)
End If
End If
If IsNumeric(sRet) Then
GetCredID = CInt(sRet)
End If
End If
End Function
' *************
' * StringIfy *
' *************
Function StringIfy(str)
If IsNull(str) Then
StringIfy = "NULL"
Else
StringIfy = "'" & QuoteChars(str) & "'"
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
' **********************
' * DateTimeConversion *
' **********************
Function DateTimeConversion(DateTime)
Dim aDate, aTime
aDate = Split(DateTime, ",")
aTime = Split(aDate(1), ".")
DateTime = CDate(aDate(0) & " " & aTime(0))
DateTimeConversion = DateTime
End Function
Here is a version of the above which does not use "Context.GetDB", and so can be run under the Isolated Process execution model.
Written with much assistance from Mike Rockwell.
You will need to change the variables for: