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>

     

  • 相关阅读:
    IPv6时代,中小企业该如何布局?
    并发场景下的幂等问题——分布式锁详解
    阿里巴巴服务网格技术三位一体战略背后的思考与实践
    阿里云 Serverless 助力企业全面拥抱云原生
    阿里云徐立:面向容器和 Serverless Computing 的存储创新
    如何使用 Kubernetes 监测定位慢调用
    双11特刊 | 全面云原生化,数据库实例独共享混部 最高降低30%成本
    VS2010显示行号 po
    Webservice更新时出错。下载”。。。”时出错。请求失败,错误信息为: po
    google地图 无法定位 请在系统设置中启用“我的位置”源 po
  • 原文地址:https://www.cnblogs.com/maocai2018/p/9933454.html
Copyright © 2011-2022 走看看