/*
* ServerMonitor.java
*
* Created on April 29, 2004, 12:40 PM
*
* Copyright 2004 Andrew Kaluzniacki. All Rights Reserved.
* http://drewk.net
*
*
* ToDO: handle many hosts - initialize by config file - propertiesfile maybe
*/
/**
*
* @author drew
*/
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
/**
* ServerMonitor is a class for monitoring a server by checking to see if a
* Socket connection can be made to named host at a specific port.
*/
public class ServerMonitor {
private String host = null;
private int port = 0;
private String errorString = null;
private boolean sentMail = false;
/**
* Creates a new instance of ServerMonitor
* @param host name of host to monitor
* @param port port on host to be monitored
*/
public ServerMonitor(String host, int port) {
this.host = host;
this.port = port;
}
/**
* Check if a socket connection can be made to host:port
* @return true if connection made.
* false if no connection, and errorString is set to reason.
*/
public boolean checkServer() {
Socket socket = null;
boolean retValue = false;
errorString = "Success";
try {
socket = new Socket(host, port);
retValue = true;
} catch (UnknownHostException e) {
errorString = "Unknown host exception:" + host;
} catch (IOException e) {
errorString = "IOException connecting to:" + host;
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
System.out.println("IOException closing socket:" + e);
}
}
return retValue;
}
/**
* Send email to notify administrator of failure.
* This is only done once.
*/
public void emailAdmin(String adminEmail, String smtpHost) {
if (sentMail) { // only once
return;
}
sentMail = true;
System.out.println("Sending mail to admin.");
try {
System.getProperties().put("mail.host", smtpHost);
URL u = new URL("mailto:" + adminEmail);
URLConnection c = u.openConnection();
c.setDoInput(false);
c.setDoOutput(true);
c.connect();
PrintWriter mail_out =
new PrintWriter(new OutputStreamWriter(c.getOutputStream() ) );
mail_out.println("To:" + adminEmail);
mail_out.println("Subject:[Server Monitor] " + host);
mail_out.println();
mail_out.println("Check the log. This will be the only message until you restart ServerMonitor.");
mail_out.close();
} catch (Exception e) {
System.out.println("ERROR: unable to send mail. " + e);
sentMail = false;
}
}
/**
* Returns a string describing the result of last call to checkServer
* @return status string
*/
public String getError() { return errorString; }
/**
* Run the ServerMonitor
* @param args the command line arguments - none
* Arguments are passed as system properties.
* sm.host
* sm.port
* sm.interval [minutes]
* sm.refHost
* sm.refPort
* sm.adminEmail
* sm.smtpHost
*
* @throws InterruptedException only if the call to sleep is interupted.
*/
public static void main(String[] args) throws InterruptedException {
String host = System.getProperty("sm.host", "localhost");
int port = Integer.getInteger("sm.port", 80).intValue();
int intervalMinutes = Integer.getInteger("sm.interval", 10).intValue();
String refHost = System.getProperty("sm.refHost", "localhost");
int refPort = Integer.getInteger("sm.refPort", 80).intValue();
String adminEmail = System.getProperty("sm.adminEmail", "root@localhost");
String smtpHost = System.getProperty("sm.smtpHost", "localhost");
System.out.println("Starting ServerMonitor. host:" + host + " port:" +
port + " refHost:" + refHost + " refPort:" +
refPort + " interval:" + intervalMinutes +
" adminEmail:" + adminEmail + " smtpHost:" + smtpHost);
ServerMonitor sm = new ServerMonitor(host, port);
ServerMonitor rm = new ServerMonitor(refHost , refPort);
while (true) {
Date date = new Date();
if (sm.checkServer()) {
System.out.println( "[" + date + "] " + host +
" seems to be running.");
} else {
System.out.println( "[" + date + "] Error: " + host +
" not reachable. " + sm.getError());
if (rm.checkServer()) {
System.out.println( " Checking reference host. " + refHost +
" seems to be running.");
} else {
System.out.println( " Checking reference host. Error: " +
refHost + " not reachable. " + rm.getError());
}
sm.emailAdmin(adminEmail, smtpHost);
}
long millis = intervalMinutes * ( 60 * 1000 );
Thread.sleep(millis);
}
}
}