Search in sources :

Example 1 with SimpleConstraintChecker

use of org.apache.sling.hc.util.SimpleConstraintChecker in project sling by apache.

the class JmxAttributeHealthCheck method execute.

@Override
public Result execute() {
    final FormattingResultLog resultLog = new FormattingResultLog();
    resultLog.debug("Checking {} / {} with constraint {}", mbeanName, attributeName, constraint);
    try {
        final MBeanServer jmxServer = ManagementFactory.getPlatformMBeanServer();
        final ObjectName objectName = new ObjectName(mbeanName);
        if (jmxServer.queryNames(objectName, null).size() == 0) {
            resultLog.warn("MBean not found: {}", objectName);
        } else {
            final Object value = jmxServer.getAttribute(objectName, attributeName);
            resultLog.debug("{} {} returns {}", mbeanName, attributeName, value);
            new SimpleConstraintChecker().check(value, constraint, resultLog);
        }
    } catch (final Exception e) {
        log.warn("JMX attribute {}/{} check failed: {}", new Object[] { mbeanName, attributeName, e });
        resultLog.healthCheckError("JMX attribute check failed: {}", e);
    }
    return new Result(resultLog);
}
Also used : SimpleConstraintChecker(org.apache.sling.hc.util.SimpleConstraintChecker) FormattingResultLog(org.apache.sling.hc.util.FormattingResultLog) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Result(org.apache.sling.hc.api.Result)

Example 2 with SimpleConstraintChecker

use of org.apache.sling.hc.util.SimpleConstraintChecker in project sling by apache.

the class HealthCheckMBeanTest method assertJmxValue.

private void assertJmxValue(String mbeanName, String attributeName, String constraint, boolean expected) throws Exception {
    final MBeanServer jmxServer = ManagementFactory.getPlatformMBeanServer();
    final ObjectName objectName = new ObjectName(mbeanName);
    if (jmxServer.queryNames(objectName, null).size() == 0) {
        fail("MBean not found: " + objectName);
    }
    final Object value = jmxServer.getAttribute(objectName, attributeName);
    final ResultLog resultLog = new ResultLog();
    new SimpleConstraintChecker().check(value, constraint, resultLog);
    assertEquals("Expecting result " + expected + "(" + resultLog + ")", expected, resultLog.getAggregateStatus().equals(Result.Status.OK));
}
Also used : SimpleConstraintChecker(org.apache.sling.hc.util.SimpleConstraintChecker) ResultLog(org.apache.sling.hc.api.ResultLog) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Aggregations

MBeanServer (javax.management.MBeanServer)2 ObjectName (javax.management.ObjectName)2 SimpleConstraintChecker (org.apache.sling.hc.util.SimpleConstraintChecker)2 Result (org.apache.sling.hc.api.Result)1 ResultLog (org.apache.sling.hc.api.ResultLog)1 FormattingResultLog (org.apache.sling.hc.util.FormattingResultLog)1