use of org.opennms.netmgt.model.ScanReportPollResult in project opennms by OpenNMS.
the class ScanReportPollerFrontEnd method performServiceScans.
/**
* Perform all scans for a given location and return the results as a {@link ScanReport}
* object. To filter by application, specify the selected applications by using the
* {@link #setSelectedApplications(Set)} method.
*/
private void performServiceScans() {
firePropertyChange(ScanReportProperties.percentageComplete.toString(), null, 0.0);
ScanReport scanReport = new ScanReport();
scanReport.setLocation(m_location);
// scanReport.addProperty("monitoring-system-id", getMonitoringSystemId());
scanReport.setTimestamp(new Date());
// Add all of the OS and connection metadata to the scan report
for (final Map.Entry<String, String> entry : getDetails().entrySet()) {
scanReport.addProperty(entry);
}
// Add all of the metadata to the scan report
for (final Map.Entry<String, String> entry : m_metadata.entrySet()) {
scanReport.addProperty(entry);
}
// Add the selected applications as scan report metadata
if (m_selectedApplications != null && m_selectedApplications.size() > 0) {
scanReport.addProperty("applications", m_selectedApplications.stream().collect(Collectors.joining(", ")));
}
// Create a log appender that will capture log output to the root logger
Log4j2StringAppender appender = Log4j2StringAppender.createAppender();
appender.start();
try {
m_pollService.setServiceMonitorLocators(m_backEnd.getServiceMonitorLocators(DistributionContext.REMOTE_MONITOR));
m_pollerConfiguration = retrieveLatestConfiguration();
appender.addToLogger(LogManager.ROOT_LOGGER_NAME, Level.DEBUG);
Set<PolledService> polledServices = getPolledServices().stream().filter(s -> matchesApplications(s, m_selectedApplications)).collect(Collectors.toSet());
PolledService[] services = polledServices.toArray(POLLED_SERVICE_ARRAY);
LOG.debug("Polling {} services.", services.length);
for (int i = 0; i < services.length; i++) {
PolledService service = services[i];
try {
final PollStatus result = doPoll(service);
if (result == null) {
LOG.warn("Null poll result for service {}", service.getServiceId());
} else {
LOG.info(new ToStringBuilder(this).append("statusName", result.getStatusName()).append("reason", result.getReason()).toString());
scanReport.addPollResult(new ScanReportPollResult(service.getSvcName(), service.getServiceId(), service.getNodeLabel(), service.getNodeId(), service.getIpAddr(), result));
}
} catch (Throwable e) {
LOG.error("Unexpected exception occurred while polling service ID {}", service.getServiceId(), e);
setState(new FatalExceptionOccurred(e));
}
firePropertyChange(ScanReportProperties.percentageComplete.toString(), null, ((double) i / (double) services.length));
}
} catch (final Throwable e) {
LOG.error("Error while performing scan", e);
} finally {
// Remove the log appender from the root logger
appender.removeFromLogger(LogManager.ROOT_LOGGER_NAME);
}
// Set the percentage complete to 100%
firePropertyChange(ScanReportProperties.percentageComplete.toString(), null, 1.0);
scanReport.setLog(new ScanReportLog(scanReport.getId(), appender.getOutput()));
LOG.debug("Returning scan report: {}", scanReport);
/*
LOG.debug("=============== Scan report log START ===============");
LOG.debug("Scan report log: '{}'", appender.getOutput());
LOG.debug("=============== Scan report log END ===============");
*/
// Fire an exitNecessary event with the scanReport as the parameter
firePropertyChange(PollerFrontEndStates.exitNecessary.toString(), null, scanReport);
}
use of org.opennms.netmgt.model.ScanReportPollResult in project opennms by OpenNMS.
the class PollerBackEndTest method testUnsuccessfulScanReportMessage.
public void testUnsuccessfulScanReportMessage() {
expect(m_scanReportDao.save(EasyMock.anyObject(ScanReport.class))).andReturn("");
m_mocks.replayAll();
List<ScanReportPollResult> scanReportPollResults = new ArrayList<>();
scanReportPollResults.add(new ScanReportPollResult("ICMP", 1, "Test Node", 1, "127.0.0.1", PollStatus.available(20.0)));
scanReportPollResults.add(new ScanReportPollResult("HTTP", 2, "Test Node", 1, "127.0.0.1", PollStatus.unavailable("Weasels ate my HTTP server")));
scanReportPollResults.add(new ScanReportPollResult("SNMP", 3, "Test Node", 1, "127.0.0.1", PollStatus.available(400.0)));
scanReportPollResults.add(new ScanReportPollResult("POP3", 3, "Test Node", 1, "127.0.0.1", PollStatus.available(300.0)));
scanReportPollResults.add(new ScanReportPollResult("IMAP", 4, "Test Node", 1, "127.0.0.1", PollStatus.unavailable("Kiwis infested my mail server")));
ScanReport report = new ScanReport();
report.setId(UUID.randomUUID().toString());
report.setPollResults(scanReportPollResults);
m_backEnd.reportSingleScan(report);
// Fetch the event that was sent
Event unsuccessfulScanEvent = m_eventIpcManager.getEventAnticipator().getUnanticipatedEvents().iterator().next();
assertTrue(unsuccessfulScanEvent.getParm(DefaultPollerBackEnd.PARM_SCAN_REPORT_FAILURE_MESSAGE).getValue().getContent(), unsuccessfulScanEvent.getParm(DefaultPollerBackEnd.PARM_SCAN_REPORT_FAILURE_MESSAGE).getValue().getContent().contains("2 out of 5 service polls failed"));
}
use of org.opennms.netmgt.model.ScanReportPollResult in project opennms by OpenNMS.
the class ScanReportTest method testSerialization.
/**
* Test the JAXB marshaling of the {@link ScanReport} class.
*/
@Test
public void testSerialization() throws Exception {
ScanReport report = new ScanReport();
report.addProperty("customer-account-number", "12345");
report.addProperty("customer-name", "Zombo.com");
report.addProperty("reference-id", "ABZ135");
report.addProperty("time-zone", "-5:00");
report.setLocale("en-US");
report.setLocation("RDU");
report.setTimestamp(new Date());
report.setLog(new ScanReportLog(report.getId(), "Hey, a log!"));
for (int i = 0; i < 5; i++) {
PollStatus status = PollStatus.get(PollStatus.SERVICE_AVAILABLE, "Anything is possible", 4.5d);
status.setProperty("whatever", 2.0);
report.addPollResult(new ScanReportPollResult("Foo", 1, "zombonode", 1, "1.2.3.4", status));
}
String reportString = JaxbUtils.marshal(report);
LOG.debug("Report string: \n " + reportString);
assertTrue(reportString.contains("<key>customer-account-number</key>"));
assertTrue(reportString.contains("<value>12345</value>"));
assertTrue(reportString.contains("<key>customer-name</key>"));
assertTrue(reportString.contains("<value>Zombo.com</value>"));
assertTrue(reportString.contains("response-time=\"4.5\""));
// object is not serialized as JAXB/JSON, only to the database
assertFalse(reportString.contains("a log!"));
}
use of org.opennms.netmgt.model.ScanReportPollResult in project opennms by OpenNMS.
the class DefaultPollerBackEnd method reportSingleScan.
@Override
public void reportSingleScan(final ScanReport report) {
if (report == null) {
throw new IllegalArgumentException("ScanReport cannot be null");
}
LOG.info("Scan report complete: {}", report);
m_scanReportDao.save(report);
if (report.getPollResults().stream().allMatch(a -> {
return a.getPollStatus().isAvailable();
})) {
// If all polls returned 'available' then send the success event
sendSuccessfulScanReportEvent(report.getId(), report.getLocation());
} else {
// Otherwise send the unsuccessful event
int total = 0;
int failed = 0;
final StringBuilder failedPollResults = new StringBuilder();
for (ScanReportPollResult result : report.getPollResults()) {
total++;
if (!result.getPollStatus().isAvailable()) {
failed++;
failedPollResults.append(String.format(FAILED_POLL_RESULT_MESSAGE_FORMAT, result.getNodeLabel(), result.getIpAddress(), result.getServiceName(), result.getPollStatus().getReason()));
}
}
StringBuffer finalMessage = new StringBuffer();
finalMessage.append(String.format(FAILURE_SUMMARY_MESSAGE_FORMAT, failed, total));
finalMessage.append("<ul>");
finalMessage.append(failedPollResults);
finalMessage.append("</ul>");
sendUnsuccessfulScanReportEvent(report.getId(), report.getLocation(), finalMessage.toString());
}
}
Aggregations