Having a Watchdog enabled Load Balancer work with SharePoint

By | 2012-08-16

For a project of mine, I wanted to have the Load Balancer check if SharePoint was still working as part of the Load Balance configuration. This is done with a WatchDog on the Load Balancer side. This WatchDog performs a HTTP Request [HEAD] on a URL to check if the HTTP STATUS CODE is [200] (OK).
If the Url the WatchDog uses point to a Page residing within SharePoint and not just a flat file on a IIS folder, we can actually verify of SharePoint (kind of) works.
So I created a [status.aspx] Page, and uploaded this to a SharePoint Document Library.

How to deploy a List and Page?
Step 1: Create List in the RootWeb of the Root SiteCollection of your Web Application

Write-Host -f Yellow " Getting RootWeb for: $url"  -NoNewline
$web = Get-SPWeb $url
Write-Host -f Green " .. OK" 

Write-Host -f Yellow " Creating new DocLib: $title"  -NoNewline
# Create the List
$list = $web.Lists.Add($title ,$description,$listTemplate)
Write-Host -f Green " .. OK" 

Write-Host -f Yellow " Setting Properties for DocLib: $title" -NoNewline
$list = $web.Lists[$title ]

$list.NoCrawl = $true
$list.OnQuickLaunch = $false
$list.Followable = $false
$list.EnableVersioning = $false

$list.EnableFolderCreation = $false
$list.EnableMinorVersions= $false
$list.EnableModeration = $false

Write-Host -f Green " .. OK"

Step 2: Add a page to the new List

Write-Host -f Yellow " Uploading status.aspx to DocLib"  -NoNewline

# Get instance of RootFolder
$folder = $web.GetFolder($title)

# Get the file on Disk that we want to upload
$file = Get-Item .status.aspx

# Upload the file.
$spfile = $folder.Files.Add($title +"/status.aspx",$file.OpenRead(), $false)

$spfile.Item["Title"] = "Systeem status pagina voor de load balancer"
Write-Host -f Green " .. OK"

Step 2a: Add a WebPart for kicks, just in case users end up opening the page:

Write-Host -f Yellow " Adding WebPart to status.aspx"  -NoNewline

# Add WebPart
$webpartmanager=$web.GetLimitedWebPartManager($spfile.Url, $PersonalizationScopeShared )

$webpart = New-Object Microsoft.SharePoint.WebPartPages.ContentEditorWebPart  
$webpart.ChromeType = $ChromeTypeTitleOnly
$webpart.Title = $spfile.Item["Title"]

[string]$content = "<p>Dit is een systeem pagina. U kunt via deze <a href=""/"" title=""Homepage"">link</a> terug naar de homepage.</p><div id='healthscore'></div>"
$docXml = New-Object System.Xml.XmlDocument
$contentXml = $docXml.CreateElement(“Content”)
$webpart.Content = $contentXml

$webpartmanager.AddWebPart($webpart, "FullPage", 0); 
Write-Host -f Green " .. OK"

Step 3: Setup the Load Balancer Watchdog to poll for the new Page

You now have a Page with Url: $siteCollectionURL/LoadBalancer/Status.aspx that your Watchdog can use to check if SharePoint still “works”. You can even check the HTTPRESPONSE Header: X-SharePointHealthScore to do smart load balancing.

Preview of the [status.aspx] page with an added js based status indicator:

Javascript for this status information:

var httpRequest = new XMLHttpRequest();
var httpHeader = "X-SharePointHealthScore";

function GetScore(){
    // Do the request and wait for it to complete.
    httpRequest.open("HEAD", window.location, true);
    httpRequest.onreadystatechange = OnStateChange

function OnStateChange () {
    if (httpRequest.readyState == 0 || httpRequest.readyState == 4) {
        var healthScore = httpRequest.getResponseHeader(httpHeader);  
        var healthscorediv;
        healthscorediv = document.getElementById("healthscore");
        var hcolor= "green";
	var htext = "goed";
        if ( healthScore>4 ) {
            hcolor= "orange";
	    var htext = "druk";
        if ( healthScore>7) {
            hcolor= "red";
	var htext = "heel erg druk";
        healthscorediv.innerHTML = "Huidige Health Score is: " + healthScore.toString() + " ("+htext+").";

// add to onload stack:

Download [status.aspx] Page and PowerShell script

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.