First running version with osgi service restarter and mail logger

This commit is contained in:
Simon Pamies committed 2012-11-26 18:36:05 +01:00
1 parent 12e999380e
commit ea66262b86
7 files changed
+168 -24

No files matched your search

@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry exported="true" kind="lib" path="lib/mail.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
@@ -15,3 +15,5 @@ Require-Bundle: javax.servlet;bundle-version="2.5.0",
org.eclipse.jetty.util;bundle-version="7.5.4",
org.eclipse.jetty.io;bundle-version="7.5.4",
org.eclipse.jetty.servlet;bundle-version="7.5.4"
Bundle-ClassPath: lib/mail.jar,
.
@@ -1,4 +1,5 @@
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.
.,\
lib/mail.jar
Binary file not shown.
@@ -1,9 +1,11 @@
package com.sap.sailing.monitoring;
import java.io.IOException;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.logging.Logger;
/**
* Simple post monitoring application. Checks given ports in an regular interval.
@@ -14,14 +16,18 @@ import java.net.SocketTimeoutException;
*/
public abstract class AbstractPortMonitor extends Thread {
Logger log = Logger.getLogger(AbstractPortMonitor.class.getName());
private final int TIMEOUT = 1000;
private final int GRACEFUL = 5000;
private final int PAUSE = 1000;
private InetSocketAddress[] endpoints = null;
private int interval;
protected InetSocketAddress[] endpoints = null;
protected int interval;
boolean started = false;
boolean paused = false;
private long lastmillis;
private InetSocketAddress currentendpoint;
@@ -34,6 +40,8 @@ public abstract class AbstractPortMonitor extends Thread {
this.interval = interval;
this.started = true;
log.info("Initialized monitoring!");
}
public void startMonitoring() {
@@ -48,32 +56,39 @@ public abstract class AbstractPortMonitor extends Thread {
Thread.sleep(GRACEFUL);
while (started) {
long currentmillis = System.currentTimeMillis();
try {
if (currentmillis >= (lastmillis + interval + (100*endpoints.length))) {
for (int i = 0; i < endpoints.length; i++) {
currentendpoint = endpoints[i];
Socket sn = new Socket();
sn.connect(currentendpoint, TIMEOUT);
handleConnection(currentendpoint);
lastmillis = System.currentTimeMillis();
if (!paused) {
long currentmillis = System.currentTimeMillis();
try {
if (currentmillis >= (lastmillis + interval + (100*endpoints.length))) {
for (int i = 0; i < endpoints.length; i++) {
currentendpoint = endpoints[i];
Socket sn = new Socket();
sn.connect(currentendpoint, TIMEOUT);
log.info("Connection succeeded to " + currentendpoint.toString());
handleConnection(currentendpoint);
lastmillis = System.currentTimeMillis();
}
}
} catch (SocketTimeoutException|ConnectException ex) {
log.info("Connection FAILED to " + currentendpoint.toString());
handleFailure(currentendpoint);
lastmillis = System.currentTimeMillis();
} catch (IOException ex) {
ex.printStackTrace();
}
Thread.sleep(PAUSE);
} catch (SocketTimeoutException ex) {
handleFailure(currentendpoint);
} catch (IOException ex) {
ex.printStackTrace();
} else {
log.info("Pausing...");
}
Thread.sleep(PAUSE);
}
} catch (InterruptedException ex) {
System.out.println("Interrupted");
System.err.println("Interrupted");
}
}
@@ -82,6 +97,17 @@ public abstract class AbstractPortMonitor extends Thread {
this.started = false;
}
protected void removeEndpoint(InetSocketAddress endpoint) {
InetSocketAddress[] new_endpoints = new InetSocketAddress[endpoints.length-1];
for (int i=0;i<endpoints.length;i++) {
if (endpoints[i] != endpoint)
new_endpoints[i] = endpoint;
}
this.endpoints = new_endpoints;
}
public abstract void handleFailure(InetSocketAddress endpoint);
public abstract void handleConnection(InetSocketAddress endpoint);
@@ -28,8 +28,17 @@ public class Activator implements BundleActivator {
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
/* Configuration */
InetSocketAddress[] endpoints = new InetSocketAddress[]{
new InetSocketAddress(InetAddress.getByName("localhost"), 8080)
};
String[] services = new String[] {
"com.sap.sailing.server"
};
/* Starts a new port monitoring app */
monitor = new SystemPrinterPortMonitor(new InetSocketAddress[]{new InetSocketAddress(InetAddress.getByName("173.194.69.94"), 80)}, 10000);
monitor = new OSGiRestartingPortMonitor(endpoints, services, 10000);
monitor.startMonitoring();
}
@@ -0,0 +1,105 @@
package com.sap.sailing.monitoring;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Properties;
import java.util.logging.Logger;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleEvent;
import org.osgi.framework.BundleException;
/**
* This monitor tries to restart registered services
* on failure. It also send out email to fixed recipients.
*
* @author Simon Pamies (info@pamies.de)
* @since Nov 26, 2012
*/
public class OSGiRestartingPortMonitor extends AbstractPortMonitor {
Logger log = Logger.getLogger(OSGiRestartingPortMonitor.class.getName());
HashMap<InetSocketAddress, String> endpointservices = new HashMap<InetSocketAddress, String>();
HashMap<String, Bundle> bundles = new HashMap<String, Bundle>();
public OSGiRestartingPortMonitor(InetSocketAddress[] endpoints, String[] services, int interval) {
super(endpoints, interval);
/* store bundles so that we can reference them later */
for (Bundle bundle : Activator.getContext().getBundles()) {
bundles.put(bundle.getSymbolicName(), bundle);
}
for (int i=0; i<endpoints.length;i++) {
endpointservices.put(endpoints[i], services[i]);
}
}
@Override
public void handleFailure(InetSocketAddress endpoint) {
String servicename = endpointservices.get(endpoint);
Bundle bundle = bundles.get(servicename);
if (bundle.getState() == BundleEvent.STARTED || bundle.getState() == BundleEvent.STOPPED) {
try {
bundle.stop();
} catch (BundleException e) {
log.severe("Could not stop " + servicename + " trying to start anyway.");
}
try {
bundle.start();
} catch (BundleException e) {
log.severe("Could not start " + servicename + "! Handler will try again next time");
}
}
log.info("Bundle " + servicename + " restarted");
/* Send out mail */
Properties props = new Properties();
props.setProperty("mail.from", "s.pamies@banality.de");
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", "mail.banality.de");
props.setProperty("mail.smtp.port", "25");
props.setProperty("mail.smtp.auth", "true");
try {
Session session = Session.getDefaultInstance(props, new SMTPAuthenticator());
MimeMessage msg = new MimeMessage(session);
msg.setSubject("Bundle " + servicename + " restarted");
msg.setContent("The Bundle " + servicename + " has been restarted - port check on " + endpoint.getPort() + " didn't respond!", "text/plain");
msg.addRecipient(RecipientType.TO, new InternetAddress("spamsch@gmail.com"));
Transport ts = session.getTransport();
ts.connect();
ts.sendMessage(msg, msg.getRecipients(RecipientType.TO));
ts.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = "pamiesmail@banality.de";
String password = "qay?234wsx";
return new PasswordAuthentication(username, password);
}
}
@Override
public void handleConnection(InetSocketAddress endpoint) {
}
}