Search in sources :

Example 6 with NorthbounderException

use of org.opennms.netmgt.alarmd.api.NorthbounderException 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)

Aggregations

NorthbounderException (org.opennms.netmgt.alarmd.api.NorthbounderException)6 IOException (java.io.IOException)3 GeneralSecurityException (java.security.GeneralSecurityException)3 ClientProtocolException (org.apache.http.client.ClientProtocolException)3 NorthboundAlarm (org.opennms.netmgt.alarmd.api.NorthboundAlarm)3 URI (java.net.URI)2 HttpVersion (org.apache.http.HttpVersion)2 HttpResponseRange (org.opennms.core.utils.HttpResponseRange)2 HttpClientWrapper (org.opennms.core.web.HttpClientWrapper)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1 NameValuePair (org.apache.http.NameValuePair)1 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpEntityEnclosingRequestBase (org.apache.http.client.methods.HttpEntityEnclosingRequestBase)1 HttpGet (org.apache.http.client.methods.HttpGet)1