1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Net.NetworkInformation
Imports System.Threading
Public Class PingMonitorService
Dim ThreadTask As Thread
Dim ConnStr As String
Dim ReloadConfigFrequency As Integer
Dim dtHosts As New DataTable ' Hosts to be checked
Protected Overrides Sub OnStart(ByVal args() As String)
RefreshSettings()
ThreadTask = New Thread(AddressOf MainTask)
ThreadTask.Start()
End Sub
Protected Overrides Sub OnStop()
ThreadTask.Abort()
End Sub
Private Sub RefreshSettings()
ConnStr = ConfigurationManager.AppSettings("ConnStr").ToString()
ReloadConfigFrequency = CInt(ConfigurationManager.AppSettings("ReloadConfigFrequency"))
Dim daHosts As New SqlDataAdapter("SELECT Host, PingFreq FROM HostList ", ConnStr)
dtHosts.Clear()
daHosts.Fill(dtHosts)
End Sub
Private Sub MainTask()
Do While True
TryPing()
' Sleep 900ms to be sure at least one Tick happens on each second
Thread.Sleep(900)
Loop
End Sub
Private Sub TryPing()
' CurrentSecond is the number of seconds passed after midnight
Dim CurrentSecond As Long = CLng(Now().Subtract(Today()).TotalSeconds())
' Eventually reload configuration
If CurrentSecond Mod ReloadConfigFrequency = 0 Then
RefreshSettings()
End If
' Analyze the list of hosts to be pinged
Dim dr As DataRow
For Each dr In dtHosts.Rows
' Check if time is come to ping the specific host
If CurrentSecond Mod CInt(dr("PingFreq")) = 0 Then
' Ping the specific host
Dim IsAlive As Boolean = HostIsAlive(dr("Host"))
Dim LastStatus As String = HostLastStatus(dr("Host"))
If LastStatus = "" OrElse _
(LastStatus = "ON" And Not IsAlive) OrElse _
(LastStatus = "OFF" And IsAlive) Then
StoreStatusTransition(dr("Host"), IsAlive)
End If
End If
Next
End Sub
Private Function HostIsAlive(ByVal Host As String) As Boolean
Dim pingSender As New Ping
Dim reply As PingReply
Try
reply = pingSender.Send(Host)
If reply.Status = IPStatus.Success Then
Return True
Else
Return False
End If
Catch ex As Exception
Return False
End Try
End Function
Private Function HostLastStatus(ByVal Host As String) As String
Dim cnn As New SqlConnection(ConnStr)
Dim cmd As SqlCommand = cnn.CreateCommand
cmd.CommandText = "SELECT TOP 1 Status FROM PingLog WHERE Host='" & Host & "' ORDER BY RecordingDate DESC"
cnn.Open()
Dim obj As Object = cmd.ExecuteScalar
cnn.Close()
Try
Return obj.ToString().ToUpper()
Catch
Return ""
End Try
End Function
Private Sub StoreStatusTransition(ByVal Host As String, ByVal IsAlive As Boolean)
Dim cnn As New SqlConnection(ConnStr)
Dim cmd As SqlCommand = cnn.CreateCommand
If IsAlive Then
cmd.CommandText = "INSERT PingLog (Host, Status, RecordingDate) VALUES ('" & Host & "', 'ON', GETDATE())"
Else
cmd.CommandText = "INSERT PingLog (Host, Status, RecordingDate) VALUES ('" & Host & "', 'OFF', GETDATE())"
End If
Try
cnn.Open()
cmd.ExecuteNonQuery()
Catch
Finally
Try
cnn.Close()
Catch
End Try
End Try
End Sub
End Class |
Partager