• snmp


    win7下安装NET-SNMP  http://blog.csdn.net/yangyinbo/article/details/6029492

    @FixMethodOrder ( MethodSorters.JVM )

    public class AgentTest
    {
     
        private static final String OID_SYS_DESCR = "1.3.6.1.2.1.1.1.0";
     
        private static Logger       LOGGER        = LoggerFactory.getLogger( AgentTest.class );
     
        private static final String AGENT_ADDR    = "udp:127.0.0.1/16100";
     
        private static final String MANAGER_ADDR  = "udp:127.0.0.1/16200";
     
        private static UdpAddress   agentAddr     = (UdpAddress) GenericAddress.parse( AGENT_ADDR );
     
        private static UdpAddress   mgrAddr       = (UdpAddress) GenericAddress.parse( MANAGER_ADDR );
     
        private static SnmpAgent    agent;
     
        private static Snmp         mgrSession;
     
        @BeforeClass
        public static void setUpBeforeClass() throws Exception
        {
            agent = new SnmpAgent( AGENT_ADDR );
            agent.start();
     
            TransportMapping transportMapping = new DefaultUdpTransportMapping( mgrAddr );
            mgrSession = new Snmp( transportMapping );
            transportMapping.listen();
     
     
        }
     
        @AfterClass
        public static void tearDownAfterClass() throws Exception
        {
            agent.stop();
            mgrSession.close();
        }
     
        private Target createSNMPv2Target()
        {
            Target target = new CommunityTarget( agentAddr, new OctetString( "public" ) );
            target.setVersion( SnmpConstants.version2c );
            target.setAddress( agentAddr );
            return target;
        }
     
        private PDU createSNMPv2PDU_GET( String oid )
        {
            PDU req = new PDU();
            req.setType( PDU.GET );
            req.add( new VariableBinding( new OID( oid ) ) );
            return req;
        }
     
        private PDU createSNMPv2PDU_GETBULK( String oid, int maxRepetitions )
        {
            PDU req = new PDU();
            req.setType( PDU.GETBULK );
            req.setMaxRepetitions( maxRepetitions );
            req.setNonRepeaters( 0 );
            req.add( new VariableBinding( new OID( oid ) ) );
            return req;
        }
     
        private String getAsString( String oid ) throws Exception
        {
            ResponseEvent respEvent = mgrSession.send( createSNMPv2PDU_GET( oid ), createSNMPv2Target() );
            PDU resp = respEvent.getResponse();
            return resp.get( 0 ).getVariable().toString();
        }
     
        private String variableToString( Variable var ) throws UnsupportedEncodingException
        {
            if ( var instanceof OctetString )
            {
                return new String( ( (OctetString) var ).getValue() );
            }
            else
            {
                return var.toString();
            }
        }
     
        /**
         * BaseAgent注册了一些默认的MIB,我们这里发起一个1.3.6.1.2.1.1.1请求(sysDescr)测试其是否正常工作
         */
        @Test
        public void testDefaultMIB() throws Exception
        {
            //测试BaseAgent注册的默认MIB-II
            assertEquals( "SNMP4J-Agent - Windows 7 - amd64 - 6.1", getAsString( OID_SYS_DESCR ) );
        }
     
        /**
         * 创建一个标量并且注册到Agent
         */
        @Test
        public void testUserDefinedScalar() throws Exception
        {
            //解除注册默认的Snmpv2 MIB
            agent.unregisterManagedObject( agent.getSnmpv2MIB() );
            String sysDescr = "Ubuntu 14 - amd64";
            MOScalar mo = new MOScalar( //创建一个标量
                    new OID( OID_SYS_DESCR ), //OID
                    MOAccessImpl.ACCESS_READ_ONLY,//最高访问级别
                    new OctetString( sysDescr ) //值
                    );
            agent.registerManagedObject( mo );
            assertEquals( sysDescr, getAsString( OID_SYS_DESCR ) );
        }
     
        /**
         * 创建一个表格并且注册到Agent,该用例将打印:
         * 1.3.6.1.4.1.8808.8.1.1: 19
         * 1.3.6.1.4.1.8808.8.1.2: 20
         * 1.3.6.1.4.1.8808.8.2.1: 汪震
         * 1.3.6.1.4.1.8808.8.2.2: 汪静好
         * 1.3.6.1.4.1.8808.8.3.1: 198609
         * 1.3.6.1.4.1.8808.8.3.2: 201411
         *
         * 可以看到SNMP表这种按列遍历的顺序以及Table中变量OID的组织方式
         */
        @SuppressWarnings ( "unchecked" )
        @Test
        public void testUserDefinedTable() throws Exception
        {
            final String STAFF_TAB_OID = "1.3.6.1.4.1.8808.8";
            OID staffTableOID = new OID( STAFF_TAB_OID ); //员工表
            MOTableSubIndex[] subIndexes = new MOTableSubIndex[] { new MOTableSubIndex( SMIConstants.SYNTAX_INTEGER ) };
            MOColumn[] columns = new MOColumn[] {
                    new MOColumn( 1, SMIConstants.SYNTAX_INTEGER, MOAccessImpl.ACCESS_READ_ONLY ),//工号
                    new MOColumn( 2, SMIConstants.SYNTAX_OCTET_STRING, MOAccessImpl.ACCESS_READ_ONLY ),//姓名
                    new MOColumn( 3, SMIConstants.SYNTAX_INTEGER, MOAccessImpl.ACCESS_READ_ONLY ) //出生年月
            };
            DefaultMOTable table = new DefaultMOTable( staffTableOID, new MOTableIndex( subIndexes, false ), columns );
            MOMutableTableModel model = (MOMutableTableModel) table.getModel();
            model.addRow( new DefaultMOMutableRow2PC( new OID( "1" ), new Variable[] {
                    new Integer32( 19 ), new OctetString( "汪震" ), new Integer32( 198609 )
            } ) );
            model.addRow( new DefaultMOMutableRow2PC( new OID( "2" ), new Variable[] {
                    new Integer32( 20 ), new OctetString( "汪静好" ), new Integer32( 201411 )
            } ) );
            table.setVolatile( true );
            agent.registerManagedObject( table );
     
            PDU req = createSNMPv2PDU_GETBULK( STAFF_TAB_OID, 6 );
            ResponseEvent respEvent = mgrSession.send( req, createSNMPv2Target() );
            PDU resp = respEvent.getResponse();
            if ( resp != null && resp.getErrorStatus() == PDU.noError )
            {
                Vector<? extends VariableBinding> vbs = resp.getVariableBindings();
                StringBuilder buf = new StringBuilder();
                for ( VariableBinding vb : vbs )
                {
                    Variable var = vb.getVariable();
                    buf.append( String.format( "%s: %s ", vb.getOid(), variableToString( var ) ) );
                }
                LOGGER.debug( buf.toString() );
            }
        }
    }
  • 相关阅读:
    关于JS事件冒泡与JS事件代理(事件委托)
    异步、同步和阻塞、非阻塞
    大数据高并发
    前段clam安装
    JavaScript动态修改CSS
    键盘按键js效果
    键盘键值表总结
    移动端不可缩放
    JS基本语法
    计算几何——ICPC Latin American Regional Contests 2019 B
  • 原文地址:https://www.cnblogs.com/alamps/p/5418358.html
Copyright © 2020-2023  润新知