• SNMP简单网络管理协议


      简单网络管理协议(SNMP-Simple Network Management Protocol)是一个与网络设备交互的简单方法。该规范是由IETF在1990年五月发布的RFC 1157中定义的,是各种管理应用程序用来控制设备的方法

      一个网络设备以守护进程的方式运行SNMP代理,该守护进程能够响应来自网络的各种请求信息。该SNMP代理提供大量的对象标识符(OID-Object Identifiers)。一个OID是一个唯一的键值对。该代理存放这些值并让它们可用。一个SNMP管理器(客户)可以向代理查询键值对中的特定信息。SNMP中有一个基本的认证框架,能够让管理员发送公共名来对OID读取或写入的认证。绝大多数的设备使用不安全的公共名 "public" 。 SNMP协议通过UDP端口161和162进行通信的。

      SNMP的常用版本有三个:SNMPv1、SNMPv2、SNMPv3。

      SNMP简介:https://blog.csdn.net/u011857683/article/details/79915260

      几款关于SNMP的调试工具:

        ManageEngine MibBrowser(适用与有现成的.mib库文件)

        Paessler SNMP Tester(测试OID)

        FreeSnmp(使用Walk探测OID)

        Snmputil工具

    1 OID信息  

    https://blog.51cto.com/lanfenggo/1840427

    oid列表

    OID(对象标识符),是SNMP代理提供的具有唯一标识的键值。MIB(管理信息基)提供数字化OID到可读文本的映射。

    根据oid获取到的内存信息可能是英文,也可能是ascii码,还可能是其他编码,这时候不能根据单纯通过“1.3.6.1.2.1.25.2.3.1.3”取到的内存名称来判断,可参考以下信息:

    “1.3.6.1.2.1.25.2.1.3”<==>虚拟内存

    "1.3.6.1.2.1.25.2.1.2"<==>物理内存

    2 snmp 安装配置

    Windows10开启SNMP服务

    linux

    Snmputil工具

    SugarNMSTool 网络管理工具(SNMP工具、网络管理、网络分析等):https://www.jb51.net/softs/439487.html

    Snmputil工具:https://www.jb51.net/softs/563509.html

    snmpwalk工具使用

    4 java代码调用

    引入jar包

             <!--snmp-->
            <dependency>
                <groupId>org.snmp4j</groupId>
                <artifactId>snmp4j</artifactId>
                <version>2.5.0</version>
            </dependency>    

    示例java代码

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.snmp4j.*;
    import org.snmp4j.event.ResponseEvent;
    import org.snmp4j.mp.MessageProcessingModel;
    import org.snmp4j.mp.SnmpConstants;
    import org.snmp4j.smi.*;
    import org.snmp4j.transport.DefaultUdpTransportMapping;
    import org.snmp4j.util.PDUFactory;
    import org.snmp4j.util.TableEvent;
    import org.snmp4j.util.TableUtils;
    
    import java.io.IOException;
    import java.util.List;
    
    public class Snmp4Uitl {
        private static final Logger log = LoggerFactory.getLogger(Snmp4Uitl.class);
        private static final String IP ="10.243.141.114";
        public static void main(String[] args) throws IOException {
    
            List<TableEvent> aPublic = getTable(IP, new String[]{"1.3.6.1.2.1.25.2.3.1.2"}, "public");
            long sum = 0;
    
            for (TableEvent event : aPublic) {
                System.out.println(event.getColumns()[0].getVariable().toString());
    //            sum +=event.getColumns()[0].getVariable().toLong();
            }
    
          /*  PDU pdu =  send(IP ,"1.3.6.1.2.1.25.2.3.1.5", "public" );
            System.out.println(pdu.get(0).getVariable().toString());*/
    //        log.info(sum /1024L +"");
        }
    
        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);
                }
            }
        }
    }

    如何使用snmp计算接口带宽

  • 相关阅读:
    Rsync
    SpringCloud-Eureka
    mysql-主从复制
    java中延迟任务的处理方式
    mysql-分组
    rpm相关
    shell中#*,##*,#*,##*,% *,%% *的含义及用法
    cygwin的用途
    cocos2d-lua-win
    ant打包遇到的问题
  • 原文地址:https://www.cnblogs.com/personsiglewine/p/12467097.html
Copyright © 2020-2023  润新知