Search in sources :

Example 1 with ScanReportLog

use of org.opennms.netmgt.model.ScanReportLog 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);
}
Also used : Arrays(java.util.Arrays) PolledService(org.opennms.netmgt.poller.remote.PolledService) ServicePollStateChangedListener(org.opennms.netmgt.poller.remote.ServicePollStateChangedListener) ServicePollState(org.opennms.netmgt.poller.remote.ServicePollState) ScanReportLog(org.opennms.netmgt.model.ScanReportLog) Date(java.util.Date) PollerFrontEnd(org.opennms.netmgt.poller.remote.PollerFrontEnd) ToStringBuilder(org.apache.commons.lang.builder.ToStringBuilder) LoggerFactory(org.slf4j.LoggerFactory) Level(org.apache.logging.log4j.Level) HashMap(java.util.HashMap) DistributionContext(org.opennms.netmgt.poller.DistributionContext) PollStatus(org.opennms.netmgt.poller.PollStatus) InitializingBean(org.springframework.beans.factory.InitializingBean) InetAddress(java.net.InetAddress) ScanReport(org.opennms.netmgt.model.ScanReport) Map(java.util.Map) PollerBackEnd(org.opennms.netmgt.poller.remote.PollerBackEnd) ScanReportPollResult(org.opennms.netmgt.model.ScanReportPollResult) LinkedList(java.util.LinkedList) PropertyChangeEvent(java.beans.PropertyChangeEvent) ConfigurationChangedListener(org.opennms.netmgt.poller.remote.ConfigurationChangedListener) Properties(java.util.Properties) Logger(org.slf4j.Logger) HOST_ADDRESS_KEY(org.opennms.netmgt.poller.remote.PollerBackEnd.HOST_ADDRESS_KEY) TimeAdjustment(org.opennms.netmgt.poller.remote.TimeAdjustment) InetAddressUtils(org.opennms.core.utils.InetAddressUtils) Collection(java.util.Collection) ObjectUtils(org.springframework.util.ObjectUtils) HOST_NAME_KEY(org.opennms.netmgt.poller.remote.PollerBackEnd.HOST_NAME_KEY) Set(java.util.Set) Collectors(java.util.stream.Collectors) PollService(org.opennms.netmgt.poller.remote.PollService) List(java.util.List) PropertyChangeListener(java.beans.PropertyChangeListener) DisposableBean(org.springframework.beans.factory.DisposableBean) PollerConfiguration(org.opennms.netmgt.poller.remote.PollerConfiguration) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) Assert(org.springframework.util.Assert) PollStatus(org.opennms.netmgt.poller.PollStatus) ScanReport(org.opennms.netmgt.model.ScanReport) Date(java.util.Date) ScanReportLog(org.opennms.netmgt.model.ScanReportLog) PolledService(org.opennms.netmgt.poller.remote.PolledService) ToStringBuilder(org.apache.commons.lang.builder.ToStringBuilder) ScanReportPollResult(org.opennms.netmgt.model.ScanReportPollResult) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with ScanReportLog

use of org.opennms.netmgt.model.ScanReportLog 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!"));
}
Also used : PollStatus(org.opennms.netmgt.poller.PollStatus) ScanReportLog(org.opennms.netmgt.model.ScanReportLog) ScanReport(org.opennms.netmgt.model.ScanReport) ScanReportPollResult(org.opennms.netmgt.model.ScanReportPollResult) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Date (java.util.Date)2 ScanReport (org.opennms.netmgt.model.ScanReport)2 ScanReportLog (org.opennms.netmgt.model.ScanReportLog)2 ScanReportPollResult (org.opennms.netmgt.model.ScanReportPollResult)2 PollStatus (org.opennms.netmgt.poller.PollStatus)2 PropertyChangeEvent (java.beans.PropertyChangeEvent)1 PropertyChangeListener (java.beans.PropertyChangeListener)1 InetAddress (java.net.InetAddress)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Properties (java.util.Properties)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 ToStringBuilder (org.apache.commons.lang.builder.ToStringBuilder)1 Level (org.apache.logging.log4j.Level)1