How would you define a web server as being “up”? It doesn’t just respond to a simple Internet Control Message Protocol (ICMP) echo request (ping) but more importantly, an HTTP monitor is showing as down. That’s where a PowerShell script comes in.
When monitoring systems, it’s important to track as close to the real service as possible. In a web server’s case, that service is an HTTP request.
A web request consists of lots of interdependent parts, including things like the server being connected to the network, having a MAC address, an IP address, a TCP port that’s listening on port 80, and, at the top of this stack, the HTTP status code that the server returns. It’s important to monitor as close to the service as possible, so let’s focus on the HTTP status code.
Table of Contents
Building the HTTP Monitor
There are numerous ways to monitor the status code returned by a web server. We’ll focus on rolling command-line script, which is an excellent way to incrementally build upon certain functionality.
To create these scripts, I always use PowerShell. It’s a great scripting language and the de facto standard in the Windows world.
Because PowerShell doesn’t natively have a command that can get the HTTP status code from a web server, we’ll need to take a standard approach and use a .NET object. And since a PowerShell script is ultimately built on top of the .NET framework, this is a trivial task.
To start off, we’ll need to use the System.Net.WebRequest object. In particular, you’ll need to call a static method called Create and pass the URL to this to build the request.
$req = [system.Net.WebRequest]::Create('http://www.google.com')
You also use PowerShell’s Invoke-WebRequest for this task. Check out the in-depth post here on the ATA blog for more info.
Making the Request
You can see above that I’m creating a web request for google.com. If you look at the request, you’ll see many common properties that will be used to make the request.
But the code doesn’t have much yet. It’s not actually an HTTP monitor just yet. I still need to make the actual request to Google’s web server and get the response. To do this, I’ll need to use the
GetResponse() method. This makes the request and returns the response from the web server.
$res = $req.GetResponse()
You can see now that all of the information returned. Notice that the
$res object has a
StatusCode property – this is what we’re looking for. At this point, we can either leave this as is and use this code in a larger script, or individually pull out the
StatusCode by referring to it in dot notation.
Assigning an Integer
This will return “OK,” but there’s no such thing as an “OK” HTTP status code. They’re unique integers. To get the actual HTTP status code for the HTTP monitor script, we’ll need to cast this property to an integer. This will return the actual HTTP status code.
Notice now we can get the real HTTP status code, which is 200.
PowerShell is a powerful scripting language that does a lot more than most sysadmins are aware of. And even if PowerShell doesn’t natively support a task, chances are it can still be done through native .NET objects.
Once you begin to create more code like this, you’ll inevitably find that this kind of code can be treated as building blocks. Continue learning small snippets of code like this, and you’ll have a large bag of script tricks that can be applied in numerous situations.
More from Adam The Automator & Friends
Find out how many of your Active Directory users are using leaked passwords by running a free read-only scan with Specops Password Auditor.
Why not write on a platform with an existing audience and share your knowledge with the world?
We've put together a list of the resources we, at ATA, can wholeheartedly recommend.