Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / Visual Basic
Article

Ping Monitor

Rate me:
Please Sign up or sign in to vote.
4.90/5 (24 votes)
20 Jul 20075 min read 205.2K   10.4K   102   48
Yet another Ping Monitor utility written in VB.NET on .NET Framework 2.0

Introduction

This is yet another Ping Monitor utility written in VB.NET on .NET Framework 2.0. It fulfills the need of steadily monitoring through a "PING" the status (the aliveness) of some PCs and servers on a network, keeping a history of each state transition (ON to OFF and viceversa) and visually highlighting the last status of each machine.

This application is far from being a complete and sophisticated tool. I wrote it for a colleague who needed a starting point for a more advanced and customized tool implementation.

One service, one database, one IU

In order to continuously monitor some machine, we need to have a monitoring EXE continuously runnning. So, I decided to implement the "monitor engine" as a Windows Service application (taking advantage of unattended execution, automatic restart in case of power failure, and so on). This PingMonitorService simply has to:

  • read from some configuration storage (such as a database table) the list of machines to be monitored (along with the monitoring frequency);
  • execute the actual monitoring sending a "PING" to each machine configured as to be monitored;
  • log the outcome of each "PING" somewhere.

Other than a Windows Service for the monitoring and a database as a storage, we obviously need also a user interface (UI) to interact with the configuration and the log; that's why I created a simple Windows Form application (named PingMonitor) to accomplish these tasks: showing the last PING status of the monitored machines and editing the PingMonitorService configuration.

I decided to use a SQL Server 2000 (or 2005) database to host both the configuration (that is, a list of machines to be monitored) and the log.

The following are the two table schemas, expressed in T-SQL code, with some explanations.

The configuration table

TSQL
CREATE TABLE HostList (
    ID int IDENTITY(1,1) NOT NULL,
    Host nvarchar(50) NULL,
    IsHost char(1) NULL,
    ShowInMonitor char(1) NULL,
    DoPing char(1) NULL,
    PingFreq int NULL,
    IDparent int NULL,
    CONSTRAINT PK_HostList PRIMARY KEY CLUSTERED (ID ASC)
    WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON PRIMARY
) ON PRIMARY

The HostList table will contain the list of the machines to be monitored. For each machine, it stores: the machine name with a unique ID (Host and ID fields), the frenquency of monitoring (PingFreq field, expressed in seconds), a flag indicating if the monitoring is currently activated (DoPing field) and a flag indicating if the specific machine has to be shown in the UI (ShowInMonitor field).

Being able to list a hierarchycal tree enables you to organize the machines to be monitored in groups and subgroups, the HostList table supports the concept of parent-child node relationships (via the IDparent field) and the concept of "folder" tree nodes, that are intermediate nodes not corresponding to an host but simply being the parent of other nodes (for them, the IsHost flag is not set to "yes").

Screenshot - config.jpg

The user interface provided by the PingMonitor application allows you to visually edit the tree while directly modifying the underlying HostList table (please, notice that the deletion of nodes and the addition of children nodes is allowed through the context menu on the tree).

The log table

TSQL
CREATE TABLE PingLog (
    Host varchar(50) NOT NULL,
    Status varchar(5) NOT NULL,
    RecordingDate datetime NOT NULL,
    CONSTRAINT [PK_PingLog] PRIMARY KEY CLUSTERED (Host ASC, Status ASC,
        RecordingDate ASC)
    WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON PRIMARY
) ON PRIMARY

The PingLog table will contain an entry for each detected state transition (ON to OFF or viceversa) for each monitored machine during a monitored period of time. The Host field contains the name of the monitored machine, the RecordingDate field contains a timestamp of the recorded entry and the Status field simply contains the value "ON" or "OFF" (respectively indicating a successful and an unsuccessful "PING").

Some notes about the service

The PingMonitorService has been implemented on the standard Windows Service application template of Visual Studio 2005. Being a standard .NET Windows Service, it has to be installed through the installutil.exe utility (see here for reference).

When started, the service reads its CONFIG file in order to retrieve:

  1. the connection string to the configuration database (see ConnStr appSetting)
  2. the configuration reloading frequency (see ReloadConfigFrequency appSetting): it indicates the number of seconds before reloading all the configuration parameters (this enables the service to adapt its behavior based on new settings in the HostList table eventually modified by the user through the PingMonitor UI).

To determine if it's time to "PING" a machine, the service simply looks at the total number of seconds passed after midnight and checks if this number is a multiple of the specific PingFreq for that host (notice that only status transitions are stored, not each status detection).

VB.NET
' 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

Notice that the polling interval in the MainTask() is currently set to 900ms, to be sure that at least one "tick" happens for every second (obviously, it could occur twice in some seconds). If the host list is very very big, the execution of the TryPing() method could be long enough to make the service miss some "PING" to some hosts (so please carefully configure the hosts list and the specific PingFreqs).

The "PING" itself is done, in a very simple way, through the System.Net.NetworkInformation.Ping class:

VB.NET
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

Some notes about the UI

The UI application is very simple; it consists of two forms: the monitoring form and the configuration form.

The application needs a CONFIG file where the connection string to the support database is specified (see ConnStr appSetting).

The monitoring form is just a DataGridView populated by a query on the PingLog table; the query is designed to show only the last entry for each host having ShowInMonitor='Y'.

Screenshot - monitor.jpg

The refresh frequency for the DataGridView can be specified in the ReloadFrequency appSetting of the application configuration file. Entries with an "OFF" status are highlighted by simply implementing the CellFormatting() event handler:

VB.NET
Private Sub dgMonitor_CellFormatting(...)
  Handles dgMonitor.CellFormatting
  Dim v As Object = dgMonitor.Rows(e.RowIndex).Cells("Status").Value
  If v IsNot Nothing AndAlso v.ToString() = "OFF" Then
    For Each i As DataGridViewCell In dgMonitor.Rows(e.RowIndex).Cells
      i.Style.BackColor = Color.Orange
    Next
  End If
End Sub

The configuration form contains a treeview with a detail panel, shown when a host node is selected (see the first picture in the article).

No special remarks needed on this. Just notice that the update of the underlying HostList table is done immediately after modifying values on the UI controls, through updates on the corresponding DataTable suddenly written down by the DataAdapter.

Conclusions

As stated in the Introduction, this application is to be considered as a starting point for a more advanced and customized tool implementation. Its basic idea and implementation are intentionally very simple, hoping it can be useful for you in building something more complex.

Downloads

Content of the downloadable ZIP (now available also in a C# version) for this article:

  • the DBTablesCreation.sql file - contains the T-SQL scripts for creating support database tables on Microsoft SQL Server 2000/2005;
  • the PingMonitorService folder - contains the Visual Studio 2005 project for the service;
  • the PingMonitor folder - contains the Visual Studio 2005 project for the UI WinForm application.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Technical Lead
Italy Italy
I was born in 1970.

My first computer experience dates back to early 80s, with a Sinclair ZX81.
From that time on, as many "friends" say, my IT-illness has increased year by year.

I graduated in Electronic Engineering and earned the following Microsoft certifications:
MCP, MCT, MCDBA, MCSD, MCAD, MCSD for .NET (early achiever).

I worked in IT as a developer, a teacher, a consultant, a technical writer, a technical leader.
IT knowledge applied to real life is my primary interest and focus.

Comments and Discussions

 
QuestionCould you please provide a compiled EXE files for VB and CS ready to run package Pin
Member 1470178828-Dec-19 2:19
Member 1470178828-Dec-19 2:19 
QuestionNice Mini Project Pin
shadiy12-Dec-19 22:47
shadiy12-Dec-19 22:47 
QuestionMAC Address Information to be included. Pin
Shahid ali Ashraf25-Oct-14 8:27
Shahid ali Ashraf25-Oct-14 8:27 
AnswerRe: MAC Address Information to be included. Pin
Alberto Venditti29-Oct-14 0:37
Alberto Venditti29-Oct-14 0:37 
QuestionSQL database server Pin
Arzmeen14-Jun-13 20:06
Arzmeen14-Jun-13 20:06 
AnswerRe: SQL database server Pin
Alberto Venditti14-Jun-13 21:36
Alberto Venditti14-Jun-13 21:36 
GeneralMy vote of 5 Pin
xilione30-May-13 0:22
xilione30-May-13 0:22 
Questioninstallutil.exe Pin
Sarathy806-Nov-12 0:42
Sarathy806-Nov-12 0:42 
AnswerRe: installutil.exe Pin
Alberto Venditti6-Nov-12 2:52
Alberto Venditti6-Nov-12 2:52 
QuestionHow to setup the project Pin
eddiechits 3-Jun-12 23:40
eddiechits 3-Jun-12 23:40 
QuestionCan we have mailing functionalities as an addon here. Pin
Prabhu884-Apr-12 19:44
Prabhu884-Apr-12 19:44 
QuestionE bello, mi piace Tuo programma Pin
imie_osoby27-Jan-12 10:54
imie_osoby27-Jan-12 10:54 
Questionproblem in running Pin
amitc18930-Nov-11 6:34
amitc18930-Nov-11 6:34 
AnswerRe: problem in running Pin
Alberto Venditti4-Dec-11 6:09
Alberto Venditti4-Dec-11 6:09 
Questionplz help Pin
karan rana16-Nov-11 5:16
karan rana16-Nov-11 5:16 
AnswerRe: plz help Pin
Alberto Venditti22-Nov-11 22:57
Alberto Venditti22-Nov-11 22:57 
Questioncan not start the PingMonitorService Pin
N.Zh14-Jul-11 22:08
N.Zh14-Jul-11 22:08 
AnswerRe: can not start the PingMonitorService Pin
Alberto Venditti15-Jul-11 7:39
Alberto Venditti15-Jul-11 7:39 
GeneralRe: can not start the PingMonitorService Pin
N.Zh21-Jul-11 22:24
N.Zh21-Jul-11 22:24 
GeneralRe: can not start the PingMonitorService Pin
N.Zh21-Jul-11 22:35
N.Zh21-Jul-11 22:35 
GeneralRe: can not start the PingMonitorService Pin
Alberto Venditti24-Jul-11 22:48
Alberto Venditti24-Jul-11 22:48 
GeneralRe: can not start the PingMonitorService Pin
N.Zh17-Aug-11 22:59
N.Zh17-Aug-11 22:59 
GeneralRe: can not start the PingMonitorService Pin
Alberto Venditti18-Aug-11 3:46
Alberto Venditti18-Aug-11 3:46 
GeneralRe: can not start the PingMonitorService Pin
Antonio Garcia Cruz21-Aug-11 9:36
Antonio Garcia Cruz21-Aug-11 9:36 
GeneralRe: can not start the PingMonitorService Pin
Alberto Venditti21-Aug-11 21:25
Alberto Venditti21-Aug-11 21:25 
Thank you for your contribution!
AV

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.