Click here to Skip to main content
13,353,322 members (53,286 online)
Click here to Skip to main content
Add your own
alternative version


100 bookmarked
Posted 4 Jul 2007

Ping Monitor

, 20 Jul 2007
Rate this:
Please Sign up or sign in to vote.
Yet another Ping Monitor utility written in VB.NET on .NET Framework 2.0


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

    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,

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

    Host varchar(50) NOT NULL,
    Status varchar(5) NOT NULL,
    RecordingDate datetime NOT NULL,
        RecordingDate ASC)

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).

' 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:

Private Function HostIsAlive(ByVal Host As String) As Boolean
  Dim pingSender As New Ping
  Dim reply As PingReply
    reply = pingSender.Send(Host)
    If reply.Status = IPStatus.Success Then
      Return True
      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:

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


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.


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.


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


About the Author

Alberto Venditti
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.

You may also be interested in...


Comments and Discussions

QuestionMAC Address Information to be included. Pin
Member 1117964125-Oct-14 9:27
memberMember 1117964125-Oct-14 9:27 
AnswerRe: MAC Address Information to be included. Pin
Alberto Venditti29-Oct-14 1:37
memberAlberto Venditti29-Oct-14 1:37 
QuestionSQL database server Pin
Arzmeen14-Jun-13 21:06
memberArzmeen14-Jun-13 21:06 
AnswerRe: SQL database server Pin
Alberto Venditti14-Jun-13 22:36
memberAlberto Venditti14-Jun-13 22:36 
GeneralMy vote of 5 Pin
xilione30-May-13 1:22
memberxilione30-May-13 1:22 
Questioninstallutil.exe Pin
Sarathy806-Nov-12 1:42
memberSarathy806-Nov-12 1:42 
AnswerRe: installutil.exe Pin
Alberto Venditti6-Nov-12 3:52
memberAlberto Venditti6-Nov-12 3:52 
QuestionHow to setup the project Pin
eddiechits4-Jun-12 0:40
membereddiechits4-Jun-12 0:40 
QuestionCan we have mailing functionalities as an addon here. Pin
Prabhu884-Apr-12 20:44
memberPrabhu884-Apr-12 20:44 
QuestionE bello, mi piace Tuo programma Pin
imie_osoby27-Jan-12 11:54
memberimie_osoby27-Jan-12 11:54 
Questionproblem in running Pin
amitc18930-Nov-11 7:34
memberamitc18930-Nov-11 7:34 
AnswerRe: problem in running Pin
Alberto Venditti4-Dec-11 7:09
memberAlberto Venditti4-Dec-11 7:09 
Questionplz help Pin
karan rana16-Nov-11 6:16
memberkaran rana16-Nov-11 6:16 
can you tell me how to run this code. i tried to run but i m getting errors.

AnswerRe: plz help Pin
Alberto Venditti22-Nov-11 23:57
memberAlberto Venditti22-Nov-11 23:57 
Questioncan not start the PingMonitorService Pin
N.Zh14-Jul-11 23:08
memberN.Zh14-Jul-11 23:08 
AnswerRe: can not start the PingMonitorService Pin
Alberto Venditti15-Jul-11 8:39
memberAlberto Venditti15-Jul-11 8:39 
GeneralRe: can not start the PingMonitorService Pin
N.Zh21-Jul-11 23:24
memberN.Zh21-Jul-11 23:24 
GeneralRe: can not start the PingMonitorService Pin
N.Zh21-Jul-11 23:35
memberN.Zh21-Jul-11 23:35 
GeneralRe: can not start the PingMonitorService Pin
Alberto Venditti24-Jul-11 23:48
memberAlberto Venditti24-Jul-11 23:48 
GeneralRe: can not start the PingMonitorService Pin
N.Zh17-Aug-11 23:59
memberN.Zh17-Aug-11 23:59 
GeneralRe: can not start the PingMonitorService Pin
Alberto Venditti18-Aug-11 4:46
memberAlberto Venditti18-Aug-11 4:46 
GeneralRe: can not start the PingMonitorService Pin
Antonio Garcia Cruz21-Aug-11 10:36
memberAntonio Garcia Cruz21-Aug-11 10:36 
GeneralRe: can not start the PingMonitorService Pin
Alberto Venditti21-Aug-11 22:25
memberAlberto Venditti21-Aug-11 22:25 
Questioninstallutil.exe - Installing the Service Pin
JavedWahid21-Feb-09 18:29
memberJavedWahid21-Feb-09 18:29 
AnswerRe: installutil.exe - Installing the Service Pin
Alberto Venditti24-Feb-09 0:14
memberAlberto Venditti24-Feb-09 0:14 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 20 Jul 2007
Article Copyright 2007 by Alberto Venditti
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid