window snmp服务开启及测试
转自:https://blog.csdn.net/qq_33314107/article/details/80031446
一 安装
二 开启服务
Linux下安装与配置snmp服务
https://blog.csdn.net/macrothunder/article/details/50394566
三 测试
3.1 MIB 浏览器测试
iReasoning MIB Browser下载地址:http://ireasoning.com/download.shtml
3.2 程序测试
maven导入依赖:
<!--snmp-->
<dependency>
<groupId>org.snmp4j</groupId>
<artifactId>snmp4j</artifactId>
<version>2.5.0</version>
</dependency>
oid列表:
参照:https://blog.csdn.net/qq_28657577/article/details/82834442
public enum IndicatorOIDName { //网络接口信息描述 网络接口类型 接口发送和接收的最大IP数据报[BYTE] 接口当前带宽[bps] 接口的物理地址 接口当前操作状态[up|down] TEST("test", "测试", ".1.3.6.1.2.1.2.2.1.2," + ".1.3.6.1.2.1.2.2.1.3," + ".1.3.6.1.2.1.2.2.1.4," + ".1.3.6.1.2.1.2.2.1.5" + ".1.3.6.1.2.1.2.2.1.6," + ".1.3.6.1.2.1.2.2.1.8"),
CPURATE("cpuRate", "总cpu占用率", "1.3.6.1.2.1.25.3.3.1.2"), //y DISK_USE_RATE("diskUseRate", "磁盘使用率", "1.3.6.1.2.1.25.2.3.1.5,1.3.6.1.2.1.25.2.3.1.6"), //y ,前-总,后-已使用 //1:Physical memory ;3:Virtual memory; 6:Memory buffers;7:Cached memory PHY_MEM_USE_PERCENT("phyMemUsePercent", "物理内存利用率","1.3.6.1.2.1.25.2.3.1.3," + "1.3.6.1.2.1.25.2.3.1.5," + "1.3.6.1.2.1.25.2.3.1.6"), UP_STREAM("upStream", "上行流量监控(专用)", ".1.3.6.1.2.1.2.2.1.16"), DOWN_STREAM("downStream", "下行流量监控(专用)", ".1.3.6.1.2.1.2.2.1.10"), STREAM_SUM("streamSum", "上下行流量总和", ".1.3.6.1.2.1.2.2.1.10,.1.3.6.1.2.1.2.2.1.16"), //网络接口信息描述 网络接口类型 NETWORK_INTERFACE("networkInterface", "网络接口", ".1.3.6.1.2.1.2.2.1.2,.1.3.6.1.2.1.2.2.1.3");
private String name; private String mes; private String oid;
IndicatorOIDName(String name, String mes, String oid) { this.name = name; this.mes = mes; this.oid = oid; }
public static IndicatorOIDName getByName(String name) { for (IndicatorOIDName IndicatorOIDName : IndicatorOIDName.values()) { if (name.startsWith(IndicatorOIDName.getName())) { return IndicatorOIDName; } } throw new RuntimeException("不支持类型 IndicatorOIDName.name=" + name); }
public String getName() { return name; }
public String getMes() { return mes; }
public String getOid() { return oid; } }
|
Snmp4Uitl .java 测试类
public class Snmp4Uitl { private static final Logger log = LoggerFactory.getLogger(Snmp4Uitl.class);
public static void main(String[] args) throws IOException { //ord aPublic 1.3.6.1.2.1.25.3.3.1.2 10.253.46.140 10.243.141.114 List<ChildIndicatorVo> indicatorVos= getChildIndicatorList("10.243.141.114",IndicatorOIDName.NETWORK_INTERFACE); System.out.println(JSON.toJSONString(indicatorVos)); } /*** * @Date: @Auth:xinsen.liao @Desc(V1.06): 获取磁盘内存指标 */ public static List<ChildIndicatorVo> getChildIndicatorList(String ip,IndicatorOIDName indicatorOIDName) throws IOException {
List<ChildIndicatorVo> indicatorVoList=new ArrayList<>(); List<TableEvent> aPublic = getTable(ip, indicatorOIDName.getOid().split(","), "public"); // System.out.println(JSON.toJSONString(aPublic)); for (TableEvent event : aPublic) { OID oid = event.getIndex(); Integer index = oid.get(0); VariableBinding[] valueBinds = event.getColumns(); Variable value = event.getColumns()[0].getVariable(); if(value!=null){ ChildIndicatorVo vo=new ChildIndicatorVo(); switch (indicatorOIDName.getName()){ case "cpuRate": case "upStream": case "downStream": case "streamSum": vo.setIndex(index); vo.setOid(indicatorOIDName.getOid()); indicatorVoList.add(vo); break; case "networkInterface": case "diskUseRate": if(value.toString().toUpperCase().indexOf("MEMORY")>=0){ continue; } if(value!=null) vo.setField(value.toString()); vo.setIndex(index); vo.setOid(indicatorOIDName.getOid()); indicatorVoList.add(vo); break; } } } return indicatorVoList; } public static PDU send(String ip, String oid, String community) throws IOException { TransportMapping<UdpAddress> transportMapping = new DefaultUdpTransportMapping(); Snmp snmp = new Snmp(transportMapping); try { snmp.listen(); ResponseEvent response = null; PDU pdu = new PDU(); pdu.add(new VariableBinding(new OID(oid))); pdu.setType(PDU.GET); String address = ip + "/" + 161; Address targetAddress = new UdpAddress(address); CommunityTarget target = new CommunityTarget(); target.setCommunity(new OctetString(community)); // 改字符串是我们在上面配置的 target.setAddress(targetAddress); target.setRetries(2); target.setTimeout(3000); target.setVersion(SnmpConstants.version2c); response = snmp.get(pdu, target); PDU result = response.getResponse(); if (result == null) { throw new RuntimeException("连接失败" + address + " community:" + community); } return result; } catch (Exception ex) { throw ex; } finally { snmp.close(); } }
public static List<TableEvent> getTable(String ip, String[] oids, String community) throws IOException { TransportMapping transport = null; Snmp snmp = null; CommunityTarget target; try { transport = new DefaultUdpTransportMapping(); snmp = new Snmp(transport);//创建snmp snmp.listen();//监听消息 target = new CommunityTarget(); target.setCommunity(new OctetString(community)); target.setRetries(2); target.setAddress(GenericAddress.parse("udp:" + ip + "/161")); target.setTimeout(8000); target.setVersion(SnmpConstants.version2c); TableUtils tableUtils = new TableUtils(snmp, new PDUFactory() { @Override public PDU createPDU(Target arg0) { PDU request = new PDU(); request.setType(PDU.GET); return request; }
@Override public PDU createPDU(MessageProcessingModel messageProcessingModel) { PDU request = new PDU(); request.setType(PDU.GET); return request; } }); OID[] columns = new OID[oids.length]; for (int i = 0; i < oids.length; i++) columns[i] = new OID(oids[i]); List<TableEvent> list = tableUtils.getTable(target, columns, null, null); return list; } catch (Exception e) { throw e; } finally { try { if (transport != null) transport.close(); if (snmp != null) snmp.close(); } catch (IOException e) { log.error("Snmp4Uitl[]getTable[]error", e); } } } }
|