Search in sources :

Example 1 with NorthbounderException

use of org.opennms.netmgt.alarmd.api.NorthbounderException in project opennms by OpenNMS.

the class HttpNorthbounder method forwardAlarms.

/* (non-Javadoc)
     * @see org.opennms.netmgt.alarmd.api.support.AbstractNorthbounder#forwardAlarms(java.util.List)
     */
@Override
public void forwardAlarms(List<NorthboundAlarm> alarms) throws NorthbounderException {
    LOG.info("Forwarding {} alarms", alarms.size());
    //Need a configuration bean for these
    int connectionTimeout = 3000;
    int socketTimeout = 3000;
    Integer retryCount = Integer.valueOf(3);
    URI uri = m_config.getURI();
    final HttpClientWrapper clientWrapper = HttpClientWrapper.create().setConnectionTimeout(connectionTimeout).setSocketTimeout(socketTimeout).setRetries(retryCount).useBrowserCompatibleCookies();
    if (m_config.getVirtualHost() != null && !m_config.getVirtualHost().trim().isEmpty()) {
        clientWrapper.setVirtualHost(m_config.getVirtualHost());
    }
    if (m_config.getUserAgent() != null && !m_config.getUserAgent().trim().isEmpty()) {
        clientWrapper.setUserAgent(m_config.getUserAgent());
    }
    if ("https".equals(uri.getScheme())) {
        try {
            clientWrapper.useRelaxedSSL("https");
        } catch (final GeneralSecurityException e) {
            throw new NorthbounderException("Failed to configure HTTP northbounder for relaxed SSL.", e);
        }
    }
    HttpUriRequest method = null;
    if (HttpMethod.POST == (m_config.getMethod())) {
        HttpPost postMethod = new HttpPost(uri);
        //TODO: need to configure these
        List<NameValuePair> postParms = new ArrayList<NameValuePair>();
        //FIXME:do this for now
        NameValuePair p = new BasicNameValuePair("foo", "bar");
        postParms.add(p);
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParms, StandardCharsets.UTF_8);
        postMethod.setEntity(formEntity);
        HttpEntity entity = null;
        try {
            //I have no idea what I'm doing here ;)
            entity = new StringEntity("XML HERE");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        postMethod.setEntity(entity);
        method = postMethod;
    } else if (HttpMethod.GET == m_config.getMethod()) {
        //TODO: need to configure these
        //List<NameValuePair> getParms = null;
        method = new HttpGet(uri);
    }
    HttpVersion httpVersion = determineHttpVersion(m_config.getHttpVersion());
    clientWrapper.setVersion(httpVersion);
    HttpResponse response = null;
    try {
        response = clientWrapper.execute(method);
        int code = response.getStatusLine().getStatusCode();
        HttpResponseRange range = new HttpResponseRange("200-399");
        if (!range.contains(code)) {
            LOG.debug("response code out of range for uri:{}.  Expected {} but received {}", uri, range, code);
            throw new NorthbounderException("response code out of range for uri:" + uri + ".  Expected " + range + " but received " + code);
        }
        LOG.debug("HTTP Northbounder received response: {}", response.getStatusLine().getReasonPhrase());
    } catch (final ClientProtocolException e) {
        throw new NorthbounderException(e);
    } catch (final IOException e) {
        throw new NorthbounderException(e);
    } finally {
        IOUtils.closeQuietly(clientWrapper);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpEntity(org.apache.http.HttpEntity) GeneralSecurityException(java.security.GeneralSecurityException) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) URI(java.net.URI) ClientProtocolException(org.apache.http.client.ClientProtocolException) HttpResponseRange(org.opennms.core.utils.HttpResponseRange) StringEntity(org.apache.http.entity.StringEntity) NorthbounderException(org.opennms.netmgt.alarmd.api.NorthbounderException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) HttpClientWrapper(org.opennms.core.web.HttpClientWrapper) HttpVersion(org.apache.http.HttpVersion)

Example 2 with NorthbounderException

use of org.opennms.netmgt.alarmd.api.NorthbounderException in project opennms by OpenNMS.

the class NCSNorthbounder method postAlarms.

private void postAlarms(HttpEntity entity) {
    //Need a configuration bean for these
    int connectionTimeout = 3000;
    int socketTimeout = 3000;
    Integer retryCount = 3;
    HttpVersion httpVersion = determineHttpVersion(m_config.getHttpVersion());
    URI uri = m_config.getURI();
    System.err.println("uri = " + uri);
    final HttpClientWrapper clientWrapper = HttpClientWrapper.create().setSocketTimeout(socketTimeout).setConnectionTimeout(connectionTimeout).setRetries(retryCount).useBrowserCompatibleCookies().dontReuseConnections();
    if ("https".equals(uri.getScheme())) {
        try {
            clientWrapper.useRelaxedSSL("https");
        } catch (final GeneralSecurityException e) {
            throw new NorthbounderException("Failed to configure Relaxed SSL handling.", e);
        }
    }
    final HttpEntityEnclosingRequestBase method = m_config.getMethod().getRequestMethod(uri);
    if (m_config.getVirtualHost() != null && !m_config.getVirtualHost().trim().isEmpty()) {
        method.setHeader(HTTP.TARGET_HOST, m_config.getVirtualHost());
    }
    if (m_config.getUserAgent() != null && !m_config.getUserAgent().trim().isEmpty()) {
        method.setHeader(HTTP.USER_AGENT, m_config.getUserAgent());
    }
    method.setProtocolVersion(httpVersion);
    method.setEntity(entity);
    CloseableHttpResponse response = null;
    try {
        System.err.println("execute: " + method);
        response = clientWrapper.execute(method);
    } catch (ClientProtocolException e) {
        throw new NorthbounderException(e);
    } catch (IOException e) {
        throw new NorthbounderException(e);
    } finally {
        IOUtils.closeQuietly(clientWrapper);
    }
    if (response != null) {
        try {
            int code = response.getStatusLine().getStatusCode();
            final HttpResponseRange range = new HttpResponseRange("200-399");
            if (!range.contains(code)) {
                LOG.warn("response code out of range for uri: {}.  Expected {} but received {}", uri, range, code);
                throw new NorthbounderException("response code out of range for uri:" + uri + ".  Expected " + range + " but received " + code);
            }
        } finally {
            IOUtils.closeQuietly(clientWrapper);
        }
    }
    LOG.debug(response != null ? response.getStatusLine().getReasonPhrase() : "Response was null");
}
Also used : HttpResponseRange(org.opennms.core.utils.HttpResponseRange) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) NorthbounderException(org.opennms.netmgt.alarmd.api.NorthbounderException) GeneralSecurityException(java.security.GeneralSecurityException) HttpClientWrapper(org.opennms.core.web.HttpClientWrapper) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) HttpVersion(org.apache.http.HttpVersion) URI(java.net.URI) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 3 with NorthbounderException

use of org.opennms.netmgt.alarmd.api.NorthbounderException in project opennms by OpenNMS.

the class NCSNorthbounder method createEntity.

private HttpEntity createEntity(List<NorthboundAlarm> alarms) {
    ByteArrayOutputStream out = null;
    OutputStreamWriter writer = null;
    try {
        out = new ByteArrayOutputStream();
        writer = new OutputStreamWriter(out);
        // marshall the output
        JaxbUtils.marshal(toServiceAlarms(alarms), writer);
        // verify its matches the expected results
        byte[] utf8 = out.toByteArray();
        ByteArrayEntity entity = new ByteArrayEntity(utf8);
        entity.setContentType("application/xml");
        return entity;
    } catch (Exception e) {
        throw new NorthbounderException("failed to convert alarms to xml", e);
    } finally {
        IOUtils.closeQuietly(writer);
        IOUtils.closeQuietly(out);
    }
}
Also used : ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) NorthbounderException(org.opennms.netmgt.alarmd.api.NorthbounderException) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ClientProtocolException(org.apache.http.client.ClientProtocolException) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) NorthbounderException(org.opennms.netmgt.alarmd.api.NorthbounderException)

Example 4 with NorthbounderException

use of org.opennms.netmgt.alarmd.api.NorthbounderException in project opennms by OpenNMS.

the class EmailNorthbounder 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.";
        NorthbounderException e = new NorthbounderException(errorMsg);
        LOG.error(errorMsg, e);
        throw e;
    }
    LOG.info("Forwarding {} alarms to destination {}", alarms.size(), m_destination.getName());
    for (NorthboundAlarm alarm : alarms) {
        try {
            JavaSendMailer mailer = new JavaSendMailer(getSendmailConfig(alarm), false);
            mailer.send();
        } catch (JavaMailerException e) {
            LOG.error("Can't send email for {}", alarm, e);
        }
    }
}
Also used : NorthbounderException(org.opennms.netmgt.alarmd.api.NorthbounderException) JavaSendMailer(org.opennms.javamail.JavaSendMailer) NorthboundAlarm(org.opennms.netmgt.alarmd.api.NorthboundAlarm) JavaMailerException(org.opennms.javamail.JavaMailerException)

Example 5 with NorthbounderException

use of org.opennms.netmgt.alarmd.api.NorthbounderException in project opennms by OpenNMS.

the class SnmpTrapNorthbounder 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 SNMP Trap forwarding.";
        NorthbounderException e = new NorthbounderException(errorMsg);
        LOG.error(errorMsg, e);
        throw e;
    }
    LOG.info("Forwarding {} alarms to destination {}", alarms.size(), m_trapSink.getName());
    for (NorthboundAlarm alarm : alarms) {
        try {
            SnmpTrapConfig config = m_trapSink.createTrapConfig(alarm);
            if (config != null) {
                m_trapHelper.forwardTrap(config);
            }
        } catch (SnmpTrapException e) {
            LOG.error("Can't send trap for {}", alarm, e);
        }
    }
}
Also used : NorthbounderException(org.opennms.netmgt.alarmd.api.NorthbounderException) NorthboundAlarm(org.opennms.netmgt.alarmd.api.NorthboundAlarm)

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