Search in sources :

Example 1 with J4pReadRequest

use of org.jolokia.client.request.J4pReadRequest in project camel by apache.

the class DefaultJolokiaCamelController method getCamelContexts.

@Override
public List<Map<String, String>> getCamelContexts() 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>>();
    J4pSearchResponse sr = jolokia.execute(new J4pSearchRequest("*:type=context,*"));
    List<J4pReadRequest> list = new ArrayList<J4pReadRequest>();
    for (ObjectName on : sr.getObjectNames()) {
        list.add(new J4pReadRequest(on, "CamelId", "State", "Uptime", "ExchangesTotal", "ExchangesInflight", "ExchangesFailed"));
    }
    List<J4pReadResponse> lrr = jolokia.execute(list);
    for (J4pReadResponse rr : lrr) {
        Map<String, String> row = new LinkedHashMap<String, String>();
        row.put("name", rr.getValue("CamelId").toString());
        row.put("state", rr.getValue("State").toString());
        row.put("uptime", rr.getValue("Uptime").toString());
        row.put("exchangesTotal", rr.getValue("ExchangesTotal").toString());
        row.put("exchangesInflight", rr.getValue("ExchangesInflight").toString());
        row.put("exchangesFailed", rr.getValue("ExchangesFailed").toString());
        answer.add(row);
    }
    return answer;
}
Also used : J4pReadRequest(org.jolokia.client.request.J4pReadRequest) ArrayList(java.util.ArrayList) J4pSearchRequest(org.jolokia.client.request.J4pSearchRequest) J4pSearchResponse(org.jolokia.client.request.J4pSearchResponse) ObjectName(javax.management.ObjectName) LinkedHashMap(java.util.LinkedHashMap) J4pReadResponse(org.jolokia.client.request.J4pReadResponse) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with J4pReadRequest

use of org.jolokia.client.request.J4pReadRequest in project camel by apache.

the class DefaultJolokiaCamelController method getRoutes.

@Override
public List<Map<String, String>> getRoutes(String camelContextName, String filter) 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 = camelContextName != null ? lookupCamelContext(camelContextName) : null;
    if (found != null) {
        String pattern = String.format("%s:context=%s,type=routes,*", found.getDomain(), found.getKeyProperty("context"));
        J4pSearchResponse sr = jolokia.execute(new J4pSearchRequest(pattern));
        List<J4pReadRequest> list = new ArrayList<J4pReadRequest>();
        for (ObjectName on : sr.getObjectNames()) {
            list.add(new J4pReadRequest(on, "CamelId", "RouteId", "State", "Uptime", "ExchangesTotal", "ExchangesInflight", "ExchangesFailed"));
        }
        List<J4pReadResponse> lrr = jolokia.execute(list);
        for (J4pReadResponse rr : lrr) {
            String routeId = rr.getValue("RouteId").toString();
            if (filter == null || routeId.matches(filter)) {
                Map<String, String> row = new LinkedHashMap<String, String>();
                row.put("camelContextName", rr.getValue("CamelId").toString());
                row.put("routeId", routeId);
                row.put("state", rr.getValue("State").toString());
                row.put("uptime", rr.getValue("Uptime").toString());
                row.put("exchangesTotal", rr.getValue("ExchangesTotal").toString());
                row.put("exchangesInflight", rr.getValue("ExchangesInflight").toString());
                row.put("exchangesFailed", rr.getValue("ExchangesFailed").toString());
                answer.add(row);
            }
        }
    } else {
        List<Map<String, String>> camelContexts = this.getCamelContexts();
        for (Map<String, String> row : camelContexts) {
            List<Map<String, String>> routes = getRoutes(row.get("name"), filter);
            answer.addAll(routes);
        }
    }
    // sort the list
    Collections.sort(answer, new Comparator<Map<String, String>>() {

        @Override
        public int compare(Map<String, String> o1, Map<String, String> o2) {
            // group by camel context first, then by route name
            String c1 = o1.get("camelContextName");
            String c2 = o2.get("camelContextName");
            int answer = c1.compareTo(c2);
            if (answer == 0) {
                // okay from same camel context, then sort by route id
                answer = o1.get("routeId").compareTo(o2.get("routeId"));
            }
            return answer;
        }
    });
    return answer;
}
Also used : J4pReadRequest(org.jolokia.client.request.J4pReadRequest) ArrayList(java.util.ArrayList) J4pSearchRequest(org.jolokia.client.request.J4pSearchRequest) ObjectName(javax.management.ObjectName) J4pSearchResponse(org.jolokia.client.request.J4pSearchResponse) LinkedHashMap(java.util.LinkedHashMap) J4pReadResponse(org.jolokia.client.request.J4pReadResponse) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with J4pReadRequest

use of org.jolokia.client.request.J4pReadRequest 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;
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) PollStatus(org.opennms.netmgt.poller.PollStatus) J4pReadRequest(org.jolokia.client.request.J4pReadRequest) J4pExecRequest(org.jolokia.client.request.J4pExecRequest) J4pConnectException(org.jolokia.client.exception.J4pConnectException) J4pClient(org.jolokia.client.J4pClient) J4pRemoteException(org.jolokia.client.exception.J4pRemoteException) J4pReadResponse(org.jolokia.client.request.J4pReadResponse) TimeoutTracker(org.opennms.core.utils.TimeoutTracker) J4pException(org.jolokia.client.exception.J4pException) J4pClientBuilder(org.jolokia.client.J4pClientBuilder) J4pExecResponse(org.jolokia.client.request.J4pExecResponse) InetAddress(java.net.InetAddress)

Example 4 with J4pReadRequest

use of org.jolokia.client.request.J4pReadRequest in project syndesis by syndesisio.

the class PodMetricsReader method getRoutes.

public List<Map<String, String>> getRoutes(String camelContextName, String filter) 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 = camelContextName != null ? lookupCamelContext(camelContextName) : null;
    if (found != null) {
        String pattern = String.format("%s:context=%s,type=routes,*", found.getDomain(), found.getKeyProperty("context"));
        J4pSearchResponse sr = jolokia.execute(new J4pSearchRequest(pattern));
        List<J4pReadRequest> list = new ArrayList<J4pReadRequest>();
        for (ObjectName on : sr.getObjectNames()) {
            list.add(new J4pReadRequest(on, ROUTE_ID, RESET_TIMESTAMP, EXCHANGES_TOTAL, EXCHANGES_FAILED, LAST_COMPLETED_TIMESTAMP, LAST_FAILED_TIMESTAMP, START_TIMESTAMP));
        }
        List<J4pReadResponse> lrr = jolokia.execute(list);
        for (J4pReadResponse rr : lrr) {
            String routeId = rr.getValue(ROUTE_ID).toString();
            if (filter == null || routeId.matches(filter)) {
                Map<String, String> row = new LinkedHashMap<String, String>();
                for (String attribute : rr.getAttributes()) {
                    if (rr.getValue(attribute) != null) {
                        row.put(attribute, rr.getValue(attribute).toString());
                    }
                }
                answer.add(row);
            }
        }
    }
    // sort the list
    Collections.sort(answer, new Comparator<Map<String, String>>() {

        @Override
        public int compare(Map<String, String> o1, Map<String, String> o2) {
            // group by camel context first, then by route name
            String c1 = o1.get("camelContextName");
            String c2 = o2.get("camelContextName");
            int answer = c1.compareTo(c2);
            if (answer == 0) {
                // okay from same camel context, then sort by route id
                answer = o1.get("routeId").compareTo(o2.get("routeId"));
            }
            return answer;
        }
    });
    return answer;
}
Also used : J4pReadRequest(org.jolokia.client.request.J4pReadRequest) ArrayList(java.util.ArrayList) J4pSearchRequest(org.jolokia.client.request.J4pSearchRequest) ObjectName(javax.management.ObjectName) J4pSearchResponse(org.jolokia.client.request.J4pSearchResponse) LinkedHashMap(java.util.LinkedHashMap) J4pReadResponse(org.jolokia.client.request.J4pReadResponse) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 5 with J4pReadRequest

use of org.jolokia.client.request.J4pReadRequest in project fabric8 by fabric8io.

the class JolokiaAssert method attributeValue.

/**
 * Returns the attribute value of the given mbean and attribute name
 */
public Object attributeValue(String mbean, String attribute) throws MalformedObjectNameException, J4pException {
    ObjectName objectName = new ObjectName(mbean);
    J4pResponse<J4pReadRequest> results = client.execute(new J4pReadRequest(objectName, attribute));
    return results.getValue();
}
Also used : J4pReadRequest(org.jolokia.client.request.J4pReadRequest) ObjectName(javax.management.ObjectName)

Aggregations

J4pReadRequest (org.jolokia.client.request.J4pReadRequest)11 ObjectName (javax.management.ObjectName)7 J4pReadResponse (org.jolokia.client.request.J4pReadResponse)6 ArrayList (java.util.ArrayList)5 LinkedHashMap (java.util.LinkedHashMap)5 J4pSearchRequest (org.jolokia.client.request.J4pSearchRequest)5 J4pSearchResponse (org.jolokia.client.request.J4pSearchResponse)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 J4pException (org.jolokia.client.exception.J4pException)4 J4pRemoteException (org.jolokia.client.exception.J4pRemoteException)3 J4pExecRequest (org.jolokia.client.request.J4pExecRequest)3 List (java.util.List)2 MalformedObjectNameException (javax.management.MalformedObjectNameException)2 J4pClient (org.jolokia.client.J4pClient)2 J4pConnectException (org.jolokia.client.exception.J4pConnectException)2 AbtractJ4pMBeanRequest (org.jolokia.client.request.AbtractJ4pMBeanRequest)2 J4pResponse (org.jolokia.client.request.J4pResponse)2 J4pWriteRequest (org.jolokia.client.request.J4pWriteRequest)2 Container (io.fabric8.kubernetes.api.model.Container)1