Click here to Skip to main content
15,885,875 members
Articles / Programming Languages / C# 3.5

Test Web Host Reliability with HostTracker

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
26 Jul 2012CPOL2 min read 16.5K   133   7   1
Visits to configured web sites in every configured interval and logs connectivity.

Introduction

Does your website seem to be down more often than you would like? Are you getting the run around when you contact support? HostTracker provides you with the log files you can send to your web host documenting your connectivity issues. 

Background

I wrote HostTracker specifically to address problems I was having with my webhost.

The Solution

The HostTracker Visual Studio 2008 project contains every thing you need to log connectivity via a Windows service.

Configuration is very easy. Shown below Url1 is the web site to be tested, Url2 is for comparison purposes, the host providers main web site is a good choice. If your connection to the internet fails, then both Urls will fail. However, if Url2 passes and Url1 fails, that would likely indicate a problem with your web host's server.

XML
<configuration>
 <appSettings>
  <add key="TestMode" value="N" />
  <add key="Url1" value="http://godaddy.hugetiger.com/Time.aspx" />
  <add key="Url2" value="http://www.godaddy.com                " />
  <add key="WaitMillisecs" value="300000" />
 </appSettings>
</configuration>

Testing and debugging a windows Service can be tricky.

For simplicity, I include a TestMode variable in the configuration file to run the software as an easy to debug console application, or a Windows Service as shown below.

C#
static void Main() 
{
 BasicUtil bu = new BasicUtil();
 bool bTest = bu.StringToBool(ConfigurationManager.AppSettings["TestMode"]);
 if (bTest)
 {
  Console.WriteLine("Start of Main (HostTracker)");
  TestHostTracker hw = new TestHostTracker();
  hw.TestOnStart();				
  Console.WriteLine("To run as a Service change TestMode in HostTracker.exe.config to N");
  Console.WriteLine("======== Press Key to end program");
  Console.ReadKey();
  hw.TestOnStop();
 }
 else
 {
  ServiceBase[] ServicesToRun = new ServiceBase[] { new HostTracker() };
  ServiceBase.Run(ServicesToRun);
 }
}

The ProjectInstaller class method InitializeComponent() code segment below is used during service installation to set the initial service parameters which can be changed via the services control panel icon.

C#
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;

this.HostTrackerInstaller.ServiceName = "HostTrackerServic";
this.HostTrackerInstaller.Description = "Visits 2 configured web sites every interval";
this.HostTrackerInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;

I took ProjectInstaller.cs and ProjectInstaller.Designer.cs from a previous project. It appears that this code is somehow auto generated in some cases. However, I simply copied the code and modified it as needed.

The class WebScrape inherits from Webclient, method GetWebPage shown below, reaches out to the passed URL and retrieves the web page html, and the time span needed to do so, and returns a bool to indicate success or failure.

C#
public bool GetWebPage(String pUrl, String pPost, out String pPageHtml, out TimeSpan ts)
{
 pPageHtml = String.Empty;
 bool rv = false;
 objRequest = (HttpWebRequest)WebRequest.Create(pUrl);
 if (pPost == String.Empty)
 {
  objRequest.Method = "GET";
 }
 else
 {
  objRequest.Method = "POST";
 }
 objRequest.ContentLength = pPost.Length;
 objRequest.ContentType = "application/x-www-form-urlencoded";
 String s = @"Mozilla/4.0+(compatible;+MSIE+8.0;+Windows+NT+6.0;+Trident/4.0";
 s += @";+SLCC1;+.NET+CLR+2.0.50727;+.NET+CLR+1.1.4322;";
 s += @"+.NET+CLR+3.5.30729;+.NET+CLR+3.0.30729)";

 // the user agent identifies the web host about browser details, such as type and version
 // of browser application and operating system/
 objRequest.UserAgent = s;

 if (pPost != String.Empty)
 {
  try
  {
   myWriter = new StreamWriter(objRequest.GetRequestStream());
   myWriter.Write(pPost);
  }
  catch (Exception)
  {
   //WriteLine(e.Message);
  }
  finally
  {
   myWriter.Close();
  }
 }
 DateTime dtStart = DateTime.Now;
 HttpWebResponse objResponse = null;
 try
 {
  objResponse = (HttpWebResponse)objRequest.GetResponse();
 }
 catch (Exception)
 {
  ts = new TimeSpan();
  return rv;
 }
 DateTime dtFin = new DateTime();

 //Check out the html.
 Stream aStream = objResponse.GetResponseStream();
 if (aStream != null)
 {
  using (StreamReader sr = new StreamReader(aStream))
  {
   pPageHtml = sr.ReadToEnd(); // page html goes to string pPageHtml
   dtFin = DateTime.Now;
   sr.Close();
   rv = true;
  }
 }
 ts = dtFin - dtStart;
 return rv;
}

Installing the Service

To install use: InstallUtil.exe     HostTracker.exe

To uninstall use: InstallUtil.exe /u HostTracker.exe

or use InstallService.bat below.

rem uninstall HostTracker
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe /u "C:\path\HostTracker.exe"
rem install   HostTracker
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe    "C:\path\HostTracker.exe"
pause

You will need to edit the path in InstallService.bat

Points of Interest

HostTracker is written in a style that I like and feel is appropriate for small projects for a sole programmer. Other styles of organization may be better for a team programming effort or for larger projects.

It was fun writing this software, documenting it, and authoring this article for codeproject.com.

Tested on Windows 7 and MS Server 2003.

History

First version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Bachelors of Electrical Engineering 1981. Strengths are Software Project Management, Automated Commodities Trading, Defense - Radar Guided Missile Systems (Amraam). Webmaster - Design, e-commerce, database driven websites. Banking - Treasury Operations, FX, Cryptographically Secure Communications. Security Clearance: Secret, US DoD. Expertise in OOD, OOP, C# .NET,MS SQL Server, Oracle & Sybase, C++, VB, Java.

I learned fortran in school, C#, SQL and C++ were pretty much self taught.

I’m seeking contracts.

http://www.hugetiger.com/Contact.aspx

There is a direct positive correlation between freedom and prosperity.

Comments and Discussions

 
GeneraltagBeep.com Pin
Uwe Keim26-Jul-12 18:36
sitebuilderUwe Keim26-Jul-12 18:36 

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.