Chapter 4. SNMP Manager Application

Table of Contents

4.1. Overview
4.2. SNMP Client Program Sample

This chapter describes how to develop JEUS SNMP Manager applications and how to configure them.

4.1. Overview

SNMP Managers that conforms to the SNMP (SNMPv1, SNMPv2, and SNMPv3) standard can be integrated with JEUS SNMP.

Note

Refer to the SNMP Management Framework related specification (J2EE management 1.1 specification) for more information about SNMP.

4.2. SNMP Client Program Sample

Application types vary depending on the occasion. This section shows a client program that uses the SNMP4J API.

SNMP4J is an open source API and provides SNMP implementation classes for J2SE 1.4 or higher. The SNMP client application, that uses SNMP4J API, must perform the following steps:

  1. Set PDU (Protocol Data Unit).

    Set the SNMP object OID and the PDU type.

  2. Set the target.

    Set the address and port number of the target, where SNMP information will be retrieved from, and set the SNMP version. Since JEUS SNMP uses UDP, use the UDPAddress.

  3. Create SNMP message object.

    Create a message to send.

  4. Send and receive the message.

    Send a message and receive a response.

Example

The following is a SNMP client application program example that uses the SNMP4J API.

[Example 4.1] <<SnmpclientSample.java>>

import java.net.*;
import java.util.*;
import org.snmp4j.*;
import org.snmp4j.PDU;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.transport.*;
import org.snmp4j.smi.*;

public class SnmpclientSample{
    static int defaultPort = 9999;
    static String defaultIP = "127.0.0.1";
    static String defaultOID = "1.3.6.1.4.1.14586.100.77.1";

    static void testGetNext(String oid) throws java.io.IOException {
        //1. Make Protocol Data Unit
        PDU pdu = new PDU();
        pdu.add(new VariableBinding(new OID(defaultOID)));
        pdu.setType(PDU.GETNEXT);

        //2. Make target
        CommunityTarget target = new CommunityTarget();
        UdpAddress targetAddress = new UdpAddress();
        targetAddress.setInetAddress(InetAddress.getByName(defaultIP));
        targetAddress.setPort(defaultPort);
        target.setAddress(targetAddress);
        target.setCommunity(new OctetString("public"));
        target.setVersion(SnmpConstants.version1);

        //3. Make SNMP Message. Simple!
        Snmp snmp = new Snmp(new DefaultUdpTransportMapping());

        //4. Send Message and Recieve Response
        snmp.listen();
        ResponseEvent response = snmp.send(pdu, target);
        if (response.getResponse() == null) {
            System.out.println("Error: There is some problems.");
        } else {
            Vector variableBindings = response.getResponse().getVariableBindings();
                for( int i = 0; i < variableBindings.size(); i++){
                    System.out.println(variableBindings.get(i));
                }
        }
        snmp.close();
    }

    public static void main(String[] args) throws java.io.IOException {
        // get the SNMP port number
        if (args.length > 0) {
             defaultPort = Integer.parseInt (args[0]);
        }
        System.out.println ("PORT : " + defaultPort);

        // get the ip address of the machine that the SNMP agent runs on
        if (args.length > 1) {
             defaultIP = args[1];
        }
        System.out.println ("IP : " + defaultIP);

        // get the OID number that you want to get the value of
        if (args.length > 2) {
            defaultOID = args[2];
        }
        System.out.println ("OID : " + defaultOID);
        try {
            testGetNext(defaultOID);
        } catch (Exception ex) {
            System.out.println ("ex *** : " + ex);
            ex.printStackTrace ();
        }
    }
}

Note

www.snmp4j.org provides the SNMP4J API. Refer to SNMP4J documents for more information.