CheckUrl Groovy Script

What is CheckUrl?

CheckUrl is a script created to ping a website to check if it is giving a 200 response in a favorable response time.

Use Case

The perfect use case for this script is to check until a site comes online after a deploy. If it doesn't get a 200 response after a specified interval, then the deploy can be considered a failure. Works great for deploying a test or production site.

Where is it?

The script is transferred to the /var/lib/jenkins/shared_ansible location on each machine provisioned. This ensures there is a consistent location on Jenkins Master and all slaves to enable seamless builds across the environment. The script does not run indefinitely; it runs for a specified amount of tries and will exit with a failure or success depending on the Final Response.

How to Use

groovy and curl must be installed to run the script. Luckily these are already installed when provisioning with Ansible.

To get help with figuring out the arguments to pass when running the script, you can use the -h argument to see instructions.

$ groovy checkurl.groovy -h

usage: checkurl.groovy -[hmst] [url]
 -h,--help                 Show usage information
 -m,--max-time <maxTime>   max time in seconds before killing a curl
                           command, Default: 20
 -s,--sleep <sleep>        time in seconds before next retry, Default: 4
 -t,--tries <tries>        number of tries before exit, Default: 3

Example

The example below pings a site every minute and attempts 12 tries before exiting.

$ groovy /var/lib/jenkins/shared_ansible/checkurl.groovy -m 30 -s 60 -t 12 http://ec2-54-221-51-114.compute-1.amazonaws.com

Checking 'http://ec2-54-221-51-114.compute-1.amazonaws.com' with: 
  12 tries 
  30 seconds timeout per try 
  60 seconds interval before next try 

LETS ROCK!
Response is 404: not satisfactory ... executing retry (11 left)
elapsed time: 0.359 seconds 

Response is 404: not satisfactory ... executing retry (10 left)
elapsed time: 1 minutes, 0.480 seconds 

Response is 404: not satisfactory ... executing retry (9 left)
elapsed time: 2 minutes, 0.512 seconds 


(0) Final Response is 200: site is ready!
elapsed time: 3 minutes, 4.586 seconds

The (0) in this line (0) Final Response is 200: site is ready! is the exit code.