我已经应用了使用 snmp4j 的示例来发送和接收陷阱,一切正常。
但问题是:
当使用 mule esb 接收snmp陷阱时,我无法将传入消息有效负载转换为 PDU (或任何适合的snmp4j对象)来提取数据来自,我做了很多搜索但是徒劳无功。
任何人都可以帮助我:
将我从udp端点收到的mule esb消息有效负载转换为org.snmp4j.PDU对象以从中提取陷阱数据?
这是我的代码:
public synchronized MuleEvent process(MuleEvent event) throws MuleException { byte[] encodedMessage = event.getMessage().getPayload(byte[].class); //next line is not working but its only sample of what I Am looking for PDU pdu = new PDU(encodedMessage ); .....
答案 0 :(得分:1)
public class SNMP4JParser implements Callable {
/**
* The following objects are all necessary in order to use SNMP4j as a parser for raw messages.
* This was all inspired by SNMP4j source code, in particular MessageDispatcherImpl.java
*/
private MessageProcessingModel model = null;
private MessageDispatcher dispatcher = null;
private Address listenAddress = null;
private Integer32 messageProcessingModel = null;
private Integer32 securityModel = null;
private OctetString securityName = null;
private Integer32 securityLevel = null;
private PduHandle handle = null;
private StatusInformation statusInfo = null;
private MutableStateReference mutableStateReference = null;
/**
* Taken from org.snmp4j.transport.AbstractTransportMapping class
*/
protected Integer32 maxInboundMessageSize = new Integer32 ( (1 << 16) - 1 );
/**
* Taken from org.snmp4j.MessageDispatcherImpl class
*/
private int transactionID = new Random().nextInt(Integer.MAX_VALUE - 2) + 1;
/**
* Create all objects that SNMP4j needs to parse a raw SNMP message
*/
public SNMP4JParser()
{
model = new MPv1();
dispatcher = new MessageDispatcherImpl();
listenAddress = GenericAddress.parse("udp:0.0.0.0/2001");
messageProcessingModel = new Integer32();
securityModel = new Integer32();
securityName = new OctetString();
securityLevel = new Integer32();
handle = new PduHandle(transactionID);
statusInfo = new StatusInformation();
mutableStateReference = new MutableStateReference();
}
/**
* @see org.mule.api.lifecycle.Callable#onCall(org.mule.api.MuleEventContext)
*/
@Override
public Object onCall(MuleEventContext eventContext) throws Exception
{
byte[] payloadBytes = eventContext.getMessage().getPayloadAsBytes();
ByteBuffer buffer = ByteBuffer.wrap(payloadBytes);
BERInputStream wholeMessage = new BERInputStream(buffer);
MutablePDU mutablePdu = new MutablePDU();
int status = model.prepareDataElements(
dispatcher,
listenAddress,
wholeMessage,
messageProcessingModel,
securityModel,
securityName,
securityLevel,
mutablePdu,
handle,
maxInboundMessageSize,
statusInfo,
mutableStateReference);
if ( status != SnmpConstants.SNMP_MP_OK )
throw new RuntimeException(
"Couldn't parse SNMP message. model.prepareDataElements() returned " + status);
return mutablePdu.getPdu();
}
}
我用这样的流测试了它(我使用 snmp4j-1.11.5 和 mule-standalone-3.4.0 )
<udp:connector name="connector" doc:name="UDP"/>
<flow name="snmp-demo-trapHandlingFlow" doc:name="snmp-demo-trapHandlingFlow">
<udp:inbound-endpoint host="0.0.0.0" port="2001" responseTimeout="10000" doc:name="UDP"/>
<logger message="TRAP RECEIVED - #[System.currentTimeMillis()]" level="DEBUG" doc:name="Inbound timestamp"/>
<component class="com.netboss.flow.demo.SNMP4JParser" doc:name="SNMP4JParser"/>
[...]
它有效。
现在,我意识到还有一些悬而未决的问题:
答案 1 :(得分:1)
当您实现自己的TransportMapping并将其与SNMP4J MessageDispatcherImpl关联时,您可以更轻松地将BER流转换为SNMP4J PDU。然后将所有必需的MessageProcessingModels和SecurityProtocols添加到消息调度程序。
最后,将您的CommandResponder接口实现添加到消息调度程序中,您就完成了。
答案 2 :(得分:0)
您需要创建一个自定义转换器,用于转换相关SNMP4J对象中的消息有效内容。或者,如果SNMP4J API足够简单,可以使用表达式转换器来完成。