Search in sources :

Example 1 with SyslogIF

use of org.graylog2.syslog4j.SyslogIF in project opennms by OpenNMS.

the class SyslogNorthbounder method forwardAlarms.

/**
     * Each implementation of the AbstractNorthbounder has a nice queue
     * (Nagle's algorithmic) and the worker thread that processes the queue
     * calls this method to send alarms to the northern NMS.
     *
     * @param alarms the alarms
     * @throws NorthbounderException the northbounder exception
     */
@Override
public void forwardAlarms(List<NorthboundAlarm> alarms) throws NorthbounderException {
    if (alarms == null) {
        String errorMsg = "No alarms in alarms list for syslog forwarding.";
        IllegalStateException e = new IllegalStateException(errorMsg);
        LOG.error(errorMsg, e);
        throw e;
    }
    LOG.info("Forwarding {} alarms to destination:{}", alarms.size(), m_destination.getName());
    SyslogIF instance;
    try {
        instance = Syslog.getInstance(m_destination.getName());
    } catch (SyslogRuntimeException e) {
        LOG.error("Could not find Syslog instance for destination: '{}': {}", m_destination.getName(), e);
        throw e;
    }
    /*
         * Iterate over the list of alarms to be forwarded N.
         */
    for (NorthboundAlarm alarm : alarms) {
        Integer count = alarm.getCount();
        if (count > 1 && m_destination.isFirstOccurrenceOnly()) {
            LOG.debug("Destination {} is configured for new alarm instances only.  Alarm has count of {}.", m_destination.getName(), count);
            continue;
        }
        LOG.debug("Creating formatted log message for alarm: {}.", alarm.getId());
        String syslogMessage;
        int level;
        try {
            LOG.debug("Making substitutions for tokens in message format for alarm: {}.", alarm.getId());
            String msgFormat = m_destination.getCustomMessageFormat(alarm);
            if (msgFormat == null) {
                msgFormat = getConfig().getMessageFormat();
            }
            syslogMessage = PropertiesUtils.substitute(msgFormat, createMapping(alarm));
            LOG.debug("Determining LOG_LEVEL for alarm: {}", alarm.getId());
            level = SyslogUtils.determineLogLevel(alarm.getSeverity());
            LOG.debug("Forwarding alarm: {} via syslog to destination: {}", alarm.getId(), m_destination.getName());
            instance.log(level, syslogMessage);
        } catch (Exception ex) {
            LOG.error("Caught exception sending to destination: '{}': {}", m_destination.getName(), ex);
        }
    }
}
Also used : SyslogIF(org.graylog2.syslog4j.SyslogIF) SyslogRuntimeException(org.graylog2.syslog4j.SyslogRuntimeException) NorthboundAlarm(org.opennms.netmgt.alarmd.api.NorthboundAlarm) SyslogRuntimeException(org.graylog2.syslog4j.SyslogRuntimeException) NorthbounderException(org.opennms.netmgt.alarmd.api.NorthbounderException)

Example 2 with SyslogIF

use of org.graylog2.syslog4j.SyslogIF in project opennms by OpenNMS.

the class SyslogEventForwarder method forward.

/**
     * Forwards an event.
     *
     * @param event the event to be forwarded
     * @param node the node associated with the event if apply
     */
public void forward(Event event, OnmsNode node) {
    if (initialized) {
        LOG.info("Forwarding event {} to destination:{}", event.getUei(), destination.getName());
        SyslogIF instance;
        try {
            instance = Syslog.getInstance(destination.getName());
        } catch (SyslogRuntimeException e) {
            LOG.error("Could not find Syslog instance for destination: '{}': {}", destination.getName(), e);
            return;
        }
        try {
            LOG.debug("Making substitutions for tokens in message format for event: {}.", event.getDbid());
            String msgFormat = null;
            for (SyslogFilter filter : destination.getFilters()) {
                if (passFilter(filter, event)) {
                    msgFormat = filter.getMessageFormat();
                }
            }
            if (msgFormat != null) {
                String syslogMessage = getTranslatedMessage(event, node, msgFormat);
                LOG.debug("Determining LOG_LEVEL for event: {}", event.getDbid());
                int level = SyslogUtils.determineLogLevel(OnmsSeverity.get(event.getSeverity()));
                LOG.debug("Forwarding event: {} via syslog to destination: {}", event.getDbid(), destination.getName());
                instance.log(level, syslogMessage);
            } else {
                LOG.warn("Can't find message format for the incoming. Check your destination's configuration.");
            }
        } catch (Exception ex) {
            LOG.error("Caught exception sending to destination: '{}': {}", destination.getName(), ex);
        }
    } else {
        LOG.error("Can't forward event {} because the facility has not been initialized.", event.getUei());
    }
}
Also used : SyslogFilter(org.opennms.netmgt.alarmd.northbounder.syslog.SyslogFilter) SyslogIF(org.graylog2.syslog4j.SyslogIF) SyslogRuntimeException(org.graylog2.syslog4j.SyslogRuntimeException) IOException(java.io.IOException) SyslogRuntimeException(org.graylog2.syslog4j.SyslogRuntimeException)

Example 3 with SyslogIF

use of org.graylog2.syslog4j.SyslogIF in project opennms by OpenNMS.

the class SyslogEventForwarder method shutdown.

/**
     * Shutdown.
     */
public void shutdown() {
    if (initialized) {
        try {
            LOG.info("Shutting down Syslog instance for destination: '{}'", destination.getName());
            initialized = false;
            SyslogIF instance = Syslog.getInstance(destination.getName());
            Syslog.destroyInstance(instance);
        } catch (SyslogRuntimeException e) {
            LOG.error("Could not find Syslog instance for destination: '{}': {}", destination.getName(), e);
            return;
        }
    } else {
        LOG.error("Can't shutdown a SYslog instance for an uninitialized forwarder.");
    }
}
Also used : SyslogIF(org.graylog2.syslog4j.SyslogIF) SyslogRuntimeException(org.graylog2.syslog4j.SyslogRuntimeException)

Aggregations

SyslogIF (org.graylog2.syslog4j.SyslogIF)3 SyslogRuntimeException (org.graylog2.syslog4j.SyslogRuntimeException)3 IOException (java.io.IOException)1 NorthboundAlarm (org.opennms.netmgt.alarmd.api.NorthboundAlarm)1 NorthbounderException (org.opennms.netmgt.alarmd.api.NorthbounderException)1 SyslogFilter (org.opennms.netmgt.alarmd.northbounder.syslog.SyslogFilter)1