Permalink
Browse files

[bamboo] Automated branch merge (from release-23.0.2:1e1db6aae7483ca4…

…4e4b5ea7117828f819e480f5)
  • Loading branch information...
opennms-bamboo committed Jan 10, 2019
2 parents 9285d77 + 1e1db6a commit dce85eec0ea37b70e0ff7a85aae990621d44ca93
@@ -29,6 +29,7 @@
package org.opennms.features.eifadapter;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;

import org.apache.camel.component.netty4.ChannelHandlerFactory;
@@ -44,7 +45,7 @@
public class EifMessageDecoder extends MessageToMessageDecoder<ByteBuf> implements ChannelHandlerFactory {

private final StringBuilder buffer = new StringBuilder();
private final Charset charset = Charset.defaultCharset();
private final Charset charset = StandardCharsets.UTF_8;

private NodeDao nodeDao;

@@ -44,13 +44,13 @@
import org.opennms.netmgt.xml.event.Parm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xbill.DNS.Address;

import com.google.common.net.InetAddresses;

public class EifParser {

private static final Logger LOG = LoggerFactory.getLogger(EifParser.class);
private static final int eifStartOffset = 37;

enum EifSeverity {
FATAL, CRITICAL, MINOR, WARNING, OK, INFO, HARMLESS, UNKNOWN;
@@ -87,7 +87,24 @@ public OnmsSeverity toOnmsSeverity() {
// Extract a single event from the package
int eventStart = eifBuff.indexOf("<START>>");
int eventEnd = eifBuff.indexOf(";END");
String eifEvent = eifBuff.substring(eventStart + eifStartOffset,eventEnd);
/*
Both known versions of the EIF protocol have a 2-byte identifier beginning at offset 30.
If the same value exists at offset 34, the event begins 36 bytes after '<START>>'.
Otherwise, it's 37 bytes.
The precise meaning / purpose of this identifier is not public.
*/
byte[] eifVersionBytes1 = eifBuff.substring(eventStart+30,eventStart+32).getBytes();
byte[] eifVersionBytes2 = eifBuff.substring(eventStart+34,eventStart+36).getBytes();
LOG.debug("eifVersion1Bytes[0]: {}",eifVersionBytes1[0]);
LOG.debug("eifVersion2Bytes[0]: {}",eifVersionBytes2[0]);
LOG.debug("eifVersion1Bytes[1]: {}",eifVersionBytes1[1]);
LOG.debug("eifVersion2Bytes[1]: {}",eifVersionBytes2[1]);
String eifEvent;
if (eifVersionBytes1[0] == eifVersionBytes2[0] && eifVersionBytes1[1] == eifVersionBytes2[1]){
eifEvent = eifBuff.substring(eventStart + 36,eventEnd);
} else {
eifEvent = eifBuff.substring(eventStart + 37,eventEnd);
}
eifBuff.delete(0,eventEnd+4);

// Parse the EIF slots into OpenNMS parms, and try to look up the source's nodeId
@@ -142,10 +159,13 @@ private static long connectEifEventToNode(NodeDao nodeDao, Map<String, String> e
String fqdn = "";
if (!"".equals(eifSlotMap.get("fqhostname")) && eifSlotMap.get("fqhostname") != null) {
fqdn = eifSlotMap.get("fqhostname");
LOG.debug("Using fqhostname for fqdn: {}",fqdn);
} else if (!"".equals(eifSlotMap.get("hostname")) && eifSlotMap.get("hostname") != null) {
String hostname = eifSlotMap.get("hostname");
LOG.debug("Trying hostname for fqdn: {}",hostname);
try {
fqdn = InetAddress.getByName(hostname).getCanonicalHostName();
fqdn = Address.getHostName(InetAddress.getByName(hostname));
LOG.debug("Using hostname for fqdn: {}",fqdn);
} catch (UnknownHostException uhe) {
LOG.error("UnknownHostException while resolving hostname {}",hostname);
}
@@ -168,6 +188,7 @@ private static long connectEifEventToNode(NodeDao nodeDao, Map<String, String> e
List<OnmsNode> matchingNodes = new ArrayList<>();
OnmsNode firstMatch;
try {
LOG.debug("Searching for node by label {}",fqdn);
matchingNodes = nodeDao.findByLabel(fqdn);
} catch (NullPointerException npe) {
LOG.debug("No node located for {}",fqdn);
@@ -33,10 +33,9 @@

import com.google.common.collect.Lists;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Dictionary;
import java.util.List;
import java.util.Map;
@@ -139,4 +138,36 @@ public void onEvent(Event e) {
matches("^Situation \\d{2}"));
}
}

@Test
public void testCanParseEifWith36ByteOffset() throws Exception {
// Register an event listener
final List<Event> receivedEvents = Lists.newArrayList();
eventIpcManager.addEventListener(new EventListener() {
@Override
public String getName() {
return "test";
}

@Override
public void onEvent(Event e) {
receivedEvents.add(e);
}
});
BufferedReader eifPacketCapture = new BufferedReader(
new InputStreamReader(
new FileInputStream(new File("src/test/resources/eif_36_byte_offset_test.dat")), StandardCharsets.UTF_8
)
);
Socket clientSocket = new Socket(InetAddrUtils.getLocalHostAddress(), 1828);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.write( IOUtils.toByteArray(eifPacketCapture) );
outToServer.flush();
clientSocket.close();

await().atMost(15, SECONDS).until(() -> receivedEvents.size() == 1);
for (Event event : receivedEvents) {
assertTrue("UEI must match regex.", event.getUei().matches("^uei.opennms.org/vendor/IBM/EIF/EIF_TEST_EVENT_TYPE_G$"));
}
}
}
@@ -67,21 +67,25 @@ public void setUp() {
OnmsNode fqhostnameNode = new OnmsNode(m_locationDao.getDefaultLocation(), "localhost.localdomain");
OnmsNode shortnameNode = new OnmsNode(m_locationDao.getDefaultLocation(), "localhost");
OnmsNode originNode = new OnmsNode(m_locationDao.getDefaultLocation(), "10.0.0.7");
OnmsNode localhostIpNode = new OnmsNode(m_locationDao.getDefaultLocation(),"127.0.0.1");

fqhostnameNode.setForeignSource("eifTestSource");
fqhostnameNode.setForeignId("eifTestId");
shortnameNode.setForeignSource("eifTestSource");
shortnameNode.setForeignId("eifTestId");
originNode.setForeignSource("eifTestSource");
originNode.setForeignId("eifTestId");
localhostIpNode.setForeignId("eifTestLocalhostIp");

fqhostnameNode.setId(1);
shortnameNode.setId(2);
originNode.setId(3);
localhostIpNode.setId(4);

m_nodeDao.saveOrUpdate(fqhostnameNode);
m_nodeDao.saveOrUpdate(shortnameNode);
m_nodeDao.saveOrUpdate(originNode);
m_nodeDao.saveOrUpdate(localhostIpNode);
m_nodeDao.flush();
}

@@ -133,7 +137,7 @@ public void testCanParseEifSlotsWithEmbeddedSemicolons() {

@Test
public void testCanParseEifEventWithSemicolonInSlot() {
String incomingEif = ".<START>>.............................EIF_TEST_EVENT_TYPE_A;cms_hostname='hubtems01';cms_port='3661';"
String incomingEif = ".<START>>......................LL.....EIF_TEST_EVENT_TYPE_A;cms_hostname='hubtems01';cms_port='3661';"
+"integration_type='U';master_reset_flag='';appl_label='';situation_name='Situation 01';"
+"situation_type='S';situation_origin='';situation_time='07/28/2016 12:19:11.000';situation_status='P';"
+"situation_thrunode='REMOTE_teps_host';situation_fullname='Situation 01';situation_displayitem='';"
@@ -158,15 +162,15 @@ public void testCanParseMultipleEvents() {
+"situation_displayitem='';source='EIF';sub_source='managedsystem01:08';hostname='managedsystem01';"
+"origin='10.0.0.1';adapter_host='managedsystem01';date='07/22/2016';severity='WARNING';"
+"msg='EIF Test Message 1';situation_eventdata='~';END";
String incomingEif_2 = ".<START>>.............................EIF_TEST_EVENT_TYPE_B;cms_hostname='hubtems01';"
String incomingEif_2 = ".<START>>......................L......EIF_TEST_EVENT_TYPE_B;cms_hostname='hubtems01';"
+"cms_port='3661';integration_type='U';master_reset_flag='';appl_label='';"
+"situation_name='DummyMonitoringSituation';situation_type='S';situation_origin='managedsystem02:LZ';"
+"situation_time='07/22/2016 14:07:52.000';situation_status='Y';situation_thrunode='HUB_hubtems01';"
+"situation_displayitem='';source='EIF';sub_source='managedsystem02:LZ';hostname='managedsystem02';"
+"origin='10.0.0.2';adapter_host='managedsystem02';date='07/22/2016';severity='HARMLESS';"
+"msg='EIF_Heartbeat';situation_eventdata='Day_Of_Month=22;Day_Of_Week=06;Hours=15;Minutes=50;"
+"Month_Of_Year=07;System_Name=managedsystem02:LZ;Seconds=25;Timestamp=1160722155025000~';END";
String incomingEif_3 = ".<START>>.............................EIF_TEST_EVENT_TYPE_A;cms_hostname='hubtems01';"
String incomingEif_3 = ".<START>>......................8......EIF_TEST_EVENT_TYPE_A;cms_hostname='hubtems01';"
+"cms_port='3661';integration_type='U';master_reset_flag='';appl_label='';"
+"situation_name='DummyMonitoringSituation';situation_type='S';situation_origin='managedsystem03:LZ';"
+"situation_time='07/22/2016 14:08:07.000';situation_status='Y';situation_thrunode='REMOTE_hubtems02';"
@@ -211,7 +215,9 @@ public void testCanConnectEifEventToNodeWithHostname() {
+"fqhostname='';hostname='localhost';origin='127.0.0.1';adapter_host='dummyHost';"
+"date='07/22/2016';severity='WARNING';msg='My Dummy Event Message';situation_eventdata='~';END";
Event e = translateEifToOpenNMS(m_nodeDao, new StringBuilder(incomingEif)).get(0);
assertEquals("NodeId "+e.getNodeid()+" must equal 2","2",e.getNodeid().toString());
// nodeId will be 4 if 'localhost' fails to resolve, and we fall back to using the IP address.
assertTrue("NodeId " + e.getNodeid() + " must be either 2 or 4",
e.getNodeid() == 2 || e.getNodeid() == 4);
}

@Test
Binary file not shown.

0 comments on commit dce85ee

Please sign in to comment.