[SNMP4J] Need help with Weblogic message trap Manager
Rabun Jones
rjones at CellularSouth.com
Thu Dec 22 16:17:17 CET 2005
I have weblogic configured to send a log filter snmp v1 trap message to
port 162. The same server listens for responses from the manager on port
161. Everything seems to be set up correctly but (being new to the whole
snmp thing) I can't seem to write a manager that will receive the
message and parse it. I am using the snmpRequest.java example from the
api, but I am not having any luck. I would greatly appreciate any help
that could be given. What I am tying to do is to receive a trap based
off of a thread stuck message in weblogic and use my snmp manager to
send sms messages to server managers. The sms part I can handle, but my
manger isn't working properly. Extra info (The weblogic server is
running on the same box as the manager, the version is 8.1, and I can
readily create the stuck thread problem so the trap should be called).
Here is all of the code I have tried so far. It is probably a mess
because I don't know which parts I need and those I don't
private class SNMPClient implements CommandResponder, PDUFactory
,Runnable
{
//SNMP VARS//
private Address listenAddress = null;
private Address targetAddress = null;
private Target target = null;
private OctetString community = new OctetString("public");;
private Vector vbs = new Vector();
private PDUv1 v1TrapPDU = new PDUv1();
private int pduType = PDU.V1TRAP;
private int numDispatcherThreads = 2;
public SNMPClient()
{
//Constuctor
//Counter listener to return proper USM and MP error
counters
CounterSupport.getInstance().addCounterListener(new
DefaultCounterListener());
try
{
vbs.add(new VariableBinding(new OID("1.3.6")));
listenAddress = new
UdpAddress(InetAddress.getLocalHost(),162); //UDP Listener on Default
Port
targetAddress = new
UdpAddress(InetAddress.getLocalHost(),161); //UDP sender on Default Port
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void run()
{
//Required by Runnable when spawning a new thread
try
{
//Fire up listener
SNMPClient client = new SNMPClient();
//Init Log4j Logging
LogFactory.setLogFactory(new Log4jLogFactory());
BER.setCheckSequenceLength(false);
//Set to listen
client.listen();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public synchronized void processPdu(CommandResponderEvent event)
{
System.out.println("ProcessPdu Called");
//This is for Listening for SNMP notifications of stuck
threads
//A PDU represents an SNMP protocol data unit
PDU command = event.getPDU();
if(command!=null)
{
System.out.println("PDU command = " +
command.toArray());
//Check for TRAP command
if(command.getType() != PDU.TRAP && command.getType()
!= PDU.V1TRAP)
{
System.out.println("Received PDU is of Unrecognized
type");
command.setErrorIndex(0);
command.setErrorStatus(0);
command.setType(PDU.RESPONSE);
StatusInformation statusInformation = new
StatusInformation();
StateReference ref = event.getStateReference();
try
{
event.getMessageDispatcher().returnResponsePdu(event.getMessageProcessin
gModel(),
event.getSecurityModel(),event.getSecurityName(),event.getSecurityLevel(
),
command,event.getMaxSizeResponsePDU(),ref,statusInformation);
}
catch(MessageException me)
{
System.out.println("Error while sending
threadwatch snmp response");
me.printStackTrace();
}
//Event now processed
event.setProcessed(true);
}
else
{
System.out.println("Valid PDU");
int index = 0;
//Message Trap
Vector binding = command.getVariableBindings();
index = binding.indexOf("trapTime");
String trapTime = (String) binding.get(index);
index = binding.indexOf("trapServerName");
String trapServerName = (String)
binding.get(index);
index = binding.indexOf("trapMachineName");
String trapMachineName = (String)
binding.get(index);
index = binding.indexOf("trapLogThreadId");
String trapLogThreadId = (String)
binding.get(index);
index = binding.indexOf("trapLogMsgId");
String trapLogMsgId = (String) binding.get(index);
index = binding.indexOf("trapLogSeverity");
String trapLogSeverity = (String)
binding.get(index);
index = binding.indexOf("trapLogMessage");
String trapLogMessage = (String)
binding.get(index);
System.out.println("TRAP TIME = " + trapTime + " |
SERVER NAME = " + trapServerName + " | MACHINE NAME = " +
trapMachineName);
System.out.println("THREAD ID = " + trapLogThreadId
+ " | MSG ID = " + trapLogMsgId + " | SEVERITY = " + trapLogSeverity);
System.out.println("MESSAGE = " + trapLogMessage);
//This event has now been processed
event.setProcessed(true);
}
}
}
public PDU createPDU(Target target)
{
System.out.println("Create PDU called");
PDU request;
if(pduType == PDU.V1TRAP)
{
request = v1TrapPDU;
}
else
{
request = new PDU();
}
request.setType(pduType);
return request;
}
public synchronized void listen() throws IOException{
System.out.println("Listen Called");
//Set up Listener
AbstractTransportMapping transport;
if(listenAddress instanceof TcpAddress)
{
transport = new DefaultTcpTransportMapping((TcpAddress)
listenAddress);
}
else
{
transport = new DefaultUdpTransportMapping((UdpAddress)
listenAddress);
}
ThreadPool threadPool = ThreadPool.create("DispatcherPool",
numDispatcherThreads); //Threadpool size of 2
MessageDispatcher mtDispatcher = new
MultiThreadedMessageDispatcher(threadPool, new MessageDispatcherImpl());
//Add Message processing models
mtDispatcher.addMessageProcessingModel(new MPv1());
mtDispatcher.addMessageProcessingModel(new MPv2c());
mtDispatcher.addMessageProcessingModel(new MPv3());
//add all security protocols
SecurityProtocols.getInstance().addDefaultProtocols();
//Dispatcher Processing models and protocols added to Snmp
object and listener attached
Snmp snmp = new Snmp(mtDispatcher,transport);
this.target = new CommunityTarget(targetAddress,community);
snmp.addCommandResponder(this);
transport.listen();
System.out.println("ThreadWatch SNMP Manager Listening on "
+ listenAddress);
try
{
this.wait();
}
catch(InterruptedException ex)
{
}
}
}
More information about the SNMP4J
mailing list