Monday, December 05, 2005

Checking your servers with Ant

Let's say you need to regularly check to make sure that several web servers are up and running. The little Ant script below shows how to do this. On a side note, it also illustrates Ant 1.6 "if" and "unless" conditionals. Add a top level target to call all of your hosts as dependencies.

The target will connect to a port (8080) and see if it is running. If it is up, then darya.server.available is set to not-null (the value doesn't matter) and everything is OK. If it is not running, then darya.server.available is null and the failure condition is applied. You run it with the command "ant test.darya.all".

This will also email you on target failure, assuming you have mail server running on the host that executes the ant script.

Set it up in a CRON job to run regularly.

<!-- These targets test darya -->
<target name="test.darya.ports">
<condition property="darya.server.available">
<socket server="" port="8080"/>

<target name="darya.success" if="darya.server.available" depends="test.darya.ports">
<echo message="Darya server is running"/>

<target name="darya.failure" depends="test.darya.ports" unless="darya.server.available">
<echo message="Darya server is down."/>
<mail mailhost=""
subject="Darya server is down">
<from address=""/>
<to address=""/>
Darya server is down.

<target name="test.darya.all" depends="darya.failure,darya.success"/>

No comments: