概述
snmpGet功能
snmp工具类
首先用maven引入jar包
注意如果使用以下代码,这个地方snmp4j的版本不能过高,否则会报错
org/snmp4j/CommandResponder has been compiled by a more recent version of the Java Runtime (class file version 53.0), this version of the Java Runtime only recognizes class file versions up to 52.0
<dependency>
<groupId>org.snmp4j</groupId>
<artifactId>snmp4j</artifactId>
<version>2.8.2</version>
</dependency>
package jasonwang.myeureka8082;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.snmp4j.*;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.MPv1;
import org.snmp4j.mp.MPv2c;
import org.snmp4j.mp.MPv3;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.*;
import org.snmp4j.smi.*;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import java.io.IOException;
/**
author: Administrator
createtime: 2020/6/23 17:56
*/
public class SnmpUtil {
private static Logger log = LoggerFactory.getLogger(SnmpUtil.class);
public static Snmp snmp = null;
private static String community = "public";
private static String ipAddress = "udp:10.10.112.105/";
/**
* @description 初始化snmp
* @author YuanFY
* @date 2017年12月16日 上午10:28:01
* @version 1.0
* @throws IOException
*/
public static void initSnmp() throws IOException {
//1、初始化多线程消息转发类
MessageDispatcher messageDispatcher = new MessageDispatcherImpl();
//其中要增加三种处理模型。如果snmp初始化使用的是Snmp(TransportMapping<? extends Address> transportMapping) ,就不需要增加
messageDispatcher.addMessageProcessingModel(new MPv1());
messageDispatcher.addMessageProcessingModel(new MPv2c());
//当要支持snmpV3版本时,需要配置user
OctetString localEngineID = new OctetString(MPv3.createLocalEngineID());
USM usm = new USM(SecurityProtocols.getInstance().addDefaultProtocols(), localEngineID, 0);
UsmUser user = new UsmUser(new OctetString("SNMPV3"), AuthSHA.ID, new OctetString("authPassword"),
PrivAES128.ID, new OctetString("privPassword"));
usm.addUser(user.getSecurityName(), user);
messageDispatcher.addMessageProcessingModel(new MPv3(usm));
//2、创建transportMapping
UdpAddress updAddr = (UdpAddress) GenericAddress.parse("udp:10.10.112.177/161");
TransportMapping<?> transportMapping = new DefaultUdpTransportMapping(updAddr);
//3、正式创建snmp
snmp = new Snmp(messageDispatcher, transportMapping);
//开启监听
snmp.listen();
}
private static Target createTarget(int version, int port) {
Target target = null;
if (!(version == SnmpConstants.version3 || version == SnmpConstants.version2c || version == SnmpConstants.version1)) {
log.error("参数version异常");
return target;
}
if (version == SnmpConstants.version3) {
target = new UserTarget();
//snmpV3需要设置安全级别和安全名称,其中安全名称是创建snmp指定user设置的new OctetString("SNMPV3")
target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
target.setSecurityName(new OctetString("SNMPV3"));
} else {
//snmpV1和snmpV2需要指定团体名名称
target = new CommunityTarget();
((CommunityTarget)target).setCommunity(new OctetString(community));
if (version == SnmpConstants.version2c) {
target.setSecurityModel(SecurityModel.SECURITY_MODEL_SNMPv2c);
}
}
target.setVersion(version);
//必须指定,没有设置就会报错。
target.setAddress(GenericAddress.parse(ipAddress+port));
target.setRetries(5);
target.setTimeout(3000);
return target;
}
private static PDU createPDU(int version, int type, String oid){
PDU pdu = null;
if (version == SnmpConstants.version3) {
pdu = new ScopedPDU();
}else {
pdu = new PDUv1();
}
pdu.setType(type);
//可以添加多个变量oid
pdu.add(new VariableBinding(new OID(oid)));
return pdu;
}
public static void snmpGet(String oid){
try {
//1、初始化snmp,并开启监听
initSnmp();
//2、创建目标对象
Target target = createTarget(SnmpConstants.version2c, SnmpConstants.DEFAULT_COMMAND_RESPONDER_PORT);
//3、创建报文
PDU pdu = createPDU(SnmpConstants.version2c, PDU.GET, oid);
System.out.println("-------> 发送PDU <-------");
//4、发送报文,并获取返回结果
ResponseEvent responseEvent = snmp.send(pdu, target);
PDU response = responseEvent.getResponse();
System.out.println("返回结果:" + response);
}
catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
snmpGet("1.3.6.1.2.1.1.1.0");
}
}
监控接收服务器发过来的故障然后提示个用户,这就需要用到snmptrap了。接下来我们直接看案例:
package jasonwang.myeureka8082;
import org.snmp4j.PDU;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.snmp4j.CommandResponder;
import org.snmp4j.CommandResponderEvent;
import org.snmp4j.MessageDispatcher;
import org.snmp4j.MessageDispatcherImpl;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.mp.MPv1;
import org.snmp4j.mp.MPv2c;
import org.snmp4j.mp.MPv3;
import org.snmp4j.security.AuthSHA;
import org.snmp4j.security.PrivAES128;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.security.USM;
import org.snmp4j.security.UsmUser;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.MultiThreadedMessageDispatcher;
import org.snmp4j.util.ThreadPool;
public class SnmpTrapHandler implements CommandResponder{
private static Logger log = LoggerFactory.getLogger(SnmpTrapHandler.class);
private static int threadNum = 200;
private static String ipAddress = "udp:10.10.112.177/162";
private Snmp snmp = null;
public void init(){
//1、初始化多线程消息转发类
ThreadPool threadPool = ThreadPool.create("SnmpTrap", threadNum);
MessageDispatcher messageDispatcher = new MultiThreadedMessageDispatcher(threadPool, new MessageDispatcherImpl());
//其中要增加三种处理模型。如果snmp初始化使用的是Snmp(TransportMapping<? extends Address> transportMapping) ,就不需要增加
messageDispatcher.addMessageProcessingModel(new MPv1());
messageDispatcher.addMessageProcessingModel(new MPv2c());
OctetString localEngineID = new OctetString(MPv3.createLocalEngineID());
USM usm = new USM(SecurityProtocols.getInstance().addDefaultProtocols(), localEngineID, 0);
UsmUser user = new UsmUser(new OctetString("SNMPV3"), AuthSHA.ID, new OctetString("authPassword"),
PrivAES128.ID, new OctetString("privPassword"));
usm.addUser(user.getSecurityName(), user);
messageDispatcher.addMessageProcessingModel(new MPv3(usm));
//2、创建transportMapping
TransportMapping<?> transportMapping = null;
try {
UdpAddress updAddr = (UdpAddress) GenericAddress.parse(System.getProperty("snmp4j.listenAddress", ipAddress));
transportMapping = new DefaultUdpTransportMapping(updAddr);
//3、正式创建snmp
snmp = new Snmp(messageDispatcher, transportMapping);
//开启监听
snmp.listen();
} catch (IOException e) {
log.error("初始化transportMapping失败:", e.getMessage());
e.printStackTrace();
}
}
public void start() {
init();
//一定要将当前对象添加至commandResponderListeners中
snmp.addCommandResponder(this);
System.out.println("开始监听trap信息:");
}
/**
* 处理信息方法
*/
@Override
public void processPdu(CommandResponderEvent event) {
String version = null ;
String community = null;
if (event.getPDU().getType() == PDU.V1TRAP) {
version = "v1";
community = new String(event.getSecurityName());
} else if (event.getPDU().getType() == PDU.TRAP){
if (event.getSecurityModel() == 2) {
version = "v2";
community = new String(event.getSecurityName());
}else {
version = "v3";
}
}
System.out.println("接收到的trap信息:[发送来源="+event.getPeerAddress()+",snmp版本="+version+",团体名="+community+", 携带的变量="+event.getPDU().getVariableBindings()+"]");
}
public static void main(String[] args) {
SnmpTrapHandler handler = new SnmpTrapHandler();
handler.start();
}
}
SNMPWalk功能测试
public static void snmpWalk(String oid) {
try {
//1、初始化snmp,并开启监听
initSnmp();
//2、创建目标对象
Target target = createTarget(SnmpConstants.version2c, SnmpConstants.DEFAULT_COMMAND_RESPONDER_PORT);
//3、创建报文
PDU pdu = createPDU(SnmpConstants.version2c, PDU.GETNEXT, oid);
System.out.println("-------> 发送PDU <-------");
//4、发送报文,并获取返回结果
boolean matched = true;
while (matched) {
ResponseEvent responseEvent = snmp.send(pdu, target);
if (responseEvent == null || responseEvent.getResponse() == null) {
break;
}
PDU response = responseEvent.getResponse();
String nextOid = null;
Vector<? extends VariableBinding> variableBindings = response.getVariableBindings();
for (int i = 0; i < variableBindings.size(); i++) {
VariableBinding variableBinding = variableBindings.elementAt(i);
Variable variable = variableBinding.getVariable();
nextOid = variableBinding.getOid().toDottedString();
//如果不是这个节点下的oid则终止遍历,否则会输出很多,直到整个遍历完。
if (!nextOid.startsWith(oid)) {
matched = false;
break;
}
//System.out.println(variable);
}
if (!matched) {
break;
}
pdu.clear();
pdu.add(new VariableBinding(new OID(nextOid)));
System.out.println("返回结果:" + response);
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
//snmpGet("1.3.6.1.2.1.1.1.0");
snmpWalk("1.3.6.1.2.1.25.3.3.1.2");//CPU的当前负载,N个核就有N个负载4
}
原文地址:https://www.cnblogs.com/yuanfy008/p/8046189.html
最后
以上就是感性蜻蜓为你收集整理的基于Java使用Snmp4j进行监控与采集的全部内容,希望文章能够帮你解决基于Java使用Snmp4j进行监控与采集所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复