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>

     

  • 相关阅读:
    Atitit sql计划任务与查询优化器统计信息模块
    Atitit  数据库的事件机制触发器与定时任务attilax总结
    Atitit 图像处理知识点体系知识图谱 路线图attilax总结 v4 qcb.xlsx
    Atitit 图像处理 深刻理解梯度原理计算.v1 qc8
    Atiti 数据库系统原理 与数据库方面的书籍 attilax总结 v3 .docx
    Atitit Mysql查询优化器 存取类型 范围存取类型 索引存取类型 AND or的分析
    Atitit View事件分发机制
    Atitit 基于sql编程语言的oo面向对象大规模应用解决方案attilax总结
    Atitti 存储引擎支持的国内点与特性attilax总结
    Atitit 深入理解软件的本质 attilax总结 软件三原则"三次原则"是DRY原则和YAGNI原则的折
  • 原文地址:https://www.cnblogs.com/maocai2018/p/9933454.html
Copyright © 2011-2022 走看看