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: , , , , , , , ,

About these ads

25 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 eric October 4, 2010 at 10:27 am

      Hello,

      Thanks for these informations,

      Where do you fix the URL to test (At which line)
      And in the case of multipules URL ?

      Im a begginer in PS.

      Regards,

      Eric

      • 4 Dmitry Sotnikov October 4, 2010 at 4:38 pm

        The URL is the function parameter and is being supplied in the call at the end of the script:

        Test-Site “http://powergui.org”

        Just change this to yours.

  3. 5 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. 6 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. 7 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

    • 8 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. 9 Dmitry Sotnikov November 23, 2009 at 8:03 am

    Thanks MadLogik!

  7. 10 Igor March 11, 2010 at 8:52 pm

    this is exactly what I was looking for.

    How ever iam getting this error while sending the email:

    Exception calling “Send” with “4″ argument(s): “The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated”
    At O:\test\rendertest.ps1:12 char:19
    + $smtp.Send <<<< ($emailFrom, $emailTo, $subject, $body)
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

    Also I have multiple websites to test, is there a way to test 10 sites or more. maybe get-content from sites.txt?

  8. 11 Igor March 11, 2010 at 11:57 pm

    nvm i figured it out.

    $smtp.Credentials = New-Object System.Net.NetworkCredential(“username”,”password”)

    • 12 Dmitry Sotnikov March 12, 2010 at 10:28 am

      Igor, good catch! In my environment I was already at my office desktop authenticated in AD so did not have to explicitely provide username and password.

      Thanks for finding the issue and providing the fix!

      Dmitry

  9. 13 Igor March 12, 2010 at 5:15 pm

    Im having problems with this code. Please help!
    here is what i want to do. get a list of sites. if any give errors, send an email with the error. All other that work, put into a table and send a seperate email.

    • 14 Igor March 12, 2010 at 5:16 pm
      function Test-Site {
      	BEGIN{}
      	PROCESS{
      	$URL = $_	
      	$webclient = New-Object Net.WebClient
          $webdata = $webclient.DownloadString($URL)
      	if ($webdata -ne $nulll) 
      		{#Write-Host `t "$URL is good" -ForegroundColor Cyan
      		GoodRender}
      	else 
      		{#return $false
      		  SendBadRenderMail}
      	}
      	END {}
       }
      
      Set-ExecutionPolicy RemoteSigned
      Get-Content o:\Test\sites.txt | test-site
      
      • 15 Igor March 12, 2010 at 5:18 pm

        OUTPUT: (suppose to give me false for the bad sites)

        PS O:\test> .\rendertest.ps1
                 http://microsoft.com is good
        Exception calling "DownloadString" with "1" argument(s): "The remote name could not be resolved: 'badsite'" At O:\test\rendertest.ps1:9 char:41
        +     $webdata = $webclient.DownloadString <<<< ($URL)
            + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
            + FullyQualifiedErrorId : DotNetMethodException
        
                 http://badsite:8080 is good
        Exception calling "DownloadString" with "1" argument(s): "The remote server returned an error: (502) Bad Gateway." At O:\test\rendertest.ps1:9 char:41
        +     $webdata = $webclient.DownloadString <<<< ($URL)
            + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
            + FullyQualifiedErrorId : DotNetMethodException
        
                 http://badsite.com:123 is good
        PS O:\test> 
      • 16 Dmitry Sotnikov March 17, 2010 at 10:32 am

        Igor,

        Your code is failing where it should indeed fail. :) For invalid/offline site $webclient.DownloadString($URL) does NOT return $null – instead it throws an exception.

        In my original code (see the blog post above) I used trap to catch it and report unavailable site. (In PowerShell 2.0 you can also use try/catch statement).

        Change your script to catch the exception (instead of testing for $null) and you should be fine.

        Sorry for delayed response. Forums at http://powergui.org are really a much better way to get answers fast. I am a bit slow on responding to comments and email.

        Dmitry

  10. 17 eric October 4, 2010 at 11:07 am

    Hello,

    It’seems to be not working for HTTPS URL §

    Eric

    • 18 Dmitry Sotnikov October 4, 2010 at 4:42 pm

      Eric,

      Seems to work for https for me. For example this worked just fine (Google supports both HTTP and HTTPS connections):

      Test-Site “https://www.google.com”

      Dmitry

  11. 19 Kanna November 11, 2010 at 6:50 am

    This worked for me, nice one ……..

    Thanks a lot for Sharing this information.

  12. 20 Gowrish February 15, 2012 at 6:33 am

    Hi All,

    I am unable to get the infromation for a link. I am getting some syntax error.

    $webclient = New-Object Net.WebClient;$webdata = ${webclient.DownloadString(https://stbap701.gsp.accenture.com/portal/login/login.do)} if ($webdata -ne $nulll) {#Write-Host `t “$URL is good”} else {#return $false}

    Please correct me where I am wrong

  13. 21 Anonymous August 30, 2012 at 4:39 pm

    Thanks you. It’s simple and work best. I had been trying to create something like this in SCOM with so much time and still can make it work.

    awsome


  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
  3. 3 Automate Website Monitoring : Trisummit Technologies Trackback on March 29, 2010 at 2:23 am
  4. 4 PowerShell scripts to monitor web server crash eventhang and send notification emails | MSDN Blogs Trackback on June 21, 2012 at 11:59 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s




My Recent Tweets

RSS My company’s blog

  • Fun at HostingCon 2013
    The smell of the hosting industry’s finest is in the air, and of course we sent some of the Jelastic team to attend and proudly exhibit at HostingCon 2013 this week in Austin, Texas.  From what I have been reading, there are more than 50 scheduled sessions from Sales and Marketing, Technology, Issues and Trends and [...]The post Fun at HostingCon 2013 appear […]
  • Secure Access to Your Jetty Web Application
    Today’s post focuses on the web application security related features of Jetty app server. After reading this article you should be able to configure security realms to provide authentication and access control for your Jetty web application, as well as to grant access to your app for dedicated IP-addresses only. Before we start let’s examine what Jetty real […]
  • Software Stacks Market Share: May 2013
    We are back to update you with the latest trends in software stacks popularity for May 2013. This time we decided to collect the numbers in a different way to get more accurate statistics. As you remember previously we counted the number of the environments, where each software stack was used. We’ve changed the reporting [...]The post Software Stacks Market […]
  • Setting Up a Cronjob in Jelastic Cloud
    Cronjob allows you to configure regularly scheduled tasks so that the jobs can be run automatically at a pre-set point of time. It repeats itself and does not need any regular manual instructions. Cron automates your system and can be used for quite different purposes. This wonderful tool is a standard part of all sysadmins toolkit. Also cronjob has a [...]T […]
  • How to Deploy Joget Cluster into the Cloud
    Joget Workflow is an open source platform that allows you to build enterprise web applications easily, due to its rich set of tools. It is also a rapid application development platform that provides complete agile development capabilities, including consisting of processes, forms, lists, CRUD and UI; not just back-end EAI/orchestration/integration or the tas […]
  • Liferay Cluster in the Cloud
    Liferay Portal is one of the most popular Java CMSs in the world due to its impressive ease-of-use. Since we published the tutorial on Liferay deployment to the cloud we have seen an extremely positive reaction from its community. Also we have received multiple requests from Liferay fans asking about clustering, replication and fail-over capabilities in the […]
  • Mark Zbikowski Veteran Microsoft Architect Joins Jelastic
    Jelastic already has an impressive team of advisers including Serguei Beloussov, the founder of Parallels. We also have technical geniuses who endorse and use Jelastic including the father of Java James Gosling, David Blevins who founded the TomEE project, and Michael “Monty” Widenius, the author of the original version of the open-source MySQL database and […]

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 former employer - Quest Software, or my current employer - Jelastic or anyone else for that matter. All trademarks acknowledged.

© 2007-2013 Dmitry Sotnikov

Pages

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

Follow

Get every new post delivered to your Inbox.

Join 67 other followers

%d bloggers like this: