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

In part 1 and 2, we learned how to use a custom health monitor page to keep an eye on all important aspects of our Sitecore servers and take them out of a load balancer in case something is wrong. In the final part of our tutorial, we are going to learn how to actually run this task on a schedule so it monitors our server continuously.

Let’s start by creating this scheduled task manually. Then, we’ll learn how to create this task automatically during a deployment using Octopus. So, let’s go ahead and open Task Scheduler (Windows Key + r, and run taskschd.msc) in our CD server. Within the left panel of the Task Scheduler, we can choose the location for our new task. Since our task will run a Powershell script, navigate to: \Microsoft\Windows\PowerShell\ScheduledJobs and right click in the ScheduledJobs folder and select Create Task.

In the General tab of the Create Task window, we will give our task a name and description:

In the Triggers tab, we’ll specify how often we want our script to run. So, lets click in New and select “Repeat Task Every…”. Here, we can choose our desired interval. (make sure that duration is set to “indefinitely”):

Finally, under the Actions tab, we’ll select Powershell.exe as our Program/Script (No need to specify the full path to the executable). And, under parameters, we’ll add the following string. This will register our PS script so it runs in the background without a PS window popping up:

-NoLogo -NonInteractive -WindowsStyle Hidden [PATH TO OUR SCRIPT]\HealthMonitor.ps1

Hit OK and our Scheduled task will be created! It will run our Health Monitor PS script every 5 minutes indefinitely in the background.

BONUS:

This method is adequate if we only have to create this task in a few CD servers and there’s no risk of losing our schedules tasks (through servers maintenance, etc). However, if we have many CD servers and we deploy our sites using automated deployment tools, like Octopus, we can create this scheduled task as a part of our deployment process.

This is accomplished by adding a step in our Octopus Process that will execute a Powershell script that will register our job in the Task Scheduler. This also allows us to use Octopus variables to specify folder paths, intervals, etc. Let’s create this script that we are going to call from Octopus:

param([string]$jobName, [string]$scriptPath, [string]$scriptInterval)
clear

$jobExists = Get-ScheduledJob -Name $jobName -ErrorAction Ignore

if($jobExists) {
    Write-Host "Scheduled Job Already Exists"
} 
else 
{
    # Trigger
    $atStartupeveryFiveMinutesTrigger = New-JobTrigger -once -At $(get-date) -RepetitionInterval $([timespan]::FromMinutes($scriptInterval)) -RepeatIndefinitely

    Register-ScheduledJob -Name $jobName -FilePath $scriptPath -Trigger $atStartupeveryFiveMinutesTrigger
}

As you can see, we can add the job name, script location, and interval as parameters so that we can change them directly through Octopus. So, finally, let’s add a step in our Octopus deployment process to call this script like this:

In line 2, we can see how we are calling our new script using our desired parameters.

To summarize, this tutorial came from the need to keep a close eye in the status of Sitecore CD servers and respond accordingly depending on their status. Sometimes, pinging a server or trying to load some content from a CD server doesn’t give us the whole picture of the health of the server. It’s important to understand all the different aspects a CD server requires to operate correctly For instance, even though a server might be serving content correctly, it might have lost connection to MongoDB or xConnect and analytics might not be working as expected. So, we might want to take the server out of the load balancer.

Since most of this tutorial involves Powershell and a standalone aspx health monitor page, we can always expand or modify the features of this monitor and adapt it to our specific needs. Hope this tutorial was helpful and it helps you maintain some happy and healthy Sitecore servers. 😉

Leave a Reply

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