#!/bin/csh -f # check_web_site # ----------------------------------------------------------------------------- # Shell script to check whether a Web site is up. # ----------------------------------------------------------------------------- # Usage: See Usage section below or run with -h or --help option to see usage. # Assumptions: # Effects: # Notes: # Implementation Notes: # Portability Issues: # Revision History: # $Log$ # ----------------------------------------------------------------------------- if ("$1" == "" || "$1" == "-h" || "$1" == "--help") then echo "Usage: $0:t [options] [curl_options] URL_or_IP_Address" echo "Options:" echo "-h = Show this help text" echo "--help = Show this help text" echo "--show = Show the HTML page or other returned text" echo "--stats = Show time/size/speed stats" echo "--beep = Beep when an error occurs" echo "--dots = Show a dot for each successful attempt: ......" echo "--mail 'address address address...'" echo " = Send e-mail to specified addresses if an error occurs" echo "Curl_options: Any options accepted by curl. For example:" echo "--connect-timeout secs" echo "--max-time secs" echo "--head" echo "Examples:" echo " $0:t" echo " $0:t -h" echo " $0:t --help" echo " $0:t bristle.com" echo " $0:t --stats bristle.com" echo " $0:t --dots bristle.com" echo " $0:t --mail 'fred@bristle.com bob@bristle.com' bristle.com" echo " $0:t --connect-timeout 10 bristle.com" exit 1 endif set timestamp = `date "+%Y_%m_%d__%H_%M_%S"` set tempfile = /tmp/$0:t_$$_$timestamp set show_option = "-s -o /dev/null" set stats_option = "" set beep_cmd = "echo" set dots_option = "" set mail_addresses = "" set mail_cmd = "echo" # Explicitly handle common curl options to be able to find the URL # for use in messages. set connect_timeout_option = "" set max_time_option = "" while ($#argv > 0) if ("$1" == "-h" || "$1" == "--help") then shift # If help option was anywhere among the options, call recursively # with just that option, and exit. $0:t --help exit 1 else if ("$1" == "--show") then shift set show_option = "" else if ("$1" == "--stats") then shift set stats_option = "--stats" else if ("$1" == "--beep") then shift set beep_cmd = "~fred/bin/beep" else if ("$1" == "--dots") then shift set dots_option = "true" else if ("$1" == "--mail") then shift set mail_cmd = "mail -s" set mail_addresses = $1:q shift else if ("$1" == "--connect-timeout") then set connect_timeout_option = "$1 $2" shift shift else if ("$1" == "--max-time") then set max_time_option = "$1 $2" shift shift else # Not a recognized option. Assume it's the first curl param/option. break endif end set display_time = `date "+%Y/%m/%d %H:%M:%S"` set curl_cmd = "curl --fail ${show_option}" $curl_cmd -w "Time: ${display_time}\nResponse code: %{http_code}\nDNS time: %{time_namelookup}\nConnect time: %{time_connect}\nApp connect time: %{time_appconnect}\nPre-transfer time: %{time_pretransfer}\nRedirect time: %{time_redirect}\nStart transfer time: %{time_starttransfer}\nTotal time: %{time_total}\nNum redirects: %{num_redirects}\nDownload size: %{size_download}\nDownload speed: %{speed_download}\n" $connect_timeout_option $max_time_option $*:q > $tempfile set rc = $status if ("$stats_option" != "" || $rc != 0) then cat $tempfile endif rm $tempfile if ($rc == 0) then if ($dots_option != "" && $stats_option == "") then echo -n "." endif else if ($rc == 1) then set msg = "Curl error 1: Unsupported protocol: $*:q" $mail_cmd "$msg" $mail_addresses < /dev/null > /dev/null $beep_cmd "$msg" else if ($rc == 3) then set msg = "Curl error 3: Malformed URL: $*:q" $mail_cmd "$msg" $mail_addresses < /dev/null > /dev/null $beep_cmd "$msg" else if ($rc == 6) then # Make the message more useful on a phone which may truncate it #set msg = "Curl error 6: Unable to resolve URL: $*:q" set msg = "No DNS: $*:q" $mail_cmd "$msg" $mail_addresses < /dev/null > /dev/null $beep_cmd "$msg" else if ($rc == 7) then # Make the message more useful on a phone which may truncate it #set msg = "Curl error 7: Failed to connect to URL: $*:q" set msg = "No connect: $*:q" $mail_cmd "$msg" $mail_addresses < /dev/null > /dev/null $beep_cmd "$msg" else if ($rc == 22) then # Make the message more useful on a phone which may truncate it #set msg = "Curl error 22: HTTP error while retrieving page at URL: $*:q" set msg = "No HTTP: $*:q" $mail_cmd "$msg" $mail_addresses < /dev/null > /dev/null $beep_cmd "$msg" else if ($rc == 28) then # Make the message more useful on a phone which may truncate it #set msg = "Curl error 28: Timeout at URL: $*:q" set msg = "Timeout: $*:q" $mail_cmd "$msg" $mail_addresses < /dev/null > /dev/null $beep_cmd "$msg" else if ($rc == 56) then # Make the message more useful on a phone which may truncate it #set msg = "Curl error 56: Receive error at URL: $*:q" set msg = "No receive: $*:q" $mail_cmd "$msg" $mail_addresses < /dev/null > /dev/null $beep_cmd "$msg" # Add more of the specific curl errors from the curl man page, if they # start to be common. Otherwise, the catchall below is fine. else set msg = "Curl error ${rc}: See curl man page for details. URL: $*:q" $mail_cmd "$msg" $mail_addresses < /dev/null > /dev/null $beep_cmd "$msg" endif exit $rc