Web Health Check

Web-Health-Check.ps1 is a PowerShell script to monitor a website and automatically take remedial action if the site is not functioning properly. You can download the latest version here.

It works by retrieving a web page from a specified URL and checking the page for the presence of a specific piece of text.

All parameters, such as the URL to be tested and the required text, can be coded into the script or passed as arguments from the command line.

The script is designed to work with virtual machines running in a VMware ESX environment. However, it could easily be modified to work in a simple Windows environment.

Please post a “Response” using the form below to report any bugs or suggestions for improvements.

Advantages
It has several advantages over VMware’s HA feature. Firstly, it handles web servers that have frozen but where VMware Tools is still running.

Secondly, it handles websites that are driven by a back-end database and the database has crashed. In this case, the web server would likely still be running and would still serve a page, but the page would be a basic error report such as “MySQL database is unavailable.”

How it works
If the web page is not available or the specified text not present on the page, the script checks to see if VMware Tools are running inside the VM. If so, the script attempts to gracefully reboot the VM.

If VMware Tools are not running, the script forces a reset – which is rather like pressing the power button on a physical server – which causes the VM to reboot. Finally, the script sends an email to inform the server administrator exactly what happened.

Working with Windows Task Scheduler
The script is designed to be run at a regular interval, such as every 10 minutes, using Windows built in Scheduled Tasks utility.

This is an example of a suitable command to use in Scheduled Tasks:

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe " & "C:\web-health-check.ps1 vc-server -testpattern XYZ"

You could add other parameters to the command line, such as URL or the name of the VM using command line arguments such as -url and -CHECKVM.

Change log

  • Version 1.0
    First version, seems to work well in my environment. If you use Task Scheduler, ensure you configure Task Scheduler with user credentials that have the necessary administrator privileges in to log in to VMware vCenter.

Leave a Response