Enhanced Microsoft IIS health checks using VBscript

Microsoft Published on 3 mins Last updated

By default, the load balancer uses a TCP connect to the port defined in the Virtual Service to verify the health of each real (backend) server. For IIS, this would typically be port 80. In many cases this kind of health check is adequate but for IIS this is 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 be configured to perform a more comprehensive HTTP negotiate check which 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 aim of the script below is to demonstrate how this is done. It illustrates 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 (N.B. the file is only written when the status changes to reduce repetative disk writes)
  5. Wait for 5 seconds
  6. Goto step 2 and repeat

The script:

' setup values
' define the application pool to check
PoolToCheck = "DefaultAppPool"
' 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:root\WebAdministration")
' 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:\inetpub\wwwroot\check.txt", 2, True)
CheckFile.WriteLine "FAILED"
CheckFile.Close
FailWritten = True
PassWritten = False
End If
Else
If not PassWritten Then
Set CheckFile = fso.OpenTextFile("C:\inetpub\wwwroot\check.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

You can either configure each IIS server to run this script at startup by copying/pasting the script into a check.vbs file and placing it in a suitable startup folder, or if preferred, the loop can be removed and the script can be called on a regular basis using Windows Task Scheduler. If configured to run in a loop, by default the script runs every 5 seconds. The script can be ended using task manager to stop the associated wscript.exe process. All default settings are set at the top of the script, and should be changed to suit your requirements. Default settings are:

PoolToCheck = "DefaultAppPool"
PageToCheck = "http://localhost/"
CheckFrequency = 5000

NOTE: Make sure that the IIS WMI provider is installed by selecting the IIS Management Scripts and Tools Role Service component under Management Tools (or Web Management Tools). If it's not, you'll probably receive WMI related errors because the root\WebAdministration namespace cannot be found.

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.