TCPPort Monitor

Topics: Developer Forum
Aug 26, 2006 at 9:36 PM
I have developed a simple but useful monitor to check if specific port is open on remote host.

using System;
using System.Net;
using System.Xml;
using BytesRoad.Net.Sockets;
using Microsoft.VisualBasic;

namespace PolyMon
{
namespace Monitors
{
/// <summary>
/// This minitor will try to connect to a specific port on a remote host
///
/// The XML definition for this monitor's settings is specified as follows:
/// <TcpPortMonitor>
/// <Host>localhost</Host>
/// <Port>80</Port>
/// <Timeout>2000</Timeout>
/// </TcpPortMonitor>
///
/// OK if port is open
///
/// This class uses a smart socket library from BytesRoad http://www.bytesroad.com/NetSuit/default.aspx
/// </summary>
public class TcpPortMonitor : MonitorExecutor
{
public TcpPortMonitor(int MonitorID) : base(MonitorID)
{
}

/// <summary>
/// Monitors the test.
/// </summary>
/// <param name="StatusMessage">The status message.</param>
/// <param name="Counters">The counters.</param>
/// <returns></returns>
protected override MonitorStatus MonitorTest(ref string StatusMessage, ref CounterList Counters)
{
string Host = null;
int TCPPort = 0;
int Timeout = 0;

XmlNode RootNode = MonitorXML.DocumentElement;
string NodeValue = null;

try
{
// Host
Host = ReadXMLNodeValue(RootNode.SelectSingleNode("Host"));

// TCPPort
NodeValue = ReadXMLNodeValue(RootNode.SelectSingleNode("TCPPort"));
TCPPort = Convert.ToInt32(NodeValue);

// Timeout
NodeValue = ReadXMLNodeValue(RootNode.SelectSingleNode("Timeout"));
Timeout = (NodeValue == null ? 2000 : Convert.ToInt32(NodeValue));

// Check One Port

DateTime StartTime = DateTime.Now;

IPAddress hostaddress = Dns.GetHostEntry(Host).AddressList0;
IPEndPoint iep = new IPEndPoint(hostaddress, TCPPort);

SocketEx sck = new SocketEx();
sck.ConnectTimeout = Timeout;

sck.Connect(iep);

if (sck.Connected) StatusMessage = "OK";

DateTime EndTime = DateTime.Now;

Double ElapsedSecs = EndTime.Subtract(StartTime).TotalSeconds;

//Add Load time to counters
Counters.Add(new Counter("ConnectTime", ElapsedSecs));

// Passed Test
StatusMessage = "OK.";
return MonitorStatus.OK;
}
catch (Exception ex)
{
StatusMessage = ex.Message + Constants.vbCrLf + ex.InnerException.Message;
return MonitorStatus.Fail;
}
finally
{
RootNode = null;
}
}

/// <summary>
/// Reads the XML node value.
/// </summary>
/// <param name="myXMLNode">My XML node.</param>
/// <returns></returns>
private string ReadXMLNodeValue(XmlNode myXMLNode)
{
if (myXMLNode == null)
{
return null;
}
else
{
if (myXMLNode == null || myXMLNode.InnerXml == null)
{
return null;
}
else
{
return myXMLNode.InnerXml.Trim();
}
}
}
}
}
}

Hope it is useful.
Coordinator
Aug 29, 2006 at 1:24 PM
Thanks!! I will incorporate this as a new monitor type into a future release.
Fred.
Coordinator
Aug 29, 2006 at 1:24 PM
This discussion has been copied to Work Item 2696. You may wish to continue further discussion there.
Coordinator
Aug 31, 2006 at 5:24 PM
This monitor has been added to PolyMon and is now part of the Beta 3 release. Thanks to Marco for the idea and initial implementation.

Please note that I did ended up not using the suggested TCP library because of the extra overhead this library would have added (and also because I was not sure if the provided binary was .NET 1.x or 2.x and I was unable to recompile it easily - my C# just isn't good enough :-)).
TCPPort Monitor only needed a tiny fraction of the functionality contained in that great library and hence I decided to write my own asynchronous tcp socket sonnection code.

If you are interested in that socket library that Marco used in his code, it is called BytesRoad. It looks like an outstanding library and can be found here:
http://www.bytesroad.com/NetSuit/default.aspx

Note that if you prefer Marco's version of the TCP Port monitor you can still use his dll for the monitor and since I retained the same XML format for the parameters, you could still use the TCPPortMonitor Editor dll (simply register TCP Port Monitor in PolyMon manager with his dll for the monitor and the new editor dll for the editor).

Thanks,
Fred.