Search in sources :

Example 1 with DefaultVariableNameExpansion

use of org.opennms.features.ifttt.helper.DefaultVariableNameExpansion in project opennms by OpenNMS.

the class IfTttDaemon method startPoller.

/**
 * Initializes and starts the alarm poller.
 *
 * @param pollInterval the poller interval to be used
 */
private void startPoller(final long pollInterval) {
    LOG.debug("Starting alarm poller (interval {}s).", pollInterval);
    m_alarmPoller = Executors.newScheduledThreadPool(1);
    m_alarmPoller.scheduleWithFixedDelay(new Runnable() {

        private Map<Boolean, Map<String, Integer>> oldAlarmCount = new HashMap<>();

        private Map<Boolean, Map<String, OnmsSeverity>> oldSeverity = new HashMap<>();

        {
            oldAlarmCount.put(Boolean.TRUE, new HashMap<>());
            oldAlarmCount.put(Boolean.FALSE, new HashMap<>());
            oldSeverity.put(Boolean.TRUE, new HashMap<>());
            oldSeverity.put(Boolean.FALSE, new HashMap<>());
        }

        private List<OnmsAlarm> filterAlarms(List<OnmsAlarm> alarms, TriggerPackage triggerPackage) {
            if (triggerPackage.getOnlyUnacknowledged()) {
                return alarms.stream().filter(alarm -> alarm.getNodeId() != null).filter(alarm -> !alarm.isAcknowledged()).filter(alarm -> Strings.isNullOrEmpty(triggerPackage.getCategoryFilter()) || alarm.getNode().getCategories().stream().anyMatch(category -> category.getName().matches(triggerPackage.getCategoryFilter()))).collect(Collectors.toList());
            } else {
                return alarms.stream().filter(alarm -> alarm.getNodeId() != null).filter(alarm -> Strings.isNullOrEmpty(triggerPackage.getCategoryFilter()) || alarm.getNode().getCategories().stream().anyMatch(category -> category.getName().matches(triggerPackage.getCategoryFilter()))).collect(Collectors.toList());
            }
        }

        @Override
        public void run() {
            try {
                final IfTttConfig ifTttConfig = m_fileReloadContainer.getObject();
                if (ifTttConfig.getPollInterval() != pollInterval) {
                    restartPoller(ifTttConfig.getPollInterval());
                    return;
                }
                if (!ifTttConfig.getEnabled()) {
                    LOG.debug("Disabled - skipping alarm polling.");
                    return;
                }
                transactionOperations.execute(new TransactionCallbackWithoutResult() {

                    @Override
                    protected void doInTransactionWithoutResult(TransactionStatus status) {
                        // Retrieve the alarms with an associated node and filter for matching categories.
                        final CriteriaBuilder criteriaBuilder = new CriteriaBuilder(OnmsAlarm.class).isNotNull("node").gt("severity", OnmsSeverity.NORMAL);
                        final List<OnmsAlarm> alarms = alarmDao.findMatching(criteriaBuilder.toCriteria());
                        for (final TriggerPackage triggerPackage : ifTttConfig.getTriggerPackages()) {
                            if (!oldSeverity.get(triggerPackage.getOnlyUnacknowledged()).containsKey(triggerPackage.getCategoryFilter())) {
                                oldSeverity.get(triggerPackage.getOnlyUnacknowledged()).put(triggerPackage.getCategoryFilter(), OnmsSeverity.INDETERMINATE);
                                oldAlarmCount.get(triggerPackage.getOnlyUnacknowledged()).put(triggerPackage.getCategoryFilter(), 0);
                            }
                            final List<OnmsAlarm> filteredAlarms = filterAlarms(alarms, triggerPackage);
                            // Compute the maximum severity.
                            final Optional<OnmsSeverity> maxAlarmsSeverity = filteredAlarms.stream().map(OnmsAlarm::getSeverity).max(Comparator.naturalOrder());
                            final OnmsSeverity newSeverity = maxAlarmsSeverity.orElse(OnmsSeverity.NORMAL);
                            final int newAlarmCount = filteredAlarms.size();
                            LOG.debug("Received {} filtered, {} new severity", newAlarmCount, newSeverity);
                            final DefaultVariableNameExpansion defaultVariableNameExpansion = new DefaultVariableNameExpansion(oldSeverity.get(triggerPackage.getOnlyUnacknowledged()).get(triggerPackage.getCategoryFilter()), newSeverity, oldAlarmCount.get(triggerPackage.getOnlyUnacknowledged()).get(triggerPackage.getCategoryFilter()), newAlarmCount);
                            if (!newSeverity.equals(oldSeverity.get(triggerPackage.getOnlyUnacknowledged()).get(triggerPackage.getCategoryFilter())) || newAlarmCount != oldAlarmCount.get(triggerPackage.getOnlyUnacknowledged()).get(triggerPackage.getCategoryFilter())) {
                                fireIfTttTriggerSet(ifTttConfig, triggerPackage.getCategoryFilter(), newSeverity, defaultVariableNameExpansion);
                            }
                            LOG.debug("Old severity: {}, new severity: {}, old alarm count: {}, new alarm count: {}", oldSeverity.get(triggerPackage.getOnlyUnacknowledged()).get(triggerPackage.getCategoryFilter()), newSeverity, oldAlarmCount.get(triggerPackage.getOnlyUnacknowledged()).get(triggerPackage.getCategoryFilter()), newAlarmCount);
                            oldSeverity.get(triggerPackage.getOnlyUnacknowledged()).put(triggerPackage.getCategoryFilter(), newSeverity);
                            oldAlarmCount.get(triggerPackage.getOnlyUnacknowledged()).put(triggerPackage.getCategoryFilter(), newAlarmCount);
                        }
                    }
                });
            } catch (Exception e) {
                LOG.error("Error while polling alarm table.", e);
            } finally {
                LOG.debug("Run complete. Next poll in {}s.", pollInterval);
            }
        }
    }, pollInterval, pollInterval, TimeUnit.SECONDS);
}
Also used : Trigger(org.opennms.features.ifttt.config.Trigger) TriggerPackage(org.opennms.features.ifttt.config.TriggerPackage) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) OnmsAlarm(org.opennms.netmgt.model.OnmsAlarm) Strings(com.google.common.base.Strings) DefaultVariableNameExpansion(org.opennms.features.ifttt.helper.DefaultVariableNameExpansion) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) JaxbUtils(org.opennms.core.xml.JaxbUtils) FileReloadContainer(org.opennms.core.spring.FileReloadContainer) Logger(org.slf4j.Logger) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) IfTttConfig(org.opennms.features.ifttt.config.IfTttConfig) TriggerSet(org.opennms.features.ifttt.config.TriggerSet) Collectors(java.util.stream.Collectors) File(java.io.File) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Paths(java.nio.file.Paths) VariableNameExpansion(org.opennms.features.ifttt.helper.VariableNameExpansion) AlarmDao(org.opennms.netmgt.dao.api.AlarmDao) TransactionOperations(org.springframework.transaction.support.TransactionOperations) TransactionStatus(org.springframework.transaction.TransactionStatus) Optional(java.util.Optional) CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) Comparator(java.util.Comparator) IfTttTrigger(org.opennms.features.ifttt.helper.IfTttTrigger) OnmsSeverity(org.opennms.netmgt.model.OnmsSeverity) CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) TriggerPackage(org.opennms.features.ifttt.config.TriggerPackage) HashMap(java.util.HashMap) OnmsAlarm(org.opennms.netmgt.model.OnmsAlarm) TransactionStatus(org.springframework.transaction.TransactionStatus) OnmsSeverity(org.opennms.netmgt.model.OnmsSeverity) IfTttConfig(org.opennms.features.ifttt.config.IfTttConfig) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) DefaultVariableNameExpansion(org.opennms.features.ifttt.helper.DefaultVariableNameExpansion)

Aggregations

Strings (com.google.common.base.Strings)1 File (java.io.File)1 Paths (java.nio.file.Paths)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Executors (java.util.concurrent.Executors)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 CriteriaBuilder (org.opennms.core.criteria.CriteriaBuilder)1 FileReloadContainer (org.opennms.core.spring.FileReloadContainer)1 JaxbUtils (org.opennms.core.xml.JaxbUtils)1 IfTttConfig (org.opennms.features.ifttt.config.IfTttConfig)1 Trigger (org.opennms.features.ifttt.config.Trigger)1 TriggerPackage (org.opennms.features.ifttt.config.TriggerPackage)1 TriggerSet (org.opennms.features.ifttt.config.TriggerSet)1 DefaultVariableNameExpansion (org.opennms.features.ifttt.helper.DefaultVariableNameExpansion)1