Search in sources :

Example 31 with HealthCheckExecutionResult

use of org.apache.sling.hc.api.execution.HealthCheckExecutionResult in project sling by apache.

the class HealthCheckResultCache method updateWith.

/**
     * Update the cache with the result
     */
public void updateWith(HealthCheckExecutionResult result) {
    final ExecutionResult executionResult = (ExecutionResult) result;
    cache.put(executionResult.getServiceId(), result);
}
Also used : HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult)

Example 32 with HealthCheckExecutionResult

use of org.apache.sling.hc.api.execution.HealthCheckExecutionResult in project sling by apache.

the class HealthCheckExecutorServlet method doGet.

protected void doGet(final HttpServletRequest request, final HttpServletResponse response, final String format) throws ServletException, IOException {
    HealthCheckSelector selector = HealthCheckSelector.empty();
    String pathInfo = request.getPathInfo();
    String pathTokensStr = StringUtils.removeStart(splitFormat(pathInfo)[0], "/");
    List<String> tags = new ArrayList<String>();
    List<String> names = new ArrayList<String>();
    if (StringUtils.isNotBlank(pathTokensStr)) {
        String[] pathTokens = pathTokensStr.split(PARAM_SPLIT_REGEX);
        for (String pathToken : pathTokens) {
            if (pathToken.indexOf(' ') >= 0) {
                // token contains space. assume it is a name
                names.add(pathToken);
            } else {
                tags.add(pathToken);
            }
        }
    }
    if (tags.size() == 0) {
        // if not provided via path use parameter or default
        tags = Arrays.asList(StringUtils.defaultIfEmpty(request.getParameter(PARAM_TAGS.name), "").split(PARAM_SPLIT_REGEX));
    }
    selector.withTags(tags.toArray(new String[0]));
    if (names.size() == 0) {
        // if not provided via path use parameter or default
        names = Arrays.asList(StringUtils.defaultIfEmpty(request.getParameter(PARAM_NAMES.name), "").split(PARAM_SPLIT_REGEX));
    }
    selector.withNames(names.toArray(new String[0]));
    final Boolean includeDebug = Boolean.valueOf(request.getParameter(PARAM_INCLUDE_DEBUG.name));
    final Map<Result.Status, Integer> statusMapping = request.getParameter(PARAM_HTTP_STATUS.name) != null ? getStatusMapping(request.getParameter(PARAM_HTTP_STATUS.name)) : null;
    HealthCheckExecutionOptions executionOptions = new HealthCheckExecutionOptions();
    executionOptions.setCombineTagsWithOr(Boolean.valueOf(StringUtils.defaultString(request.getParameter(PARAM_COMBINE_TAGS_WITH_OR.name), "true")));
    executionOptions.setForceInstantExecution(Boolean.valueOf(request.getParameter(PARAM_FORCE_INSTANT_EXECUTION.name)));
    String overrideGlobalTimeoutVal = request.getParameter(PARAM_OVERRIDE_GLOBAL_TIMEOUT.name);
    if (StringUtils.isNumeric(overrideGlobalTimeoutVal)) {
        executionOptions.setOverrideGlobalTimeout(Integer.valueOf(overrideGlobalTimeoutVal));
    }
    List<HealthCheckExecutionResult> executionResults = this.healthCheckExecutor.execute(selector, executionOptions);
    Result.Status mostSevereStatus = Result.Status.DEBUG;
    for (HealthCheckExecutionResult executionResult : executionResults) {
        Status status = executionResult.getHealthCheckResult().getStatus();
        if (status.ordinal() > mostSevereStatus.ordinal()) {
            mostSevereStatus = status;
        }
    }
    Result overallResult = new Result(mostSevereStatus, "Overall status " + mostSevereStatus);
    sendNoCacheHeaders(response);
    if (statusMapping != null) {
        Integer httpStatus = statusMapping.get(overallResult.getStatus());
        response.setStatus(httpStatus);
    }
    if (FORMAT_HTML.equals(format)) {
        sendHtmlResponse(overallResult, executionResults, request, response, includeDebug);
    } else if (FORMAT_JSON.equals(format)) {
        sendJsonResponse(overallResult, executionResults, null, response, includeDebug);
    } else if (FORMAT_JSONP.equals(format)) {
        String jsonpCallback = StringUtils.defaultIfEmpty(request.getParameter(PARAM_JSONP_CALLBACK.name), JSONP_CALLBACK_DEFAULT);
        sendJsonResponse(overallResult, executionResults, jsonpCallback, response, includeDebug);
    } else if (StringUtils.endsWith(format, FORMAT_TXT)) {
        sendTxtResponse(overallResult, response, StringUtils.equals(format, FORMAT_VERBOSE_TXT), executionResults, includeDebug);
    } else {
        response.setContentType("text/plain");
        response.getWriter().println("Invalid format " + format + " - supported formats: html|json|jsonp|txt|verbose.txt");
    }
}
Also used : Status(org.apache.sling.hc.api.Result.Status) ArrayList(java.util.ArrayList) Result(org.apache.sling.hc.api.Result) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Status(org.apache.sling.hc.api.Result.Status) HealthCheckSelector(org.apache.sling.hc.api.execution.HealthCheckSelector) HealthCheckExecutionOptions(org.apache.sling.hc.api.execution.HealthCheckExecutionOptions) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult)

Example 33 with HealthCheckExecutionResult

use of org.apache.sling.hc.api.execution.HealthCheckExecutionResult in project sling by apache.

the class ResultHtmlSerializer method serialize.

public String serialize(final Result overallResult, final List<HealthCheckExecutionResult> executionResults, String escapedHelpText, boolean includeDebug) {
    StringWriter stringWriter = new StringWriter();
    PrintWriter writer = new PrintWriter(stringWriter);
    writer.println("<html><head><title>System Health</title>" + "<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' /><style>" + styleString + "</style></head><body><h1>System Health</h1>");
    writer.println("<p><span class=\"" + getClassForStatus(overallResult.getStatus()) + "\"><strong>Overall Result: " + overallResult.getStatus() + "</strong></span></p>");
    final DateFormat dfShort = new SimpleDateFormat("HH:mm:ss.SSS");
    final DateFormat dfLong = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    writer.println("<table id=\"healthCheckResults\" cellspacing=\"0\">");
    writer.println("<thead><tr><th>Health Check <span style='color:gray'>(tags)</span></th><th>Status</th><th>Log</th><th>Finished At</th><th>Time</th></tr></thead>");
    for (HealthCheckExecutionResult executionResult : executionResults) {
        Result result = executionResult.getHealthCheckResult();
        List<String> tags = executionResult.getHealthCheckMetadata().getTags();
        boolean hasTags = tags != null && tags.size() > 0 && StringUtils.isNotBlank(tags.get(0));
        writer.print("<tr class=\"" + getClassForStatus(result.getStatus()) + "\">");
        writer.print("<td><p title=\"" + StringEscapeUtils.escapeHtml(executionResult.getHealthCheckMetadata().getName()) + "\">" + StringEscapeUtils.escapeHtml(executionResult.getHealthCheckMetadata().getTitle()) + "");
        if (hasTags) {
            writer.println("<br/><span style='color:gray'>" + StringEscapeUtils.escapeHtml(StringUtils.join(tags, ", ")) + "</span>");
        }
        writer.println("</p></td>");
        writer.println("<td style='font-weight:bold;'>" + StringEscapeUtils.escapeHtml(result.getStatus().toString()) + "</td>");
        writer.println("<td>");
        boolean isFirst = true;
        boolean isSingleResult = isSingleResult(result);
        for (Entry entry : result) {
            if (!includeDebug && entry.getStatus() == Result.Status.DEBUG) {
                continue;
            }
            if (isFirst) {
                isFirst = false;
            } else {
                writer.println("<br/>\n");
            }
            boolean showStatus = !isSingleResult && entry.getStatus() != Result.Status.DEBUG && entry.getStatus() != Result.Status.INFO;
            String message = StringEscapeUtils.escapeHtml(entry.getMessage());
            if (entry.getStatus() == Result.Status.DEBUG) {
                message = "<span style='color:gray'/>" + message + "</span>";
            }
            writer.println((showStatus ? StringEscapeUtils.escapeHtml(entry.getStatus().toString()) + " " : "") + message);
            Exception exception = entry.getException();
            if (exception != null) {
                writer.println("<span style='width:20px'/>" + StringEscapeUtils.escapeHtml(exception.toString()));
                writer.println("<!--");
                exception.printStackTrace(writer);
                writer.println("-->");
            }
        }
        writer.println("</td>");
        Date finishedAt = executionResult.getFinishedAt();
        writer.println("<td>" + (isToday(finishedAt) ? dfShort.format(finishedAt) : dfLong.format(finishedAt)) + "</td>");
        writer.println("<td>" + FormattingResultLog.msHumanReadable(executionResult.getElapsedTimeInMs()) + "</td>");
        writer.println("</tr>");
    }
    writer.println("</table>");
    writer.println("<div class='helpText'>");
    writer.println(escapedHelpText);
    writer.println("</div>");
    writer.println("</body></html>");
    return stringWriter.toString();
}
Also used : Entry(org.apache.sling.hc.api.ResultLog.Entry) StringWriter(java.io.StringWriter) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Date(java.util.Date) PrintWriter(java.io.PrintWriter) Result(org.apache.sling.hc.api.Result) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult)

Example 34 with HealthCheckExecutionResult

use of org.apache.sling.hc.api.execution.HealthCheckExecutionResult in project acs-aem-commons by Adobe-Consulting-Services.

the class HealthCheckStatusEmailerTest method resultToPlainText.

@Test
public void resultToPlainText() throws Exception {
    final List<HealthCheckExecutionResult> successResults = new ArrayList<>();
    successResults.add(successExecutionResult);
    final String actual = healthCheckStatusEmailer.resultToPlainText("HC Test", successResults);
    Matcher titleMatcher = Pattern.compile("^HC Test$", Pattern.MULTILINE).matcher(actual);
    Matcher entryMatcher = Pattern.compile("^\\[ OK \\]\\s+hc success$", Pattern.MULTILINE).matcher(actual);
    Matcher negativeMatcher = Pattern.compile("^\\[ CRTICAL \\]\\s+hc failure", Pattern.MULTILINE).matcher(actual);
    assertTrue(titleMatcher.find());
    assertTrue(entryMatcher.find());
    assertFalse(negativeMatcher.find());
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Test(org.junit.Test)

Aggregations

HealthCheckExecutionResult (org.apache.sling.hc.api.execution.HealthCheckExecutionResult)34 Result (org.apache.sling.hc.api.Result)18 Test (org.junit.Test)16 Date (java.util.Date)9 HealthCheckExecutionOptions (org.apache.sling.hc.api.execution.HealthCheckExecutionOptions)9 HealthCheckMetadata (org.apache.sling.hc.util.HealthCheckMetadata)8 ExecutionResult (org.apache.sling.hc.core.impl.executor.ExecutionResult)7 ArrayList (java.util.ArrayList)6 TreeSet (java.util.TreeSet)6 LinkedList (java.util.LinkedList)5 Matchers.anyString (org.mockito.Matchers.anyString)5 SimpleDateFormat (java.text.SimpleDateFormat)4 DateFormat (java.text.DateFormat)3 HealthCheckSelector (org.apache.sling.hc.api.execution.HealthCheckSelector)3 ServiceReference (org.osgi.framework.ServiceReference)3 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 HashSet (java.util.HashSet)2 ResultLog (org.apache.sling.hc.api.ResultLog)2 FormattingResultLog (org.apache.sling.hc.util.FormattingResultLog)2