Test Web Host Reliability with HostTracker





5.00/5 (2 votes)
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.
<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.
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.
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.
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.