1 Objectifs du projet 2 Utilisation de SNMP4j

publicité
Département R & T - S4 - Module M4207
[email protected]
Projet : Développement d’un client graphique SNMP
8 février 2017
Ce projet est à réaliser en binôme. Chaque binôme doit fournir à la fin du projet : le code
commenté, un rapport décrivant l’étude de conception et de réalisation du projet. La restitution
du projet se fait sous forme d ?une soutenance de 15 minutes le 28 février pour le groupe A et le
2 Mars pour le groupe B
1
Objectifs du projet
L’objectif de ce projet est de développer un simple client de supervision d’équipements réseaux
en utilisant le protocole SNMP (Simple network Management Protocol). La bibliothèque snmp4j
offre des classes java pour la programmation simple de clients/serveurs snmp. Dans ce projet nous
nous limitons au développement d’un client snmp. Le cahier des charges à respecter est le suivant :
7
0
2
4
M
1 Le client offre un champ de saisi de l’adresse d’un agent SNMP à interroger.
2 Le client offer un champ de saisi d’un ou plusieurs variables d’observation (ex. OID).
3 Il est possible d’effectuer des opérations de lecture ou de modification des variables (sous
reserve d’utilisation des noms des communautés adéquates).
4 Pour des opérations de lecture, il serait possible de spécifier une périodicité à respecter (ex.
lecture d’une variable tous les x seconds).
5 Un espace d’affichage des valeurs des variables observées et/ou des messages d’erreurs est à
prévoir.
6 Le client doit permettre d’employer la version 1 du protocole SNMP.
2
Utilisation de SNMP4j
SNMP4J est une API 1 java qui permet de développer des clients/serveurs SNMP. L’API est
disponible sur le site : http://www.snmp4j.org. Vous pouvez télécharger le fichier jar de l’API sur
le site cité ou à partir de http://lipn.fr/~kanawati/java/snmp4j-2.3.3.jar. La documentation
complète est disponible sur http://www.snmp4j.org/doc/index.html.
Pour complier un programme Java qui utilise snmp4j n’oublier pas d’ajouter le fichier jar à la
variable d’environment classpath ou de le spécifier lors de la compilation comme suite :
javac -classpath . :./snmp4j-2.3.3.jar MyClasse.java
L’exécution du programme se fait de la même manière.
java -classpath . :./snmp4j-2.3.3.jar MyClasse
1. Application Programming Interface
1
Département R & T - S4 - Module M4207
[email protected]
Un exemple type d’un client snmp qui effectue une opération GET est donné ci-après.
import j a v a . i o . IOException ;
import j a v a . u t i l . ∗ ;
import
import
import
import
import
import
o r g . snmp4j . ∗ ;
o r g . snmp4j . e v e n t . ∗ ;
o r g . snmp4j .mp. SnmpConstants ;
o r g . snmp4j . smi . ∗ ;
o r g . snmp4j . t r a n s p o r t . DefaultUdpTransportMapping ;
o r g . snmp4j . u t i l . ∗ ;
public c l a s s SimpleSnmpClient {
private S t r i n g a d d r e s s ; /∗ a d r e s s e de l ’ a g e n t SNMP ∗/
private Snmp snmp ;
7
0
2
4
M
public SimpleSnmpClient ( S t r i n g a d d r e s s ) {
super ( ) ;
this . address = address ;
try { s t a r t ( ) ; } catch ( IOException e ) { throw new RuntimeException ( e ) ;
}
public void s t o p ( ) throws IOException {
snmp . c l o s e ( ) ; }
private void s t a r t ( ) throws IOException {
TransportMapping t r a n s p o r t = new DefaultUdpTransportMapping ( ) ;
snmp = new Snmp( t r a n s p o r t ) ;
/∗ La méthode l i s t e n permet au c l i e n t d ’ e n v o y e r e t r e c e v o i r l e s datagram
transport . l i s t e n ( ) ;
}
public ResponseEvent g e t (OID o i d s [ ] ) throws IOException {
ResponseEvent e v e n t = snmp . send ( getPDU ( o i d s ) , g e t T a r g e t ( ) , null ) ;
i f ( e v e n t != null ) {
return e v e n t ;
}
throw new RuntimeException ( ”GET timed out ” ) ;
}
public S t r i n g g e t A s S t r i n g (OID o i d ) throws IOException {
ResponseEvent e v e n t = g e t (new OID [ ] { o i d } ) ;
return e v e n t . g e t R e s p o n s e ( ) . g e t ( 0 ) . g e t V a r i a b l e ( ) . t o S t r i n g ( ) ;
}
2
Département R & T - S4 - Module M4207
[email protected]
private PDU getPDU (OID o i d s [ ] ) {
/∗ s p e c i f i c a t i o n de l a PDU à e n v o y e r ∗/
PDU pdu = new PDU( ) ;
f o r (OID o i d : o i d s ) {
pdu . add (new V a r i a b l e B i n d i n g ( o i d ) ) ;
}
pdu . setType (PDU.GET) ;
return pdu ;
}
private Target g e t T a r g e t ( ) {
/∗ s p e c i f i c a t i o n de l a d e s t i n a t i o n de l a r e q u ê t e
SNMP à é n v o y e r ∗/
Address t a r g e t A d d r e s s = G e n e r i c A d d r e s s . p a r s e ( t h i s . a d d r e s s ) ;
CommunityTarget t a r g e t = new CommunityTarget ( ) ;
t a r g e t . setCommunity (new O c t e t S t r i n g ( ” p u b l i c ” ) ) ;
target . setAddress ( targetAddress ) ;
target . setRetries (2);
t a r g e t . setTimeout ( 1 5 0 0 ) ;
t a r g e t . s e t V e r s i o n ( SnmpConstants . v e r s i o n 2 c ) ;
return t a r g e t ;
}
7
0
2
4
M
public s t a t i c S t r i n g e x t r a c t S i n g l e S t r i n g ( ResponseEvent e v e n t ) {
return e v e n t . g e t R e s p o n s e ( ) . g e t ( 0 ) . g e t V a r i a b l e ( ) . t o S t r i n g ( ) ;
}
public s t a t i c void main ( S t r i n g [ ] a r g s ) {
try {
SimpleSnmpClient c l i e n t = new SimpleSnmpClient ( ”udp : 1 2 7 . 0 . 0 . 1 / 1 6 1 ” ) ;
S t r i n g s y s D e s c r = c l i e n t . g e t A s S t r i n g (new OID( ” . 1 . 3 . 6 . 1 . 2 . 1 . 1 . 1 . 0 ” ) ) ;
System . out . p r i n t l n ( s y s D e s c r ) ;
} catch ( E x c e p t i o n e ) { }
}
}
3
Téléchargement