| 12
 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
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 
 |  
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.Target;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.event.ResponseListener;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.DefaultPDUFactory;
import org.snmp4j.util.TreeEvent;
import org.snmp4j.util.TreeListener;
import org.snmp4j.util.TreeUtils;
 
 
 
public class SnmpCommand implements ResponseListener {
    private static Log log = LogFactory.getLog(SnmpCommand.class);
 
    class WalkCounts {
        public int requests;
        public int objects;
    }
 
    String targetAddress;
    VariableBinding variableBinding;
 
    public SnmpCommand(String address) {
        this.targetAddress = address;
    }
 
    public SnmpCommand() {
		// TODO Auto-generated constructor stub
	}
 
	public void send(String oid, int ReqType) {
        Address targetAddress = GenericAddress.parse("udp:"
                + this.targetAddress + "/161");
        TransportMapping transport = null;
        try {
            transport = new DefaultUdpTransportMapping();
        } catch (IOException e1) {
            log.error("Echec lors de l'initialisation du thread pour l'écoute des requetes UDP",e1);
        }
        Snmp snmp = new Snmp(transport);
 
        try {
            snmp.listen();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        // setting up target
        CommunityTarget target = new CommunityTarget();
        target.setCommunity(new OctetString("public"));
        target.setAddress(targetAddress);
        target.setRetries(2);
        target.setTimeout(1500);
        target.setVersion(SnmpConstants.version1);
        target.setTimeout(5000);
 
        // creating PDU
        PDU pdu = new PDU();
        pdu.add(new VariableBinding(new OID(oid)));
        pdu.setType(ReqType);
 
        // The Send Action
        try {
            snmp.send(pdu, target, null, this);
        } catch (IOException e) {
            log.error("Echec lors du lancement de la requete SNMP",e);
        }
 
        synchronized (this) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                log.warn("Tree retrieval interrupted: ",e);
            }
        }
 
    }
 
 
 
    public PDU createPDU(Target target) {
        PDU request;
 
            request = new PDU();
 
        request.setType(PDU.GETNEXT);
        return request;
    }
 
    public VariableBinding request(String oid) {
        send(oid, PDU.GET);
        return variableBinding;
    }
 
 
    public List<VariableBinding> walk( OID oid) throws IOException {
 
		final List<VariableBinding> snapshot;
		snapshot = new LinkedList<VariableBinding>();	
 
		/////
		 Address targetAddress = GenericAddress.parse("udp:"
	                + this.targetAddress + "/161");
		//Address targetAddress = new UdpAddress("127.0.0.1/161");
	        TransportMapping transport = null;
	        try {
	            transport = new DefaultUdpTransportMapping();
	        } catch (IOException e1) {
	            log.error("Echec lors de l'initialisation du thread pour l'écoute des requetes UDP",e1);
	        }
	        Snmp snmp = new Snmp(transport);
 
	        try {
	            snmp.listen();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
 
	        // setting up target
	        CommunityTarget target = new CommunityTarget();
	        target.setCommunity(new OctetString("public"));
	        target.setAddress(targetAddress);
	        target.setRetries(2);
	        target.setTimeout(1500);
	        target.setVersion(SnmpConstants.version1);
 
 
	        /////
 
		PDU request =  createPDU(target);
		request.add(new VariableBinding(oid));
 
 
		request.setNonRepeaters(0);
 
		OID rootOID = request.get(0).getOid();
 
		final WalkCounts counts = new WalkCounts();
		final long startTime = System.currentTimeMillis();
		TreeUtils treeUtils = new TreeUtils(snmp, new DefaultPDUFactory());
		TreeListener treeListener = new TreeListener() {
			public boolean next(TreeEvent e) {
				counts.requests++;
				if (e.getVariableBindings() != null) {
					VariableBinding[] vbs = e.getVariableBindings();
					counts.objects += vbs.length;
					for (int i = 0; i < vbs.length; i++) {
						if (snapshot != null) {
							snapshot.add(vbs[i]);
						}
						System.out.println(vbs[i].toString());
					}
				}
				return true;
			}
 
			public void finished(TreeEvent e) {
				if ((e.getVariableBindings() != null)
						&& (e.getVariableBindings().length > 0)) {
					next(e);
				}
 
				log.debug("Total requests sent:    "
						+ counts.requests);
				log.debug("Total objects received: "
						+ counts.objects);
				log.debug("Total walk time:        "
						+ (System.currentTimeMillis() - startTime)
						+ " milliseconds");
				if (e.isError()) {
					System.err
					.println("The following error occurred during walk:");
					System.err.println(e.getErrorMessage());
				}
				synchronized (this) {
					this.notify();
				}
			}
		};
		synchronized (treeListener) {
			treeUtils.getSubtree(target, rootOID, null, treeListener);
			try {
				treeListener.wait();
			} catch (InterruptedException ex) {
				log.warn("Tree retrieval interrupted: ",ex);
				Thread.currentThread().interrupt();
			}
		}
		return snapshot;
	}
 
    public static void main(String [] argv){
    	SnmpCommand s = new SnmpCommand();
 
    	List<VariableBinding> snapshot;
    	snapshot = null;
    	try{
    		snapshot = s.walk(new OID(".1.3.6.1.2.1.1.9.1.3.7"));
 
    	for (VariableBinding vb : snapshot){
			System.out.println(vb.getOid());
			System.out.println(vb.getSyntax());
			System.out.println(vb.getVariable());
		}
    	}catch (Exception e) {
			// TODO: handle exception
    		e.printStackTrace();
		}
 
    }
 
    public void onResponse(ResponseEvent event) {
        synchronized (this) {
            ((Snmp) event.getSource()).cancel(event.getRequest(), this);
            this.variableBinding = event.getResponse().get(0);
 
            this.notify();    
        }        
    }
} | 
Partager