Click here to Skip to main content
Click here to Skip to main content

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

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

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

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

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

  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

Share

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.

Comments and Discussions

 
QuestionMAC Address Information to be included. PinmemberMember 111796412hrs 24mins ago 
QuestionSQL database server PinmemberArzmeen14-Jun-13 20:06 
AnswerRe: SQL database server PinmemberAlberto Venditti14-Jun-13 21:36 
GeneralMy vote of 5 Pinmemberxilione30-May-13 0:22 
Questioninstallutil.exe PinmemberSarathy806-Nov-12 0:42 
AnswerRe: installutil.exe PinmemberAlberto Venditti6-Nov-12 2:52 
QuestionHow to setup the project Pinmembereddiechits3-Jun-12 23:40 
QuestionCan we have mailing functionalities as an addon here. PinmemberPrabhu884-Apr-12 19:44 
QuestionE bello, mi piace Tuo programma Pinmemberimie_osoby27-Jan-12 10:54 
Questionproblem in running Pinmemberamitc18930-Nov-11 6:34 
sir, I downloaded your code and tried to run it but i encountered with the following error can u tell what should i do?
the error occurred was:
An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
 
and also can you send some screenshot showing how to run the code. same thing is required by kiran rana. do response soon sir?
AnswerRe: problem in running PinmemberAlberto Venditti4-Dec-11 6:09 
Questionplz help Pinmemberkaran rana16-Nov-11 5:16 
AnswerRe: plz help PinmemberAlberto Venditti22-Nov-11 22:57 
Questioncan not start the PingMonitorService PinmemberN.Zh14-Jul-11 22:08 
AnswerRe: can not start the PingMonitorService PinmemberAlberto Venditti15-Jul-11 7:39 
GeneralRe: can not start the PingMonitorService PinmemberN.Zh21-Jul-11 22:24 
GeneralRe: can not start the PingMonitorService PinmemberN.Zh21-Jul-11 22:35 
GeneralRe: can not start the PingMonitorService PinmemberAlberto Venditti24-Jul-11 22:48 
GeneralRe: can not start the PingMonitorService PinmemberN.Zh17-Aug-11 22:59 
GeneralRe: can not start the PingMonitorService PinmemberAlberto Venditti18-Aug-11 3:46 
GeneralRe: can not start the PingMonitorService PinmemberAntonio Garcia Cruz21-Aug-11 9:36 
GeneralRe: can not start the PingMonitorService PinmemberAlberto Venditti21-Aug-11 21:25 
Questioninstallutil.exe - Installing the Service PinmemberJavedWahid21-Feb-09 17:29 
AnswerRe: installutil.exe - Installing the Service PinmemberAlberto Venditti23-Feb-09 23:14 
GeneralGood work Alberto, PinmemberAzharkp11-Nov-08 23:22 
GeneralNot about the topic PinmemberAnderson Olympio14-Apr-08 3:32 
GeneralThank-you Pinmemberwired20-Nov-07 10:05 
GeneralRe: Thank-you PinmemberChillAmmar26-Jun-09 19:45 
Generalimprovement PinmemberXin_Yu6-Nov-07 1:02 
GeneralRe: improvement PinmemberAlberto Venditti7-Nov-07 9:17 
QuestionNetwork Monitor Pinmemberurpiyush00729-Sep-07 1:54 
AnswerRe: Network Monitor PinmemberAlberto Venditti30-Sep-07 21:00 
Generalping Pinmemberghweston1231-Sep-07 21:24 
Questionafter ping,handle window service Pinmemberkk_upadhyay25-Jul-07 0:13 
AnswerRe: after ping,handle window service PinmemberAlberto Venditti9-Aug-07 4:24 
Generalmonitor w/ email Pinmemberdavid_george24-Jul-07 15:29 
GeneralRe: monitor w/ email PinmemberAlberto Venditti9-Aug-07 4:31 
Questionvb dot net Pinmemberramayis23-Jul-07 2:36 
AnswerRe: vb dot net PinmemberAlberto Venditti8-Aug-07 0:17 
GeneralVery Nice PinmemberPaul Conrad22-Jul-07 18:58 
GeneralBello, ma... PinmemberAntonello Provenzano4-Jul-07 6:09 
GeneralC# version PinmemberAlberto Venditti4-Jul-07 10:25 
GeneralRe: C# version PinmemberKentuckyEnglishman10-Jul-07 7:33 
GeneralRe: C# version Pinmemberc.barca13-Jul-07 10:08 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 20 Jul 2007
Article Copyright 2007 by Alberto Venditti
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid