2010-06-21 5 views
2

En fonction du titre, je souhaite créer un simulateur d'agent de périphérique snmp. Maintenant, dans ma main, j'ai un fichier texte contenant un tas d'oids et sa valeur (via la marche snmp).Comment créer son propre simulateur snmp en utilisant l'agent snmp4j?

Je voudrais ajouter tout cela dans le simulateur comme aides statiques. J'ai créé une fonction simple pour tester si elle enregistre cet oid (je pense que c'est correct). Mais jusqu'à présent, je ne peux pas obtenir le résultat que j'ai voulu.

Je copie et colle le code TestAgent.

public class AgentUtil étend BaseAgent {

static { 
    LogFactory.setLogFactory(new Log4jLogFactory()); 
} 

private String address; 
public static boolean STOP=false; 
private Hashtable oid_values = new Hashtable(); 

public AgentUtil(String address,Hashtable oid_values) throws IOException { 

    super(new File("conf.agent"), new File("bootCounter.agent"), 
      new CommandProcessor(new OctetString(MPv3.createLocalEngineID()))); 
    this.address = address; 
    this.oid_values = oid_values; 
    agent.setWorkerPool(ThreadPool.create("RequestPool", 4));   
}  

@Override 
protected void registerManagedObjects() { 

} 

public void registerManagedObject(ManagedObject mo) { 
    try { 
     server.register(mo, null); 

    } catch (DuplicateRegistrationException ex) { 
     throw new RuntimeException(ex); 
    } 
} 


public void registerCustomMO() { 

//  String key = ""; 

     try {  
//    Iterator iterator = oid_values.keySet().iterator(); 
//    while(iterator.hasNext()) { 
//     key = (String) iterator.next();  
       server.register(createManagedObject(),null);      
//    }  

     } catch (DuplicateRegistrationException e) { 
      e.printStackTrace(); 
     }  
} 

private ManagedObject createManagedObject() { 


    VariableBinding vb = new VariableBinding(new OID("0"),new OctetString("WinsXP")); 
    StaticMOGroup group = new StaticMOGroup(new OID("1.2.3.4.5.6.7"),new  VariableBinding[]{vb}); 
    return group; 
} 

public void unregisterManagedObject(MOGroup moGroup) { 
    moGroup.unregisterMOs(server, getContext(moGroup)); 
} 

/* 
* Empty implementation 
*/ 
@Override 
protected void addNotificationTargets(SnmpTargetMIB targetMIB, 
     SnmpNotificationMIB notificationMIB) { 

     targetMIB.addDefaultTDomains(); 
} 

/** 
* Minimal View based Access Control 
* 
* http://www.faqs.org/rfcs/rfc2575.html 
*/ 
@Override 
protected void addViews(VacmMIB vacm) { 

    vacm.addGroup(SecurityModel.SECURITY_MODEL_SNMPv2c, new OctetString(
      "cpublic"), new OctetString("v1v2group"), 
      StorageType.nonVolatile); 

    vacm.addAccess(new OctetString("v1v2group"), new OctetString("public"), 
      SecurityModel.SECURITY_MODEL_ANY, SecurityLevel.NOAUTH_NOPRIV, 
      MutableVACM.VACM_MATCH_EXACT, new OctetString("fullReadView"), 
      new OctetString("fullWriteView"), new OctetString(
        "fullNotifyView"), StorageType.nonVolatile); 

    vacm.addViewTreeFamily(new OctetString("fullReadView"), new OID("1.3"), 
      new OctetString(), VacmMIB.vacmViewIncluded, 
      StorageType.nonVolatile); 
} 

/** 
* User based Security Model, only applicable to 
* SNMP v.3 
* 
*/ 
protected void addUsmUser(USM usm) { 
} 

protected void initTransportMappings() throws IOException { 
    transportMappings = new TransportMapping[1]; 
    Address addr = GenericAddress.parse(address); 
    TransportMapping tm = TransportMappings.getInstance() 
      .createTransportMapping(addr); 
    transportMappings[0] = tm; 
}  

/** 
* Start method invokes some initialization methods needed to 
* start the agent 
* @throws IOException 
*/ 
public void start() throws IOException { 

    init(); 
    loadConfig(ImportModes.REPLACE_CREATE); 
    addShutdownHook(); 
    getServer().addContext(new OctetString("public")); 
    finishInit(); 
    registerCustomMO();   
    run(); 
    sendColdStartNotification(); 

} 


protected void unregisterManagedObjects() { 
    // here we should unregister those objects previously registered... 
} 

/** 
* The table of community strings configured in the SNMP 
* engine's Local Configuration Datastore (LCD). 
* 
* We only configure one, "public". 
*/ 
protected void addCommunities(SnmpCommunityMIB communityMIB) { 
    Variable[] com2sec = new Variable[] { 
      new OctetString("public"), // community name 
      new OctetString("cpublic"), // security name 
      getAgent().getContextEngineID(), // local engine ID 
      new OctetString("public"), // default context name 
      new OctetString(), // transport tag 
      new Integer32(StorageType.nonVolatile), // storage type 
      new Integer32(RowStatus.active) // row status 
    }; 
    MOTableRow row = communityMIB.getSnmpCommunityEntry().createRow(
      new OctetString("public2public").toSubIndex(true), com2sec); 
    communityMIB.getSnmpCommunityEntry().addRow(row); 
} 

public void runAgent() throws IOException, InterruptedException { 
    while(!STOP) { 
     System.out.println("Agent running..."); 
     Thread.sleep(5000); 
    } 
}  

} 

Voici mon impression outs testeur snmp

[Client] Adresse cible: udp: 127.0.0.1/162 [Client] Envoyer PDU: GET [requestID = 1075357385, ErrorStatus = succès (0), errorindex = 0, VBS [1.2.3.4.5.6.7 = NULL]] [client] Réponse de 127.0.0.1/162: noSuchObject

Voici quelques impression du côté de l'agent

WARN: org.snmp4j.agent.io.MOServerPersistence - Contexte 'public' est pas plus pris en charge par l'agent INFO: org.snmp4j.agent.DefaultMOServer - Enregistré MO statique StaticMOGroup [racine = 1.2.3.4.5.6.7, vbs = {}] par défaut contexte avec la portée org.snmp4j.agent.DefaultMOScope [lowerBound = 1.2.3.4.5.6.7, lowerIncluded = true, upperBound = 1.2.3.4.5.6.8, upperIncluded = false] INFO: org.snmp4j.agent.mo.snmp.NotificationOriginatorImpl - Notification 1.3.6.1.6.3.1.1.5.1 signalée par [] pour le contexte
Agent en cours d'exécution. .. Agent en cours d'exécution ... Agent en cours d'exécution ... Agent en cours d'exécution ... Agent en cours d'exécution ... Agent en cours d'exécution ...

que je fais correctement? Oh, btw, j'utilise la bibliothèque snmp4j-Agent. Faites-moi savoir si vous avez besoin d'informations supplémentaires.

Cordialement, Simon

+1

Dans un exemple plus récent, il dit que la ligne « loadConfig (ImportModes. REPLACE_CREATE); " est la source de problèmes et il est commenté ... – Fildor

Répondre

2

Comme une note de côté, il y a un outil pythonique avec une fonctionnalité très similaire:

http://snmpsim.sf.net

Questions connexes