use of org.opennms.netmgt.model.ScanReport 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.ScanReport 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.ScanReport in project opennms by OpenNMS.
the class CriteriaBuilderSearchVisitorIT method testScanReportAndCondition.
@Test
public void testScanReportAndCondition() {
CriteriaBuilder builder = new ScanReportRestService().getCriteriaBuilder(null);
CriteriaBuilderSearchVisitor<ScanReport, ScanReport> visitor = new CriteriaBuilderSearchVisitor<>(builder, ScanReport.class);
// Simulates /opennms/api/v2/scanreports?_s=applications%3D%3DLocal+Access;timestamp%3Dle%3D2016-02-01T15:07:14.340-0500&limit=20&offset=0&order=desc&orderBy=timestamp
List<SearchCondition<ScanReport>> conditions = new ArrayList<SearchCondition<ScanReport>>();
conditions.add(new PrimitiveSearchCondition<ScanReport>("applications", "blah", String.class, ConditionType.EQUALS, new ScanReport()));
conditions.add(new PrimitiveSearchCondition<ScanReport>("timestamp", new Date(), Date.class, ConditionType.LESS_OR_EQUALS, new ScanReport()));
SearchCondition<ScanReport> andCondition = new AndSearchCondition<ScanReport>(conditions);
visitor.visit(andCondition);
Criteria criteria = visitor.getQuery().toCriteria();
System.out.println(criteria.toString());
m_dao.countMatching(criteria);
}
use of org.opennms.netmgt.model.ScanReport in project opennms by OpenNMS.
the class CriteriaBuilderSearchVisitorIT method testScanReportTwoConditions.
@Test
public void testScanReportTwoConditions() {
CriteriaBuilder builder = new ScanReportRestService().getCriteriaBuilder(null);
CriteriaBuilderSearchVisitor<ScanReport, ScanReport> visitor = new CriteriaBuilderSearchVisitor<>(builder, ScanReport.class);
visitor.visit(new PrimitiveSearchCondition<ScanReport>("applications", "blah", String.class, ConditionType.EQUALS, new ScanReport()));
visitor.visit(new PrimitiveSearchCondition<ScanReport>("timestamp", new Date(), Date.class, ConditionType.LESS_OR_EQUALS, new ScanReport()));
Criteria criteria = visitor.getQuery().toCriteria();
System.out.println(criteria.toString());
m_dao.countMatching(criteria);
}
use of org.opennms.netmgt.model.ScanReport in project opennms by OpenNMS.
the class ScanReportPollerFrontEndIT method testRegister.
@Test
public void testRegister() throws Exception {
// Check preconditions
assertFalse(m_frontEnd.isRegistered());
assertEquals(new Integer(1), m_jdbcTemplate.queryForObject("select count(*) from monitoringsystems", Integer.class));
assertEquals(new Integer(0), m_jdbcTemplate.queryForObject("select count(*) from monitoringsystemsproperties", Integer.class));
assertTrue("There were unexpected poll results", 0 == m_jdbcTemplate.queryForObject("select count(*) from location_specific_status_changes", Integer.class));
// Add a PropertyChangeListener that will report the scan result to
// the PollerBackEnd
m_frontEnd.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals(PollerFrontEndStates.exitNecessary.toString())) {
final ScanReport report = (ScanReport) evt.getNewValue();
System.out.println("Finished scan: " + report);
m_backEnd.reportSingleScan(report);
// uei.opennms.org/test <-- Standard test event
// uei.opennms.org/remote/unsuccessfulScanReport
assertEquals(2, getEventCount());
queryEvents();
// Check to see if the expected metadata was stored in the database
assertEquals(System.getProperty("os.arch"), m_jdbcTemplate.queryForObject("select propertyValue from scanreportproperties where scanreportid = ? and property = ?", String.class, report.getId(), "os.arch"));
assertEquals(System.getProperty("os.name"), m_jdbcTemplate.queryForObject("select propertyValue from scanreportproperties where scanreportid = ? and property = ?", String.class, report.getId(), "os.name"));
assertEquals(System.getProperty("os.version"), m_jdbcTemplate.queryForObject("select propertyValue from scanreportproperties where scanreportid = ? and property = ?", String.class, report.getId(), "os.version"));
m_frontEnd.stop();
}
}
});
m_frontEnd.initialize();
// Initialization shouldn't change anything since we're unregistered
assertFalse(m_frontEnd.isRegistered());
assertEquals(new Integer(1), m_jdbcTemplate.queryForObject("select count(*) from monitoringsystems", Integer.class));
assertEquals(new Integer(0), m_jdbcTemplate.queryForObject("select count(*) from monitoringsystemsproperties", Integer.class));
assertTrue("There were unexpected poll results", 0 == m_jdbcTemplate.queryForObject("select count(*) from location_specific_status_changes", Integer.class));
// Start up the remote poller
m_frontEnd.register("RDU");
assertTrue("Front end not started!", m_frontEnd.isStarted());
// String monitorId = m_frontEnd.getMonitoringSystemId();
assertTrue(m_frontEnd.isRegistered());
for (Map.Entry<String, String> entry : ((ScanReportPollerFrontEnd) m_frontEnd).getDetails().entrySet()) {
LOG.info("Front end detail: " + entry.getKey() + " -> " + entry.getValue());
}
}
Aggregations