Enhanced Microsoft IIS health checks using VBscript

By default, the load balancer uses a TCP connect to the port defined in the Virtual Service to verify the health of the real (backend) servers. For IIS this would typically be port 80. In many cases this kind of health check is adequate but for IIS this if often not the case. When a particular IIS site is stopped, whilst it’s not possible to perform an HTTP GET, it’s often possible to still be able to perform a TCP connect to the port. This means that this basic type of health check cannot be relied on for IIS. The load balancer can also be configured to perform an HTTP negotiate check that verifies that IIS can actually serve web content and this is sufficient in most cases.

However, you may also want to check that other applications and services are running correctly before reporting that the server is OK. Microsoft VBscript is a powerful tool that can be used to do exactly this.

The script below gives an example of how VBscript can be used to determine the state of applications and services running on the server.

 

In this example, the script checks if:

a) it’s possible to make a successful HTTP request to the local server

b) the associated application pool is in the running state

Once a) and b) have been checked, either OK or FAILED is written to the text file c:inetpubwwwrootcheck.txt. The load balancer then regularly reads this file to determine if the server is healthy and able to receive and process inbound requests.

 

Script overview:

1. Setup values

2. Attempt an HTTP read

3. Check application pool is running

4. Write status (either OK or FAILED) to the check.txt file

5. Wait for 5 seconds

6. Goto step 2 and repeat

 

The script:

' setup values
' define the application pool to check
PoolToCheck = "ROB"
' set page to check to be the local server
PageToCheck = "http://localhost/"
' loop every 5 seconds
CheckFrequency = 5000
 
' establish connection to the WMI provider
Set oWebAdmin = GetObject("winmgmts:rootWebAdministration")
 
' set the pool to check
Set oAppPool = oWebAdmin.Get("ApplicationPool.Name='" & PoolToCheck & "'")
 
' set the filesystem object
Set fso = CreateObject("Scripting.FileSystemObject")
 
' set the http object
set website = CreateObject("MSXML2.ServerXMLHTTP")
 
' start check loop
Do While True
On Error Resume Next
 
' request the web page
website.Open "GET", PageToCheck, False
website.Send ""
 
' test the site & set the site error flag
If Err.Number <> 0 Then
	SiteError = True
Else If website.StatusText = "OK" Then
	SiteError = False
End If
End If
 
'write to the check file but only when state changes to minimize repetative disk writes
If oAppPool.GetState <> 1 Or SiteError then
	If Not FailWritten Then
		Set CheckFile = fso.OpenTextFile("C:inetpubwwwrootcheck.txt", 2, True)
	        CheckFile.WriteLine "FAILED"
		CheckFile.Close
		FailWritten = True
		PassWritten = False
	End If
Else
	If not PassWritten Then
		Set CheckFile = fso.OpenTextFile("C:inetpubwwwrootcheck.txt", 2, True)
        	CheckFile.WriteLine "OK"
		CheckFile.Close
		PassWritten = True
		FailWritten = False
	End if
End If
 
' loop round again
Wscript.sleep CheckFrequency
Loop

 

Configuring the Web Servers

Simply configure each IIS server to run this script at startup. The script can be auto started by placing a shortcut in the appropriate startup folder. Once started, the script runs on each server in an infinite loop updating the status every 5 seconds. The script can be ended using task manager to stop the associated wscript.exe process. If preferred, the loop can be removed and the script can be called on a regular basis using Windows Task Scheduler.

 

Configuring the Load Balancer

To configure the load balancer to read this file, simply set the Virtual Services health check (either layer 4 or layer 7) to be a negotiate check where Request to Send is set to check.txt and Response Expected is set to OK.

 

And that’s it! More detailed scripts can be created if needed, just make sure that the status is regularly written to the check.txt file so the load balancer is aware of the servers state.

 

 

 

 

 

You Can Learn More About the LoadBalancer.org’s Product Line By Going to www.LoadBalancerSolutions.com/LoadBalancer-org

The original article/video can be found at Enhanced Microsoft IIS health checks using VBscript

Leave a Reply