Search in sources :

Example 1 with SyslogRuntimeException

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

the class SyslogUtils method createNorthboundInstance.

/**
     * Creates the northbound instance.
     *
     * @param destination the destination
     * @throws SyslogRuntimeException the syslog runtime exception
     */
public static void createNorthboundInstance(SyslogDestination destination) throws SyslogRuntimeException {
    LOG.info("Creating Syslog Northbound Instance {}", destination.getName());
    int facility = convertFacility(destination.getFacility());
    SyslogProtocol protocol = destination.getProtocol();
    SyslogConfigIF instanceConfiguration = createConfig(destination, protocol, facility);
    instanceConfiguration.setIdent("OpenNMS");
    instanceConfiguration.setCharSet(destination.getCharSet());
    instanceConfiguration.setMaxMessageLength(destination.getMaxMessageLength());
    instanceConfiguration.setSendLocalName(destination.isSendLocalName());
    instanceConfiguration.setSendLocalTimestamp(destination.isSendLocalTime());
    instanceConfiguration.setTruncateMessage(destination.isTruncateMessage());
    instanceConfiguration.setUseStructuredData(SyslogConstants.USE_STRUCTURED_DATA_DEFAULT);
    try {
        Syslog.createInstance(destination.getName(), instanceConfiguration);
    } catch (SyslogRuntimeException e) {
        LOG.error("Could not create northbound instance, '{}': {}", destination.getName(), e);
        throw e;
    }
}
Also used : SyslogConfigIF(org.graylog2.syslog4j.SyslogConfigIF) SyslogRuntimeException(org.graylog2.syslog4j.SyslogRuntimeException) SyslogProtocol(org.opennms.netmgt.alarmd.northbounder.syslog.SyslogDestination.SyslogProtocol)

Example 2 with SyslogRuntimeException

use of org.graylog2.syslog4j.SyslogRuntimeException 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 3 with SyslogRuntimeException

use of org.graylog2.syslog4j.SyslogRuntimeException 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 4 with SyslogRuntimeException

use of org.graylog2.syslog4j.SyslogRuntimeException 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

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