Search in sources :

Example 1 with SendmailConfig

use of org.opennms.netmgt.config.javamail.SendmailConfig in project opennms by OpenNMS.

the class DefaultJavamailConfigurationDao method addSendMailConfig.

/* (non-Javadoc)
     * @see org.opennms.netmgt.dao.api.JavaMailConfigurationDao#addSendMailConfig(org.opennms.netmgt.config.javamail.SendmailConfig)
     */
@Override
public void addSendMailConfig(SendmailConfig sendmailConfig) {
    int index = -1;
    List<SendmailConfig> configs = getSendmailConfigs();
    for (int i = 0; i < configs.size(); i++) {
        final SendmailConfig c = configs.get(i);
        if (c.getName().equals(sendmailConfig.getName())) {
            index = i;
            break;
        }
    }
    if (index > -1) {
        configs.remove(index);
        configs.add(index, sendmailConfig);
    } else {
        configs.add(sendmailConfig);
    }
}
Also used : SendmailConfig(org.opennms.netmgt.config.javamail.SendmailConfig)

Example 2 with SendmailConfig

use of org.opennms.netmgt.config.javamail.SendmailConfig in project opennms by OpenNMS.

the class JavaMailAckReaderIT method createSendMailer.

private JavaSendMailer createSendMailer(String gmailAccount, String gmailPassword) throws JavaMailerException {
    SendmailConfig config = new SendmailConfig();
    config.setAttemptInterval(1000l);
    config.setDebug(true);
    config.setName("test");
    SendmailMessage sendmailMessage = new SendmailMessage();
    sendmailMessage.setBody("service is down");
    sendmailMessage.setFrom("bamboo.opennms@gmail.com");
    sendmailMessage.setSubject("Notice #1234: service down");
    sendmailMessage.setTo("bamboo.opennms@gmail.com");
    config.setSendmailMessage(sendmailMessage);
    SendmailHost host = new SendmailHost();
    host.setHost("smtp.gmail.com");
    host.setPort(465);
    config.setSendmailHost(host);
    SendmailProtocol protocol = new SendmailProtocol();
    protocol.setSslEnable(true);
    protocol.setTransport("smtps");
    config.setSendmailProtocol(protocol);
    config.setUseAuthentication(true);
    config.setUseJmta(false);
    UserAuth auth = new UserAuth();
    auth.setUserName(gmailAccount);
    auth.setPassword(gmailPassword);
    config.setUserAuth(auth);
    return new JavaSendMailer(config);
}
Also used : SendmailProtocol(org.opennms.netmgt.config.javamail.SendmailProtocol) SendmailMessage(org.opennms.netmgt.config.javamail.SendmailMessage) JavaSendMailer(org.opennms.javamail.JavaSendMailer) SendmailConfig(org.opennms.netmgt.config.javamail.SendmailConfig) UserAuth(org.opennms.netmgt.config.javamail.UserAuth) SendmailHost(org.opennms.netmgt.config.javamail.SendmailHost)

Example 3 with SendmailConfig

use of org.opennms.netmgt.config.javamail.SendmailConfig in project opennms by OpenNMS.

the class EmailNorthbounderTest method testNorthbounderForServers.

/**
     * Test northbounder for servers.
     *
     * @throws Exception the exception
     */
@Test
public void testNorthbounderForServers() throws Exception {
    // Setup test node
    OnmsNode node = new OnmsNode();
    node.setForeignSource("Servers-MacOS");
    node.setForeignId("1");
    node.setId(1);
    node.setLabel("my-test-server");
    OnmsSnmpInterface snmpInterface = new OnmsSnmpInterface(node, 1);
    snmpInterface.setId(1);
    snmpInterface.setIfAlias("Connection to OpenNMS Wifi");
    snmpInterface.setIfDescr("en1");
    snmpInterface.setIfName("en1/0");
    snmpInterface.setPhysAddr("00:00:00:00:00:01");
    InetAddress address = InetAddress.getByName("10.0.1.1");
    OnmsIpInterface onmsIf = new OnmsIpInterface(address, node);
    onmsIf.setSnmpInterface(snmpInterface);
    onmsIf.setId(1);
    onmsIf.setIfIndex(1);
    onmsIf.setIpHostName("my-test-server");
    onmsIf.setIsSnmpPrimary(PrimaryType.PRIMARY);
    node.getIpInterfaces().add(onmsIf);
    // Setup test alarm
    OnmsAlarm onmsAlarm = new OnmsAlarm();
    onmsAlarm.setId(100);
    onmsAlarm.setNode(node);
    onmsAlarm.setIpAddr(address);
    onmsAlarm.setUei("uei.opennms.org/trap/myTrap1");
    onmsAlarm.setEventParms("alarmId=10(Int32,text);alarmMessage=this is a test(string,text);");
    onmsAlarm.setLogMsg("Test log message");
    onmsAlarm.setDescription("Test description");
    NorthboundAlarm alarm = new NorthboundAlarm(onmsAlarm);
    Assert.assertEquals(2, alarm.getEventParametersCollection().size());
    // Verify the nortbound alarm and send it to the test receiver
    Assert.assertTrue(nbi.accepts(alarm));
    SendmailConfig sendmail = nbi.getSendmailConfig(alarm);
    Assert.assertEquals("noc@networksRus.com", sendmail.getSendmailMessage().getTo());
    Assert.assertEquals("ALARM 100 FROM NODE my-test-server@Servers-MacOS", sendmail.getSendmailMessage().getSubject());
    Assert.assertEquals("ALARM 100 FROM NODE my-test-server@Servers-MacOS: Test log message\nDescription: Test description", sendmail.getSendmailMessage().getBody());
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) OnmsAlarm(org.opennms.netmgt.model.OnmsAlarm) NorthboundAlarm(org.opennms.netmgt.alarmd.api.NorthboundAlarm) OnmsSnmpInterface(org.opennms.netmgt.model.OnmsSnmpInterface) SendmailConfig(org.opennms.netmgt.config.javamail.SendmailConfig) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 4 with SendmailConfig

use of org.opennms.netmgt.config.javamail.SendmailConfig in project opennms by OpenNMS.

the class EmailNorthbounderTest method testNorthbounderForRouters.

/**
     * Test northbounder for routers.
     *
     * @throws Exception the exception
     */
@Test
public void testNorthbounderForRouters() throws Exception {
    // Setup test node
    OnmsNode node = new OnmsNode();
    node.setForeignSource("Routers-Cisco");
    node.setForeignId("1");
    node.setId(1);
    node.setLabel("my-test-router");
    OnmsSnmpInterface snmpInterface = new OnmsSnmpInterface(node, 1);
    snmpInterface.setId(1);
    snmpInterface.setIfAlias("Connection to OpenNMS Wifi");
    snmpInterface.setIfDescr("en1");
    snmpInterface.setIfName("en1/0");
    snmpInterface.setPhysAddr("00:00:00:00:00:01");
    InetAddress address = InetAddress.getByName("10.0.1.1");
    OnmsIpInterface onmsIf = new OnmsIpInterface(address, node);
    onmsIf.setSnmpInterface(snmpInterface);
    onmsIf.setId(1);
    onmsIf.setIfIndex(1);
    onmsIf.setIpHostName("my-test-router");
    onmsIf.setIsSnmpPrimary(PrimaryType.PRIMARY);
    node.getIpInterfaces().add(onmsIf);
    // Setup test alarm
    OnmsAlarm onmsAlarm = new OnmsAlarm();
    onmsAlarm.setId(100);
    onmsAlarm.setNode(node);
    onmsAlarm.setIpAddr(address);
    onmsAlarm.setUei("uei.opennms.org/trap/myTrap1");
    onmsAlarm.setEventParms("alarmId=10(Int32,text);alarmMessage=this is a test(string,text);");
    onmsAlarm.setLogMsg("Test log message");
    onmsAlarm.setDescription("Test description");
    NorthboundAlarm alarm = new NorthboundAlarm(onmsAlarm);
    Assert.assertEquals(2, alarm.getEventParametersCollection().size());
    // Verify the nortbound alarm and send it to the test receiver
    Assert.assertTrue(nbi.accepts(alarm));
    SendmailConfig sendmail = nbi.getSendmailConfig(alarm);
    Assert.assertEquals("tarus@opennms.org, jeff@opennms.org", sendmail.getSendmailMessage().getTo());
    Assert.assertEquals("my-test-router : Something is wrong!", sendmail.getSendmailMessage().getSubject());
    Assert.assertEquals("Test log message - Test description", sendmail.getSendmailMessage().getBody());
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) OnmsAlarm(org.opennms.netmgt.model.OnmsAlarm) NorthboundAlarm(org.opennms.netmgt.alarmd.api.NorthboundAlarm) OnmsSnmpInterface(org.opennms.netmgt.model.OnmsSnmpInterface) SendmailConfig(org.opennms.netmgt.config.javamail.SendmailConfig) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 5 with SendmailConfig

use of org.opennms.netmgt.config.javamail.SendmailConfig in project opennms by OpenNMS.

the class JavaMailDeliveryService method deliverReport.

/* (non-Javadoc)
     * @see org.opennms.netmgt.reporting.service.ReportDeliveryService#deliverReport(org.opennms.netmgt.config.reportd.Report, java.lang.String)
     */
@Override
public void deliverReport(Report report, String fileName) throws ReportDeliveryException {
    try {
        SendmailConfig config = null;
        if (report.getMailer().isPresent()) {
            final String mailer = report.getMailer().get();
            LOG.debug("deliverReport with mailer={}", mailer);
            config = m_JavamailConfigDao.getSendMailConfig(mailer);
        } else {
            LOG.debug("deliverReport with default sendmail config");
            config = m_JavamailConfigDao.getDefaultSendmailConfig();
        }
        JavaSendMailer sm = new JavaSendMailer(config);
        MimeMessage msg = new MimeMessage(sm.getSession());
        if (config.getSendmailMessage() != null && config.getSendmailProtocol() != null) {
            final SendmailMessage sendmailMessage = config.getSendmailMessage();
            final SendmailProtocol sendmailProtocol = config.getSendmailProtocol();
            MimeMessageHelper helper = new MimeMessageHelper(msg, true, sendmailProtocol.getCharSet());
            helper.setFrom(sendmailMessage.getFrom());
            helper.setTo(report.getRecipients().toArray(new String[0]));
            helper.setSubject("OpenNMS Report: " + report.getReportName());
            if ("text/html".equals(sendmailProtocol.getMessageContentType().toLowerCase())) {
                helper.setText(sendmailMessage.getBody().replaceAll("\\<[^>]*>", ""), sendmailMessage.getBody());
            } else {
                helper.setText(sendmailMessage.getBody());
            }
            helper.addAttachment(fileName, new File(fileName));
            sm.send(msg);
        } else {
            LOG.error("sendmail-message or sendmail-protocol is not configured!");
        }
    } catch (JavaMailerException e) {
        LOG.error("Problem with JavaMailer {}", e.getMessage(), e);
        throw new ReportDeliveryException("Caught JavaMailerException: " + e.getMessage());
    } catch (MessagingException e) {
        LOG.error("Problem with Messaging {}", e.getMessage(), e);
        throw new ReportDeliveryException("Caught MessagingException: " + e.getMessage());
    } catch (Throwable e) {
        LOG.error("Unexpected exception: {}", e.getMessage(), e);
        throw new ReportDeliveryException("Caught unexpected " + e.getClass().getName() + ": " + e.getMessage());
    }
}
Also used : SendmailProtocol(org.opennms.netmgt.config.javamail.SendmailProtocol) MimeMessage(javax.mail.internet.MimeMessage) SendmailMessage(org.opennms.netmgt.config.javamail.SendmailMessage) MessagingException(javax.mail.MessagingException) JavaSendMailer(org.opennms.javamail.JavaSendMailer) SendmailConfig(org.opennms.netmgt.config.javamail.SendmailConfig) JavaMailerException(org.opennms.javamail.JavaMailerException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) File(java.io.File)

Aggregations

SendmailConfig (org.opennms.netmgt.config.javamail.SendmailConfig)8 SendmailMessage (org.opennms.netmgt.config.javamail.SendmailMessage)3 SendmailProtocol (org.opennms.netmgt.config.javamail.SendmailProtocol)3 InetAddress (java.net.InetAddress)2 Test (org.junit.Test)2 JavaSendMailer (org.opennms.javamail.JavaSendMailer)2 NorthboundAlarm (org.opennms.netmgt.alarmd.api.NorthboundAlarm)2 SendmailHost (org.opennms.netmgt.config.javamail.SendmailHost)2 UserAuth (org.opennms.netmgt.config.javamail.UserAuth)2 OnmsAlarm (org.opennms.netmgt.model.OnmsAlarm)2 OnmsIpInterface (org.opennms.netmgt.model.OnmsIpInterface)2 OnmsNode (org.opennms.netmgt.model.OnmsNode)2 OnmsSnmpInterface (org.opennms.netmgt.model.OnmsSnmpInterface)2 File (java.io.File)1 MessagingException (javax.mail.MessagingException)1 MimeMessage (javax.mail.internet.MimeMessage)1 JavaMailerException (org.opennms.javamail.JavaMailerException)1 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)1