IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

API standards et tierces Java Discussion :

Récupération d'alarme SNMP


Sujet :

API standards et tierces Java

  1. #1
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2014
    Messages
    9
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Février 2014
    Messages : 9
    Par défaut Récupération d'alarme SNMP
    Bonjour,

    J'ai un problème pour récupérer des alarmes SNMP et l'afficher sur mon pc. Pouvez-vous m'aider par un bout de code fonctionnel sachant que j'ai testé un grand nombre d'exemple en vain :

    Sans titre.png

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    package in.techdive.snmp4j.ex;
     
    import java.io.IOException;
    import org.snmp4j.CommandResponder;
    import org.snmp4j.CommandResponderEvent;
    import org.snmp4j.CommunityTarget;
    import org.snmp4j.MessageDispatcher;
    import org.snmp4j.MessageDispatcherImpl;
    import org.snmp4j.MessageException;
    import org.snmp4j.PDU;
    import org.snmp4j.Snmp;
    import org.snmp4j.log.LogFactory;
    import org.snmp4j.mp.MPv1;
    import org.snmp4j.mp.MPv2c;
    import org.snmp4j.mp.StateReference;
    import org.snmp4j.mp.StatusInformation;
    import org.snmp4j.security.Priv3DES;
    import org.snmp4j.security.SecurityProtocols;
    import org.snmp4j.smi.OctetString;
    import org.snmp4j.smi.TcpAddress;
    import org.snmp4j.smi.TransportIpAddress;
    import org.snmp4j.smi.UdpAddress;
    import org.snmp4j.tools.console.SnmpRequest;
    import org.snmp4j.transport.AbstractTransportMapping;
    import org.snmp4j.transport.DefaultTcpTransportMapping;
    import org.snmp4j.transport.DefaultUdpTransportMapping;
    import org.snmp4j.util.MultiThreadedMessageDispatcher;
    import org.snmp4j.util.ThreadPool;
     
    public class TrapReceiver implements CommandResponder
    {
    public TrapReceiver()
    {
    }
     
    public static void main(String[] args)
    {
    TrapReceiver snmp4jTrapReceiver = new TrapReceiver();
    try
    {
    snmp4jTrapReceiver.listen(new UdpAddress("0.0.0.0/162"));
    }
    catch (IOException e)
    {
    System.err.println("Error in Listening for Trap");
    System.err.println("Exception Message = " + e.getMessage());
    }
    }
     
     
     
    /**
    * This method will listen for traps and response pdu's from SNMP agent.
    */
    public synchronized void listen(TransportIpAddress address) throws IOException
    {
    AbstractTransportMapping transport;
    if (address instanceof TcpAddress)
    {
    transport = new DefaultTcpTransportMapping((TcpAddress) address);
    }
    else
    {
    transport = new DefaultUdpTransportMapping((UdpAddress) address);
    }
     
    ThreadPool threadPool = ThreadPool.create("DispatcherPool", 10);
    MessageDispatcher mtDispatcher = new MultiThreadedMessageDispatcher(threadPool, new MessageDispatcherImpl());
     
    // add message processing models
    mtDispatcher.addMessageProcessingModel(new MPv1());
    mtDispatcher.addMessageProcessingModel(new MPv2c());
     
    // add all security protocols
    SecurityProtocols.getInstance().addDefaultProtocols();
    SecurityProtocols.getInstance().addPrivacyProtocol(new Priv3DES());
     
    //Create Target
    CommunityTarget target = new CommunityTarget();
    target.setCommunity( new OctetString("public"));
     
    Snmp snmp = new Snmp(mtDispatcher, transport);
    snmp.addCommandResponder(this);
     
    transport.listen();
    System.out.println("Listening on " + address);
     
    try
    {
    this.wait();
    }
    catch (InterruptedException ex)
    {
    Thread.currentThread().interrupt();
    }
    }
     
    /**
    * This method will be called whenever a pdu is received on the given port specified in the listen() method
    */
    public synchronized void processPdu(CommandResponderEvent cmdRespEvent)
    {
    System.out.println("Received PDU...");
    PDU pdu = cmdRespEvent.getPDU();
    if (pdu != null)
    {
     
    System.out.println("Trap Type = " + pdu.getType());
    System.out.println("Variable Bindings = " + pdu.getVariableBindings());
    int pduType = pdu.getType();
    if ((pduType != PDU.TRAP) && (pduType != PDU.V1TRAP) && (pduType != PDU.REPORT)
    && (pduType != PDU.RESPONSE))
    {
    pdu.setErrorIndex(0);
    pdu.setErrorStatus(0);
    pdu.setType(PDU.RESPONSE);
    StatusInformation statusInformation = new StatusInformation();
    StateReference ref = cmdRespEvent.getStateReference();
    try
    {
    System.out.println(cmdRespEvent.getPDU());
    cmdRespEvent.getMessageDispatcher().returnResponsePdu(cmdRespEvent.getMessageProcessingModel(),
    cmdRespEvent.getSecurityModel(), cmdRespEvent.getSecurityName(), cmdRespEvent.getSecurityLevel(),
    pdu, cmdRespEvent.getMaxSizeResponsePDU(), ref, statusInformation);
    }
    catch (MessageException ex)
    {
    System.err.println("Error while sending response: " + ex.getMessage());
    LogFactory.getLogger(SnmpRequest.class).error(ex);
    }
    }
    }
    }
    }
    Le résultat est "Listening on 0.0.0.0/162" mais j'ai déclenché des alarmes sur le routeur à superviser mais pas de résultat affiché elle reste "Listening on 0.0.0.0/162".

    Merci d'avance pour votre aide.

  2. #2
    Membre confirmé
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Février 2011
    Messages
    192
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Transports

    Informations forums :
    Inscription : Février 2011
    Messages : 192
    Par défaut
    le code semble pas mal, tu es sur quel système ? windows ? AS tu activé le protocole SNMP via le panneau de configuration ?

Discussions similaires

  1. Réponses: 14
    Dernier message: 16/09/2011, 10h57
  2. Récupération Ip protocole SNMP
    Par darkman13130 dans le forum Développement
    Réponses: 1
    Dernier message: 26/05/2010, 18h33
  3. Récupération d'informations SNMP
    Par aminos88 dans le forum API standards et tierces
    Réponses: 2
    Dernier message: 23/05/2009, 21h55
  4. récupération trap snmp
    Par chocoboy dans le forum Réseau
    Réponses: 1
    Dernier message: 10/10/2008, 19h09
  5. [snmp][WINDOWS]récupération de traps
    Par ronan99999 dans le forum Développement
    Réponses: 5
    Dernier message: 09/10/2006, 13h59

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo