CREATING A SMART HEALTH MONITOR FOR SITECORE IN LOAD BALANCED SERVERS- PART 2

In part 1 of our tutorial, we learned how to add a health monitor page into our Sitecore instance, as well as creating a very basic powershell script to read the status of this page. In this part, we are going to go further into our script to take a bad CD server out of our load balancer cluster.

At this point, our script can read whether our CD server is healthy or not. If it is, this is good enough. However, if it’s unhealthy, we need to take measures. In this case, we are going to tell the load balancer to take the server offline so our other CD servers can serve the content instead. We could also set some kind of alerts so we know that the server was taken offline (we don’t want to keep running our sites without knowing that a server was taken offline and the rest of the servers are taking all the load).

In this case, we are going to replace our app_online.html file in our Sitecore webroot with an app_offline.html. This way, the load balancer knows that it shouldn’t redirect traffic to this specific server. (double check how your load balancer works to take a server offline) So, lets update our PS script with the following code:

$healthMonitorAddress = 'http://127.0.0.1/Health/HealthMonitor.aspx?tags=cd'
$healthMonitorResponse = $null
$webRoot = '[PATH TO OUR SITECORE WEBROOT]'
$successString = 'APP_ONLINE'
$fileOnline = 'app_online.html'
$fileOffline = 'app_offline.html'
$fileLog = 'Health\log.txt'

#Get our response from our HealthMonitor.aspx page
Try
{
    $healthMonitorResponse = Invoke-WebRequest -Uri $healthMonitorAddress -UseBasicParsing -Headers @{"Cache-Control"="no-cache"} -ErrorAction Continue
}
Catch
{
    $healthMonitorResponse = $_
}

#Write our page response to the log
Add-Content -Path (Join-Path -Path $webRoot -ChildPath $fileLog) -Value "Web Request Response Body:" -PassThru
Add-Content -Path (Join-Path -Path $webRoot -ChildPath $fileLog) -Value $healthMonitorResponse.ToString() -PassThru

#Check for our healthy string in the response body
if ($healthMonitorResponse.ToString() -match $successString)
{
    #HEALTHY APPLICATION
    Add-Content -Path (Join-Path -Path $webRoot -ChildPath $fileLog) -Value 'Application Healthy' -PassThru
    if (-Not (Test-Path -Path (Join-Path -Path $webRoot -ChildPath $fileOnline)))
    {
        #Write to log and make sure app_online.html is in webroot
        write-output "online" | out-file (Join-Path -Path $webRoot -ChildPath $fileOnline) -Encoding ascii
    }
    #Remove app_offline if present
    if ((Test-Path -Path (Join-Path -Path $webRoot -ChildPath $fileOffline)))
    {
        Remove-Item -Path (Join-Path -Path $webRoot -ChildPath $fileOffline) -Force
    }
}
else
{
    #UNHEALTHY APPLICATION
    Add-Content -Path (Join-Path -Path $webRoot -ChildPath $fileLog) -Value 'Application Down' -PassThru
    if (-Not (Test-Path -Path (Join-Path -Path $webRoot -ChildPath $fileOffline)))
    {
        #Write to log and add app_offline.html to webroot
        write-output "offline" | out-file (Join-Path -Path $webRoot -ChildPath $fileOffline) -Encoding ascii
    }
    #Remove app_online
    if ((Test-Path -Path (Join-Path -Path $webRoot -ChildPath $fileOnline)))
    {
        Remove-Item -Path (Join-Path -Path $webRoot -ChildPath $fileOnline) -Force
    }
}

NOTE: Update the corresponding variables at the top of the script to accommodate your Sitecore instance. And, make sure so use the ‘?tags=cd‘ in our Health Monitor Page URL so we only make the checks corresponding to a CD server role.

Health Monitor Page for a healthy CD server

In our final part of our tutorial, we are going to create a windows scheduled task to make sure that this script runs a given schedule in our server. And as a bonus, we’ll explore how we can deploy this feature automatically to our environments though octopus. So, keep calm and stay sitecoring. 😉

Leave a Reply

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