• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 371
  • Last Modified:

Script to check status of all websites on a webserver

Hello,

I am looking for a script that will output (to a txt file) the HTTP status results of all websites hosted on a particular webserver. Is there an easy script that could be create during VB, Powershell or the like?
0
Charlie_Melega
Asked:
Charlie_Melega
  • 6
  • 5
1 Solution
 
Dale HarrisProfessional Services EngineerCommented:
This is a great start:

http://dmitrysotnikov.wordpress.com/2008/05/29/monitor-web-site-availability/

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 = "$URL down"
        $body = "$URL site is down. Details: $($_.Exception)"
        $smtpServer = "192.168.1.1"
        $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"


You can also do a 5 minute interval test on the site with an email sending when they go down.  And you can test multiples:

For ($i;$i -lt 10;$i++){
Test-Site "http://powergui.org"
Test-Site "http://www.google.com"
Test-Site "www.thisisnotavalidsite.com"
Start-Sleep 300 #seconds
$i = 1
}

HTH,

Dale Harris
0
 
Charlie_MelegaAuthor Commented:
Thanks for the post. I have been trying to edit this for my needs but as I am not a VB Script expert, it's been a bit tough. For my needs, I do not need the email action. I need to write each web site status output from that single web server to a text file.

Thanks for any additional thoughts.

Charlie
0
 
Dale HarrisProfessional Services EngineerCommented:
Okay we can do that too.  Here's the way we would write it:

##########################################################
# 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{
        $Success = $False
        "Failed. Details: $($_.Exception)"
        "$URL site is down. Details: $($_.Exception)" >> "ErrorLog.txt"
        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
}

This basically just takes out the code for the email and appends it to a text file located in the same directory as you're in when you're running Powershell.  It will output nothing if it's up.

This is provided you actually have each URL for your web sites in your single web server.

And just so you're aware, this is written with Powershell, but it can be copy and pasted directly into your Powershell console and it will become a function that is in memory temporarily.

If you need more info on Powershell and where to start:
http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/A_4327-PowerShell-Where-do-I-start.html

HTH,

Dale Harris
0
Improved Protection from Phishing Attacks

WatchGuard DNSWatch reduces malware infections by detecting and blocking malicious DNS requests, improving your ability to protect employees from phishing attacks. Learn more about our newest service included in Total Security Suite today!

 
Charlie_MelegaAuthor Commented:
Thanks very much Dale for the quick feedback. I copied the above into a .ps1 file and all seemed to work correctly but I did not receive any output. In reviewing the script, it doesn’t appear I need to provide any variable input and that "errorlog.txt" should have generated into the folder where I ran the powerShell script. Here is an image below with websites stopped. Are there any user required input to this script?:

 pshell test
0
 
Dale HarrisProfessional Services EngineerCommented:
Once the function is created, you have to use a command to make it work:

Test-Site is the function.

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

HTH,

Dale Harris
0
 
Charlie_MelegaAuthor Commented:
So does the powerShell script create the “Test=Site” function. If so, I don’t see evidence of this.  Apologies but I am not quite sure why I can't run this as a PowerShell script as shown in the graphic.   After setting the correct execution policy, the script appears to process. I've run PowerShell scripts before using the syntax shown so I assume this script cannot be run as a simple .ps1 file?
0
 
Dale HarrisProfessional Services EngineerCommented:
You can do the following:

#Script to be saved as .ps1 follows
##########################################################
# 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{
        $Success = $False
        "Failed. Details: $($_.Exception)"
        "$URL site is down. Details: $($_.Exception)" >> "ErrorLog.txt"
        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
}

For ($i;$i -lt 10;$i++){
Test-Site "http://powergui.org"
Test-Site "http://www.google.com"
Test-Site "www.thisisnotavalidsite.com"
Start-Sleep 300 #seconds
$i = 1
}

########################
#End Script

This will loop over and over again every 5 minutes and append to the text file created in your directory you're running the script from (C:\Scripts)

HTH,

Dale Harris
0
 
Charlie_MelegaAuthor Commented:
Excellent,  I assume if I remove the 2 lines.....

Start-Sleep 300 #seconds
$i = 1

.....this will eliminate the 5 minute pause on this task?  

And lastly, I noticed that the script is designed to check specific web sites. Is it possible for the script to "dynamically"  check for the status of all websites on that web server? Essentially, there would be no need to specify any particular web site as the script would check status of all by default. I have been looking at some the links you provided trying to edit the script to support this.

Thanks again for the tremendous help.
0
 
Dale HarrisProfessional Services EngineerCommented:
To answer your question, yes, if you take that out, there's no delay.

If you want to go the route of getting all website dynamically, there's a Microsoft IIS 7 Snapin to run for Powershell:
http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=7436

I haven't tried it myself.

HTH,

Dale Harris
0
 
Charlie_MelegaAuthor Commented:
Excellent assistance, Thank You Dale.
0
 
Charlie_MelegaAuthor Commented:
I do have a follow up to this item. Everything is fully functional an integrated but the one issue we are running into is that the output file (errorlog.txt) is actually creating a .txt file which is using UTF encoding. I have a separate function that tries to read this file but fails as it is not in ASCII encoding format. Is there any edit that can be made to the line in this Powershell script;

 $URL site is down. Details: $($_.Exception)" >> "ErrorLog.txt"

so that the errorlog.txt file is created in ASCII format?


Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now