Web Site (PowerShell) monitor template that allows customized healthy status codes

More than a few of us have run into a situation in which we need to monitor a web site that requires authentication, but cannot provide credentials for various reasons. In these cases, the monitored site returns a 401 (Unauthorized) status code, and Orion alerts that the site is down. I created this template because the built-in HTTP and HTTPS monitors do not provide a way to mark specific status codes as OK.

This template monitors a web site using PowerShell's Invoke-WebRequest cmdlet. It passes the node 'Caption' property to the script arguments, and this be modified to use another name-related property if necessary. The script body follows:

Web Site (PowerShell).png

Within the script, the statusCodesAllowed array is populated with 200 and 401. Any status code that you add to this array will be considered healthy by the monitor. Assuming a simple hostname-based URL and assuming HTTPS, the ${Node.Caption} argument is passed into the $serverName variable, and then used in the URI as https://$serverName. This can easily be customized to fit your needs.

In the Invoke-WebRequest cmdlet, the following optional parameters are also used:

  • -Method Head - Reduces bandwidth and processing by requesting only headers and not the full body of the web page.
  • -UseDefaultCredentials - Uses the credentials of the current user (Orion service) to send the web request.
  • -UseBasicParsing - uses the response object for HTML content without Document Object Model (DOM) parsing. This parameter is required when Internet Explorer is not installed on the computers, such as on a Server Core installation of a Windows Server operating system. This is the default behavior for PowerShell 6.

When the request is successful, the script is able to return the status code and description directly from the request results. When the web request fails (e.g. a 401 response), the Catch block takes over and processes the response. The status code is returned in the request's error message, and appears to always be written between parenthesis in a message that describes the status. The error message is parsed to get the 3-digit status code, and the code is then evaluated to see if the statusCodesAllowed array contains the current status code. This result of this evaluation is a Boolean that gets cast as an integer: 1 for true (OK) and 0 for false (NOT OK). Finally, the script cleans up its variables simply to be a good citizen and help prevent memory leaks.

Back in the template settings, the script output is returned as a statistic called "Status". I evaluate my status thresholds as follows:

  • Warning: not equal to 1 (OK) for a single poll
  • Critical: not equal to 1 (OK) for at least 3 polls

ScriptOutput.png

This template gets assigned to nodes that I usually create specifically for each web site. (This is usually because our sites are often load balanced and I might already be monitoring each server-specific URL on the server nodes themselves.)

And finally, we can now monitor web sites with a customizable list of HTTP status codes that we consider to be healthy! Happy monitoring. Ping me if you have any questions about customizing this monitor.

Inspired by: sbarger​, sage4man​, sans​, akhasheni​, irby​, and st.crispan​!