use of org.jolokia.client.request.J4pExecResponse in project opennms by OpenNMS.
the class JolokiaBeanMonitor method poll.
/**
* {@inheritDoc}
*
* Poll the specified address for service availability.
*
* During the poll an attempt is made to execute the named method (with
* optional input) connect on the specified port. If the exec on request is
* successful, the banner line generated by the interface is parsed and if
* the banner text indicates that we are talking to Provided that the
* interface's response is valid we set the service status to
* SERVICE_AVAILABLE and return.
*/
public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) {
//
// Process parameters
//
//
TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT);
// Port
int port = ParameterMap.getKeyedInteger(parameters, PARAMETER_PORT, DEFAULT_PORT);
//URL
String strURL = ParameterMap.getKeyedString(parameters, PARAMETER_URL, DEFAULT_URL);
//Username
String strUser = ParameterMap.getKeyedString(parameters, PARAMETER_USERNAME, null);
//Password
String strPasswd = ParameterMap.getKeyedString(parameters, PARAMETER_PASSWORD, null);
//AttrName
String strAttrName = ParameterMap.getKeyedString(parameters, PARAMETER_ATTRNAME, null);
//AttrPath
String strAttrPath = ParameterMap.getKeyedString(parameters, PARAMETER_ATTRPATH, null);
//BeanName
String strBeanName = ParameterMap.getKeyedString(parameters, PARAMETER_BEANNAME, null);
//MethodName
String strMethodName = ParameterMap.getKeyedString(parameters, PARAMETER_METHODNAME, null);
//Optional Inputs
String strInput1 = ParameterMap.getKeyedString(parameters, PARAMETER_METHODINPUT1, null);
String strInput2 = ParameterMap.getKeyedString(parameters, PARAMETER_METHODINPUT2, null);
// BannerMatch
String strBannerMatch = ParameterMap.getKeyedString(parameters, PARAMETER_BANNER, null);
// Get the address instance.
InetAddress ipAddr = svc.getAddress();
final String hostAddress = InetAddressUtils.str(ipAddr);
LOGGER.debug("poll: address = " + hostAddress + ", port = " + port + ", " + tracker);
strURL = strURL.replace("${ipaddr}", hostAddress);
strURL = strURL.replace("${port}", ((Integer) port).toString());
LOGGER.debug("poll: final URL address = " + strURL);
// Give it a whirl
PollStatus serviceStatus = PollStatus.unknown("Initialized");
for (tracker.reset(); tracker.shouldRetry() && !serviceStatus.isAvailable(); tracker.nextAttempt()) {
try {
tracker.startAttempt();
J4pClientBuilder j4pClientBuilder = new J4pClientBuilder();
j4pClientBuilder.url(strURL).connectionTimeout(tracker.getConnectionTimeout()).socketTimeout(tracker.getSoTimeout());
if (strUser != null && strPasswd != null) {
j4pClientBuilder.user(strUser).password(strPasswd);
}
J4pClient j4pClient = j4pClientBuilder.build();
LOGGER.debug("JolokiaBeanMonitor: connected to URLhost: " + strURL);
// We're connected, so upgrade status to unresponsive
serviceStatus = PollStatus.unresponsive();
if (strBannerMatch == null || strBannerMatch.length() == 0 || strBannerMatch.equals("*")) {
serviceStatus = PollStatus.available(tracker.elapsedTimeInMillis());
break;
}
//Exec a method or poll an attribute?
String response;
if (strAttrName != null) {
J4pReadRequest readReq = new J4pReadRequest(strBeanName, strAttrName);
readReq.setPreferredHttpMethod("POST");
if (strAttrPath != null) {
readReq.setPath(strAttrPath);
}
J4pReadResponse resp = j4pClient.execute(readReq);
response = resp.getValue().toString();
} else {
J4pExecRequest execReq;
//Default Inputs
if (strInput1 == null && strInput2 == null) {
LOGGER.debug("JolokiaBeanMonitor - execute bean: " + strBeanName + " method: " + strMethodName);
execReq = new J4pExecRequest(strBeanName, strMethodName);
} else if (strInput1 != null && strInput2 == null) {
//Single Input
LOGGER.debug("JolokiaBeanMonitor - execute bean: " + strBeanName + " method: " + strMethodName + " args: " + strInput1);
execReq = new J4pExecRequest(strBeanName, strMethodName, strInput1);
} else {
//Double Input
LOGGER.debug("JolokiaBeanMonitor - execute bean: " + strBeanName + " method: " + strMethodName + " args: " + strInput1 + " " + strInput2);
execReq = new J4pExecRequest(strBeanName, strMethodName, strInput1, strInput2);
}
execReq.setPreferredHttpMethod("POST");
J4pExecResponse resp = j4pClient.execute(execReq);
response = resp.getValue().toString();
}
double responseTime = tracker.elapsedTimeInMillis();
if (response == null) {
continue;
}
LOGGER.debug("poll: banner = " + response);
LOGGER.debug("poll: responseTime = " + responseTime + "ms");
//Could it be a regex?
if (strBannerMatch.charAt(0) == '~') {
if (!response.matches(strBannerMatch.substring(1))) {
serviceStatus = PollStatus.unavailable("Banner does not match Regex '" + strBannerMatch + "'");
} else {
serviceStatus = PollStatus.available(responseTime);
}
} else {
if (response.contains(strBannerMatch)) {
serviceStatus = PollStatus.available(responseTime);
} else {
serviceStatus = PollStatus.unavailable("Did not find expected Text '" + strBannerMatch + "'");
}
}
} catch (J4pConnectException e) {
String reason = "Connection exception for address: " + ipAddr + ":" + port + " " + e.getMessage();
LOGGER.debug(reason, e);
serviceStatus = PollStatus.unavailable(reason);
break;
} catch (J4pRemoteException e) {
String reason = "Remote exception from J4pRemote: " + e.getMessage();
LOGGER.debug(reason, e);
serviceStatus = PollStatus.unavailable(reason);
} catch (MalformedObjectNameException e) {
String reason = "Parameters for Jolokia are malformed: " + e.getMessage();
LOGGER.debug(reason, e);
serviceStatus = PollStatus.unavailable(reason);
} catch (J4pException e) {
String reason = J4pException.class.getSimpleName() + " during Jolokia monitor call: " + e.getMessage();
LOGGER.debug(reason, e);
serviceStatus = PollStatus.unavailable(reason);
}
}
return serviceStatus;
}
use of org.jolokia.client.request.J4pExecResponse in project camel by apache.
the class DefaultJolokiaCamelController method getRouteStatsAsXml.
@Override
public String getRouteStatsAsXml(String routeId, String camelContextName, boolean fullStats, boolean includeProcessors) throws Exception {
if (jolokia == null) {
throw new IllegalStateException("Need to connect to remote jolokia first");
}
ObjectName found = lookupCamelContext(camelContextName);
if (found != null) {
String pattern = String.format("%s:context=%s,type=routes,name=\"%s\"", found.getDomain(), found.getKeyProperty("context"), routeId);
ObjectName on = ObjectName.getInstance(pattern);
J4pExecResponse response = jolokia.execute(new J4pExecRequest(on, "dumpRouteStatsAsXml(boolean,boolean)", fullStats, includeProcessors));
if (response != null) {
String xml = response.getValue();
return xml;
}
}
return null;
}
use of org.jolokia.client.request.J4pExecResponse in project camel by apache.
the class DefaultJolokiaCamelController method getTransformers.
@Override
public List<Map<String, String>> getTransformers(String camelContextName) throws Exception {
if (jolokia == null) {
throw new IllegalStateException("Need to connect to remote jolokia first");
}
List<Map<String, String>> answer = new ArrayList<Map<String, String>>();
ObjectName found = lookupCamelContext(camelContextName);
if (found != null) {
String pattern = String.format("%s:context=%s,type=services,name=DefaultTransformerRegistry", found.getDomain(), found.getKeyProperty("context"));
ObjectName on = ObjectName.getInstance(pattern);
J4pExecResponse response = jolokia.execute(new J4pExecRequest(on, "listTransformers()"));
if (response != null) {
JSONObject data = response.getValue();
if (data != null) {
for (Object obj : data.values()) {
JSONObject data2 = (JSONObject) obj;
JSONObject service = (JSONObject) data2.values().iterator().next();
Map<String, String> row = new LinkedHashMap<String, String>();
row.put("scheme", asString(service.get("scheme")));
row.put("from", asString(service.get("from")));
row.put("to", asString(service.get("to")));
row.put("static", asString(service.get("static")));
row.put("dynamic", asString(service.get("dynamic")));
row.put("description", asString(service.get("description")));
answer.add(row);
}
}
}
// sort the list
Collections.sort(answer, new Comparator<Map<String, String>>() {
@Override
public int compare(Map<String, String> service1, Map<String, String> service2) {
String scheme1 = service1.get("scheme");
String scheme2 = service2.get("scheme");
if (scheme1 != null && scheme2 != null) {
return scheme1.compareTo(scheme2);
} else if (scheme1 != null) {
return -1;
} else if (scheme2 != null) {
return 1;
} else {
String from1 = service1.get("from");
String from2 = service2.get("from");
if (from1.equals(from2)) {
String to1 = service1.get("to");
String to2 = service2.get("to");
return to1.compareTo(to2);
}
return from1.compareTo(from2);
}
}
});
}
return answer;
}
use of org.jolokia.client.request.J4pExecResponse in project camel by apache.
the class DefaultJolokiaCamelController method browseInflightExchanges.
@Override
public List<Map<String, Object>> browseInflightExchanges(String camelContextName, String route, int limit, boolean sortByLongestDuration) throws Exception {
if (jolokia == null) {
throw new IllegalStateException("Need to connect to remote jolokia first");
}
List<Map<String, Object>> answer = new ArrayList<Map<String, Object>>();
ObjectName found = lookupCamelContext(camelContextName);
if (found != null) {
String pattern = String.format("%s:context=%s,type=services,name=DefaultInflightRepository", found.getDomain(), found.getKeyProperty("context"));
ObjectName on = ObjectName.getInstance(pattern);
J4pExecResponse er = jolokia.execute(new J4pExecRequest(on, "browse(String,int,boolean)", route, limit, sortByLongestDuration));
if (er != null) {
JSONObject data = er.getValue();
if (data != null) {
for (Object obj : data.values()) {
JSONObject inflight = (JSONObject) obj;
Map<String, Object> row = new LinkedHashMap<String, Object>();
row.put("exchangeId", asString(inflight.get("exchangeId")));
row.put("fromRouteId", asString(inflight.get("fromRouteId")));
row.put("routeId", asString(inflight.get("routeId")));
row.put("nodeId", asString(inflight.get("nodeId")));
row.put("elapsed", asString(inflight.get("elapsed")));
row.put("duration", asString(inflight.get("duration")));
answer.add(row);
}
}
}
}
return answer;
}
use of org.jolokia.client.request.J4pExecResponse in project camel by apache.
the class DefaultJolokiaCamelController method getRestApiDocAsJson.
@Override
public String getRestApiDocAsJson(String camelContextName) throws Exception {
if (jolokia == null) {
throw new IllegalStateException("Need to connect to remote jolokia first");
}
ObjectName found = lookupCamelContext(camelContextName);
if (found != null) {
String pattern = String.format("%s:context=%s,type=services,name=DefaultRestRegistry", found.getDomain(), found.getKeyProperty("context"));
ObjectName on = ObjectName.getInstance(pattern);
J4pExecResponse response = jolokia.execute(new J4pExecRequest(on, "apiDocAsJson()"));
if (response != null) {
String json = response.getValue();
return json;
}
}
return null;
}
Aggregations