Introduction to SNMP

Source: Internet
Author: User
Tags snmp snmp object snmpv3

Introduction to SNMP

I. Introduction of SNMP

SNMP refers to a simple network management protocol. It belongs to the application layer protocol in the TCP/IP five layer protocol. It provides a simple and convenient mode for managing individual elements in a network. The elements here are the various managed images, which can be a hardware in the Internet, such as a network card, or a collection of configuration parameters for certain hardware and software. Because the SNMP protocol is simple and reliable, it has been welcomed by many vendors and has become the most extensive network management protocol.

The SNMP protocol consists of two major components: SNMP management station and SNMP agent. SNMP Management station is a central node, responsible for collecting and maintaining the information of various SNMP elements, and processing the information, and finally feedback to the network administrator, and the SNMP agent is running on each managed network node, is responsible for the statistics of the node's information, and is responsible for interacting with the SNMP management station, Receive and execute commands from the management station to upload various local network information.

There is a loose coupling between the SNMP management station and the SNMP agent. The communication between them is done through the UDP protocol. In general, the SNMP management station sends various commands to the SNMP agent through the UDP protocol, and when the SNMP agent receives the command, it returns the parameters required by the SNMP management station. However, when the SNMP agent detects a network element anomaly, it can also proactively send a message to the SNMP management station to advertise the current exception condition.

The SNMP protocol was released in 1988. To the present altogether experienced v1,v2,v3 three editions. of which V1 has been abandoned, and v2c although not able to become a formal standard, but has been accepted by many manufacturers, V3 is currently the official Internet standards. Compared with V1, V2,v3 is more adaptable to large-scale network management, and has a great improvement in security.

Second, snmp4j detailed introduction

2.1 Important classes and interfaces

SNMP class: This class is the most core class in snmp4j. Responsible for receiving and sending SNMP messages.

PDU classes and SCOPEDPDU classes: This class is an abstraction of the SNMP message unit, where the PDU class applies to SNMPV1 and snmpv2c. The Scopedpdu class inherits from the PDU class and is suitable for SNMPv3.

The target interface and the Usertarget class: address information that corresponds to the SNMP agent, including the IP address and port number (161). Where the target interface is suitable for SNMPV1 and snmpv2c. The Usertarget class implements the target interface for SNMPv3.

Transportmapping interface: This interface represents the transport layer protocol used by snmp4j. This is also a great feature of snmp4j.

As per the RfC, SNMP is only using UDP as the Transport layer protocol. The SNMP4J supports both the management side and the proxy side using UDP or TCP for transmission. The interface has two sub-interfaces.

2.2 Two kinds of message sending modes

The SNMP4J supports two message-Sending modes: Synchronous send mode and asynchronous send mode. Where synchronous send mode is also known as blocking mode. When a message is sent by the management side, the thread is blocked until the response is received or the time expires. The synchronous send mode programming is simple, but not for sending broadcast messages. Asynchronous send mode is also known as non-blocking mode. When the program sends a message, the thread will continue to execute, and when the message is received, the program will handle the message accordingly. To implement an asynchronous send pattern, you instantiate an object of a class that implements the Responselistener interface. There is a function named Onresponse in the Responselistener interface. This is a callback function that is called automatically when the program receives a response. This function completes the processing of the response.

2.3 Overall steps to implement the management end

This section explains the general process of using SNMP4J to write SNMP management side, readers will have a macroscopic understanding of snmp4j after reading. In the appendix, the author gives a sample program to develop a management station with SNMP4J, if there is any further need, please refer to the Appendix.

2.3.1 Initialization

Explicit SNMP protocol used in the transport layer generally, we use the UDP protocol as the SNMP Transport layer protocol, so we need to instantiate a Defaultudptransportmapping interface object, instantiate an SNMP object in this process, We need to instantiate the object of the Defaultudptransportmapping interface in 1 as a parameter, and wear the SNMP class constructor. In addition, if the implementation of the SNMPV3 protocol, we also need to set security mechanisms, add security users and so on; listen for SNMP messages here, we can call the Listen method of the interface object of the defaultudptransportmapping that we just instantiated. Allow the program to listen for SNMP messages;

2.3.2 Construct send target

If you are implementing a SNMPV3 program, you need to instantiate a Usertarget object, and if you implement SNMPV2C or SNMPV1, you need to instantiate a Communitytarget object. After that, we also need to make some settings for the instantiated objects. If the object is Communitytarget, you need to set the version, retransmission time, and wait delay. If it is a Usertarget object, we need not only to set the version, Retransmit time, wait for the delay, but also set the security level and security name.

2.3.3 Structure Send Message

If you are sending a SNMPV3 message, we need to instantiate an object of the Scopedpdu class, otherwise we need to instantiate an object of a PDU class. After that, we also need to generate an OID object that contains the ID of the SNMP object we need to get in the MIB library. Then we need to bind the OID to the previously generated PDU object, or the Scopedpdu object, and set the PDU's message type (one of five types of SNMP messages).

2.3.4 Construct Response Listener object (asynchronous mode)

When using asynchronous mode, we need to instantiate an object that implements Responselistener as the listener for the response message. In constructing the object, we need to rewrite Responselistener's Onresponse function, which is a callback function that handles some of the actions that the program receives after it responds.

2.3.5 Sending messages

Once all of the above operations have been set up, you can send a message. Both synchronous mode and asynchronous mode send a message called by the function name is send, but the two functions require a different parameter. The parameters of the synchronous mode are only the target objects and the message objects constructed in 4.3.2 and 4.3.3, and the asynchronous pattern also requires the listener objects constructed in the 4.3.4. Synchronous mode waits for the response to arrive after the message is sent, and returns a Responseevent object that contains the corresponding information for the response. The asynchronous mode continues execution after the message is sent, and the onresponse function of the listener is called when the response message is received. The statement in this function is our handling of the response

Third, the routine

Import; Import org.snmp4j.*;

Import org.snmp4j.event.ResponseEvent;

Import Org.snmp4j.event.ResponseListener;




Import org.snmp4j.smi.Address;

Import org.snmp4j.smi.GenericAddress;

Import Org.snmp4j.smi.OID;

Import org.snmp4j.smi.OctetString;

Import org.snmp4j.smi.UdpAddress;

Import org.snmp4j.smi.VariableBinding;

Import org.snmp4j.transport.DefaultUdpTransportMapping;

public class Snmp_manager {


Private String Version=null;



* @param version


Public Snmp_manager (String version) {

try {


transportmapping transport = new defaultudptransportmapping ();

SNMP = new SNMP (transport);

if (Version.equals ("3"))


Setting Safe Mode

USM USM = New USM (Securityprotocols.getinstance (), New OctetString (Mpv3.createlocalengineid ()), 0);

Securitymodels.getinstance (). Addsecuritymodel (USM);


Start listening for messages

Transport.listen ();

} catch (IOException e) {

TODO auto-generated Catch block

E.printstacktrace ();





* @param if SYN is a synchronous mode

* @param whether the bro is broadcast

* @param PDU to send the message

* @param addr Target Address

* @throws IOException


public void SendMessage (Boolean syn, Final boolean bro, PDU PDU,

String addr) throws IOException {

Generate Destination Address Object

Address targetAddress = genericaddress.parse (addr);

Target Target=null;

if (Version.equals ("3"))


Add user

SNMP.GETUSM (). AddUser (

New OctetString ("Md5des"),

New Usmuser (New OctetString ("Md5des"),,

New OctetString ("Md5desuserauthpassword"),, New OctetString ("Md5desuserprivpassword"));

target = new Usertarget ();

Setting the security level

((usertarget) target). Setsecuritylevel (Securitylevel.auth_priv);

((usertarget) target). Setsecurityname (New octetstring ("Md5des"));

Target.setversion (Snmpconstants.version3);


Target=new Communitytarget ();

if (Version.equals ("1"))


Target.setversion (Snmpconstants.version1);

((communitytarget) target). Setcommunity (New octetstring ("public"));


Target.setversion (SNMPCONSTANTS.VERSION2C);

((communitytarget) target). Setcommunity (New octetstring ("public"));



Target object Related Settings

Target.setaddress (targetAddress);

Target.setretries (5);

Target.settimeout (1000);

if (Syn.equals (true)) {

Send messages and accept responses

Responseevent response = Snmp.send (PDU, target);

Handling Responses

SYSTEM.OUT.PRINTLN ("Synchronize message from" + response.getpeeraddress () + "/nrequest:"

+ response.getrequest () + "/nresponse:"

+ Response.getresponse ());

} else {

Setting Listener Objects

Responselistener listener = new Responselistener () {


public void Onresponse (Responseevent event) {

TODO auto-generated Method Stub

if (Bro.equals (false)) {

((SNMP) Event.getsource ()). Cancel (Event.getrequest (),



Handling Responses

PDU request = Event.getrequest ();

PDU response = Event.getresponse ();

SYSTEM.OUT.PRINTLN ("Asynchronise Message from"

+ event.getpeeraddress () + "/nrequest:" + Request

+ "/nresponse:" + response);



Send Message

Snmp.send (PDU, target, NULL, listener);



public static void Main (string[] args) {

Snmp_manager manager = new Snmp_manager ("2c");

Structuring messages

PDU PDU = new PDU (); PDU PDU = new SCOPEDPDU ();

Set the object ID to get

OID oids=new oid ("");

Pdu.add (New variablebinding (OIDs));

Set the message type

Pdu.settype (PDU. GETNEXT);

((SCOPEDPDU) PDU). Setcontextname (New octetstring ("Priv"));

try {

Send Message The last one is the destination address you want to send

Manager.sendmessage (False, True, PDU, "udp:");

} catch (IOException e) {

TODO auto-generated Catch block

E.printstacktrace ();




Introduction to SNMP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.