Search in sources :

Example 6 with FormattingResultLog

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

the class OsgiScriptBindingsProviderTest method testInactiveBundles.

@Test
public void testInactiveBundles() throws Exception {
    final BundleContext ctx = Mockito.mock(BundleContext.class);
    final Bundle[] bundles = { mockBundle(false, true), mockBundle(false, false), mockBundle(false, true), mockBundle(true, false) };
    Mockito.when(ctx.getBundles()).thenReturn(bundles);
    final FormattingResultLog resultLog = new FormattingResultLog();
    final OsgiScriptBindingsProvider.OsgiBinding b = new OsgiScriptBindingsProvider.OsgiBinding(ctx, resultLog);
    assertEquals(1, b.inactiveBundlesCount());
}
Also used : Bundle(org.osgi.framework.Bundle) FormattingResultLog(org.apache.sling.hc.util.FormattingResultLog) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 7 with FormattingResultLog

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

the class DefaultLoginsHealthCheck method execute.

@Override
public Result execute() {
    final FormattingResultLog resultLog = new FormattingResultLog();
    int checked = 0;
    int failures = 0;
    for (String login : logins) {
        final String[] parts = login.split(":");
        if (parts.length != 2) {
            resultLog.warn("Expected login in the form username:password, got [{}]", login);
            continue;
        }
        checked++;
        final String username = parts[0].trim();
        final String password = parts[1].trim();
        final Credentials creds = new SimpleCredentials(username, password.toCharArray());
        Session s = null;
        try {
            s = repository.login(creds);
            if (s != null) {
                failures++;
                resultLog.warn("Login as [{}] succeeded, was expecting it to fail", username);
            } else {
                resultLog.debug("Login as [{}] didn't throw an Exception but returned null Session", username);
            }
        } catch (RepositoryException re) {
            resultLog.debug("Login as [{}] failed, as expected", username);
        } finally {
            if (s != null) {
                s.logout();
            }
        }
    }
    if (checked == 0) {
        resultLog.warn("Did not check any logins, configured logins={}", logins);
    } else if (failures != 0) {
        resultLog.warn("Checked {} logins, {} failures", checked, failures);
    } else {
        resultLog.debug("Checked {} logins, all successful", checked, failures);
    }
    return new Result(resultLog);
}
Also used : SimpleCredentials(javax.jcr.SimpleCredentials) FormattingResultLog(org.apache.sling.hc.util.FormattingResultLog) RepositoryException(javax.jcr.RepositoryException) SimpleCredentials(javax.jcr.SimpleCredentials) Credentials(javax.jcr.Credentials) Session(javax.jcr.Session) Result(org.apache.sling.hc.api.Result)

Example 8 with FormattingResultLog

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

the class ThreadUsageHealthCheck method execute.

@Override
public Result execute() {
    FormattingResultLog log = new FormattingResultLog();
    log.debug("Checking threads for exceeding {}% CPU time within time period of {}ms", cpuTimeThresholdWarn, samplePeriodInMs);
    try {
        ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();
        List<ThreadTimeInfo> threadTimeInfos = collectThreadTimeInfos(log, threadMxBean);
        Collections.sort(threadTimeInfos);
        float totalCpuTimePercentage = 0;
        for (int i = 0; i < threadTimeInfos.size(); i++) {
            ThreadTimeInfo threadInfo = threadTimeInfos.get(i);
            float cpuTimePercentage = ((float) threadInfo.getCpuTime() / ((float) samplePeriodInMs * 1000000)) * 100f;
            totalCpuTimePercentage += cpuTimePercentage;
            String msg = String.format("%4.1f", cpuTimePercentage) + "% used by thread \"" + threadInfo.name + "\"";
            if (i < 3 || (i < 10 && cpuTimePercentage > 15)) {
                log.info(msg);
            } else {
                log.debug(msg);
            }
        }
        log.info(threadTimeInfos.size() + " threads using " + String.format("%4.1f", totalCpuTimePercentage) + "% CPU Time");
        boolean isHighCpuTime = totalCpuTimePercentage > cpuTimeThresholdWarn;
        if (isHighCpuTime) {
            log.warn("Threads are consuming significant CPU time " + (cpuTimeThresholdWarnIsConfigured ? "(more than configured " + cpuTimeThresholdWarn + "% within " + samplePeriodInMs + "ms)" : "(more than " + availableProcessors + " cores * 90% = " + cpuTimeThresholdWarn + "%)"));
        }
        checkForDeadlock(log, threadMxBean);
    } catch (Exception e) {
        LOG.error("Could not analyse thread usage " + e, e);
        log.healthCheckError("Could not analyse thread usage", e);
    }
    return new Result(log);
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) FormattingResultLog(org.apache.sling.hc.util.FormattingResultLog) Result(org.apache.sling.hc.api.Result)

Example 9 with FormattingResultLog

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

the class JmxScriptBindingsProviderTest method testJmxAttribute.

@Test
public void testJmxAttribute() throws Exception {
    final FormattingResultLog resultLog = new FormattingResultLog();
    final JmxScriptBindingsProvider.AttributeBinding b = new JmxScriptBindingsProvider.AttributeBinding(ManagementFactory.getPlatformMBeanServer(), resultLog);
    final Object value = b.attribute("java.lang:type=ClassLoading", "LoadedClassCount");
    assertNotNull("Expecting non-null attribute value", value);
    assertTrue("Expecting non-empty value", value.toString().length() > 0);
}
Also used : FormattingResultLog(org.apache.sling.hc.util.FormattingResultLog) Test(org.junit.Test)

Example 10 with FormattingResultLog

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

the class JUnitHealthCheck method execute.

public Result execute() {
    final String extension = "json";
    final FormattingResultLog resultLog = new FormattingResultLog();
    final CustomRunListener listener = new CustomRunListener(resultLog);
    final Renderer r = new CustomRenderer(listener, extension, resultLog);
    final Collection<String> testNames = testsManager.getTestNames(testSelector);
    if (testNames.isEmpty()) {
        resultLog.warn("No tests found for selector {}", testSelector);
    } else {
        try {
            testsManager.executeTests(testNames, r, testSelector);
            if (listener.nTests == 0) {
                resultLog.warn("No tests executed by {}", testSelector);
            }
        } catch (Exception e) {
            resultLog.warn("Exception while executing tests (" + testSelector + ")" + e);
        }
    }
    return new Result(resultLog);
}
Also used : FormattingResultLog(org.apache.sling.hc.util.FormattingResultLog) Renderer(org.apache.sling.junit.Renderer) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Result(org.apache.sling.hc.api.Result)

Aggregations

FormattingResultLog (org.apache.sling.hc.util.FormattingResultLog)17 Result (org.apache.sling.hc.api.Result)15 Date (java.util.Date)3 MBeanServer (javax.management.MBeanServer)2 ObjectName (javax.management.ObjectName)2 HealthCheckExecutionResult (org.apache.sling.hc.api.execution.HealthCheckExecutionResult)2 Test (org.junit.Test)2 ServiceReference (org.osgi.framework.ServiceReference)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ThreadMXBean (java.lang.management.ThreadMXBean)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Credentials (javax.jcr.Credentials)1 RepositoryException (javax.jcr.RepositoryException)1 Session (javax.jcr.Session)1