代码:
// This code was generated by
// AdventNet AgentToolkit Ver4.2
// Any changes made to this file will be lost, if regenerated.
// User code should be added within user tags for code merging support, if regenerated.// Package Name (Dont Delete this comment)
package com.adventnet.examples.snmpv3 ; import com.adventnet.snmp.snmp2.*;
import com.adventnet.snmp.snmp2.agent.*;
import com.adventnet.utils.agent.*;
import com.adventnet.common.agent.*;
import com.adventnet.utils.*;import java.io.*;
import java.util.*; // This is required if we are going to use SnmpDBAdaptor.
import com.adventnet.snmp.mibs.agent.*; /**
* Agent for MIB module SimpleV3Agent
*/public class SimpleV3Agent extends SnmpAgent implements Runnable { public SimpleV3Agent(){
this(new AgentParamOptions());
} public SimpleV3Agent(AgentParamOptions agentOptions){
this.agentOptions = agentOptions;
Thread th = new Thread(this);
th.start();
} public SimpleV3Agent(String[] args){
//this takes care of the options
agentOptions = new AgentParamOptions( args); Thread th = new Thread(this);
th.start();
} public void run(){
try{
init();
}
catch(AgentRuntimeException e){
System.out.println("Bind Exception :: Port " + super.getPort() + " is in use");
}
catch(TooManyListenersException te){
System.out.println("Too Many Listeners Exception :: SnmpAgent is a Unicast Bean");
}
} public void init() throws AgentRuntimeException, TooManyListenersException{
try{
initSimpleV3Agent();
}
catch(AgentRuntimeException e){
throw e;
}
catch(TooManyListenersException te){
throw te;
}
} public void initSimpleV3Agent() throws AgentRuntimeException, TooManyListenersException { String version = "V3";
int debugLevel = 1;
int port = 8001; if(agentOptions.getVersion() != null)
version = agentOptions.getVersion(); if(agentOptions.getDebugLevel() != -1)
debugLevel = agentOptions.getDebugLevel(); if(agentOptions.getPort() != -1)
port = agentOptions.getPort(); super.setPort(port, false);
super.setSnmpVersion(version, false);
super.setDebugLevel(debugLevel); // Setting the community String to the agent
if(agentOptions.getCommunity() != null){
super.setReadCommunity(agentOptions.getCommunity());
} // Setting the write community String to the agent
if(agentOptions.getWriteCommunity() != null){
super.setWriteCommunity(agentOptions.getWriteCommunity());
}
else if(agentOptions.getCommunity() != null){
super.setWriteCommunity(agentOptions.getCommunity());
} try{
super.addSnmpPduRequestListener(hdlr);
super.addTrapRequestListener((TrapRequestListener)trapListener);
}
catch(TooManyListenersException e){
throw e;
} super.setAsyncMode(false);
trapListener.addRegistrationListener(hdlr);
try{
super.restartSnmpAgent();
}
catch(AgentRuntimeException ee){
// Here add appropriate code to handle session
throw ee;
} //Setting the agent reference in CommonUtils, so that
//this can be used in xxxInstrument and xxxEntries
CommonUtils.setAgentReference(this);
initSnmpExtensionNodes (); /** Please uncomment this piece of code inorder to use the TCP transport provider
* reference implementation. That is SnmpAgent will listen for request at
* TCP port and SnmpTrapService send traps through TCP transport.
*/
//super.setProtocol(SnmpAgent.TRANSPORT_PROVIDER);
//transportListener.setProtocol(SnmpAgent.TRANSPORT_PROVIDER); } /**
* Adds VarBindRequestListeners to SnmpAgent
*/
public void initSnmpExtensionNodes(){ agentSystemListener = new AgentSystemRequestHandler (this);
agentSystemListener.setAgentSystemInstrument(getAgentSystemInstance());
agentSystemListener.addRegistrationListener(hdlr); if(!subAgent){
AgentSnmpGroup grp = new AgentSnmpGroup();
grp.addRegistrationListener(hdlr);
super.addAuthenticationListener(grp.getAuthenticationListener());
sysORTable = new com.adventnet.snmp.snmp2.agent.SysORTableRequestHandler((SnmpAgent)this);
sysORTable.addRegistrationListener(hdlr);
SystemGroupInstrument instru = new SystemGroupInstrument();
super.addSystemGroupListener(instru);
acl = new
// This code was generated by
// AdventNet AgentToolkit Ver4.2
// Any changes made to this file will be lost, if regenerated.
// User code should be added within user tags for code merging support, if regenerated.// Package Name (Dont Delete this comment)
package com.adventnet.examples.snmpv3 ; import com.adventnet.snmp.snmp2.*;
import com.adventnet.snmp.snmp2.agent.*;
import com.adventnet.utils.agent.*;
import com.adventnet.common.agent.*;
import com.adventnet.utils.*;import java.io.*;
import java.util.*; // This is required if we are going to use SnmpDBAdaptor.
import com.adventnet.snmp.mibs.agent.*; /**
* Agent for MIB module SimpleV3Agent
*/public class SimpleV3Agent extends SnmpAgent implements Runnable { public SimpleV3Agent(){
this(new AgentParamOptions());
} public SimpleV3Agent(AgentParamOptions agentOptions){
this.agentOptions = agentOptions;
Thread th = new Thread(this);
th.start();
} public SimpleV3Agent(String[] args){
//this takes care of the options
agentOptions = new AgentParamOptions( args); Thread th = new Thread(this);
th.start();
} public void run(){
try{
init();
}
catch(AgentRuntimeException e){
System.out.println("Bind Exception :: Port " + super.getPort() + " is in use");
}
catch(TooManyListenersException te){
System.out.println("Too Many Listeners Exception :: SnmpAgent is a Unicast Bean");
}
} public void init() throws AgentRuntimeException, TooManyListenersException{
try{
initSimpleV3Agent();
}
catch(AgentRuntimeException e){
throw e;
}
catch(TooManyListenersException te){
throw te;
}
} public void initSimpleV3Agent() throws AgentRuntimeException, TooManyListenersException { String version = "V3";
int debugLevel = 1;
int port = 8001; if(agentOptions.getVersion() != null)
version = agentOptions.getVersion(); if(agentOptions.getDebugLevel() != -1)
debugLevel = agentOptions.getDebugLevel(); if(agentOptions.getPort() != -1)
port = agentOptions.getPort(); super.setPort(port, false);
super.setSnmpVersion(version, false);
super.setDebugLevel(debugLevel); // Setting the community String to the agent
if(agentOptions.getCommunity() != null){
super.setReadCommunity(agentOptions.getCommunity());
} // Setting the write community String to the agent
if(agentOptions.getWriteCommunity() != null){
super.setWriteCommunity(agentOptions.getWriteCommunity());
}
else if(agentOptions.getCommunity() != null){
super.setWriteCommunity(agentOptions.getCommunity());
} try{
super.addSnmpPduRequestListener(hdlr);
super.addTrapRequestListener((TrapRequestListener)trapListener);
}
catch(TooManyListenersException e){
throw e;
} super.setAsyncMode(false);
trapListener.addRegistrationListener(hdlr);
try{
super.restartSnmpAgent();
}
catch(AgentRuntimeException ee){
// Here add appropriate code to handle session
throw ee;
} //Setting the agent reference in CommonUtils, so that
//this can be used in xxxInstrument and xxxEntries
CommonUtils.setAgentReference(this);
initSnmpExtensionNodes (); /** Please uncomment this piece of code inorder to use the TCP transport provider
* reference implementation. That is SnmpAgent will listen for request at
* TCP port and SnmpTrapService send traps through TCP transport.
*/
//super.setProtocol(SnmpAgent.TRANSPORT_PROVIDER);
//transportListener.setProtocol(SnmpAgent.TRANSPORT_PROVIDER); } /**
* Adds VarBindRequestListeners to SnmpAgent
*/
public void initSnmpExtensionNodes(){ agentSystemListener = new AgentSystemRequestHandler (this);
agentSystemListener.setAgentSystemInstrument(getAgentSystemInstance());
agentSystemListener.addRegistrationListener(hdlr); if(!subAgent){
AgentSnmpGroup grp = new AgentSnmpGroup();
grp.addRegistrationListener(hdlr);
super.addAuthenticationListener(grp.getAuthenticationListener());
sysORTable = new com.adventnet.snmp.snmp2.agent.SysORTableRequestHandler((SnmpAgent)this);
sysORTable.addRegistrationListener(hdlr);
SystemGroupInstrument instru = new SystemGroupInstrument();
super.addSystemGroupListener(instru);
acl = new
acl.addRegistrationListener(hdlr);
}
} public void sendscalarNotification (Vector varbindVector){
TrapRequestEvent te = null;
if (super.isTrapSourceDefault()) {
te = new TrapRequestEvent(this,
varbindVector,
TrapRequestEvent.DEFAULT,
".1.3.6.1.4.1.2162.4.7",
2);
}
else {
te = new TrapRequestEvent(this,
varbindVector,
TrapRequestEvent.TFTABLE,
".1.3.6.1.4.1.2162.4.7",
2);
} te.setTimeTicks(super.getUpTime());
te.setSubAgent(windowsSubAgent); // setting the trap version to SNMP v3
trapListener.setVersion(SnmpAPI.SNMP_VERSION_3); // setting the manager host to localhost
trapListener.setManagerHost("localhost"); // setting the manager port to 162
trapListener.setManagerPort(162); if(trapListener != null){
trapListener.sendTrap(te);
}
} public void sendtableNotification (Vector varbindVector){
TrapRequestEvent te = null;
if (super.isTrapSourceDefault()) {
te = new TrapRequestEvent(this,
varbindVector,
TrapRequestEvent.DEFAULT,
".1.3.6.1.4.1.2162.4.7",
1);
} else {
te = new TrapRequestEvent(this,
varbindVector,
TrapRequestEvent.TFTABLE,
".1.3.6.1.4.1.2162.4.7",
1);
} te.setTimeTicks(super.getUpTime());
te.setSubAgent(windowsSubAgent); // setting the trap version to SNMP v3
trapListener.setVersion(SnmpAPI.SNMP_VERSION_3); // setting the manager host to localhost
trapListener.setManagerHost("localhost"); // setting the manager port to 162
trapListener.setManagerPort(162); if(trapListener != null){
trapListener.sendTrap(te);
}
} public AgentSystemRequestHandler getAgentSystem(){
return agentSystemListener;
} public AgentSystemInstrument getAgentSystemInstance(){
AgentSystemInstrument instrument = null; if(instrument == null){
instrument = new AgentSystemInstrument();
}
return instrument;
} // For not registering for SnmpGroup
// While acting as subagent
protected boolean subAgent = false; public void setSubAgent(boolean val){
subAgent = val;
} /**
* This Method takes care of registration while acting
* as windows SubAgent
*/
public void initSubAgent(){ hdlr = new PduRequestHandler ();
trapListener = new SnmpTrapService(); try{
super.addSnmpPduRequestListener(hdlr);
}
catch(TooManyListenersException e){
utils.debugPrintMedium("TooManyListenerException caught");
}
try{
trapListener.addRegistrationListener(hdlr);
}
catch(Exception e){
utils.debugPrintMedium("Exception caught");
}
initSnmpExtensionNodes ();
} protected PropertyRegistrationListener agentListener = null; public void setPropertyRegistrationListener(PropertyRegistrationListener agentListener){ this.agentListener = agentListener;
windowsSubAgent = agentListener.getWindowsSubAgent();
} // Variable Declarations
protected AgentParamOptions agentOptions = null; protected boolean windowsSubAgent = false; // Initializing PduRequestHandler
protected PduRequestHandler hdlr = new PduRequestHandler (); protected SnmpTrapService trapListener = new SnmpTrapService (); protected com.adventnet.snmp.snmp2.agent.SysORTableRequestHandler sysORTable = null; protected com.adventnet.snmp.snmp2.agent.AclTableRequestHandler acl = null; private AgentSystemRequestHandler agentSystemListener = null; public static void main(String [] args){
SimpleV3Agent agent_sim = new SimpleV3Agent (args);
}}
acl.addRegistrationListener(hdlr);
}
} public void sendscalarNotification (Vector varbindVector){
TrapRequestEvent te = null;
if (super.isTrapSourceDefault()) {
te = new TrapRequestEvent(this,
varbindVector,
TrapRequestEvent.DEFAULT,
".1.3.6.1.4.1.2162.4.7",
2);
}
else {
te = new TrapRequestEvent(this,
varbindVector,
TrapRequestEvent.TFTABLE,
".1.3.6.1.4.1.2162.4.7",
2);
} te.setTimeTicks(super.getUpTime());
te.setSubAgent(windowsSubAgent); // setting the trap version to SNMP v3
trapListener.setVersion(SnmpAPI.SNMP_VERSION_3); // setting the manager host to localhost
trapListener.setManagerHost("localhost"); // setting the manager port to 162
trapListener.setManagerPort(162); if(trapListener != null){
trapListener.sendTrap(te);
}
} public void sendtableNotification (Vector varbindVector){
TrapRequestEvent te = null;
if (super.isTrapSourceDefault()) {
te = new TrapRequestEvent(this,
varbindVector,
TrapRequestEvent.DEFAULT,
".1.3.6.1.4.1.2162.4.7",
1);
} else {
te = new TrapRequestEvent(this,
varbindVector,
TrapRequestEvent.TFTABLE,
".1.3.6.1.4.1.2162.4.7",
1);
} te.setTimeTicks(super.getUpTime());
te.setSubAgent(windowsSubAgent); // setting the trap version to SNMP v3
trapListener.setVersion(SnmpAPI.SNMP_VERSION_3); // setting the manager host to localhost
trapListener.setManagerHost("localhost"); // setting the manager port to 162
trapListener.setManagerPort(162); if(trapListener != null){
trapListener.sendTrap(te);
}
} public AgentSystemRequestHandler getAgentSystem(){
return agentSystemListener;
} public AgentSystemInstrument getAgentSystemInstance(){
AgentSystemInstrument instrument = null; if(instrument == null){
instrument = new AgentSystemInstrument();
}
return instrument;
} // For not registering for SnmpGroup
// While acting as subagent
protected boolean subAgent = false; public void setSubAgent(boolean val){
subAgent = val;
} /**
* This Method takes care of registration while acting
* as windows SubAgent
*/
public void initSubAgent(){ hdlr = new PduRequestHandler ();
trapListener = new SnmpTrapService(); try{
super.addSnmpPduRequestListener(hdlr);
}
catch(TooManyListenersException e){
utils.debugPrintMedium("TooManyListenerException caught");
}
try{
trapListener.addRegistrationListener(hdlr);
}
catch(Exception e){
utils.debugPrintMedium("Exception caught");
}
initSnmpExtensionNodes ();
} protected PropertyRegistrationListener agentListener = null; public void setPropertyRegistrationListener(PropertyRegistrationListener agentListener){ this.agentListener = agentListener;
windowsSubAgent = agentListener.getWindowsSubAgent();
} // Variable Declarations
protected AgentParamOptions agentOptions = null; protected boolean windowsSubAgent = false; // Initializing PduRequestHandler
protected PduRequestHandler hdlr = new PduRequestHandler (); protected SnmpTrapService trapListener = new SnmpTrapService (); protected com.adventnet.snmp.snmp2.agent.SysORTableRequestHandler sysORTable = null; protected com.adventnet.snmp.snmp2.agent.AclTableRequestHandler acl = null; private AgentSystemRequestHandler agentSystemListener = null; public static void main(String [] args){
SimpleV3Agent agent_sim = new SimpleV3Agent (args);
}}
才能运行通过,如果你直接输入java SimpleV3Agent 就会出现这个错误
2.注意文件名。
3.同意happyegg