mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-10-08 05:11:04 +00:00
Added check for URL endpoints
This commit is contained in:
1 parent
7719bb4974
commit
64d4e061d0
5 files changed
+107
-32
No files matched your search
+37
-9
@@ -2,10 +2,13 @@ package com.sap.sailing.monitoring;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ConnectException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.net.URL;
|
||||
import java.util.Properties;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -41,13 +44,24 @@ public abstract class AbstractPortMonitor extends Thread {
|
||||
|
||||
this.endpoints = new Endpoint[prop_endpoints.length];
|
||||
for (int i=0; i<prop_endpoints.length;i++) {
|
||||
String[] data = prop_endpoints[i].split(":");
|
||||
|
||||
try {
|
||||
EndpointImpl e = new EndpointImpl(new InetSocketAddress(InetAddress.getByName(data[0].trim()), Integer.parseInt(data[1].trim())));
|
||||
this.endpoints[i] = e;
|
||||
} catch (Exception ex) {
|
||||
log.severe("Could not parse endpoint definition " + prop_endpoints[i]);
|
||||
if (prop_endpoints[i].trim().startsWith("http")) {
|
||||
try {
|
||||
URL url = new URL(prop_endpoints[i].trim());
|
||||
EndpointImpl e = new EndpointImpl(url);
|
||||
this.endpoints[i] = e;
|
||||
} catch (MalformedURLException ex) {
|
||||
log.severe("Could not parse endpoint definition " + prop_endpoints[i]);
|
||||
}
|
||||
} else {
|
||||
String[] data = prop_endpoints[i].split(":");
|
||||
|
||||
try {
|
||||
EndpointImpl e = new EndpointImpl(new InetSocketAddress(InetAddress.getByName(data[0].trim()), Integer.parseInt(data[1].trim())));
|
||||
this.endpoints[i] = e;
|
||||
} catch (Exception ex) {
|
||||
log.severe("Could not parse endpoint definition " + prop_endpoints[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,9 +91,23 @@ public abstract class AbstractPortMonitor extends Thread {
|
||||
for (int i = 0; i < endpoints.length; i++) {
|
||||
currentendpoint = endpoints[i];
|
||||
|
||||
Socket sn = new Socket();
|
||||
sn.connect(currentendpoint.getAddress(), timeout);
|
||||
sn.close();
|
||||
if (!currentendpoint.isURL()) {
|
||||
Socket sn = new Socket();
|
||||
sn.connect(currentendpoint.getAddress(), timeout);
|
||||
sn.close();
|
||||
} else {
|
||||
|
||||
/* despite its name this does NOT open a real TCP connection */
|
||||
HttpURLConnection conn = (HttpURLConnection)currentendpoint.getURL().openConnection();
|
||||
conn.setConnectTimeout(timeout);
|
||||
conn.setRequestMethod("GET");
|
||||
conn.connect();
|
||||
|
||||
int code = conn.getResponseCode();
|
||||
if (code != 200)
|
||||
throw new ConnectException("Could not successfully connect to endpoint " + currentendpoint.toString());
|
||||
|
||||
}
|
||||
|
||||
log.info("Connection succeeded to " + currentendpoint.toString());
|
||||
handleConnection(currentendpoint);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.sap.sailing.monitoring;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* Definition of an endpoint that submits to checks by
|
||||
@@ -12,10 +13,27 @@ import java.net.InetSocketAddress;
|
||||
*/
|
||||
public interface Endpoint {
|
||||
|
||||
/**
|
||||
* @return the address of this endpoint
|
||||
*/
|
||||
InetSocketAddress getAddress();
|
||||
|
||||
/**
|
||||
* @return the name of this endpoint. This information can be used
|
||||
* to reference other services.
|
||||
*/
|
||||
String getName();
|
||||
|
||||
/**
|
||||
* @return true if this endpoint represents an url
|
||||
*/
|
||||
boolean isURL();
|
||||
|
||||
/**
|
||||
* @return the associated URL or null if there is none
|
||||
*/
|
||||
URL getURL();
|
||||
|
||||
long lastSucceeded();
|
||||
|
||||
long lastFailed();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.sap.sailing.monitoring;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URL;
|
||||
|
||||
|
||||
/**
|
||||
@@ -13,12 +14,22 @@ public class EndpointImpl implements Endpoint {
|
||||
|
||||
private InetSocketAddress address;
|
||||
private String name = "";
|
||||
private URL url;
|
||||
private boolean is_url = false;
|
||||
private long last_success = 0;
|
||||
private long last_fail = 0;
|
||||
private boolean already_checked = false;
|
||||
|
||||
public EndpointImpl(InetSocketAddress address) {
|
||||
this.address = address;
|
||||
this.is_url = false;
|
||||
this.url = null;
|
||||
}
|
||||
|
||||
public EndpointImpl(URL input) {
|
||||
this.address = null;
|
||||
this.is_url = true;
|
||||
this.url = input;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -56,7 +67,7 @@ public class EndpointImpl implements Endpoint {
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return getAddress().toString();
|
||||
return isURL() ? getURL().toString() : getAddress().toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -69,4 +80,14 @@ public class EndpointImpl implements Endpoint {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isURL() {
|
||||
return is_url;
|
||||
}
|
||||
|
||||
@Override
|
||||
public URL getURL() {
|
||||
return url;
|
||||
}
|
||||
|
||||
}
|
||||
+18
-18
@@ -1,6 +1,5 @@
|
||||
package com.sap.sailing.monitoring;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Properties;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -17,7 +16,7 @@ import org.osgi.framework.BundleException;
|
||||
|
||||
/**
|
||||
* This monitor tries to restart registered services
|
||||
* on failure. It also send out email to fixed recipients.
|
||||
* on failure. It also sends out email to fixed recipients.
|
||||
*
|
||||
* @author Simon Pamies (info@pamies.de)
|
||||
* @since Nov 26, 2012
|
||||
@@ -25,17 +24,10 @@ import org.osgi.framework.BundleException;
|
||||
public class OSGiRestartingPortMonitor extends AbstractPortMonitor {
|
||||
Logger log = Logger.getLogger(OSGiRestartingPortMonitor.class.getName());
|
||||
|
||||
HashMap<String, Bundle> bundles = new HashMap<String, Bundle>();
|
||||
|
||||
public OSGiRestartingPortMonitor(Properties properties) {
|
||||
super(properties);
|
||||
|
||||
/* store bundles so that we can reference them later without iterating every time */
|
||||
for (Bundle bundle : Activator.getContext().getBundles()) {
|
||||
bundles.put(bundle.getSymbolicName(), bundle);
|
||||
}
|
||||
|
||||
String[] prop_services = properties.getProperty("monitor.services").split(",");
|
||||
String[] prop_services = properties.getProperty("monitor.bundles").split(",");
|
||||
|
||||
for (int i=0; i<endpoints.length;i++) {
|
||||
endpoints[i].setName(prop_services[i].trim());
|
||||
@@ -44,7 +36,7 @@ public class OSGiRestartingPortMonitor extends AbstractPortMonitor {
|
||||
|
||||
@Override
|
||||
public void handleFailure(Endpoint endpoint) {
|
||||
Bundle bundle = bundles.get(endpoint.getName());
|
||||
Bundle bundle = getBundleByName(endpoint.getName());
|
||||
|
||||
if (bundle.getState() == BundleEvent.STARTED || bundle.getState() == BundleEvent.STOPPED) {
|
||||
try {
|
||||
@@ -63,13 +55,13 @@ public class OSGiRestartingPortMonitor extends AbstractPortMonitor {
|
||||
log.info("Bundle " + endpoint.getName() + " restarted");
|
||||
|
||||
/* only send mail if service has not failed before */
|
||||
if (!endpoint.hasFailed()) {
|
||||
if (!endpoint.hasFailed() /*before*/) {
|
||||
try {
|
||||
Session session = Session.getDefaultInstance(this.properties, new SMTPAuthenticator());
|
||||
MimeMessage msg = new MimeMessage(session);
|
||||
|
||||
msg.setSubject("Bundle " + endpoint.getName() + " restarted");
|
||||
msg.setContent("The Bundle " + endpoint.getName() + " has been restarted - port check on " + endpoint.getAddress().getPort() + " didn't respond!\n" +
|
||||
msg.setContent("The Bundle " + endpoint.getName() + " has been restarted - check on " + endpoint + " didn't respond!\n" +
|
||||
"This Mail won't be send again if service continues to fail.", "text/plain");
|
||||
msg.addRecipient(RecipientType.TO, new InternetAddress(this.properties.getProperty("mail.to")));
|
||||
|
||||
@@ -83,6 +75,19 @@ public class OSGiRestartingPortMonitor extends AbstractPortMonitor {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleConnection(Endpoint endpoint) {
|
||||
}
|
||||
|
||||
protected Bundle getBundleByName(String name) {
|
||||
for (Bundle bundle : Activator.getContext().getBundles()) {
|
||||
if (bundle.getSymbolicName().equalsIgnoreCase(name))
|
||||
return bundle;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private class SMTPAuthenticator extends javax.mail.Authenticator {
|
||||
public PasswordAuthentication getPasswordAuthentication() {
|
||||
String username = properties.getProperty("mail.smtp.user");
|
||||
@@ -90,9 +95,4 @@ public class OSGiRestartingPortMonitor extends AbstractPortMonitor {
|
||||
return new PasswordAuthentication(username, password);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleConnection(Endpoint endpoint) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,11 +17,19 @@ monitor.timeout = 2000
|
||||
# provided in milliseconds
|
||||
monitor.wait_after_failure = 60000
|
||||
|
||||
# endpoints that should be checked, separate by comma, each in the format host:port, e.g., test.something.com:80
|
||||
monitor.endpoints = 127.0.0.1:8888, 127.0.0.1:8889
|
||||
# endpoints that should be checked, separate by comma
|
||||
# two formats are valid, each yielding a different check
|
||||
# - address:port
|
||||
# This format leads to a simple port check. Use this if
|
||||
# you want to check if a server is still running (e.g. jetty)
|
||||
#
|
||||
# - http://domain_or_address/path/to/check
|
||||
# This format leads to a check that only succeeds if a status
|
||||
# code of 200 is returned.
|
||||
monitor.endpoints = http://127.0.0.1:8889/gwt/RaceBoard.html, http://127.0.0.1:8889/
|
||||
|
||||
# service name associated to endpoints
|
||||
monitor.services = com.sap.sailing.server, com.sap.sailing.server
|
||||
# bundle associated to endpoints
|
||||
monitor.bundles = com.sap.sailing.gwt.ui, com.sap.sailing.www
|
||||
|
||||
# mail configuration
|
||||
mail.from = info@sapsailing.com
|
||||
|
||||
Reference in new issue
Block a user