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;
}
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 ======");
}
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;
}
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());
}
Aggregations