use of org.opennms.netmgt.alarmd.api.NorthboundAlarm in project opennms by OpenNMS.
the class SnmpTrapNorthbounderTest method testNorthbounder.
/**
* Tests the trap northbounder.
*
* @throws Exception the exception
*/
// FIXME Verify the content of the trap sent.
@Test
public void testNorthbounder() throws Exception {
// Setup the configuration DAO
FileSystemResource resource = new FileSystemResource(new File("src/test/resources/etc/snmptrap-northbounder-config.xml"));
SnmpTrapNorthbounderConfigDao configDao = new SnmpTrapNorthbounderConfigDao();
configDao.setConfigResource(resource);
configDao.afterPropertiesSet();
// Setup the trap northbounder (overriding the settings of the first sink to use the test trap receiver)
SnmpTrapSink sink = configDao.getConfig().getSnmpTrapSink("localTest1");
sink.setIpAddress(TRAP_DESTINATION.getHostAddress());
sink.setPort(TRAP_PORT);
SnmpTrapNorthbounder nbi = new SnmpTrapNorthbounder(configDao, sink.getName());
nbi.afterPropertiesSet();
// Setup test node
OnmsNode node = new OnmsNode();
node.setForeignSource("Server-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.setLastEvent(new OnmsEvent() {
{
this.setEventParameters(Lists.newArrayList(new OnmsEventParameter(this, "alarmId", "10", "Int32"), new OnmsEventParameter(this, "alarmMessage", "this is a test", "string")));
}
});
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));
nbi.forwardAlarms(Collections.singletonList(alarm));
// Introduce a delay to make sure the trap was sent and received.
Thread.sleep(5000);
Assert.assertEquals(1, getTrapsReceivedCount());
}
use of org.opennms.netmgt.alarmd.api.NorthboundAlarm 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);
}
}
}
use of org.opennms.netmgt.alarmd.api.NorthboundAlarm in project opennms by OpenNMS.
the class SyslogNorthBounderTest method testForwardAlarms.
/**
* This tests forwarding of 7 alarms, one for each OpenNMS severity to
* verify the LOG_LEVEL agrees with the Severity based on our algorithm.
*
* @throws Exception the exception
*/
@Test
public void testForwardAlarms() throws Exception {
String xml = generateConfigXml();
Resource resource = new ByteArrayResource(xml.getBytes());
SyslogNorthbounderConfigDao dao = new SyslogNorthbounderConfigDao();
dao.setConfigResource(resource);
dao.afterPropertiesSet();
SyslogNorthbounderConfig config = dao.getConfig();
List<SyslogNorthbounder> nbis = new LinkedList<>();
for (SyslogDestination syslogDestination : config.getDestinations()) {
SyslogNorthbounder nbi = new SyslogNorthbounder(dao, syslogDestination.getName());
nbi.afterPropertiesSet();
nbis.add(nbi);
}
int j = 7;
List<NorthboundAlarm> alarms = new LinkedList<>();
OnmsNode node = new OnmsNode(m_locationDao.getDefaultLocation(), "p-brane");
node.setForeignSource("TestGroup");
node.setForeignId("1");
node.setId(m_nodeDao.getNextNodeId());
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");
Set<OnmsIpInterface> ipInterfaces = new LinkedHashSet<OnmsIpInterface>(j);
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("p-brane");
onmsIf.setIsSnmpPrimary(PrimaryType.PRIMARY);
ipInterfaces.add(onmsIf);
node.setIpInterfaces(ipInterfaces);
m_nodeDao.save(node);
m_nodeDao.flush();
for (SyslogNorthbounder nbi : nbis) {
for (int i = 1; i <= j; ++i) {
OnmsAlarm onmsAlarm = new OnmsAlarm();
onmsAlarm.setId(i);
onmsAlarm.setUei("uei.opennms.org/test/syslogNorthBounder");
onmsAlarm.setNode(node);
onmsAlarm.setSeverityId(i);
onmsAlarm.setIpAddr(InetAddress.getByName("127.0.0.1"));
onmsAlarm.setCounter(i);
onmsAlarm.setLogMsg("Node Down");
onmsAlarm.setX733AlarmType(NorthboundAlarm.x733AlarmType.get(i).name());
onmsAlarm.setX733ProbableCause(NorthboundAlarm.x733ProbableCause.get(i).getId());
NorthboundAlarm a = new NorthboundAlarm(onmsAlarm);
Assert.assertFalse(nbi.accepts(a));
onmsAlarm.setUei("uei.opennms.org/nodes/nodeDown");
a = new NorthboundAlarm(onmsAlarm);
Assert.assertTrue(nbi.accepts(a));
alarms.add(a);
}
nbi.forwardAlarms(alarms);
}
Thread.sleep(100);
BufferedReader r = new BufferedReader(new StringReader(m_logStream.readStream()));
List<String> messages = new LinkedList<>();
String line = null;
while ((line = r.readLine()) != null) {
messages.add(line);
Thread.sleep(10);
}
Assert.assertTrue("Log messages sent: 7, Log messages received: " + messages.size(), 7 == messages.size());
for (String message : messages) {
System.out.println(message);
}
int i = 0;
for (String message : messages) {
if (i == 0) {
i++;
continue;
}
switch(i) {
case 1:
Assert.assertTrue("Alarm (OnmsSeverity: " + OnmsSeverity.get(i) + ") = LEVEL_INFO.", message.contains("INFO"));
Assert.assertTrue(message.contains("NODE:p-brane"));
break;
case 2:
Assert.assertTrue("Alarm (OnmsSeverity: " + OnmsSeverity.get(i) + ") = LEVEL_NOTICE.", message.contains("NOTICE"));
Assert.assertTrue(message.contains("NODE:p-brane"));
break;
case 3:
Assert.assertTrue("Alarm (OnmsSeverity: " + OnmsSeverity.get(i) + ") = LEVEL_WARN.", message.contains("WARN"));
Assert.assertTrue(message.contains("NODE:p-brane"));
break;
case 4:
Assert.assertTrue("Alarm (OnmsSeverity: " + OnmsSeverity.get(i) + ") = LEVEL_ERROR.", message.contains("ERROR"));
Assert.assertTrue(message.contains("NODE:p-brane"));
break;
case 5:
Assert.assertTrue("Alarm (OnmsSeverity: " + OnmsSeverity.get(i) + ") = LEVEL_ERROR.", message.contains("ERROR"));
Assert.assertTrue(message.contains("NODE:p-brane"));
break;
case 6:
Assert.assertTrue("Alarm (OnmsSeverity: " + OnmsSeverity.get(i) + ") = LEVEL_CRITICAL.", message.contains("CRITICAL"));
Assert.assertTrue(message.contains("NODE:p-brane"));
break;
}
i++;
}
}
use of org.opennms.netmgt.alarmd.api.NorthboundAlarm in project opennms by OpenNMS.
the class SyslogNorthBounderWithFiltersTest method testForwardAlarms.
/* (non-Javadoc)
* @see org.opennms.netmgt.alarmd.northbounder.syslog.SyslogNorthBounderTest#testForwardAlarms()
*/
@Test
@Override
public void testForwardAlarms() throws Exception {
// Initialize the configuration
File configFile = new File("target/syslog-northbounder-config.xml");
FileUtils.copyFile(new File("src/test/resources/syslog-northbounder-config1.xml"), configFile);
// Initialize the configuration DAO
SyslogNorthbounderConfigDao dao = new SyslogNorthbounderConfigDao();
dao.setConfigResource(new FileSystemResource(configFile));
dao.afterPropertiesSet();
// Initialize the Syslog northbound interfaces
List<SyslogNorthbounder> nbis = new LinkedList<>();
for (SyslogDestination syslogDestination : dao.getConfig().getDestinations()) {
SyslogNorthbounder nbi = new SyslogNorthbounder(dao, syslogDestination.getName());
nbi.afterPropertiesSet();
nbis.add(nbi);
}
// Add a sample node to the database
OnmsNode node = new OnmsNode();
node.setForeignSource("TestGroup");
node.setForeignId("1");
node.setId(m_nodeDao.getNextNodeId());
node.setLabel("agalue");
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");
Set<OnmsIpInterface> ipInterfaces = new LinkedHashSet<>();
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("agalue");
onmsIf.setIsSnmpPrimary(PrimaryType.PRIMARY);
ipInterfaces.add(onmsIf);
node.setIpInterfaces(ipInterfaces);
m_nodeDao.save(node);
m_nodeDao.flush();
// Create a sample Alarm
OnmsAlarm onmsAlarm = new OnmsAlarm();
onmsAlarm.setId(10);
onmsAlarm.setUei("uei.opennms.org/nodes/interfaceDown");
onmsAlarm.setNode(node);
onmsAlarm.setSeverityId(6);
onmsAlarm.setIpAddr(address);
onmsAlarm.setCounter(1);
onmsAlarm.setLogMsg("Interface Down");
onmsAlarm.setLastEvent(new OnmsEvent() {
{
this.setEventParameters(Lists.newArrayList(new OnmsEventParameter(this, "owner", "agalue", "String")));
}
});
NorthboundAlarm nbAlarm = new NorthboundAlarm(onmsAlarm);
List<NorthboundAlarm> alarms = new LinkedList<>();
alarms.add(nbAlarm);
// Verify filters and send alarms to the northbound interfaces
for (SyslogNorthbounder nbi : nbis) {
Assert.assertTrue(nbi.accepts(nbAlarm));
nbi.forwardAlarms(alarms);
}
// Induce a delay (based on the parent code)
Thread.sleep(100);
// Extract the log messages and verify the content
BufferedReader reader = new BufferedReader(new StringReader(m_logStream.readStream()));
List<String> messages = getMessagesFromBuffer(reader);
Assert.assertTrue("Log messages sent: 2, Log messages received: " + messages.size(), 2 == messages.size());
Assert.assertTrue(messages.get(0).contains("ALARM 10 FROM NODE agalue@TestGroup"));
Assert.assertTrue(messages.get(1).contains("ALARM 10 FROM INTERFACE 10.0.1.1"));
reader.close();
// Remove the temporary configuration file
configFile.delete();
}
use of org.opennms.netmgt.alarmd.api.NorthboundAlarm in project opennms by OpenNMS.
the class AlarmNorthbounder method forwardAlarms.
@Override
public void forwardAlarms(List<NorthboundAlarm> alarms) throws NorthbounderException {
for (NorthboundAlarm alarm : alarms) {
LOG.trace("AMQPNorthbounder Forwarding alarm: {}", alarm);
alarmForwarder.sendNow(alarm);
}
}
Aggregations