views:

67

answers:

7

Hello,

I've created a Java application that is split in different subcomponents, each of those runs on a separate Tomcat instance. Also, some components use a MySQL db through Hibernate.

I'm now creating an administration console where it's reported the status of all my Tomcat instances and of MySQL. I don't need detailed information, but knowing if they are running or not it's enough.

What could be the best solution to do that?

Thanks

A: 

Firing a simple fixed query through MySQL

SELECT 'a-ok';

and have the .jsp return that a-ok text. If it times out and/or doesn't respond with a-ok, then something's hinky. If you need something more detailed, you can add extra checks, like requesting now() or something bigger, like SHOW INNODB STATUS.

Marc B
I think the OP needs to clarify whether the admin console is a GUI or a web app. If its a web app and the hosted server is down, a JSP will not do.
Vineet Reynolds
True, but then whatever's doing the ping can time out and report that the server's down. I monitor my servers using wget and a round-trip query script of this type.
Marc B
A: 

The easiest thing is to look for the MySQL and Tomcat PID files. You need to look at your start scripts to make sure of the exact location, but once you find it, you simply test for existence of the pid file.

Charlie Martin
But what if Apache and/or Tomcat died and didn't clean up after themselves? You'd get a false positive 'up', due to stale pid files lying around.
Marc B
Or what if the MySQL or Tomcat processes are still there, but they have stopped responding to requests for some reason?
Stephen C
+1  A: 

Most straightforward way would be to just connect the server and see if it succeeds.

MySQL:

Connection connection = null;
try {
    connection = DriverManager.getConnection(url, username, password);
    // Succes!
} catch (SQLException e) {
    // Fail!
} finally {
    if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
}

Tomcat:

try {
    new URL(url).openConnection().connect();
    // Succes!
} catch (IOException e) {
    // Fail!
}

If you want a bit more specific status, e.g. checking if a certain DB table is available or a specific webapp resource is available, then you have to fire a more specific SELECT statement or HTTP request respectively.

BalusC
A: 

Create a servlet as a status page. In the servlet perform a cheap query, if the query succeeds let the servlet print OK otherwise Error. Put the servlet into a war and deploy it to all instances.

This could be used for checks in yor admin console by doing a loop over all instances.

stacker
+1  A: 

I would make sure that what ever monitoring you setup is actually exercising some code. Monitoring the JVM via jmx can also be helpful after the fact. Check out http://www.cacti.net/ .

Thomas Vincent
A: 

I'd create a simple REST webservice that runs on each Tomcat instance and does a no-op query against the database. That makes it easy to drive from anywhere (command line, web app, GUI app, etc.)

If these are publicly available servers you can use a service like binarycanary.com to poll a page or service in your app.

Javid Jamae
A: 

I assume that you know the ports of which are running in advance (or from configuration files). The easiest way to check is to make socket connections to those ports like a telnet program does. Something like:

public boolean isServerUp(int port) {
    boolean isUp = false;
    try {
        Socket socket = new Socket("127.0.0.1", port);
        // Server is up
        isUp = true;
        socket.close();
    }
    catch (IOException e)
    {
        // Server is down
    }
    return isUp;
}

Usage:

isTomcatUp = isServerUp(8080);
isMysqlUp = isServerUp(3306);

However, I would say that is a false-negative check.. Sometimes it says server UP but the server is stuck or not responding...

instcode