org.snmp4j.smi.UdpAddress Java Examples

The following examples show how to use org.snmp4j.smi.UdpAddress. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SNMPUtilsTest.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Method to create community target
 * @param community community name
 * @param address address
 * @param version SNMP version
 * @return community target
 */
protected static CommunityTarget createCommTarget(String community, String address, int version) {
    CommunityTarget target = new CommunityTarget();
    target.setVersion(version);
    target.setCommunity(new OctetString(community));
    target.setAddress(new UdpAddress(address));
    target.setRetries(0);
    target.setTimeout(500);
    return target;
}
 
Example #2
Source File: SNMPUtilsTest.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Method to create a user target
 * @param address address
 * @param securityLevel security level
 * @param securityName security name
 * @return user target
 */
private static UserTarget createUserTarget(String address, int securityLevel, String securityName) {
    UserTarget target = new UserTarget();
    target.setVersion(SnmpConstants.version3);
    target.setSecurityLevel(securityLevel);
    target.setSecurityName(new OctetString(securityName));
    target.setAddress(new UdpAddress(address));
    target.setRetries(0);
    target.setTimeout(500);
    return target;
}
 
Example #3
Source File: ListenpointSnmp.java    From mts with GNU General Public License v3.0 5 votes vote down vote up
/** Send a Msg to Channel */
@Override
public synchronized boolean sendMessage(Msg msg, String remoteHost, int remotePort, String transport) throws Exception
{
    ((MsgSnmp) msg).getTarget().setAddress(new UdpAddress(remoteHost + "/" + remotePort));

    //asynchronous way, response processing is delayed to processResponse
    snmp.send(((MsgSnmp) msg).getPdu(), ((MsgSnmp) msg).getTarget(), null, processMessage);
    return true;
}
 
Example #4
Source File: SNMPUtils.java    From ingestion with Apache License 2.0 5 votes vote down vote up
public static void sendTrapV2(String port) throws IOException {
    PDU trap = new PDU();
    trap.setType(PDU.TRAP);

    OID oid = new OID("1.2.3.4.5");
    trap.add(new VariableBinding(SnmpConstants.snmpTrapOID, oid));
    trap.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(5000)));
    trap.add(new VariableBinding(SnmpConstants.sysDescr, new OctetString("System Description")));

    // Add Payload
    Variable var = new OctetString("some string");
    trap.add(new VariableBinding(oid, var));

    // Specify receiver
    Address targetaddress = new UdpAddress("127.0.0.1/" + port);
    CommunityTarget target = new CommunityTarget();
    target.setCommunity(new OctetString("public"));
    target.setVersion(SnmpConstants.version2c);
    target.setAddress(targetaddress);

    // Send
    Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
    snmp.send(trap, target, null, null);

    snmp.close();
}
 
Example #5
Source File: SNMPUtilsTest.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Method to create community target
 * @param community community name
 * @param address address
 * @param version SNMP version
 * @return community target
 */
protected static CommunityTarget createCommTarget(String community, String address, int version) {
    CommunityTarget target = new CommunityTarget();
    target.setVersion(version);
    target.setCommunity(new OctetString(community));
    target.setAddress(new UdpAddress(address));
    target.setRetries(0);
    target.setTimeout(500);
    return target;
}
 
Example #6
Source File: SNMPUtilsTest.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Method to create a user target
 * @param address address
 * @param securityLevel security level
 * @param securityName security name
 * @return user target
 */
private static UserTarget createUserTarget(String address, int securityLevel, String securityName) {
    UserTarget target = new UserTarget();
    target.setVersion(SnmpConstants.version3);
    target.setSecurityLevel(securityLevel);
    target.setSecurityName(new OctetString(securityName));
    target.setAddress(new UdpAddress(address));
    target.setRetries(0);
    target.setTimeout(500);
    return target;
}
 
Example #7
Source File: SnmpHelper.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public SnmpHelper(String address, String community) {
    _target = new CommunityTarget();
    _target.setCommunity(new OctetString(community));
    _target.setVersion(SnmpConstants.version2c);
    _target.setAddress(new UdpAddress(address));
    try {
        _snmp = new Snmp(new DefaultUdpTransportMapping());
    } catch (IOException e) {
        _snmp = null;
        throw new CloudRuntimeException(" Error in crearting snmp object, " + e.getMessage());
    }
}
 
Example #8
Source File: ListenpointSnmp.java    From mts with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean create(String protocol) throws Exception
{
    processMessage = new ProcessMessage(this);
    snmp = new Snmp(new DefaultUdpTransportMapping(new UdpAddress(getHost() + "/" + getPort())));
    snmp.addCommandResponder(processMessage);

    // TEST CODE : initialize for receiving SNMPV3
    {
        String authPassword = stack.getConfig().getString("protocol.authPassword");
        String encryptPassword = stack.getConfig().getString("protocol.encryptPassword");

        snmp.getMessageDispatcher().removeMessageProcessingModel(new MPv3());
        snmp.getMessageDispatcher().addMessageProcessingModel(new MPv3(MPv3.createLocalEngineID(new OctetString(this.getUID()))));

        USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(this.getUID().getBytes()), 0);
        SecurityModels.getInstance().addSecurityModel(usm);

        // need to get a list of users and their params from config
        snmp.getUSM().addUser(new OctetString("MD5DES"),
                new UsmUser(new OctetString("MD5DES"),
                            AuthMD5.ID, new OctetString("protocol.authPassword"),
                            PrivDES.ID, new OctetString("protocol.encryptPassword")));
    }

    snmp.listen();

    return true;
}
 
Example #9
Source File: ProcessMessage.java    From mts with GNU General Public License v3.0 4 votes vote down vote up
public void onResponse(ResponseEvent re) {
       // Always cancel async request when response has been received
       // otherwise a memory leak is created! Not canceling a request
       // immediately can be useful when sending a request to a broadcast
       // address.
       try {
            Object source = re.getSource();

           // test to ignore REPORTS from DISCOVERY messages in SNMPv3
           if (!(source instanceof Snmp)) return;

           ((Snmp) source).cancel(re.getRequest(), this);

           //create the SnmpMsg received
           MsgSnmp msg = new MsgSnmp(listenpoint.getStack());
           msg.setPdu(re.getResponse());
           
           //TODO: how to know the version here to set communityTarget or UserTarget
           Target target = new CommunityTarget();
//           ((CommunityTarget)target).setCommunity(new OctetString(re.getStateReference().getSecurityName()));
           target.setAddress(re.getPeerAddress());
           msg.setTarget((AbstractTarget)target);
           UdpAddress add = (UdpAddress) re.getPeerAddress();
           msg.setRemotePort(add.getPort());
           msg.setRemoteHost(add.getInetAddress().getHostAddress());
           msg.setListenpoint(listenpoint);

           StackFactory.getStack(StackFactory.PROTOCOL_SNMP).receiveMessage(msg);
       } catch (Exception ex) {
       }
    }
 
Example #10
Source File: SNMPUtils.java    From ingestion with Apache License 2.0 4 votes vote down vote up
public static void sendTrapV1(String port) throws IOException {

        TransportMapping<?> transport = new DefaultUdpTransportMapping();
        transport.listen();

        CommunityTarget comtarget = new CommunityTarget();
        comtarget.setCommunity(new OctetString(new OctetString("public")));
        comtarget.setVersion(SnmpConstants.version1);
        comtarget.setAddress(new UdpAddress("127.0.0.1/" + port));
        comtarget.setRetries(2);
        comtarget.setTimeout(5000);

        PDU trap = new PDUv1();
        trap.setType(PDU.V1TRAP);

        OID oid = new OID("1.2.3.4.5");
        trap.add(new VariableBinding(SnmpConstants.snmpTrapOID, oid));
        trap.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(5000)));
        trap.add(new VariableBinding(SnmpConstants.sysDescr, new OctetString("System Description")));

        // Add Payload
        Variable var = new OctetString("some string");
        trap.add(new VariableBinding(oid, var));

        // Send
        Snmp snmp = new Snmp(transport);
        snmp.send(trap, comtarget);
        transport.close();
        snmp.close();

    }