SNMP Time Compare

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


  • OIS Infrastructure OIS (Wisconsin State Laboratory of Hygiene)

    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:

    • The SNMP timeout desired.
    • The number of SNMP retries desired.
    • The desired time difference; currently set for 120 seconds.
    • The IP address of the control device (time server).
    • The DeviceID of the WUG server (can be found by opening the Device Properties of the WUG server and checking the end of the URL).
    • Your SNMPv1 Community Name.
    1. Option Explicit
    2. '***Configuration***
    3. Dim nSNMPTimeout : nSNMPTimeout= 3000 'Set ms for SNMP timeouts
    4. Dim nSNMPRetry : nSNMPRetry = 2 'Set the number of SNMP retries
    5. Dim nThreshold : nThreshold = 120 'Set the time difference in seconds
    6. Dim sControl : sControl = "123.123.123.123" 'Enter the IP address of the control device (time server)
    7. Dim nWUGID : nWUGID = XXXX 'This is the DeviceID of the WUG server in WhatsUp
    8. Dim nSnmpVersion : nSnmpVersion = 1
    9. Dim sCommunity : sCommunity = "YourSNMPv1CommunityName"
    10. '***End Configuration -- Changes below this line can break the script***
    11.  
    12. 'Get device information
    13. Dim nDeviceID : nDeviceID = Context.GetProperty("DeviceID") 'This will be the remote deviceID
    14.  
    15. 'Set default values
    16. Dim bFail : bFail = 0
    17. Dim bSNMPresult : bSNMPresult = 0
    18. Dim sErrorMsg : sErrorMsg = ""
    19. Dim nResult : nResult = 0
    20. Dim sOIDSystemDate : sOIDSystemDate = "1.3.6.1.2.1.25.1.2.0"
    21. Dim sOIDSysname : sOIDSysname = " 1.3.6.1.2.1.1.5.0"
    22. Dim sMsg : sMsg = ""
    23.  
    24. 'Create SNMP object
    25. Dim oSnmp : Set oSnmp = CreateObject("CoreAsp.SnmpRqst")
    26. Dim nTimeout : nTimeout = oSnmp.SetTimeoutMs(nSNMPTimeout)
    27. Dim nRetries : nRetries = oSnmp.SetNumRetries(nSNMPRetry)
    28.  
    29. '************Remote Time
    30. Dim rc
    31. Set rc = oSnmp.Initialize(nDeviceID)
    32. If rc.Failed Then
    33. sErrorMsg = rc.GetErrorMsg
    34. bSNMPresult = 1
    35. Else
    36. Set rc = oSnmp.Get(sOIDSysname)
    37. If rc.Failed Then
    38. sErrorMsg = rc.GetErrorMsg
    39. bFail = 1
    40. Else
    41. Dim sSysNameRemote : sSysNameRemote = rc.GetValue
    42. Context.LogMessage "Connected to SNMP on: " & sSysNameRemote
    43. End If
    44. End If
    45.  
    46. If bFail = 0 And bSNMPresult = 0 Then
    47. Dim sTime : sTime = GetSNMPVal(sOIDSystemDate, bFail, sErrorMsg)
    48. If bFail = 0 Then
    49. Dim sRemoteTime : sRemoteTime = DateTimeConversion(sTime)
    50. FormatDateTime sRemoteTime, vbLongTime
    51. Context.LogMessage "Time: " & sRemoteTime & vbCrLf
    52. Else
    53. Context.LogMessage sErrorMsg
    54. End If
    55. End If
    56.  
    57. '*********Control Time
    58. Dim rc2 : Set rc2 = oSnmp.Initialize(nWUGID)
    59. If rc2.Failed Then
    60. sErrorMsg = rc2.GetErrorMsg
    61. bSNMPresult = 1
    62. Else
    63. Set rc2 = oSnmp.Get(sOIDSysname)
    64. If rc2.Failed Then
    65. sErrorMsg = rc2.GetErrorMsg
    66. bFail = 1
    67. Context.Logmessage sErrorMsg
    68. Else
    69. Dim sSysNameLocal : sSysNameLocal = rc2.GetValue
    70. Context.LogMessage "Connected to SNMP on: " & sSysNameLocal
    71. End If
    72. End If
    73.  
    74. If bFail = 0 And bSNMPresult = 0 Then
    75. sTime = GetSNMPVal(sOIDSystemDate, bFail, sErrorMsg)
    76. If bFail = 0 Then
    77. Dim sLocalTime : sLocalTime = DateTimeConversion(sTime)
    78. FormatDateTime sLocalTime, vbLongTime
    79. Context.LogMessage "Control Time:" & sLocalTime & vbCrLf
    80. Else
    81. Context.LogMessage sErrorMsg
    82. End If
    83. End If
    84.  
    85. '***Compare localtime to remotetime
    86. Dim nDifference : nDifference = ABS(DateDiff("s",sRemoteTime,sLocalTime))
    87. Context.LogMessage nDifference & " second difference!"
    88.  
    89. '***Up or Down
    90. If nDifference > nThreshold Then
    91. sMsg = "Local time " & sLocalTime & " is not within " & nThreshold & " seconds of " & sRemoteTime & " Monitor is 'Down'!"
    92. nResult = 1
    93. Else
    94. sMsg = "Local time " & sLocalTime & " is within " & nThreshold & " seconds of " & sRemoteTime & " Monitor is 'Up'!"
    95. End If
    96. Context.SetResult nResult,sMsg
    97.  
    98. '****Clean up
    99. Set sRemoteTime = Nothing
    100. Set sLocalTime = Nothing
    101. Set sTime = Nothing
    102. Set oSnmp = Nothing
    103.  
    104. ' ****************
    105. ' * GetSNMPVal *
    106. ' ****************
    107. Function GetSNMPVal(sOID, ByRef bFail, ByRef sErrorMsg)
    108. Dim rc
    109. GetSNMPVal = 0
    110. Set rc = oSnmp.Get(sOID)
    111. If rc.Failed Then
    112. sErrorMsg = rc.GetErrorMsg
    113. bFail = bFail + 1
    114. Else
    115. GetSNMPVal = rc.GetValue
    116. End If
    117. End Function
    118.  
    119. ' *************
    120. ' * StringIfy *
    121. ' *************
    122. Function StringIfy(str)
    123. If IsNull(str) Then
    124. StringIfy = "NULL"
    125. Else
    126. StringIfy = "'" & QuoteChars(str) & "'"
    127. End If
    128. End Function
    129.  
    130. ' **************
    131. ' * QuoteChars *
    132. ' **************
    133. Function QuoteChars(ByVal szLine)
    134. Dim rc
    135. If Not IsNull(szLine) And Len(szLine) > 0 Then
    136. rc = InStr(szLine, "'")
    137. Do While Not IsNull(rc) And rc > 0
    138. szLine = Left(szLine, rc) & "'" & Right(szLine, Len(szLine) - rc)
    139. rc = InStr(rc + 2, szLine, "'")
    140. Loop
    141. End If
    142. QuoteChars = szLine
    143. End Function
    144.  
    145. ' **********************
    146. ' * DateTimeConversion *
    147. ' **********************
    148. Function DateTimeConversion(DateTime)
    149. Dim aDate, aTime
    150. aDate = Split(DateTime, ",")
    151. aTime = Split(aDate(1), ".")
    152. DateTime = CDate(aDate(0) & " " & aTime(0))
    153. DateTimeConversion = DateTime
    154. End Function