📄 agentmanager.java
字号:
package com.poson.nmi.agent ;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import com.poson.nmi.common.MOFactory;
import com.poson.nmi.common.MOConfig;
import org.snmp4j.*;
import org.snmp4j.mp.StateReference;
import org.snmp4j.mp.StatusInformation;
import org.snmp4j.smi.*;
import org.snmp4j.transport.DefaultUdpTransportMapping;
public class AgentManager {
public static class Handler implements org.snmp4j.CommandResponder
{
protected String mAddress = null ;
protected int mPort = 0 ;
protected String mCommunityName = null ;
protected TransportMapping mServerSocket = null;
protected Snmp mSNMP = null;
MOFactory moFactory ;
public Handler()
{
moFactory = new MOFactory() ;
moFactory.loadMO() ;
}
public void configure()
{
mAddress = "0.0.0.0" ;
mPort = new Integer(MOConfig.getInstance().getValue(MOConfig.NMI_AGENT_PORT)) ;
mCommunityName = "public" ;
}
public void start()
{
try {
mServerSocket = new DefaultUdpTransportMapping(
new UdpAddress(InetAddress.getByName(mAddress),mPort));
mSNMP = new Snmp(mServerSocket) ;
mSNMP.addCommandResponder(this) ;
mServerSocket.listen() ;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public synchronized void processPdu(CommandResponderEvent aEvent) {
String communityName = new String(aEvent.getSecurityName()) ;
System.out.println("Community name " + communityName) ;
PDU pdu = aEvent.getPDU() ;
if(pdu == null )
{
System.out.println(" NULL PDU") ;
}
else
{
OID reqOid = pdu.get(0).getOid() ;
System.out.println(reqOid.toString()) ;
System.out.println(" REV PDU : " + pdu.toString()) ;
switch(pdu.getType())
{
case PDU.GET:
case PDU.GETNEXT:
break ;
}
StatusInformation statusInformation = new StatusInformation();
StateReference ref = aEvent.getStateReference();
try
{
System.out.println(" Sending response : ") ;
pdu.setType(PDU.RESPONSE) ;
String respStr = "Unkown Object!" ;
respStr = moFactory.getValue(reqOid.toString()) ;
pdu.set(0, new VariableBinding(pdu.get(0).getOid(),new OctetString(respStr))) ;
aEvent.getMessageDispatcher().returnResponsePdu(
aEvent.getMessageProcessingModel(),
aEvent.getSecurityModel(),
aEvent.getSecurityName(),
aEvent.getSecurityLevel(),
pdu, aEvent.getMaxSizeResponsePDU(), ref, statusInformation) ;
}
catch(MessageException e)
{
e.printStackTrace() ;
}
}
}
}
public static void main(String[] args)
{
Handler hand = new Handler() ;
hand.configure() ;
System.out.println("here we are.........") ;
hand.start() ;
while(true)
{
synchronized(AgentManager.class)
{
try{
System.out.println("wait for request on port " + hand.mPort) ;
AgentManager.class.wait() ;
}
catch(Exception e)
{
e.printStackTrace() ;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -