Monitor web-site availability

Did you know that you can use PowerShell to monitor your website and send you alarms when something goes wrong? We had availability issues with our community site and I was quite surprised that the 20-line (!) PowerShell script did the job!

Basically, all I had to do was use the Net.WebClient object and its DownloadString method to query the page (with some proxy handling code I got from Alexey Chuikov), and trap any exception which it generates when something goes wrong. The trap is using our internal relay server to send me and everyone who is involved in the site administration the email.

Here’s the code:

##########################################################
# Test-Site - script to test web site availability
# and notify in case of any issues
# (c) Dmitry Sotnikov
# http://dmitrysotnikov.wordpress.com
##########################################################

function Test-Site {
    param($URL)
    trap{
        "Failed. Details: $($_.Exception)"
        $emailFrom = "my.email@address.com"
        # Use commas for multiple addresses
        $emailTo = "my.email@address.com,another.admin@address.com"
        $subject = "PowerGUI.org down"
        $body = "PowerGUI web site is down. Details: $($_.Exception)"
        $smtpServer = "smtp.server.to.use.for.relay"
        $smtp = new-object Net.Mail.SmtpClient($smtpServer)
        $smtp.Send($emailFrom, $emailTo, $subject, $body)
        exit 1
    }
    $webclient = New-Object Net.WebClient
    # The next 5 lines are required if your network has a proxy server
    $webclient.Credentials = [System.Net.CredentialCache]::DefaultCredentials
    if($webclient.Proxy -ne $null)     {
        $webclient.Proxy.Credentials = `
                [System.Net.CredentialCache]::DefaultNetworkCredentials
    }
    # This is the main call
    $webclient.DownloadString($URL) | Out-Null
} 

Test-Site "http://powergui.org"

To test it you can obviously just put an invalid URL into the call.

Once I had the script running, I just set up a scheduled task in Windows Task Scheduler to run the script every 15 minutes:
Windows Task Scheduler with a PowerShell task

One trick I learned from MoW and used in the task, was using the -command parameter (rather than just supplying the script) and including the exit $LASTEXITCODE into the command, so the exit code from the PowerShell script gets registered as the scheduled task result.

So here’s the command-line I have scheduled:

c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Noninteractive -command ". c:\scripts\test-site.ps1; exit $LASTEXITCODE"

Works flawlessly! And can save you tons of money on a monitoring solution. Talk about ROI from learning PowerShell! ;)

Tags: , , , , , , , ,

9 Responses to “Monitor web-site availability”


  1. 1 Server Monitoring August 15, 2008 at 2:15 pm

    That script looks pretty interesting. The problem I have with local solutions is that I would have to run my Windows box 24/7 and configure it to send me a notification whenever my websites become unavailable.

  2. 2 dmitrysotnikov August 15, 2008 at 3:33 pm

    Yes, this is an obvious limitation. At the same time specifically with the task of monitoring web site availability I actually like that this script is running remotely (actually from another network) so network-related problems such as DNS or routing get caught as well.

  3. 3 joe January 20, 2009 at 11:14 pm

    Have a look at http://www.mywebkpi.com which does web kpi monitoring around the clock for free. Try the live demo here http://www.mywebkpi.com/cgi-bin/demo-cgi to see how websites can be monitored without scripts and without software or config changes. Talk about ROI here for investing nothing and getting your site monitored.

    works great for me

  4. 4 JKav February 6, 2009 at 12:12 am

    Well I don’t see any limitations :=) Actually, I will be using this for a daily checkout script to report that critical intranet sites are up. The .Proxy and .CredentialCache was driving me crazy and this was a huge help. This ole SysAdmin is gonna have to learn .Net. Thank you very much Dmitry!

  5. 5 David Johnson March 4, 2009 at 5:25 pm

    Whilst looking for a script to monitor website availability I found another option using kixtart here:

    http://www.jjclements.co.uk/index.php/2009/02/19/kixtart-script-to-check-for-website-availability

    It seems to also report the actual error codes that may be invoked when there is a webserver issue

    • 6 MadLogik November 21, 2009 at 2:47 pm

      Thanks for this code!
      I modified it to simply return true or false,
      and to add the http:// if it’s not there.

      The only thing I’m missing is a timeout for the webclient.
      I found a few examples with a dowhile loop in powershell and timespans… but I need something better

      Here’s your modified code Mr.
      I don’t understand the parm so I skipped it…

      [CODE]
      function mad-TestSite
      {
      if ($args.count -eq 1)
      {
      $erroractionpreference = “SilentlyContinue”
      $URL = $Args[0]
      if ($URL -like “http://*”)
      {
      #write-host “URL has the http://”
      }
      else
      {
      #write-host “URL does not have the http://”
      $URL = “http://” + $URL
      }
      $webclient = New-Object Net.WebClient
      # The next 5 lines are required if your network has a proxy server
      $webclient.Credentials = [System.Net.CredentialCache]::DefaultCredentials
      if($webclient.Proxy -ne $null)
      {
      $webclient.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
      }
      # This is the main call
      $webdata = $webclient.DownloadString($URL)
      if ($webdata -ne $null)
      {return $true}
      else
      {return $false}
      }
      else
      {
      write-host “Usage: mad-TestSite http://thewebsite.com or thewebsite.com”
      }
      }
      [/CODE]

  6. 7 Dmitry Sotnikov November 23, 2009 at 8:03 am

    Thanks MadLogik!


  1. 1 Brian Nettles » Blog Archive » Automate Website Monitoring Trackback on August 27, 2008 at 2:57 pm
  2. 2 run as command | keyongtech Trackback on January 18, 2009 at 5:25 pm

Leave a Reply




View Dmitry Sotnikov's profile on LinkedIn

Follow Dmitry Sotnikov at Twitter

My Recent Tweets

Archives

See you at:

Legal

The posts on this blog are provided “as is” with no warranties and confer no rights. The opinions expressed on this site are mine and mine alone, and do not necessarily represent those of my employer Quest Software or anyone else for that matter. All trademarks acknowledged.

© 2007 Dmitry Sotnikov

Pages

 

May 2008
M T W T F S S
« Apr   Jun »
 1234
567891011
12131415161718
19202122232425
262728293031