SNMPAgent。JAVA
package com.G2.SNMP.Server;
import java.io.File;
import java.io.IOException;
import org.snmp4j.TransportMapping;
import org.snmp4j.agent.BaseAgent;
import org.snmp4j.agent.CommandProcessor;
import org.snmp4j.agent.DuplicateRegistrationException;
import org.snmp4j.agent.MOGroup;
import org.snmp4j.agent.ManagedObject;
import org.snmp4j.agent.mo.MOTableRow;
import org.snmp4j.agent.mo.snmp.RowStatus;
import org.snmp4j.agent.mo.snmp.SnmpCommunityMIB;
import org.snmp4j.agent.mo.snmp.SnmpNotificationMIB;
import org.snmp4j.agent.mo.snmp.SnmpTargetMIB;
import org.snmp4j.agent.mo.snmp.StorageType;
import org.snmp4j.agent.mo.snmp.VacmMIB;
import org.snmp4j.agent.security.MutableVACM;
import org.snmp4j.mp.MPv3;
import org.snmp4j.security.SecurityLevel;
import org.snmp4j.security.SecurityModel;
import org.snmp4j.security.USM;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.Variable;
import org.snmp4j.transport.TransportMappings;
public class SNMPAgent extends BaseAgent {
private String address;
/**
*
* @param address
* @throws IOException
*/
public SNMPAgent(String address) throws IOException {
/**
* Creates a base agent with boot-counter, config file, and a
* CommandProcessor for processing SNMP requests. Parameters:
* "bootCounterFile" - a file with serialized boot-counter information
* (read/write). If the file does not exist it is created on shutdown of
* the agent. "configFile" - a file with serialized configuration
* information (read/write). If the file does not exist it is created on
* shutdown of the agent. "commandProcessor" - the CommandProcessor
* instance that handles the SNMP requests.
*/
super(new File("conf.agent"), new File("bootCounter.agent"),
new CommandProcessor(
new OctetString(MPv3.createLocalEngineID())));
this.address = address;
}
/**
* Adds community to security name mappings needed for SNMPv1 and SNMPv2c.
*/
@Override
protected void addCommunities(SnmpCommunityMIB communityMIB) {
Variable[] com2sec = new Variable[] { new OctetString("public"),
new OctetString("cpublic"), // security name
getAgent().getContextEngineID(), // local engine ID
new OctetString("public"), // default context name
new OctetString(), // transport tag
new Integer32(StorageType.nonVolatile), // storage type
new Integer32(RowStatus.active) // row status
};
SnmpCommunityMIB.SnmpCommunityEntryRow row = communityMIB.getSnmpCommunityEntry().createRow(
new OctetString("public2public").toSubIndex(true), com2sec);
communityMIB.getSnmpCommunityEntry().addRow(row);
}
/**
* Adds initial notification targets and filters.
*/
@Override
protected void addNotificationTargets(SnmpTargetMIB arg0,
SnmpNotificationMIB arg1) {
// TODO Auto-generated method stub
}
/**
* Adds all the necessary initial users to the USM.
*/
@Override
protected void addUsmUser(USM arg0) {
// TODO Auto-generated method stub
}
/**
* Adds initial VACM configuration.
*/
@Override
protected void addViews(VacmMIB vacm) {
vacm.addGroup(SecurityModel.SECURITY_MODEL_SNMPv2c, new OctetString(
"cpublic"), new OctetString("v1v2group"),
StorageType.nonVolatile);
vacm.addAccess(new OctetString("v1v2group"), new OctetString("public"),
SecurityModel.SECURITY_MODEL_ANY, SecurityLevel.NOAUTH_NOPRIV,
MutableVACM.VACM_MATCH_EXACT, new OctetString("fullReadView"),
new OctetString("fullWriteView"), new OctetString(
"fullNotifyView"), StorageType.nonVolatile);
vacm.addViewTreeFamily(new OctetString("fullReadView"), new OID("1.3"),
new OctetString(), VacmMIB.vacmViewIncluded,
StorageType.nonVolatile);
}
/**
* Unregister the basic MIB modules from the agent's MOServer.
*/
@Override
protected void unregisterManagedObjects() {
// TODO Auto-generated method stub
}
/**
* Register additional managed objects at the agent's server.
*/
@Override
protected void registerManagedObjects() {
// TODO Auto-generated method stub
}
protected void initTransportMappings() throws IOException {
transportMappings = new TransportMapping[1];
Address addr = GenericAddress.parse(address);
TransportMapping tm = TransportMappings.getInstance()
.createTransportMapping(addr);
transportMappings[0] = tm;
}
/**
* Start method invokes some initialization methods needed to start the
* agent
*
* @throws IOException
*/
public void start() throws IOException {
init();
// This method reads some old config from a file and causes
// unexpected behavior.
// loadConfig(ImportModes.REPLACE_CREATE);
addShutdownHook();
getServer().addContext(new OctetString("public"));
finishInit();
run();
sendColdStartNotification();
}
/**
* Clients can register the MO they need
*/
public void registerManagedObject(ManagedObject mo) {
try {
server.register(mo, null);
} catch (DuplicateRegistrationException ex) {
throw new RuntimeException(ex);
}
}
public void unregisterManagedObject(MOGroup moGroup) {
moGroup.unregisterMOs(server, getContext(moGroup));
}
}
snmp代理类使用的托管对象:MOCreator。JAVA
package com.G2.SNMP.Server;
import org.snmp4j.agent.mo.MOAccessImpl;
import org.snmp4j.agent.mo.MOScalar;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.Variable;
/**
* This class creates and returns ManagedObjects
* @author Shiva
*
*/
public class MOCreator {
public static MOScalar createReadOnly(OID oid,Object value ){
return new MOScalar(oid,
MOAccessImpl.ACCESS_READ_ONLY,
getVariable(value));
}
private static Variable getVariable(Object value) {
if(value instanceof String) {
return new OctetString((String)value);
}
throw new IllegalArgumentException("Unmanaged Type: " + value.getClass());
}
}
SNMPManager。JAVA
package com.G2.SNMP.client;
import java.io.IOException;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.Target;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
public class SNMPManager {
Snmp snmp = null;
String address = null;
/**
* Constructor
* @param add
*/
public SNMPManager(String add)
{
address = add;
}
public static void main(String[] args) throws IOException {
/**
* Port 161 is used for Read and Other operations
* Port 162 is used for the trap generation
*/
SNMPManager client = new SNMPManager("udp:127.0.0.1/161");
client.start();
/**
* OID - .1.3.6.1.2.1.1.1.0 => SysDec
* OID - .1.3.6.1.2.1.1.5.0 => SysName
* => MIB explorer will be usefull here, as discussed in previous article
*/
String sysDescr = client.getAsString(new OID(".1.3.6.1.2.1.1.1.0"));
System.out.println(sysDescr);
}
/**
* Start the Snmp session. If you forget the listen() method you will not
* get any answers because the communication is asynchronous
* and the listen() method listens for answers.
* @throws IOException
*/
public void start() throws IOException {
TransportMapping transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
// Do not forget this line!
transport.listen();
}
/**
* Method which takes a single OID and returns the response from the agent as a String.
* @param oid
* @return
* @throws IOException
*/
public String getAsString(OID oid) throws IOException {
ResponseEvent event = get(new OID[] { oid });
return event.getResponse().get(0).getVariable().toString();
}
/**
* This method is capable of handling multiple OIDs
* @param oids
* @return
* @throws IOException
*/
public ResponseEvent get(OID oids[]) throws IOException {
PDU pdu = new PDU();
for (OID oid : oids) {
pdu.add(new VariableBinding(oid));
}
pdu.setType(PDU.GET);
ResponseEvent event = snmp.send(pdu, getTarget(), null);
if(event != null) {
return event;
}
throw new RuntimeException("GET timed out");
}
/**
* This method returns a Target, which contains information about
* where the data should be fetched and how.
* @return
*/
private Target getTarget() {
Address targetAddress = GenericAddress.parse(address);
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setAddress(targetAddress);
target.setRetries(2);
target.setTimeout(1500);
target.setVersion(SnmpConstants.version2c);
return target;
}
}
TestSNMPAgent.java
package com.G2.SNMP.Server;
import java.io.IOException;
import org.snmp4j.smi.OID;
import com.G2.SNMP.client.SNMPManager;
public class TestSNMPAgent {
static final OID sysDescr = new OID(".1.3.6.1.2.1.1.1.0");
public static void main(String[] args) throws IOException {
TestSNMPAgent client = new TestSNMPAgent("udp:127.0.0.1/161");
client.init();
}
SNMPAgent agent = null;
/**
* This is the client which we have created earlier
*/
SNMPManager client = null;
String address = null;
/**
* Constructor
*
* @param add
*/
public TestSNMPAgent(String add) {
address = add;
}
private void init() throws IOException {
agent = new SNMPAgent("0.0.0.0/2001");
agent.start();
// Since BaseAgent registers some MIBs by default we need to unregister
// one before we register our own sysDescr. Normally you would
// override that method and register the MIBs that you need
agent.unregisterManagedObject(agent.getSnmpv2MIB());
// Register a system description, use one from you product environment
// to test with
agent.registerManagedObject(MOCreator.createReadOnly(sysDescr,
"This Description is set By ShivaSoft"));
// Setup the client to use our newly started agent
client = new SNMPManager("udp:127.0.0.1/2001");
client.start();
// Get back Value which is set
System.out.println(client.getAsString(sysDescr));
}
}
这就是我编译代码的方式(我已经有snmp4j和api的代理. jar文件)
javac-d.-cp"snmp4j-2.5.0.jar:snmp4j-agent-2.5.0.jar"*. java
这就是我执行代码的方式
Java-cp"snmp4j-2.5.0.jar:snmp4j-agent-2.5.0.jar"com. G2. SNMP. Server. TestSNMPAgent
这里的问题是,当我尝试执行它时,我总是会遇到这个错误
错误:无法找到或加载主类com。G2。SNMP。服务器测试代理
可能是有一些错误,在我执行它的方式,欢迎任何帮助!谢谢你!
使用执行。jar libs语法如下:
java-cp one。罐子:两个。jar:。fileToRun文件
因此:
java-cp“snmp4j-2.5.0.jar:snmp4j-agent-2.5.0.jar:”com。G2。SNMP。服务器测试代理
代替
Java-cp"snmp4j-2.5.0.jar:snmp4j-agent-2.5.0.jar"com. G2. SNMP. Server. TestSNMPAgent
问题内容: 我正在尝试使用Gradle运行一个非常简单的项目,并在使用时遇到以下错误: 这是我的文件结构: 我排除了libs和tmp文件夹的内容,因为我认为这与该问题无关,但是我可以在需要时添加它。 这是我的build.gradle文件: 关于如何解决此问题的任何想法?我已经为mainClassName属性尝试了各种方法,但似乎没有任何效果。 问题答案: 我在这里看到两个问题,一个与另一个有关。
我的java应用程序编译有问题。 编译器说:
我是按照Spring的教程,一切都工作正常,直到我克隆我的项目,现在克隆的项目和原来的项目不想工作,我已经搜索槽整个堆栈溢出类似的问题,我无法解决这个问题,我已经清理了项目,重新启动eclipse,更新,添加和删除依赖项从pom.xml似乎没有什么工作。 这是我的主要课程: Beans.xml: 波姆。xml: 正如我所说,当我在同一个工作区复制粘贴项目时,一切都开始失败,当然我更改了名称。 这个
我用的是软呢帽19。HelloWorld的内容。爪哇: 我可以使用 javac HelloWorld。JAVA 但我无法使用 java HelloWorld 它给出了以下错误 错误:无法找到或加载主类HelloWorld 但我可以用 sudo java HelloWorld 我错过了什么???
我知道这个问题已经被回答过很多次了,但不幸的是我找不到我的问题的正确答案。 以下是我的软件包结构,在我的软件包中,我有简单测试.java d: \junit\src\junitfaq\SimpleTest.java 在d:\junit\src内部 d:\junit\src 但是当我尝试使用下面的命令行运行程序时 d:\junit\src 发生了此错误。错误:无法找到或加载主类junitfaq.Si
在Eclipse中,我使用JavaFX、Maven和jre 1.8。0_45。 通常,当我尝试以这种方式使用Maven编译时(运行方式- 当我将其更改为jdk(在配置、窗口属性、maven配置中随处可见)并尝试运行/构建/编译/无论遇到什么错误 没有给我上课。就这个。 我通过在我的pom中添加到javac的路径并保留jre包来解决这个问题 这是可行的。 但我还是想知道——为什么会发生这种错误?我做