Search in sources :

Example 6 with PollableNode

use of org.opennms.netmgt.poller.pollables.PollableNode in project opennms by OpenNMS.

the class PollerEventProcessor method serviceReschedule.

private void serviceReschedule(Long nodeId, String nodeLabel, String nodeLocation, Event sourceEvent, boolean rescheduleExisting) {
    if (nodeId == null || nodeId <= 0) {
        LOG.warn("Invalid node ID for event, skipping service reschedule: {}", sourceEvent);
        return;
    }
    Date closeDate = sourceEvent.getTime();
    final Set<Service> databaseServices = new HashSet<>();
    for (final String[] s : getPoller().getQueryManager().getNodeServices(nodeId.intValue())) {
        databaseServices.add(new Service(s));
    }
    LOG.debug("# of Services in Database: {}", databaseServices.size());
    LOG.trace("Database Services: {}", databaseServices);
    final Set<Service> polledServices = new HashSet<>();
    final PollableNode pnode = getNetwork().getNode(nodeId.intValue());
    if (pnode == null) {
        LOG.debug("Node {} is not already being polled.", nodeId);
    } else {
        if (pnode.getNodeLabel() != null) {
            nodeLabel = pnode.getNodeLabel();
        }
        for (final PollableInterface iface : pnode.getInterfaces()) {
            for (final PollableService s : iface.getServices()) {
                polledServices.add(new Service(s.getIpAddr(), s.getSvcName()));
            }
        }
        LOG.debug("# of Polled Services: {}", polledServices.size());
        LOG.trace("Polled Services: {}", polledServices);
    }
    // if any of these are no longer in the database, then remove them
    for (final Iterator<Service> iter = polledServices.iterator(); iter.hasNext(); ) {
        final Service polledService = iter.next();
        if (!databaseServices.contains(polledService)) {
            // We are polling the service, but it no longer exists.  Stop polling.
            if (pnode != null) {
                final PollableService service = pnode.getService(polledService.getInetAddress(), polledService.getServiceName());
                // Delete the service
                service.delete();
                while (!service.isDeleted()) {
                    try {
                        Thread.sleep(20);
                    } catch (final InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                }
            }
            // Close outages.
            LOG.debug("{} should no longer be polled.  Resolving outages.", polledService);
            closeOutagesForService(sourceEvent, nodeId, closeDate, polledService);
            iter.remove();
        }
    }
    // Delete the remaining services if we which to reschedule those that are already active
    if (rescheduleExisting && pnode != null) {
        for (final Iterator<Service> iter = polledServices.iterator(); iter.hasNext(); ) {
            final Service polledService = iter.next();
            final PollableService service = pnode.getService(polledService.getInetAddress(), polledService.getServiceName());
            // Delete the service
            service.delete();
            while (!service.isDeleted()) {
                try {
                    Thread.sleep(20);
                } catch (final InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
            iter.remove();
        }
    }
    // Schedule all of the services, if they are not already
    for (final Service databaseService : databaseServices) {
        if (polledServices.contains(databaseService)) {
            LOG.debug("{} is being skipped. Already scheduled.", databaseService);
            continue;
        }
        LOG.debug("{} is being scheduled (or rescheduled) for polling.", databaseService);
        getPoller().scheduleService(nodeId.intValue(), nodeLabel, nodeLocation, databaseService.getAddress(), databaseService.getServiceName());
        if (!getPollerConfig().isPolled(databaseService.getAddress(), databaseService.getServiceName())) {
            LOG.debug("{} is no longer polled.  Closing any pending outages.", databaseService);
            closeOutagesForService(sourceEvent, nodeId, closeDate, databaseService);
        }
    }
}
Also used : PollableNode(org.opennms.netmgt.poller.pollables.PollableNode) PollableService(org.opennms.netmgt.poller.pollables.PollableService) PollableService(org.opennms.netmgt.poller.pollables.PollableService) PollableInterface(org.opennms.netmgt.poller.pollables.PollableInterface) Date(java.util.Date) HashSet(java.util.HashSet)

Aggregations

PollableNode (org.opennms.netmgt.poller.pollables.PollableNode)6 Date (java.util.Date)3 Parm (org.opennms.netmgt.xml.event.Parm)3 Value (org.opennms.netmgt.xml.event.Value)3 PollableInterface (org.opennms.netmgt.poller.pollables.PollableInterface)2 PollableService (org.opennms.netmgt.poller.pollables.PollableService)2 InetAddress (java.net.InetAddress)1 HashSet (java.util.HashSet)1 OnmsMonitoredService (org.opennms.netmgt.model.OnmsMonitoredService)1 TransactionStatus (org.springframework.transaction.TransactionStatus)1 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)1