java-snmp4j之snmp 阿新 • • 發佈:2018-11-09 package com.Snmp4jFirstDemo; import org.snmp4j.*;import org.snmp4j.mp.SnmpConstants;import org.snmp4j.smi.GenericAddress;import org.snmp4j.smi.OID;import org.snmp4j.smi.OctetString;import org.snmp4j.smi.VariableBinding;import org.snmp4j.transport.DefaultUdpTransportMapping;import org.snmp4j.util.DefaultPDUFactory;import org.snmp4j.util.TableEvent;import org.snmp4j.util.TableUtils;import java.io.IOException;import java.util.List;public class SnmpDemo2 {public static void main(String[] args) {creatSnmp();}public static void creatSnmp() {TransportMapping transport = null;Snmp snmp = null;CommunityTarget target = null;try {transport = new DefaultUdpTransportMapping();snmp = new Snmp(transport);//建立snmpsnmp.listen();//監聽訊息target = new CommunityTarget();target.setCommunity(new OctetString("public"));//社群名稱target.setRetries(3);//連線次數target.setAddress(GenericAddress.parse("udp:172.18.12.96/161"));//監控的主機target.setTimeout(8000);//target.setVersion(SnmpConstants.version2c);String memory = collectMemory(snmp, target);System.out.println("記憶體使用率為:" + memory);String cpu = collectCPU(snmp, target);System.out.println("CPU利用率為:" + cpu);} catch (IOException e) {e.printStackTrace();} finally {try {if (transport != null)transport.close();if (snmp != null)snmp.close();} catch (IOException e) {e.printStackTrace();}}}//獲取記憶體相關資訊public static String collectMemory(Snmp snmp, CommunityTarget target) {String memory = null;String[] oids = {"1.3.6.1.2.1.25.2.3.1.2", //type 儲存單元型別"1.3.6.1.2.1.25.2.3.1.3", //descr"1.3.6.1.2.1.25.2.3.1.4", //unit 儲存單元大小"1.3.6.1.2.1.25.2.3.1.5", //size 總儲存單元數"1.3.6.1.2.1.25.2.3.1.6"}; //used 使用儲存單元數;String PHYSICAL_MEMORY_OID = "1.3.6.1.2.1.25.2.1.2";//物理儲存try {TableUtils tableUtils = new TableUtils(snmp, new DefaultPDUFactory(PDU.GETBULK));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);for (TableEvent event : list) {VariableBinding[] values = event.getColumns();if (values == null) continue;int unit = Integer.parseInt(values[2].getVariable().toString());//unit 儲存單元大小int totalSize = Integer.parseInt(values[3].getVariable().toString());//size 總儲存單元數int usedSize = Integer.parseInt(values[4].getVariable().toString());//used 使用儲存單元數String oid = values[0].getVariable().toString();if (PHYSICAL_MEMORY_OID.equals(oid)) {memory = (long) usedSize * 100 / totalSize + "%";}}} catch (Exception e) {e.printStackTrace();}return memory;}//獲取cpu使用率public static String collectCPU(Snmp snmp, CommunityTarget target) {String cpu = null;String[] oids = {"1.3.6.1.2.1.25.3.3.1.2"};try {TableUtils tableUtils = new TableUtils(snmp, new DefaultPDUFactory(PDU.GETBULK));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);int percentage = 0;for (TableEvent event : list) {VariableBinding[] values = event.getColumns();if (values != null)percentage += Integer.parseInt(values[0].getVariable().toString());}cpu = percentage / list.size() + "%";} catch (Exception e) {e.printStackTrace();}return cpu;}}============== <dependency><groupId>org.snmp4j</groupId><artifactId>snmp4j</artifactId><version>2.6.2</version></dependency>