zoukankan      html  css  js  c++  java
  • java-snmp4j之snmp

    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);//创建snmp
    snmp.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>

     

  • 相关阅读:
    mysql的小练习
    实用IMX6开发板来袭, 方便开发板方便你
    又到开学季 学习神器走一波 物联网开发板
    如何修改开发板主频--迅为iMX6UL开发板
    迅为4412开发板实战之智能网关项目
    iTOP-iMX6开发板Android系统下LVDS和HDMI双屏异显方法
    恩智浦iMX6Q核心板/飞思卡尔Cortex-A9高稳定性低功耗开发板
    嵌入式ARM开发板学习方法步骤
    迅为iMX6UL Cortex-A7架构单核ARM开发板接口介绍-支持定制
    三星系列NXP系列核心板设计研发-迅为嵌入式ARM方案提供商
  • 原文地址:https://www.cnblogs.com/maocai2018/p/9933454.html
Copyright © 2011-2022 走看看