The use of SNMP4J and project construction

SNMP protocol

The basic idea of ​​SNMP is to define a unified interface and protocol for different types of equipment, equipment produced by different manufacturers, and different types of equipment, so that administrators can use a unified appearance to manage these network equipment that needs to be managed . Through the network, administrators can manage devices located in different physical spaces, thereby greatly improving the efficiency of network management and simplifying the work of network administrators.
Simple Network Management Protocol (SNMP) is an application layer protocol of the TCP/IP protocol suite. The SNMP protocol is widely used and easy to use. A complete set of SNMP system mainly includes management information base (MIB), management information structure (SMI) and SNMP message protocol.
SNMP specifies five protocol data units:
1. get-request operation: extract one or more parameter values ​​from the agent process.
2. get-next-request operation: extract the next parameter value following the current parameter value from the agent process.
3.set-request operation: set one or more parameter values ​​of the agent process.
4. Get-response operation: return one or more parameter values. This operation is issued by the agent process, and it is the response operation of the previous three operations.
5. Trap operation: A message sent by the agent process to notify the management process that something has happened.
snmpwalk tool operation parameter
snmpwalk [version] [permission] [IP] [OID] [parameter (Integer or string)]
snmpwalk operation example:

snmpwalk -v2c public 192.168.0.107 .1.3.6.1.4.1.532.2.1.2.1.5. 11

snmp object structure
insert image description here

snmp4J

snmp4J is an open source project of Java for the snmp protocol, currently using the snmp4j dependency corresponding to Java8:

  <dependency>
      <groupId>org.snmp4j</groupId>
     <artifactId>snmp4j</artifactId>
     <version>2.8.3</version>
    </dependency>

snmp4J is mainly divided into several large object structures

  1. The snmp object can be regarded as an agent, and the ResponseEvent response=snmp.send(pdu, target) method is used to transmit messages and return data;
  2. target object , target parameter encapsulation, setting target parameter address, protocol version, timeout, retransmission, etc.;
  3. pdu object , pdu.add(VariableBinding) adds the data to be sent, such as oid and integer, string parameters;
  4. ResponseEvent object , return parameter encapsulation;
  5. VariableBinding object data binding, and then added to pdu;
  6. TransportMapping snmp object initialization parameters, the default is DefaultUdpTransportMapping, new snmp(TransportMapping);

MIB BROWSER

insert image description here

snmp4J uses

case:

project Value
equipment Remote intelligent pdu, interface document, MIB library
debugging tool MIB BROWSER
protocol SNMP protocol
SNMP framework SNMP4J
project framework springboot

1. Connect the computer to the network of the device, access the internal webpage of the device, use MIB BROWSER to import the MIB library of the device, and debug various operations of get, set, and getnext.
2. Create a new springboot project, import SNMP4J dependencies, create SNMPUtil tool classes, create scheduled tasks, and get data once a minute

package com.example.demo.task;


import com.example.demo.config.ParamsConfig;
import com.example.demo.util.SnmpUtil;
import lombok.extern.slf4j.Slf4j;
import org.snmp4j.PDU;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Vector;

@Component
@Slf4j
public class PduTask {
    
    
  @Autowired
  private ParamsConfig paramsConfig;//配置参数
  @Scheduled(fixedRate = 60000)//每分钟执行一次    
  @Async
  public void getPduInfo() {
    
    
      log.info("获取pdu数据:开始");
      int ver2c = SnmpConstants.version2c;//snmp2协议
      SnmpUtil snmpUtil=new SnmpUtil(ver2c);
      PDU pdu = new PDU();
      //PDU pdu = new ScopedPDU();
      // 设置要获取的对象ID,这个OID代表远程计算机的名称
      OID oid1 = new OID(paramsConfig.temp_oid);//温度
      OID oid2 = new OID(paramsConfig.humi_oid);//湿度
      OID oid3 = new OID(paramsConfig.smoke_oid);//烟感
      OID oid4 = new OID(paramsConfig.voltage_oid);//电压
      OID oid5 = new OID(paramsConfig.current_oid);//电流
      pdu.add(new VariableBinding(oid1));
      pdu.add(new VariableBinding(oid2));
      pdu.add(new VariableBinding(oid3));
      pdu.add(new VariableBinding(oid4));
      pdu.add(new VariableBinding(oid5));
      // 设置报文类型
      pdu.setType(PDU.GET);
      try {
    
    
          String addr=paramsConfig.transfer_protocol+":"+paramsConfig.snmp_ip+"/"+paramsConfig.snmp_port;
        
          ResponseEvent response=snmpUtil.sendMessage(false, true, pdu, addr,new OctetString("public"));//192.168.1.233 WinServer2008服务器
          Vector<? extends VariableBinding> list =response.getResponse().getVariableBindings();
          System.out.println("Synchronize(同步) message(消息) from(来自) "
                  + response.getPeerAddress() + "\r\n" + "request(发送的请求):"
                  + response.getRequest() + "\r\n" + "response(返回的响应):"
                  + list.get(0).getVariable().toString().substring(0,4)+"\r\n"+
                  list.get(1).getVariable().toString().substring(0,4)+"\r\n"+
                  list.get(2).getVariable()+"\r\n"+
                  list.get(3).getVariable()+"\r\n"+
                  list.get(4).getVariable());

          log.info("发送数据--------------------------------------------");
      } catch (IOException e) {
    
    
          e.printStackTrace();
      }
      log.info("定时获取探测站Info:结束");
  }
}

return data

Synchronize(同步) message(消息) from(来自) 192.168.0.107/161
request(发送的请求):GET[requestID=292621635, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.4.1.532.2.2.1.1.4.1 = Null; 1.3.6.1.4.1.532.2.2.1.1.5.1 = Null; 1.3.6.1.4.1.532.2.4.1.1.5.4 = Null; 1.3.6.1.4.1.532.2.1.1.1.3.1 = Null; 1.3.6.1.4.1.532.2.1.1.1.4.1 = Null]]
response(返回的响应):25.2
63.1
0
223.1
0.0

So far the use of snmp4j is complete

Guess you like

Origin blog.csdn.net/m0_38110240/article/details/109176579