[SNMP4J] get with SEQUENCE OF

bauwin jean bauwin_jean at yahoo.fr
Wed Mar 15 10:58:50 CET 2006


  hello,
    
    I find to create an agent who accept a get command (manager command) and to return a SEQUENCE . 
    Because , in my MIB , i had created an object type TOTO with a syntax TABLEAPPLICATION .
    And TABLEAPPLICATION is a sequence of object INTEGER,STRING,INTEGER and  i want than when i use commad get with the oid of TOTO , the agent  return all datas of TOTO (the sequence tableapplication).
    
    Now, i use snmp V1 and i can use command get with a OID and , in my  manager , i receive the value of OID , but only if it's a simple object  as INTEGER or STRING.
    
    this is the source of testagent after my modification with my OID.
    
    
    
    
    JEAN
    
    
    
    package org.snmp4j.agent.test;
    
    import java.io.*;
    
    import org.apache.log4j.*;
    import org.snmp4j.*;
    import org.snmp4j.agent.*;
    import org.snmp4j.agent.mo.*;
    import org.snmp4j.agent.mo.snmp.*;
    import org.snmp4j.mp.*;
    import   org.snmp4j.security.*;
    import org.snmp4j.smi.*;
    import org.snmp4j.transport.*;
    import org.snmp4j.agent.io.ImportModes;
    
    
    public class TestAgent extends BaseAgent {
    public static String address;
    
    
    public MOScalar online_evatis_b;
    public MOScalar EBIConnectionStatus;
    public MOScalar CIConnectionStatus;
    public MOScalar BIDiffusionStatus;
    public MOScalar MODULEX25ConnectionStatus;
    public MOScalar DLKIConnectionStatus;
    
    
    public MOScalar online_evatis_i;
    public MOScalar evatisMConnectionStatus;
    public MOScalar evatisBConnectionStatus;
    
    public MOScalar online_evatis_m;
    public MOScalar Source0ConnectionStatus;
    public MOScalar evatis_i_ConnectionStatus;
    
    public TestAgent(File bootCounterFile, File configFile) throws IOException {
      super(bootCounterFile, configFile,
            new   OctetString(MPv3.createLocalEngineID()));
    
    }
    
    protected void registerManagedObjects() {
      try {
          online_evatis_b= new MOScalar(new  OID(".1.3.6.1.4.1.881.1.1"),MOAccessImpl.ACCESS_READ_WRITE,new  OctetString("NULL"));  
         EBIConnectionStatus= new  MOScalar(new  OID(".1.3.6.1.4.1.4890.1.2"),MOAccessImpl.ACCESS_READ_WRITE,new  OctetString("NULL"));
         CIConnectionStatus= new MOScalar(new  OID(".1.3.6.1.4.1.4890.1.3"),MOAccessImpl.ACCESS_READ_WRITE,new  OctetString("NULL"));
         BIDiffusionStatus= new MOScalar(new  OID(".1.3.6.1.4.1.4890.1.4"),MOAccessImpl.ACCESS_READ_WRITE,new  OctetString("NULL"));
         MODULEX25ConnectionStatus= new  MOScalar(new  OID(".1.3.6.1.4.1.4890.1.5"),MOAccessImpl.ACCESS_READ_WRITE,new  OctetString("NULL"));
            DLKIConnectionStatus= new MOScalar(new  OID(".1.3.6.1.4.1.4890.1.6"),MOAccessImpl.ACCESS_READ_WRITE,new  OctetString("NULL"));
          
          
          online_evatis_i= new MOScalar(new  OID(".1.3.6.1.4.1.4890.2.1"),MOAccessImpl.ACCESS_READ_WRITE,new  OctetString("NULL"));
         evatisMConnectionStatus= new  MOScalar(new  OID(".1.3.6.1.4.1.4890.2.2"),MOAccessImpl.ACCESS_READ_WRITE,new  OctetString("NULL"));
         evatisBConnectionStatus= new  MOScalar(new  OID(".1.3.6.1.4.1.4890.2.3"),MOAccessImpl.ACCESS_READ_WRITE,new  OctetString("NULL"));
        
         online_evatis_m= new  MOScalar(new  OID(".1.3.6.1.4.1.4890.3.1"),MOAccessImpl.ACCESS_READ_WRITE,new  OctetString("NULL"));
          Source0ConnectionStatus= new  MOScalar(new    OID(".1.3.6.1.4.1.4890.3.2"),MOAccessImpl.ACCESS_READ_WRITE,new  OctetString("NULL"));
          evatis_i_ConnectionStatus= new MOScalar(new  OID(".1.3.6.1.4.1.4890.3.3"),MOAccessImpl.ACCESS_READ_WRITE,new  OctetString("NULL"));
        
        
        server.register(online_evatis_b, null);
        server.register(EBIConnectionStatus, null);
        server.register(CIConnectionStatus, null);
        server.register(BIDiffusionStatus, null);
        server.register(MODULEX25ConnectionStatus, null);
        server.register(DLKIConnectionStatus, null);
        
        server.register(online_evatis_i, null);
        server.register(evatisMConnectionStatus, null);
        server.register(evatisBConnectionStatus, null);
        
          server.register(online_evatis_m, null);
        server.register(Source0ConnectionStatus, null);
        server.register(evatis_i_ConnectionStatus, null);
        
        }
      catch (DuplicateRegistrationException ex) {
        ex.printStackTrace();
      }
    }
    
    protected void addNotificationTargets(SnmpTargetMIB targetMIB,
                                         SnmpNotificationMIB notificationMIB) {
      targetMIB.addDefaultTDomains();
    
      targetMIB.addTargetAddress(new OctetString("notification"),
                                TransportDomains.transportDomainUdpIpv4,
                                new OctetString(new UdpAddress("127.0.0.1/162").getValue()),
                                200, 1,
                                new OctetString("notify"),
                                new OctetString("v2c"),
                                StorageType.permanent);
      targetMIB.addTargetParams(new OctetString("v2c"),
                               MessageProcessingModel.MPv2c,
                               SecurityModel.SECURITY_MODEL_SNMPv2c,
                               new OctetString("public"),
                               SecurityLevel.NOAUTH_NOPRIV,
                               StorageType.permanent);
      notificationMIB.addNotifyEntry(new OctetString("default"),
                                    new OctetString("notify"),
                                    SnmpNotificationMIB.SnmpNotifyTypeEnum.trap,
                                    StorageType.permanent);
    }
    
    protected void addViews(VacmMIB vacm) {
      vacm.addGroup(SecurityModel.SECURITY_MODEL_SNMPv1,
                    new OctetString("public"),
                    new OctetString("v1v2group"),
                    StorageType.nonVolatile);
      vacm.addGroup(SecurityModel.SECURITY_MODEL_SNMPv2c,
                    new OctetString("public"),
                    new   OctetString("v1v2group"),
                    StorageType.nonVolatile);
      vacm.addGroup(SecurityModel.SECURITY_MODEL_USM,
                    new OctetString("SHADES"),
                    new OctetString("v3group"),
                    StorageType.nonVolatile);
      vacm.addGroup(SecurityModel.SECURITY_MODEL_USM,
                    new OctetString("TEST"),
                    new OctetString("v3test"),
                      StorageType.nonVolatile);
      vacm.addGroup(SecurityModel.SECURITY_MODEL_USM,
                    new OctetString("SHA"),
                    new OctetString("v3restricted"),
                    StorageType.nonVolatile);
    
      vacm.addAccess(new OctetString("v1v2group"), new OctetString(),
                    SecurityModel.SECURITY_MODEL_ANY,
                    SecurityLevel.NOAUTH_NOPRIV, VacmMIB.vacmExactMatch,
                    new OctetString("fullReadView"),
                    new OctetString("fullWriteView"),
                    new OctetString("fullNotifyView"),
                     StorageType.nonVolatile);
      vacm.addAccess(new OctetString("v3group"), new OctetString(),
                    SecurityModel.SECURITY_MODEL_USM,
                    SecurityLevel.AUTH_PRIV, VacmMIB.vacmExactMatch,
                    new OctetString("fullReadView"),
                    new OctetString("fullWriteView"),
                    new OctetString("fullNotifyView"),
                     StorageType.nonVolatile);
      vacm.addAccess(new OctetString("v3restricted"), new OctetString(),
                    SecurityModel.SECURITY_MODEL_USM,
                    SecurityLevel.AUTH_NOPRIV, VacmMIB.vacmExactMatch,
                    new OctetString("restrictedReadView"),
                    new OctetString("restrictedWriteView"),
                    new OctetString("restrictedNotifyView"),
                     StorageType.nonVolatile);
      vacm.addAccess(new OctetString("v3test"), new OctetString(),
                    SecurityModel.SECURITY_MODEL_USM,
                    SecurityLevel.AUTH_PRIV, VacmMIB.vacmExactMatch,
                    new OctetString("testReadView"),
                    new OctetString("testWriteView"),
                    new OctetString("testNotifyView"),
                     StorageType.nonVolatile);
    
      vacm.addViewTreeFamily(new OctetString("fullReadView"), new OID("1.3"),
                            new OctetString(), VacmMIB.vacmViewIncluded,
                            StorageType.nonVolatile);
      vacm.addViewTreeFamily(new OctetString("fullWriteView"), new OID("1.3"),
                            new OctetString(), VacmMIB.vacmViewIncluded,
                            StorageType.nonVolatile);
      vacm.addViewTreeFamily(new OctetString("fullNotifyView"), new OID("1.3"),
                            new OctetString(), VacmMIB.vacmViewIncluded,
                            StorageType.nonVolatile);
    
      vacm.addViewTreeFamily(new OctetString("restrictedReadView"),
                            new OID("1.3.6.1.2"),
                            new OctetString(), VacmMIB.vacmViewIncluded,
                            StorageType.nonVolatile);
      vacm.addViewTreeFamily(new OctetString("restrictedWriteView"),
                            new OID("1.3.6.1.2.1"),
                            new OctetString(),
                            VacmMIB.vacmViewIncluded,
                            StorageType.nonVolatile);
      vacm.addViewTreeFamily(new OctetString("restrictedNotifyView"),
                            new OID("1.3.6.1.2"),
                            new OctetString(), VacmMIB.vacmViewIncluded,
                            StorageType.nonVolatile);
    
        vacm.addViewTreeFamily(new OctetString("testReadView"),
                            new OID("1.3.6.1.2"),
                            new OctetString(), VacmMIB.vacmViewIncluded,
                            StorageType.nonVolatile);
      vacm.addViewTreeFamily(new OctetString("testReadView"),
                            new OID("1.3.6.1.2.1.1"),
                            new OctetString(), VacmMIB.vacmViewExcluded,
                            StorageType.nonVolatile);
      vacm.addViewTreeFamily(new OctetString("testWriteView"),
                            new OID("1.3.6.1.2.1"),
                            new OctetString(),
                            VacmMIB.vacmViewIncluded,
                            StorageType.nonVolatile);
        vacm.addViewTreeFamily(new OctetString("testNotifyView"),
                            new OID("1.3.6.1.2"),
                            new OctetString(), VacmMIB.vacmViewIncluded,
                            StorageType.nonVolatile);
    
    }
    
    protected void addUsmUser(USM usm) {
      UsmUser user = new UsmUser(new OctetString("SHADES"),
                                AuthSHA.ID,
                                new OctetString("SHADESAuthPassword"),
                                PrivDES.ID,
                                new OctetString("SHADESPrivPassword"));
      usm.addUser(user.getSecurityName(), usm.getLocalEngineID(), user);
      user = new UsmUser(new OctetString("TEST"),
                                AuthSHA.ID,
                                new OctetString("maplesyrup"),
                                PrivDES.ID,
                                new OctetString("maplesyrup"));
      usm.addUser(user.getSecurityName(), usm.getLocalEngineID(), user);
      user = new UsmUser(new OctetString("SHA"),
                                AuthSHA.ID,
                                new OctetString("SHAAuthPassword"),
                                null,
                                null);
      usm.addUser(user.getSecurityName(), usm.getLocalEngineID(), user);
    }
    
    
    public void trap(OID oid,int specific,int objectid,String timestamp,int  severity,int availstatus,int appeventcode,String problemtext,String  problemcause) throws IOException
    {
        Address targetAddress = GenericAddress.parse("udp:127.0.0.1/162");
        TransportMapping transport =   new DefaultUdpTransportMapping();
        Snmp snmp = new Snmp(transport);
        CommunityTarget target = new CommunityTarget(); 
        target.setCommunity(new OctetString("public"));
        target.setAddress(targetAddress);
        target.setRetries(2);
        target.setTimeout(1500);
        target.setVersion(SnmpConstants.version1);
        PDUv1 pdu = new PDUv1();
       
        pdu.setSpecificTrap(specific);
        pdu.setAgentAddress(new IpAddress("127.0.0.1"));
        pdu.setEnterprise(new OID(".1.3.6.1.4.1.881.5.3.200.1.1"));
        
       
        
        
        
        VariableBinding varbind1 = new VariableBinding(new  OID(".1.3.6.1.4.1.881.5.3.200.1.1.1"),new   Integer32(specific));
      VariableBinding varbind2 = new VariableBinding(new  OID(".1.3.6.1.4.1.881.5.3.200.1.1.2"),new Integer32(objectid));
      VariableBinding varbind3 = new VariableBinding(new  OID(".1.3.6.1.4.1.881.5.3.200.1.1.3"),new OctetString(timestamp));
      VariableBinding varbind4 = new VariableBinding(new  OID(".1.3.6.1.4.1.881.5.3.200.1.1.4"),new Integer32(severity));
      VariableBinding varbind5 = new VariableBinding(new  OID(".1.3.6.1.4.1.881.5.3.200.1.1.5"),new Integer32(availstatus));
      VariableBinding varbind6 = new VariableBinding(new  OID(".1.3.6.1.4.1.881.5.3.200.1.1.6"),new Integer32(appeventcode));
      VariableBinding varbind7 = new VariableBinding(new  OID(".1.3.6.1.4.1.881.5.3.200.1.1.7"),new OctetString(problemtext));
        VariableBinding varbind8 = new VariableBinding(new  OID(".1.3.6.1.4.1.881.5.3.200.1.1.8"),new   OctetString(problemcause));
        pdu.add(varbind1);
        pdu.add(varbind2);
        pdu.add(varbind3);
        pdu.add(varbind4);
        pdu.add(varbind5);
        pdu.add(varbind6);
        pdu.add(varbind7);
        pdu.add(varbind8);
       
       // pdu.setSpecificTrap(specific);
        pdu.setType(PDU.V1TRAP);
        System.out.println("pdu envoyer :" + pdu.toString());
        try {
            snmp.sendPDU(pdu, target);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("ok envoyer");
    }
    protected void   initTransportMappings() throws IOException {
      transportMappings = new TransportMapping[1];
      transportMappings[0] =
          new DefaultUdpTransportMapping(new UdpAddress(address));
    }
    /*
    public static void main(String[] args) {
      if (args.length > 0) {
        address = args[0];
      }
      else {
        address = "0.0.0.0/161";
      }
      BasicConfigurator.configure();
      try {
        TestAgent testAgent1 = new TestAgent(new File("SNMP4JTestAgentBC.cfg"),
                                            new File("SNMP4JTestAgentConfig.cfg"));
        testAgent1.init();
          testAgent1.loadConfig(ImportModes.UPDATE_CREATE);
        testAgent1.addShutdownHook();
        testAgent1.run();
        while (true) {
          try {
            Thread.sleep(1000);
          }
          catch (InterruptedException ex1) {
            break;
          }
        }
      }
      catch (IOException ex) {
        ex.printStackTrace();
      }
    
    }*/
    }
		
---------------------------------
 Nouveau : téléphonez moins cher avec Yahoo! Messenger ! Découvez les tarifs exceptionnels pour appeler la France et l'international.Téléchargez la version beta.
		
---------------------------------
 Nouveau : téléphonez moins cher avec Yahoo! Messenger ! Découvez les tarifs exceptionnels pour appeler la France et l'international.Téléchargez la version beta.


More information about the SNMP4J mailing list