@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<SyslogDestination> destinations = config.getDestinations(); List<SyslogNorthbounder> nbis = new LinkedList<SyslogNorthbounder>(); for (SyslogDestination syslogDestination : destinations) { SyslogNorthbounder nbi = new SyslogNorthbounder(config, syslogDestination); nbi.setNodeDao(new TestNodeDao()); nbi.afterPropertiesSet(); nbis.add(nbi); } int j = 7; List<NorthboundAlarm> alarms = new LinkedList<NorthboundAlarm>(); OnmsDistPoller distpoller = new OnmsDistPoller("barbrady", "172.20.1.11"); OnmsNode node = new OnmsNode(distpoller, "p-brane"); node.setForeignSource("TestGroup"); node.setForeignId("1"); node.setId(TEST_NODE_ID); 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); 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()); if (i < j) { String eventparms = "foreignSource=fabric(string,text);foreignId=space-0256012012000038(string,text);reason=Aborting node scan : Agent timed out while scanning the system table(string,text);" + ".1.3.6.1.4.1.2636.3.18.1.7.1.2.732=207795895(TimeTicks,text)"; onmsAlarm.setEventParms(eventparms ); } 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>(); 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_NOTICE.", message.contains("NOTICE")); 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++; } }
@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); LogUtils.errorf(this, e, errorMsg); throw e; } LogUtils.infof(this, "Forwarding %d alarms to destination:"+m_destination.getName(), alarms.size()); Map<Integer, Map<String, String>> alarmMappings = new HashMap<Integer, Map<String, String>>(); SyslogIF instance; try { instance = Syslog.getInstance(m_destination.getName()); } catch (SyslogRuntimeException e) { LogUtils.errorf(this, e, "Could not find Syslog instance for destination '%s': %s", m_destination.getName(), e); throw e; } for (NorthboundAlarm alarm : alarms) { Integer count = alarm.getCount(); if (count > 1 && m_destination.isFirstOccurrenceOnly()) { LogUtils.debugf(this, "Destination %s is configured for new alarm instances only. Alarm has count of %d.", m_destination.getName(), count); continue; } LogUtils.debugf(this, "Creating formatted log message for alarm: %d.", alarm.getId()); Map<String, String> mapping = null; String syslogMessage; int level; try { if (alarmMappings != null) { mapping = alarmMappings.get(alarm.getId()); } if (mapping == null) { mapping = createMapping(alarmMappings, alarm); } LogUtils.debugf(this, "Making substitutions for tokens in message format for alarm: %d.", alarm.getId()); syslogMessage = PropertiesUtils.substitute(m_config.getMessageFormat(), mapping); LogUtils.debugf(this, "Determining LOG_LEVEL for alarm: %d", alarm.getId()); level = determineLogLevel(alarm.getSeverity()); LogUtils.debugf(this, "Forwarding alarm: %d via syslog to destination: %s", alarm.getId(), m_destination.getName()); instance.log(level, syslogMessage); } catch (Exception e1) { LogUtils.errorf(this, e1, "Caught exception sending to destination '%s': %s", m_destination.getName(), e1); } } }
SyslogNorthbounder extends AbstractNorthbounder implements InitializingBean { @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); LogUtils.errorf(this, e, errorMsg); throw e; } LogUtils.infof(this, "Forwarding %d alarms to destination:"+m_destination.getName(), alarms.size()); Map<Integer, Map<String, String>> alarmMappings = new HashMap<Integer, Map<String, String>>(); SyslogIF instance; try { instance = Syslog.getInstance(m_destination.getName()); } catch (SyslogRuntimeException e) { LogUtils.errorf(this, e, "Could not find Syslog instance for destination '%s': %s", m_destination.getName(), e); throw e; } for (NorthboundAlarm alarm : alarms) { Integer count = alarm.getCount(); if (count > 1 && m_destination.isFirstOccurrenceOnly()) { LogUtils.debugf(this, "Destination %s is configured for new alarm instances only. Alarm has count of %d.", m_destination.getName(), count); continue; } LogUtils.debugf(this, "Creating formatted log message for alarm: %d.", alarm.getId()); Map<String, String> mapping = null; String syslogMessage; int level; try { if (alarmMappings != null) { mapping = alarmMappings.get(alarm.getId()); } if (mapping == null) { mapping = createMapping(alarmMappings, alarm); } LogUtils.debugf(this, "Making substitutions for tokens in message format for alarm: %d.", alarm.getId()); syslogMessage = PropertiesUtils.substitute(m_config.getMessageFormat(), mapping); LogUtils.debugf(this, "Determining LOG_LEVEL for alarm: %d", alarm.getId()); level = determineLogLevel(alarm.getSeverity()); LogUtils.debugf(this, "Forwarding alarm: %d via syslog to destination: %s", alarm.getId(), m_destination.getName()); instance.log(level, syslogMessage); } catch (Exception e1) { LogUtils.errorf(this, e1, "Caught exception sending to destination '%s': %s", m_destination.getName(), e1); } } } }
SyslogNorthbounder extends AbstractNorthbounder implements InitializingBean { @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); LogUtils.errorf(this, e, errorMsg); throw e; } LogUtils.infof(this, "Forwarding %d alarms to destination:"+m_destination.getName(), alarms.size()); Map<Integer, Map<String, String>> alarmMappings = new HashMap<Integer, Map<String, String>>(); SyslogIF instance; try { instance = Syslog.getInstance(m_destination.getName()); } catch (SyslogRuntimeException e) { LogUtils.errorf(this, e, "Could not find Syslog instance for destination '%s': %s", m_destination.getName(), e); throw e; } for (NorthboundAlarm alarm : alarms) { Integer count = alarm.getCount(); if (count > 1 && m_destination.isFirstOccurrenceOnly()) { LogUtils.debugf(this, "Destination %s is configured for new alarm instances only. Alarm has count of %d.", m_destination.getName(), count); continue; } LogUtils.debugf(this, "Creating formatted log message for alarm: %d.", alarm.getId()); Map<String, String> mapping = null; String syslogMessage; int level; try { if (alarmMappings != null) { mapping = alarmMappings.get(alarm.getId()); } if (mapping == null) { mapping = createMapping(alarmMappings, alarm); } LogUtils.debugf(this, "Making substitutions for tokens in message format for alarm: %d.", alarm.getId()); syslogMessage = PropertiesUtils.substitute(m_config.getMessageFormat(), mapping); LogUtils.debugf(this, "Determining LOG_LEVEL for alarm: %d", alarm.getId()); level = determineLogLevel(alarm.getSeverity()); LogUtils.debugf(this, "Forwarding alarm: %d via syslog to destination: %s", alarm.getId(), m_destination.getName()); instance.log(level, syslogMessage); } catch (Exception e1) { LogUtils.errorf(this, e1, "Caught exception sending to destination '%s': %s", m_destination.getName(), e1); } } } SyslogNorthbounder(SyslogNorthbounderConfig config, SyslogDestination destination); }
SyslogNorthbounder extends AbstractNorthbounder implements InitializingBean { @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); LogUtils.errorf(this, e, errorMsg); throw e; } LogUtils.infof(this, "Forwarding %d alarms to destination:"+m_destination.getName(), alarms.size()); Map<Integer, Map<String, String>> alarmMappings = new HashMap<Integer, Map<String, String>>(); SyslogIF instance; try { instance = Syslog.getInstance(m_destination.getName()); } catch (SyslogRuntimeException e) { LogUtils.errorf(this, e, "Could not find Syslog instance for destination '%s': %s", m_destination.getName(), e); throw e; } for (NorthboundAlarm alarm : alarms) { Integer count = alarm.getCount(); if (count > 1 && m_destination.isFirstOccurrenceOnly()) { LogUtils.debugf(this, "Destination %s is configured for new alarm instances only. Alarm has count of %d.", m_destination.getName(), count); continue; } LogUtils.debugf(this, "Creating formatted log message for alarm: %d.", alarm.getId()); Map<String, String> mapping = null; String syslogMessage; int level; try { if (alarmMappings != null) { mapping = alarmMappings.get(alarm.getId()); } if (mapping == null) { mapping = createMapping(alarmMappings, alarm); } LogUtils.debugf(this, "Making substitutions for tokens in message format for alarm: %d.", alarm.getId()); syslogMessage = PropertiesUtils.substitute(m_config.getMessageFormat(), mapping); LogUtils.debugf(this, "Determining LOG_LEVEL for alarm: %d", alarm.getId()); level = determineLogLevel(alarm.getSeverity()); LogUtils.debugf(this, "Forwarding alarm: %d via syslog to destination: %s", alarm.getId(), m_destination.getName()); instance.log(level, syslogMessage); } catch (Exception e1) { LogUtils.errorf(this, e1, "Caught exception sending to destination '%s': %s", m_destination.getName(), e1); } } } SyslogNorthbounder(SyslogNorthbounderConfig config, SyslogDestination destination); @Override void afterPropertiesSet(); @Override boolean accepts(NorthboundAlarm alarm); @Override void forwardAlarms(List<NorthboundAlarm> alarms); SyslogNorthbounderConfig getConfig(); void setConfig(final SyslogNorthbounderConfig config); NodeDao getNodeDao(); void setNodeDao(final NodeDao nodeDao); }
SyslogNorthbounder extends AbstractNorthbounder implements InitializingBean { @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); LogUtils.errorf(this, e, errorMsg); throw e; } LogUtils.infof(this, "Forwarding %d alarms to destination:"+m_destination.getName(), alarms.size()); Map<Integer, Map<String, String>> alarmMappings = new HashMap<Integer, Map<String, String>>(); SyslogIF instance; try { instance = Syslog.getInstance(m_destination.getName()); } catch (SyslogRuntimeException e) { LogUtils.errorf(this, e, "Could not find Syslog instance for destination '%s': %s", m_destination.getName(), e); throw e; } for (NorthboundAlarm alarm : alarms) { Integer count = alarm.getCount(); if (count > 1 && m_destination.isFirstOccurrenceOnly()) { LogUtils.debugf(this, "Destination %s is configured for new alarm instances only. Alarm has count of %d.", m_destination.getName(), count); continue; } LogUtils.debugf(this, "Creating formatted log message for alarm: %d.", alarm.getId()); Map<String, String> mapping = null; String syslogMessage; int level; try { if (alarmMappings != null) { mapping = alarmMappings.get(alarm.getId()); } if (mapping == null) { mapping = createMapping(alarmMappings, alarm); } LogUtils.debugf(this, "Making substitutions for tokens in message format for alarm: %d.", alarm.getId()); syslogMessage = PropertiesUtils.substitute(m_config.getMessageFormat(), mapping); LogUtils.debugf(this, "Determining LOG_LEVEL for alarm: %d", alarm.getId()); level = determineLogLevel(alarm.getSeverity()); LogUtils.debugf(this, "Forwarding alarm: %d via syslog to destination: %s", alarm.getId(), m_destination.getName()); instance.log(level, syslogMessage); } catch (Exception e1) { LogUtils.errorf(this, e1, "Caught exception sending to destination '%s': %s", m_destination.getName(), e1); } } } SyslogNorthbounder(SyslogNorthbounderConfig config, SyslogDestination destination); @Override void afterPropertiesSet(); @Override boolean accepts(NorthboundAlarm alarm); @Override void forwardAlarms(List<NorthboundAlarm> alarms); SyslogNorthbounderConfig getConfig(); void setConfig(final SyslogNorthbounderConfig config); NodeDao getNodeDao(); void setNodeDao(final NodeDao nodeDao);  }