diff --git a/java/com.sap.sailing.monitoring/.classpath b/java/com.sap.sailing.monitoring/.classpath new file mode 100644 index 00000000000..098194ca4b7 --- /dev/null +++ b/java/com.sap.sailing.monitoring/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/java/com.sap.sailing.monitoring/.project b/java/com.sap.sailing.monitoring/.project new file mode 100644 index 00000000000..5bf2c3ba6d0 --- /dev/null +++ b/java/com.sap.sailing.monitoring/.project @@ -0,0 +1,28 @@ + + + com.sap.sailing.monitoring + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + + org.eclipse.pde.PluginNature + org.eclipse.jdt.core.javanature + + diff --git a/java/com.sap.sailing.monitoring/.settings/org.eclipse.jdt.core.prefs b/java/com.sap.sailing.monitoring/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 00000000000..f42de363afa --- /dev/null +++ b/java/com.sap.sailing.monitoring/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,7 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/java/com.sap.sailing.monitoring/.settings/org.eclipse.pde.core.prefs b/java/com.sap.sailing.monitoring/.settings/org.eclipse.pde.core.prefs new file mode 100644 index 00000000000..f29e940a005 --- /dev/null +++ b/java/com.sap.sailing.monitoring/.settings/org.eclipse.pde.core.prefs @@ -0,0 +1,3 @@ +eclipse.preferences.version=1 +pluginProject.extensions=false +resolve.requirebundle=false diff --git a/java/com.sap.sailing.monitoring/META-INF/MANIFEST.MF b/java/com.sap.sailing.monitoring/META-INF/MANIFEST.MF new file mode 100644 index 00000000000..98014a1d466 --- /dev/null +++ b/java/com.sap.sailing.monitoring/META-INF/MANIFEST.MF @@ -0,0 +1,17 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Monitoring +Bundle-SymbolicName: com.sap.sailing.monitoring +Bundle-Version: 1.0.0.qualifier +Bundle-Activator: com.sap.sailing.monitoring.Activator +Bundle-Vendor: SAP +Bundle-RequiredExecutionEnvironment: JavaSE-1.7 +Import-Package: org.osgi.framework;version="1.3.0" +Bundle-ActivationPolicy: lazy +Require-Bundle: javax.servlet;bundle-version="2.5.0", + org.eclipse.jetty.http;bundle-version="7.5.4", + org.eclipse.jetty.continuation;bundle-version="7.5.4", + org.eclipse.jetty.server;bundle-version="7.5.4", + 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" diff --git a/java/com.sap.sailing.monitoring/build.properties b/java/com.sap.sailing.monitoring/build.properties new file mode 100644 index 00000000000..34d2e4d2dad --- /dev/null +++ b/java/com.sap.sailing.monitoring/build.properties @@ -0,0 +1,4 @@ +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + . diff --git a/java/com.sap.sailing.monitoring/src/com/sap/sailing/monitoring/AbstractPortMonitor.java b/java/com.sap.sailing.monitoring/src/com/sap/sailing/monitoring/AbstractPortMonitor.java new file mode 100644 index 00000000000..2bad8a5ab95 --- /dev/null +++ b/java/com.sap.sailing.monitoring/src/com/sap/sailing/monitoring/AbstractPortMonitor.java @@ -0,0 +1,89 @@ +package com.sap.sailing.monitoring; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.net.SocketTimeoutException; + +/** + * Simple post monitoring application. Checks given ports in an regular interval. + * If there is a problem it calls the registered handler. + * + * @author Simon Pamies (info@pamies.de) + * @since Nov 25, 2012 + */ +public abstract class AbstractPortMonitor extends Thread { + + private final int TIMEOUT = 1000; + private final int GRACEFUL = 5000; + private final int PAUSE = 1000; + + private InetSocketAddress[] endpoints = null; + private int interval; + + boolean started = false; + private long lastmillis; + private InetSocketAddress currentendpoint; + + public AbstractPortMonitor(InetSocketAddress[] endpoints, int interval) { + this.endpoints = endpoints; + + if (interval < 10000) { + throw new RuntimeException("Interval can not be lower than 10 seconds!"); + } + + this.interval = interval; + this.started = true; + } + + public void startMonitoring() { + this.start(); + } + + @Override + public void run() { + + try { + /* graceful start - give services some time */ + 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(); + } + } + + Thread.sleep(PAUSE); + + } catch (SocketTimeoutException ex) { + handleFailure(currentendpoint); + + } catch (IOException ex) { + ex.printStackTrace(); + } + } + + } catch (InterruptedException ex) { + System.out.println("Interrupted"); + } + + } + + public void stopMonitoring() { + this.started = false; + } + + public abstract void handleFailure(InetSocketAddress endpoint); + + public abstract void handleConnection(InetSocketAddress endpoint); + +} diff --git a/java/com.sap.sailing.monitoring/src/com/sap/sailing/monitoring/Activator.java b/java/com.sap.sailing.monitoring/src/com/sap/sailing/monitoring/Activator.java new file mode 100644 index 00000000000..c77ea65e7c6 --- /dev/null +++ b/java/com.sap.sailing.monitoring/src/com/sap/sailing/monitoring/Activator.java @@ -0,0 +1,45 @@ +package com.sap.sailing.monitoring; + +import java.net.InetAddress; +import java.net.InetSocketAddress; + +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; + +/** + * Activator for the monitoring service + * + * @author Simon Pamies (info@pamies.de) + * @since Nov 25, 2012 + */ +public class Activator implements BundleActivator { + + private static BundleContext context; + private AbstractPortMonitor monitor; + + static BundleContext getContext() { + return context; + } + + /* + * (non-Javadoc) + * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext) + */ + public void start(BundleContext bundleContext) throws Exception { + Activator.context = bundleContext; + + /* Starts a new port monitoring app */ + monitor = new SystemPrinterPortMonitor(new InetSocketAddress[]{new InetSocketAddress(InetAddress.getByName("173.194.69.94"), 80)}, 10000); + monitor.startMonitoring(); + } + + /* + * (non-Javadoc) + * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) + */ + public void stop(BundleContext bundleContext) throws Exception { + monitor.stopMonitoring(); + Activator.context = null; + } + +} diff --git a/java/com.sap.sailing.monitoring/src/com/sap/sailing/monitoring/SystemPrinterPortMonitor.java b/java/com.sap.sailing.monitoring/src/com/sap/sailing/monitoring/SystemPrinterPortMonitor.java new file mode 100644 index 00000000000..41e1f6a8c75 --- /dev/null +++ b/java/com.sap.sailing.monitoring/src/com/sap/sailing/monitoring/SystemPrinterPortMonitor.java @@ -0,0 +1,28 @@ +package com.sap.sailing.monitoring; + +import java.net.InetSocketAddress; + +/** + * Implementation of port monitor that prints out messages if + * given endpoint is not connected. + * + * @author Simon Pamies (info@pamies.de) + * @since Nov 25, 2012 + */ +public class SystemPrinterPortMonitor extends AbstractPortMonitor { + + public SystemPrinterPortMonitor(InetSocketAddress[] endpoints, int interval) { + super(endpoints, interval); + } + + @Override + public void handleFailure(InetSocketAddress endpoint) { + System.out.println("ERROR: Could not connect to endpoint " + endpoint.toString()); + } + + @Override + public void handleConnection(InetSocketAddress endpoint) { + System.out.println("INFO: Connection to " + endpoint.toString() + " succeeded."); + } + +}