public class Bgp_Peers extends Thread{ static TargetRouter target; Vector oids; Vector files; File targetFile; String state_s; public Bgp_Peers (TargetRouter target) throws IOException, Exception { this.target=target; this.oids=loadOids(); this.files=loadFiles(); this.targetFile=new File("/var/lib/mysql/"+this.target.getDataBase()+"/Bgp_Peers"); this.start(); } // @Override public synchronized void run() { // définir le rep d'exécution des requêtes Shell final Shell sh = new Shell(); File dir= new File("/root/Desktop/Bgp_Peers"); if (dir.exists()==false) { dir.mkdir(); } sh.setDirectory(dir); String cmd0="cd /root/Desktop/Bgp_Peers"; try { sh.command(cmd0).consume();} catch (IOException ex) {Logger.getLogger(Bgp_Peers.class.getName()).log(Level.SEVERE, null, ex); } // lancer les Threads (voir la class ExcecuteSnmp) ExecuteSnmp s=new ExecuteSnmp(this, oids, files, sh); s.start(); try { s.join(); // je fais le join } catch (InterruptedException ex) { Logger.getLogger(Bgp_Peers.class.getName()).log(Level.SEVERE, null, ex); } ConcatFile c=new ConcatFile(this, sh); // je veux que le thread ConcatFile ne soit lancé que si ExecuteSnmp soit terminé c.start(); } public Vector loadOids() { Vector oids = new Vector(); oids.add(".1.3.6.1.2.1.15.3.1.1"); oids.add(".1.3.6.1.2.1.15.3.1.2"); oids.add(".1.3.6.1.2.1.15.3.1.3"); oids.add(".1.3.6.1.2.1.15.3.1.9"); oids.add(".1.3.6.1.2.1.15.3.1.10"); oids.add(".1.3.6.1.2.1.15.3.1.11"); return(oids); } public Vector loadFiles() { Vector files=new Vector(); files.add("peerAddr"); files.add("peerState"); files.add("peerStatus"); files.add("peerRemoteAS"); files.add("peerInUpdates"); files.add("peerOutUpdates"); return(files); } class walk extends Thread // Défini les Thread { String routerIP; String community; String oid; String file; Shell sh; public walk(Bgp_Peers b, String oid, String file, Shell sh) { this.routerIP=b.target.getRouterAddr(); this.community=b.target.getCommunity(); this.oid=oid; this.file=file; this.sh=sh; } public void run() { String cmd="snmpwalk -Ovq -v2c -c "+target.getCommunity()+" "+target.getRouterAddr()+" "+oid+ "> "+file+ "";try { sh.command(cmd).consume(); } catch (IOException ex) { Logger.getLogger(Bgp_Peers.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(Bgp_Peers.class.getName()).log(Level.SEVERE, null, ex); } } } class ExecuteSnmp extends Thread // lance une multitude de thread de la class walk { Bgp_Peers b; Shell sh; public ExecuteSnmp(Bgp_Peers b, Vector oids, Vector files, Shell sh) { this.b=b; this.sh=sh; } public void run() { int i; for (i=0; i<=5;i++) { new walk(this.b, oids.elementAt(i), files.elementAt(i), this.sh).start(); } } } class ConcatFile extends Thread { Bgp_Peers b; File targetFile; Shell sh; public ConcatFile(Bgp_Peers b, Shell sh) { this.b=b; this.targetFile=b.targetFile; this.sh=sh; } public void run() { // concaténer les fichiers String cmd1="paste -d ':' 'peerAddr' 'peerState' 'peerStatus' 'peerRemoteAS' 'peerInUpdates' 'peerOutUpdates' > "+targetFile.getPath()+""; try { sh.command(cmd1).consume(); } catch (IOException ex) { Logger.getLogger(Bgp_Peers.class.getName()).log(Level.SEVERE, null, ex); } } } public static void main(String[] args) throws IOException, Exception { Bgp_Peers b=new Bgp_Peers(new TargetRouter("10.16.1.29", "Divona", "OrangeTunisie")); } }