Home > Scripting, Windows Server 2003 > KiXtart script to check for website availability

KiXtart script to check for website availability

February 19th, 2009 Leave a comment Go to comments

At work there is an old web application that is managed offsite by a hosting company. It runs on Oracle 9iAS. Apparently there is an issue with Oracle 9i and the web service will frequently crash (due to a known caching problem) taking the application offline.

Rather than spending money on the software and licensing required to upgrade both Oracle and the Operating System (as it's being replaced anyway) the company made a decision to live with the issue for the time being. I decided to write a KiXtart script to monitor the website and let me know of any issues it may incur.

The following script uses two other applications:

BMail - A command line SMTP mailing tool
WebResult - A command line URL result code tool

Full credit to Craig Peacock for BMail and John Mueller for WebResult.

Here is the script:

;===============================================
;===============================================
;
; Remove temp files
;
;===============================================
;===============================================

IF EXIST ("result.txt")
DEL ("result.txt")
ENDIF

IF EXIST ("status.txt")
DEL ("status.txt")
ENDIF

;===============================================
;===============================================
;
; Check/record http status in result.txt
;
;===============================================
;===============================================

$website = "http://www.jjclements.co.uk"

SHELL "%comspec% /c " + chr(34) + "webresult.exe " + $website + chr(34) + " >> result.txt"

;===============================================
;===============================================
;
; Open result.txt and read into variable
;
;===============================================
;===============================================

getfilecontents("result.txt")

;===============================================
;===============================================
;
; Explode variable into array
;
;===============================================
;===============================================

$myarray = SPLIT ($result, " ", -1)

;===============================================
;===============================================
;
; Declare global variable to store desc
;
;===============================================
;===============================================

$errordesc = ""

;===============================================
;===============================================
;
; Check each array element against codes
;
;===============================================
;===============================================

$element = $myarray[1]
SELECT

CASE $element = "0"
$errordesc = "The current status is: Error " + $element + " - Website Unavailable"

CASE $element = "400"
$errordesc = "The current status is: Error " + $element + " - Bad Request"

CASE $element = "403"
$errordesc = "The current status is: Error " + $element + " - Forbidden"

CASE $element = "404"
$errordesc = "The current status is: Error " + $element + " - Not Found"

CASE $element = "405"
$errordesc = "The current status is: Error " + $element + " - Method Not Allowed"

CASE $element = "406"
$errordesc = "The current status is: Error " + $element + " - Not Acceptable"

CASE $element = "408"
$errordesc = "The current status is: Error " + $element + " - Request Timeout"

CASE $element = "409"
$errordesc = "The current status is: Error " + $element + " - Conflict"

CASE $element = "410"
$errordesc = "The current status is: Error " + $element + " - Gone"

CASE $element = "500"
$errordesc = "The current status is: Error " + $element + " - Internal Server Error"

CASE $element = "501"
$errordesc = "The current status is: Error " + $element + " - Not Implemented"

CASE $element = "502"
$errordesc = "The current status is: Error " + $element + " - Bad Gateway"

CASE $element = "503"
$errordesc = "The current status is: Error " + $element + " - Internal Server Error"

CASE $element = "504"
$errordesc = "The current status is: Error " + $element + " - Gateway Timeout"

CASE $element = "505"
$errordesc = "The current status is: Error " + $element + " - HTTP Version not supported"

CASE 1
EXIT

ENDSELECT

;===============================================
;===============================================
;
; Threshold - Times to retry + interval
;
;===============================================
;===============================================

$numberofretries = "2"
$latencyseconds = "10"

IF $numberofretries > "0"

DO

SLEEP $latencyseconds

IF EXIST ("result.txt")
DEL ("result.txt")
ENDIF

SHELL "%comspec% /c " + chr(34) + "webresult.exe " + $website + chr(34) + " >> result.txt"

getfilecontents("result.txt")

$mytemparray = SPLIT ($result, " ", -1)

$tempelement = $mytemparray[1]

IF NOT $element = $tempelement
EXIT
ENDIF

$currentretry = $currentretry + 1

UNTIL $numberofretries = $currentretry

ENDIF

;===============================================
;===============================================
;
; Email Server Status
;
;===============================================
;===============================================

$emailserver = "someemailserver"
$toaddress = "james" + chr(64) + "somedomain.com"
$fromaddress = "serverfailure" + chr(64) + "somedomain.com"
$subject = "Server Issue"

;Form email body
shell "%comspec% /c echo. >> status.txt"
shell "%comspec% /c echo. >> status.txt"
shell "%comspec% /c echo WebServer is currently offline >> status.txt"
shell "%comspec% /c echo. >> status.txt"
shell "%comspec% /c echo " + $errordesc + " >> status.txt"
shell "%comspec% /c echo. >> status.txt"
shell "%comspec% /c echo Please restart the WebServer application to ensure SLAs are adhered to! >> status.txt"

;Form email command
$emailcommand = "bmail -s " + $emailserver + " -t " + $toaddress + " -f " + $fromaddress + " -h -a " + chr(34) + $subject + chr(34) + " -m " + "status.txt" + " -c"

SHELL ($emailcommand)

;=======================================
; Get File Contents
;=======================================

Function getfilecontents($filesource)

OPEN (1, "$filesource")
$result = READLINE (1)
CLOSE (1)

EndFunction

;=======================================

This script obviously requires a few variables to be modified to get it to work in another environment. The following will need to be altered:

$website - you will need to enter your own website you want to monitor here (you must use http://)
$numberofretries - the number of times the script will test the website before emailing an error
$latencyseconds - the time in seconds before each retry (if $numberofretries is NOT set to 0)
$emailserver - the name of your email server (the server running this script must be allowed to relay through it)
$toaddress - the address you want the email alert to go to
$fromaddress - the (spoof) address you want the email to appear to come from
$subject - the subject line of the email

The only other thing you may want to change is the message contained within the email (underneath ';Form email body'). I have a few instances of this script monitoring different servers so I have customised the email body for each server to inform me of the server name and IP address etc.

As mentioned the script makes use of a threshold although it isn't used if you set $numberofretries = "0". The threshold sets the number of times to try the website before sending an email. You can also set the delay between each retry (in seconds) by setting a value for $latencyseconds.

I use a scheduled task on a Windows Server 2003 to run this script every 5 minutes. When there is a problem with the web application I am alerted to it and can respond as necessary. One thing to be aware of, if you set your scheduled task to run every 5 minutes but you set the script to retry say 3 times with a latency of 120 seconds (total 360 seconds - 6 minutes) then the scheduled task will try to run again before the script has finished causing problems.

To use this:

1) Save the above script as kixtart.kix in a directory containing the kixtart application
2) Save webresult.exe and bmail.exe (available from the links above) in the same directory
3) Modify variables as appropriate
4) Make sure that the server running the script can relay email through your email server
5) Test by running the script manually and/or create the scheduled task

Alternatively, download the above script and necessary files - HERE

The end result is an email that tells you the exact http error code and description when your web application becomes unavailable and meets any threshold that may be set.

Categories: Scripting, Windows Server 2003 Tags: ,
  1. No comments yet.

*

code