Search in sources :

Example 6 with NagiosCheckResult

use of org.sakuli.services.forwarder.gearman.model.NagiosCheckResult in project sakuli by ConSol.

the class NagiosCheckResultBuilder method build.

@Override
public NagiosCheckResult build() {
    extractData(testSuite, gearmanProperties);
    NagiosCheckResult result = new NagiosCheckResult(queueName, uuid);
    SortedMap<PayLoadFields, String> payload = new TreeMap<>();
    payload.put(TYPE, type);
    //host name from properties file can overwrites the determined of the suite
    if (hostProperties != null) {
        payload.put(HOST, hostProperties);
    } else {
        payload.put(HOST, hostSuite);
    }
    payload.put(START_TIME, startTime);
    payload.put(FINISH_TIME, finishTime);
    payload.put(RETURN_CODE, returnCode);
    payload.put(SERVICE_DESC, serviceDesc);
    payload.put(OUTPUT, output.getOutputString());
    result.setPayload(payload);
    return result;
}
Also used : PayLoadFields(org.sakuli.services.forwarder.gearman.model.PayLoadFields) NagiosCheckResult(org.sakuli.services.forwarder.gearman.model.NagiosCheckResult) TreeMap(java.util.TreeMap)

Example 7 with NagiosCheckResult

use of org.sakuli.services.forwarder.gearman.model.NagiosCheckResult in project sakuli by ConSol.

the class GearmanResultServiceImpl method saveAllResults.

@Override
public void saveAllResults() {
    logger.info("======= SEND RESULTS TO GEARMAN SERVER ======");
    GearmanClient gearmanClient = getGearmanClient();
    GearmanJobServerConnection connection = getGearmanConnection(properties.getServerHost(), properties.getServerPort());
    List<NagiosCheckResult> results = new ArrayList<>();
    results.add(nagiosCheckResultBuilder.build());
    if (properties.isCacheEnabled()) {
        results.addAll(cacheService.getCachedResults());
        if (results.size() > 1) {
            logger.info(String.format("Processing %s cached results first", results.size() - 1));
        }
    }
    try {
        if (!gearmanClient.addJobServer(connection)) {
            exceptionHandler.handleException(new SakuliForwarderException(String.format("Failed to connect to Gearman server '%s:%s'", properties.getServerHost(), properties.getServerPort())), true);
        } else {
            //sending in reverse original happened order
            Collections.reverse(results);
            results = results.stream().filter(checkResult -> !sendResult(gearmanClient, checkResult)).collect(Collectors.toList());
            Collections.reverse(results);
        }
    } catch (Exception e) {
        exceptionHandler.handleException(new SakuliForwarderException(e, String.format("Could not transfer Sakuli results to the Gearman server '%s:%s'", properties.getServerHost(), properties.getServerPort())), true);
    }
    //save all not send results
    if (properties.isCacheEnabled()) {
        cacheService.cacheResults(results);
    }
    gearmanClient.shutdown();
    logger.info("======= FINISHED: SEND RESULTS TO GEARMAN SERVER ======");
}
Also used : GearmanJobServerConnection(org.gearman.common.GearmanJobServerConnection) ArrayList(java.util.ArrayList) NagiosCheckResult(org.sakuli.services.forwarder.gearman.model.NagiosCheckResult) SakuliForwarderException(org.sakuli.exceptions.SakuliForwarderException) SakuliForwarderException(org.sakuli.exceptions.SakuliForwarderException)

Example 8 with NagiosCheckResult

use of org.sakuli.services.forwarder.gearman.model.NagiosCheckResult in project sakuli by ConSol.

the class GearmanCacheService method getCachedResults.

/**
     * Reads cached results from Gearman cache file.
     * @return
     */
public List<NagiosCheckResult> getCachedResults() {
    List<NagiosCheckResult> results = new ArrayList<>();
    Path cacheFile = testSuiteProperties.getTestSuiteFolder().resolve(CACHE_FILE);
    if (Files.exists(cacheFile)) {
        try {
            List<String> lines = FileUtils.readLines(cacheFile.toFile(), Charset.forName(CHARSET_NAME));
            StringBuilder resultBuilder = new StringBuilder();
            String queueName = "";
            String uuid = "";
            for (String line : lines) {
                if (line.trim().equals(CACHE_SEPARATOR)) {
                    results.add(new NagiosCachedCheckResult(queueName, uuid, resultBuilder.toString()));
                } else if (line.startsWith(CACHE_SEPARATOR)) {
                    resultBuilder = new StringBuilder();
                    queueName = line.substring(CACHE_SEPARATOR.length() + 1, line.indexOf(":"));
                    uuid = line.substring(line.indexOf(":") + 1).trim();
                } else if (StringUtils.isNotEmpty(line)) {
                    resultBuilder.append(line).append(LINE_SEPARATOR);
                }
            }
        } catch (IOException e) {
            exceptionHandler.handleException(new SakuliForwarderException(e, String.format("Failed to read Gearman cache file '%s'", cacheFile)), true);
        }
    }
    return results;
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) NagiosCheckResult(org.sakuli.services.forwarder.gearman.model.NagiosCheckResult) NagiosCachedCheckResult(org.sakuli.services.forwarder.gearman.model.NagiosCachedCheckResult) SakuliForwarderException(org.sakuli.exceptions.SakuliForwarderException)

Example 9 with NagiosCheckResult

use of org.sakuli.services.forwarder.gearman.model.NagiosCheckResult in project sakuli by ConSol.

the class GearmanCacheServiceTest method testCacheResultsEmptyAndNew.

@Test
public void testCacheResultsEmptyAndNew() throws Exception {
    when(testSuiteProperties.getTestSuiteFolder()).thenReturn(Paths.get("target"));
    when(gearmanProperties.getServerQueue()).thenReturn("check_results");
    List<NagiosCheckResult> results = new ArrayList<>();
    testling.cacheResults(results);
    Assert.assertEquals(testling.getCachedResults().size(), 0L);
    NagiosCheckResult newResult = checkResultBuilder.build();
    results.add(newResult);
    testling.cacheResults(results);
    results = testling.getCachedResults();
    Assert.assertEquals(results.size(), 1L);
    Assert.assertEquals(results.get(0).getQueueName(), "check_results");
    Assert.assertEquals(results.get(0).getUuid(), newResult.getUuid());
    Assert.assertEquals(results.get(0).getPayloadString().trim(), newResult.getPayloadString().trim());
    NagiosCheckResult newResult2 = checkResultBuilder.build();
    results.add(newResult2);
    testling.cacheResults(results);
    results = testling.getCachedResults();
    Assert.assertEquals(results.size(), 2L);
    Assert.assertEquals(results.get(0).getQueueName(), "check_results");
    Assert.assertEquals(results.get(0).getUuid(), newResult.getUuid());
    Assert.assertEquals(results.get(0).getPayloadString().trim(), newResult.getPayloadString().trim());
    Assert.assertEquals(results.get(1).getQueueName(), "check_results");
    Assert.assertEquals(results.get(1).getUuid(), newResult2.getUuid());
    Assert.assertEquals(results.get(1).getPayloadString().trim(), newResult2.getPayloadString().trim());
}
Also used : NagiosCheckResult(org.sakuli.services.forwarder.gearman.model.NagiosCheckResult) BaseTest(org.sakuli.BaseTest) Test(org.testng.annotations.Test)

Aggregations

NagiosCheckResult (org.sakuli.services.forwarder.gearman.model.NagiosCheckResult)9 BaseTest (org.sakuli.BaseTest)5 Test (org.testng.annotations.Test)5 GearmanJobServerConnection (org.gearman.common.GearmanJobServerConnection)4 NagiosCachedCheckResult (org.sakuli.services.forwarder.gearman.model.NagiosCachedCheckResult)4 SakuliForwarderException (org.sakuli.exceptions.SakuliForwarderException)3 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 TreeMap (java.util.TreeMap)1 Future (java.util.concurrent.Future)1 TestCaseExampleBuilder (org.sakuli.builder.TestCaseExampleBuilder)1 TestCaseStepExampleBuilder (org.sakuli.builder.TestCaseStepExampleBuilder)1 TestSuiteExampleBuilder (org.sakuli.builder.TestSuiteExampleBuilder)1 TestSuite (org.sakuli.datamodel.TestSuite)1 SakuliActionException (org.sakuli.exceptions.SakuliActionException)1 SakuliRuntimeException (org.sakuli.exceptions.SakuliRuntimeException)1 PayLoadFields (org.sakuli.services.forwarder.gearman.model.PayLoadFields)1