SNMP4J入门

时间:2010-07-11 21:29:22

标签: java snmp snmp4j

我需要在SNMP4J中创建一个代理,但是关于如何开始的文档很差。有没有人有任何SNMP4J的经验,可以给我一个关于如何开始的想法?感谢。

3 个答案:

答案 0 :(得分:10)

您可以在此处下载SNMP4JAgent的源代码:

http://www.snmp4j.org/html/download.html

源代码包含一个示例代理 - 在org.snmp4j.agent.example包中查找所有相关类。

http://www.snmp4j.org/agent/doc/org/snmp4j/agent/example/SampleAgent.html

入门的一种方法是使用示例代码创建代理,然后根据您的需要对其进行修改。描述每个类的JavaDoc有点简洁,但它已经完成了。

答案 1 :(得分:1)

这是一个很棒的链接,它描述了snmp类,它是snmp4j的核心

http://www.snmp4j.org/doc/org/snmp4j/package-summary.html

另请参阅SnmpRequest.java以获取快速示例

答案 2 :(得分:0)

很难找到使用SNMP4j库的SNMPv3实现的良好文档。官方文档中没有SNMPv3代理的工作示例。我编写了一个基本的 SNMP代理,可以使用SNMPv3协议进行连接,并在服务器上执行GET和SET操作。

import java.io.IOException;

import org.snmp4j.PDU;
import org.snmp4j.ScopedPDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.UserTarget;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.MPv3;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.AuthGeneric;
import org.snmp4j.security.AuthSHA;
import org.snmp4j.security.PrivAES128;
import org.snmp4j.security.PrivacyGeneric;
import org.snmp4j.security.SecurityModels;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.security.USM;
import org.snmp4j.security.UsmUser;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.TransportIpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultTcpTransportMapping;
import org.snmp4j.transport.DefaultUdpTransportMapping;

public class SNMPV3Agent {

    private Address nmsIP;
    private String user;
    private String securityName;
    private String privacyPassword;
    private String authorizationPassword;
    private AuthGeneric authProtocol;
    private PrivacyGeneric privacyProtocol;
    private String protocol;

    private long timeOut = 1000L;
    private int noOfRetries = 2;

    private Snmp snmp;
    private UserTarget target;

    SNMPV3Agent(String ip, String protocol, int snmpPort, String username,
            String securityName, String privacyPassword, String authPassowrd,
            AuthGeneric authProtocol, PrivacyGeneric privacyProtocol) {

        nmsIP = GenericAddress.parse(protocol + ":" + ip + "/" + snmpPort);
        System.out.println("NMS IP set : " + nmsIP.toString());

        this.protocol = protocol;
        this.user = username;
        this.securityName = securityName;
        this.privacyPassword = privacyPassword;
        this.authorizationPassword = authPassowrd;
        this.authProtocol = authProtocol;
        this.privacyProtocol = privacyProtocol;

    }

    public static void main(String[] args) {

        SNMPV3Agent agent = new SNMPV3Agent("nms/server-ip", "udp", 162,
                "abhinav", "abhinav", "myprivpass", "myauthpass",
                new AuthSHA(), new PrivAES128());
        try {
            agent.startAgent();
            ResponseEvent response = agent
                    .snmpGetOperation(SnmpConstants.sysName);
            System.out.println(response.getResponse());
            // Similarly you can perform set operation.
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void startAgent() throws IOException {
        if (snmp == null) {
            TransportMapping<? extends TransportIpAddress> transport = null;
            if (protocol.equalsIgnoreCase("udp")) {
                System.out.println("UDP Protocol selected.");
                transport = new DefaultUdpTransportMapping();
            } else {
                System.out.println("TCP Protocol selected.");
                transport = new DefaultTcpTransportMapping();
            }
            snmp = new Snmp(transport);
            USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(
                    MPv3.createLocalEngineID()), 0);
            SecurityModels.getInstance().addSecurityModel(usm);
            transport.listen();
            snmp.getUSM().addUser(
                    new OctetString(user),
                    new UsmUser(new OctetString(securityName), authProtocol
                            .getID(), new OctetString(authorizationPassword),
                            privacyProtocol.getID(), new OctetString(
                                    privacyPassword)));

            target = createUserTarget();
        }

    }

    public ResponseEvent snmpSetOperation(VariableBinding[] vars)
            throws IOException {
        PDU setPdu = new ScopedPDU();
        for (VariableBinding variableBinding : vars) {
            setPdu.add(variableBinding);
        }
        return snmp.send(setPdu, target);
    }

    public ResponseEvent snmpGetOperation(OID oid) throws IOException {

        PDU getPdu = new ScopedPDU();
        getPdu.add(new VariableBinding(oid));
        return snmp.get(getPdu, target);
    }

    private UserTarget createUserTarget() {
        UserTarget target = new UserTarget();
        target.setAddress(nmsIP);
        target.setRetries(noOfRetries);
        target.setTimeout(timeOut);
        target.setVersion(3);
        target.setSecurityLevel(3);
        target.setSecurityName(new OctetString(securityName));
        return target;
    }

    public long getTimeOut() {
        return timeOut;
    }

    public void setTimeOut(long timeOut) {
        this.timeOut = timeOut;
    }

    public int getNoOfRetries() {
        return noOfRetries;
    }

    public void setNoOfRetries(int noOfRetries) {
        this.noOfRetries = noOfRetries;
    }
}

一旦理解了GET和SET的工作原理,添加其他操作(如GETBulk)将相对容易。如果您在评论中需要更多说明,请与我们联系。