check_jdbc

Mike Gerber mgerber at leitwerk.de
Tue Mar 18 17:21:22 CET 2003


Hi,

we had to check an Oracle database and a IBM DB2 on AS/400, so we
decided to use JDBC. The check_jdbc plugin is a quick hack, but 
works for us and might be of use for someone else. Of course the
performance isn't really exciting. :-)

Remarks:

o Get java getopt from http://www.urbanophile.com/arenn/hacking/download.html

o Drop the JDBC drivers and java-getopt.jar into your java-lib-directory 
  and change the classpath in Makefile and the check_jdbc shell script.

o make all install

o Use the following commandline syntax:

check_command "$USER1$/check_jdbc -c 'com.ibm.as400.access.AS400JDBCDriver' \
-s 'jdbc:as400://host' -u user -p pass -q 'SELECT field FROM my.table'"
check_command "$USER1$/check_jdbc -c 'oracle.jdbc.driver.OracleDriver' \
-s 'jdbc:oracle:thin:@host:1521:instance' -u user -p pass \
-q 'SELECT field FROM table'"

Cheers,
Mike
-- 
------------------------------------------------------------------
Mike Gerber                             
Management Internet/Security Development

LEITWERK GmbH                               http://www.leitwerk.de
Im Ettenbach 13a                            Fon: +49 7805 918 0
77767 Appenweier                            Fax: +49 7805 918 200
------------------------------------------------------------------
-------------- next part --------------
#!/bin/sh
#--------------------------------------------------------------------------------------------------------------
# CVS:   $Id: check_jdbc,v 1.2 2003/03/17 18:17:22 mgerber Exp $
# Autor: Mike Gerber <mgerber at leitwerk.de>
# Start: Mon Mar 17 19:14:55 CET 2003
# Zweck: Checken von Datenbanken ?ber JDBC
#--------------------------------------------------------------------------------------------------------------
export PATH=$PATH:/usr/java/j2sdk1.4.1/bin
export CLASSPATH=/usr/java/j2sdk1.4.1/lib/oracle8i-8.1.7.1-jdbc-classes12.zip:/usr/java/j2sdk1.4.1/lib/jt400.jar:/usr/java/j2sdk1.4.1/lib/java-getopt-1.0.9.jar:.

cd `dirname $0`

java check_jdbc "$@"
-------------- next part --------------
//--------------------------------------------------------------------------------------------------------------
// CVS:   $Id: check_jdbc.java,v 1.2 2003/03/17 18:17:22 mgerber Exp $
// Autor: Mike Gerber <mgerber at leitwerk.de>
// Start: Mon Mar 17 19:14:55 CET 2003
// Zweck: Checken von Datenbanken ?ber JDBC
//--------------------------------------------------------------------------------------------------------------
import java.sql.*;
import gnu.getopt.*;

public class check_jdbc {
	
	static String db_classname, db_connstr, db_user, db_password, db_query;

	static void getargs(String[] args) {

		int c;

		Getopt g = new Getopt("check_jdbc", args, "c:s:u:p:q:");
		while ((c = g.getopt()) != -1) {
			switch(c) {
				case 'c': db_classname = g.getOptarg(); break;
				case 's': db_connstr   = g.getOptarg(); break;
				case 'u': db_user      = g.getOptarg(); break;
				case 'p': db_password  = g.getOptarg(); break;
				case 'q': db_query     = g.getOptarg(); break;
				default: break;
			}
		}

		if (db_classname == null || db_connstr == null || db_user == null
			|| db_password == null || db_query == null) {
			System.out.println("Wrong number of arguments");
			System.exit(2);
		}
	}


	public static void main(String[] args) {
		
		String exit_string;
		int    exit_code;

		try {
			// Get command line arguments

			getargs(args);

			// Load driver and connect to the database		

			Class.forName(db_classname);
			Connection con 
				= DriverManager.getConnection(db_connstr,db_user,db_password);
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(db_query);

			// Do we get a result? If so, then return title and value of
			// the first column.

			if (rs.next()) {
				exit_string = "OK: column '" + rs.getMetaData().getColumnLabel(1) 
					+ "' returned '" + rs.getObject(1) + "'";
				exit_code   = 0;
			} else {
				exit_string = "CRITICAL: No result returned.";
				exit_code   = 2;
			}

		} catch (Exception e) {

			// If we caught an exception, we have a problem, dude.
			
			exit_string = "CRITICAL: " + e;
			exit_code   = 2; 

		}
	
		// Return our one-liner and die.
	
		System.out.println(exit_string);
		System.exit(exit_code);
	}
}
-------------- next part --------------
PATH += :/usr/java/j2sdk1.4.1/bin
CLASSPATH=/usr/java/j2sdk1.4.1/lib/oracle8i-8.1.7.1-jdbc-classes12.zip:/usr/java/j2sdk1.4.1/lib/jt400.jar:/usr/java/j2sdk1.4.1/lib/java-getopt-1.0.9.jar:.

all: check_jdbc.class

install: all
	cp check_jdbc check_jdbc.class /usr/lib/nagios/plugins

clean: 
	rm -f *.class

check_jdbc.class: check_jdbc.java 
	javac -classpath $(CLASSPATH) check_jdbc.java


More information about the Users mailing list