├── README.md ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── jayway │ │ └── snmpblogg │ │ ├── Agent.java │ │ ├── MOScalarFactory.java │ │ ├── MOTableBuilder.java │ │ └── SimpleSnmpClient.java ├── log4j.xml └── resources │ └── log4j.xml └── test └── java └── com └── jayway └── snmpblogg └── SnmpAgentAndClientTest.java /README.md: -------------------------------------------------------------------------------- 1 | # Introduction to snmp4j 2 | 3 | Read my blog here http://www.jayway.com/2010/05/21/introduction-to-snmp4j/ 4 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.jayway.snmpblogg 6 | snmp-blogg 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | snmp-blogg 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 16 | 17 | 18 | 19 | junit 20 | junit 21 | 4.8.1 22 | test 23 | 24 | 25 | org.snmp4j 26 | snmp4j 27 | 1.10.1 28 | 29 | 30 | org.snmp4j 31 | snmp4j-agent 32 | 1.3.1 33 | 34 | 35 | com.jayway.awaitility 36 | awaitility 37 | 1.2.1 38 | test 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | org.apache.maven.plugins 47 | maven-compiler-plugin 48 | 49 | 1.6 50 | 1.6 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /src/main/java/com/jayway/snmpblogg/Agent.java: -------------------------------------------------------------------------------- 1 | package com.jayway.snmpblogg; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | 6 | import org.snmp4j.TransportMapping; 7 | import org.snmp4j.agent.BaseAgent; 8 | import org.snmp4j.agent.CommandProcessor; 9 | import org.snmp4j.agent.DuplicateRegistrationException; 10 | import org.snmp4j.agent.MOGroup; 11 | import org.snmp4j.agent.ManagedObject; 12 | import org.snmp4j.agent.mo.MOTableRow; 13 | import org.snmp4j.agent.mo.snmp.RowStatus; 14 | import org.snmp4j.agent.mo.snmp.SnmpCommunityMIB; 15 | import org.snmp4j.agent.mo.snmp.SnmpNotificationMIB; 16 | import org.snmp4j.agent.mo.snmp.SnmpTargetMIB; 17 | import org.snmp4j.agent.mo.snmp.StorageType; 18 | import org.snmp4j.agent.mo.snmp.VacmMIB; 19 | import org.snmp4j.agent.security.MutableVACM; 20 | import org.snmp4j.log.Log4jLogFactory; 21 | import org.snmp4j.log.LogFactory; 22 | import org.snmp4j.mp.MPv3; 23 | import org.snmp4j.security.SecurityLevel; 24 | import org.snmp4j.security.SecurityModel; 25 | import org.snmp4j.security.USM; 26 | import org.snmp4j.smi.Address; 27 | import org.snmp4j.smi.GenericAddress; 28 | import org.snmp4j.smi.Integer32; 29 | import org.snmp4j.smi.OID; 30 | import org.snmp4j.smi.OctetString; 31 | import org.snmp4j.smi.Variable; 32 | import org.snmp4j.transport.TransportMappings; 33 | 34 | /** 35 | * This Agent contains mimimal functionality for running a version 2c snmp 36 | * agent. 37 | * 38 | * 39 | * @author johanrask 40 | * 41 | */ 42 | public class Agent extends BaseAgent { 43 | 44 | // not needed but very useful of course 45 | static { 46 | LogFactory.setLogFactory(new Log4jLogFactory()); 47 | } 48 | 49 | private String address; 50 | 51 | public Agent(String address) throws IOException { 52 | 53 | // These files does not exist and are not used but has to be specified 54 | // Read snmp4j docs for more info 55 | super(new File("conf.agent"), new File("bootCounter.agent"), 56 | new CommandProcessor( 57 | new OctetString(MPv3.createLocalEngineID()))); 58 | this.address = address; 59 | } 60 | 61 | /** 62 | * We let clients of this agent register the MO they 63 | * need so this method does nothing 64 | */ 65 | @Override 66 | protected void registerManagedObjects() { 67 | } 68 | 69 | /** 70 | * Clients can register the MO they need 71 | */ 72 | public void registerManagedObject(ManagedObject mo) { 73 | try { 74 | server.register(mo, null); 75 | } catch (DuplicateRegistrationException ex) { 76 | throw new RuntimeException(ex); 77 | } 78 | } 79 | 80 | public void unregisterManagedObject(MOGroup moGroup) { 81 | moGroup.unregisterMOs(server, getContext(moGroup)); 82 | } 83 | 84 | /* 85 | * Empty implementation 86 | */ 87 | @Override 88 | protected void addNotificationTargets(SnmpTargetMIB targetMIB, 89 | SnmpNotificationMIB notificationMIB) { 90 | } 91 | 92 | /** 93 | * Minimal View based Access Control 94 | * 95 | * http://www.faqs.org/rfcs/rfc2575.html 96 | */ 97 | @Override 98 | protected void addViews(VacmMIB vacm) { 99 | 100 | vacm.addGroup(SecurityModel.SECURITY_MODEL_SNMPv2c, new OctetString( 101 | "cpublic"), new OctetString("v1v2group"), 102 | StorageType.nonVolatile); 103 | 104 | vacm.addAccess(new OctetString("v1v2group"), new OctetString("public"), 105 | SecurityModel.SECURITY_MODEL_ANY, SecurityLevel.NOAUTH_NOPRIV, 106 | MutableVACM.VACM_MATCH_EXACT, new OctetString("fullReadView"), 107 | new OctetString("fullWriteView"), new OctetString( 108 | "fullNotifyView"), StorageType.nonVolatile); 109 | 110 | vacm.addViewTreeFamily(new OctetString("fullReadView"), new OID("1.3"), 111 | new OctetString(), VacmMIB.vacmViewIncluded, 112 | StorageType.nonVolatile); 113 | } 114 | 115 | /** 116 | * User based Security Model, only applicable to 117 | * SNMP v.3 118 | * 119 | */ 120 | protected void addUsmUser(USM usm) { 121 | } 122 | 123 | protected void initTransportMappings() throws IOException { 124 | transportMappings = new TransportMapping[1]; 125 | Address addr = GenericAddress.parse(address); 126 | TransportMapping tm = TransportMappings.getInstance() 127 | .createTransportMapping(addr); 128 | transportMappings[0] = tm; 129 | } 130 | 131 | /** 132 | * Start method invokes some initialization methods needed to 133 | * start the agent 134 | * @throws IOException 135 | */ 136 | public void start() throws IOException { 137 | 138 | init(); 139 | // This method reads some old config from a file and causes 140 | // unexpected behavior. 141 | // loadConfig(ImportModes.REPLACE_CREATE); 142 | addShutdownHook(); 143 | getServer().addContext(new OctetString("public")); 144 | finishInit(); 145 | run(); 146 | sendColdStartNotification(); 147 | } 148 | 149 | 150 | 151 | protected void unregisterManagedObjects() { 152 | // here we should unregister those objects previously registered... 153 | } 154 | 155 | /** 156 | * The table of community strings configured in the SNMP 157 | * engine's Local Configuration Datastore (LCD). 158 | * 159 | * We only configure one, "public". 160 | */ 161 | protected void addCommunities(SnmpCommunityMIB communityMIB) { 162 | Variable[] com2sec = new Variable[] { 163 | new OctetString("public"), // community name 164 | new OctetString("cpublic"), // security name 165 | getAgent().getContextEngineID(), // local engine ID 166 | new OctetString("public"), // default context name 167 | new OctetString(), // transport tag 168 | new Integer32(StorageType.nonVolatile), // storage type 169 | new Integer32(RowStatus.active) // row status 170 | }; 171 | MOTableRow row = communityMIB.getSnmpCommunityEntry().createRow( 172 | new OctetString("public2public").toSubIndex(true), com2sec); 173 | communityMIB.getSnmpCommunityEntry().addRow(row); 174 | } 175 | 176 | public static void main(String[] args) throws IOException, InterruptedException { 177 | Agent agent = new Agent("0.0.0.0/2001"); 178 | agent.start(); 179 | while(true) { 180 | System.out.println("Agent running..."); 181 | Thread.sleep(5000); 182 | } 183 | } 184 | } 185 | -------------------------------------------------------------------------------- /src/main/java/com/jayway/snmpblogg/MOScalarFactory.java: -------------------------------------------------------------------------------- 1 | package com.jayway.snmpblogg; 2 | 3 | import org.snmp4j.agent.mo.MOAccessImpl; 4 | import org.snmp4j.agent.mo.MOScalar; 5 | import org.snmp4j.smi.OID; 6 | import org.snmp4j.smi.OctetString; 7 | import org.snmp4j.smi.Variable; 8 | 9 | public class MOScalarFactory { 10 | 11 | public static MOScalar createReadOnly(OID oid,Object value ){ 12 | return new MOScalar(oid, 13 | MOAccessImpl.ACCESS_READ_ONLY, 14 | getVariable(value)); 15 | } 16 | 17 | private static Variable getVariable(Object value) { 18 | if(value instanceof String) { 19 | return new OctetString((String)value); 20 | } 21 | throw new IllegalArgumentException("Unmanaged Type: " + value.getClass()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/jayway/snmpblogg/MOTableBuilder.java: -------------------------------------------------------------------------------- 1 | package com.jayway.snmpblogg; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.snmp4j.agent.MOAccess; 7 | import org.snmp4j.agent.mo.DefaultMOMutableRow2PC; 8 | import org.snmp4j.agent.mo.DefaultMOTable; 9 | import org.snmp4j.agent.mo.MOColumn; 10 | import org.snmp4j.agent.mo.MOMutableTableModel; 11 | import org.snmp4j.agent.mo.MOTable; 12 | import org.snmp4j.agent.mo.MOTableIndex; 13 | import org.snmp4j.agent.mo.MOTableSubIndex; 14 | import org.snmp4j.smi.OID; 15 | import org.snmp4j.smi.SMIConstants; 16 | import org.snmp4j.smi.Variable; 17 | 18 | 19 | /** 20 | *

Utility class for adding dynamic data into an {@link MOTable}

21 | * 22 | * 23 |

 24 |  MOTableBuilder builder = new MOTableBuilder(new OID(".1.3.6.1.2.1.2.2.1"))
 25 | 	.addColumnType(SMIConstants.SYNTAX_INTEGER,MOAccessImpl.ACCESS_READ_ONLY)
 26 | 	.addColumnType(SMIConstants.SYNTAX_OCTET_STRING,MOAccessImpl.ACCESS_READ_ONLY);
 27 | 	for(MyObject o: myObjects) {	
 28 | 		builder.addRowValue(new Integer32(o.getId()))
 29 | 		.addRowValue(new OctetString(o.getName()));
 30 | 	}
 31 | MOTable table = builder.build();
 32 |  
 33 |  
 34 |  * @author johanrask
 35 |  *
 36 |  */
 37 | public class MOTableBuilder {
 38 | 
 39 | 	private MOTableSubIndex[] subIndexes = new MOTableSubIndex[] { new MOTableSubIndex(
 40 | 			SMIConstants.SYNTAX_INTEGER) };
 41 | 	private MOTableIndex indexDef = new MOTableIndex(subIndexes, false);
 42 | 
 43 | 	private final List columns = new ArrayList();
 44 | 	private final List tableRows = new ArrayList();
 45 | 	private int currentRow = 0;
 46 | 	private int currentCol = 0;
 47 | 
 48 | 	private OID tableRootOid;
 49 | 
 50 | 	private int colTypeCnt = 0;
 51 | 
 52 | 	
 53 | 	/**
 54 | 	 * Specified oid is the root oid of this table
 55 | 	 */
 56 | 	public MOTableBuilder(OID oid) {
 57 | 		this.tableRootOid = oid;
 58 | 	}
 59 | 
 60 | 	/**
 61 | 	 * Adds all column types {@link MOColumn} to this table.
 62 | 	 * Important to understand that you must add all types here before
 63 | 	 * adding any row values
 64 | 	 * 
 65 | 	 * @param syntax use {@link SMIConstants}
 66 | 	 * @param access
 67 | 	 * @return
 68 | 	 */
 69 | 	public MOTableBuilder addColumnType(int syntax, MOAccess access) {
 70 | 		colTypeCnt++;
 71 | 		columns.add(new MOColumn(colTypeCnt, syntax, access));
 72 | 		return this;
 73 | 	}
 74 | 
 75 | 	
 76 | 	public MOTableBuilder addRowValue(Variable variable) {
 77 | 		if (tableRows.size() == currentRow) {
 78 | 			tableRows.add(new Variable[columns.size()]);
 79 | 		}
 80 | 		tableRows.get(currentRow)[currentCol] = variable;
 81 | 		currentCol++;
 82 | 		if (currentCol >= columns.size()) {
 83 | 			currentRow++;
 84 | 			currentCol = 0;
 85 | 		}
 86 | 		return this;
 87 | 	}
 88 | 
 89 | 	public MOTable build() {
 90 | 		DefaultMOTable ifTable = new DefaultMOTable(tableRootOid, indexDef,
 91 | 				columns.toArray(new MOColumn[0]));
 92 | 		MOMutableTableModel model = (MOMutableTableModel) ifTable.getModel();
 93 | 		int i = 1;
 94 | 		
 95 | 		for (Variable[] variables : tableRows) {
 96 | 			model.addRow(new DefaultMOMutableRow2PC(new OID(String.valueOf(i)),
 97 | 					variables));
 98 | 			i++;
 99 | 		}
100 | 		ifTable.setVolatile(true);
101 | 		return ifTable;
102 | 	}
103 | }
104 | 


--------------------------------------------------------------------------------
/src/main/java/com/jayway/snmpblogg/SimpleSnmpClient.java:
--------------------------------------------------------------------------------
  1 | package com.jayway.snmpblogg;
  2 | 
  3 | import java.io.IOException;
  4 | import java.util.ArrayList;
  5 | import java.util.List;
  6 | 
  7 | import org.snmp4j.CommunityTarget;
  8 | import org.snmp4j.PDU;
  9 | import org.snmp4j.Snmp;
 10 | import org.snmp4j.Target;
 11 | import org.snmp4j.TransportMapping;
 12 | import org.snmp4j.event.ResponseEvent;
 13 | import org.snmp4j.event.ResponseListener;
 14 | import org.snmp4j.mp.SnmpConstants;
 15 | import org.snmp4j.smi.Address;
 16 | import org.snmp4j.smi.GenericAddress;
 17 | import org.snmp4j.smi.OID;
 18 | import org.snmp4j.smi.OctetString;
 19 | import org.snmp4j.smi.VariableBinding;
 20 | import org.snmp4j.transport.DefaultUdpTransportMapping;
 21 | import org.snmp4j.util.DefaultPDUFactory;
 22 | import org.snmp4j.util.TableEvent;
 23 | import org.snmp4j.util.TableUtils;
 24 | 
 25 | 
 26 | /**
 27 |  * Simplest client possible
 28 |  * 
 29 |  * @author johanrask
 30 |  *
 31 |  */
 32 | public class SimpleSnmpClient {
 33 | 
 34 | 	private String address;
 35 | 
 36 | 	private Snmp snmp;
 37 | 	
 38 | 
 39 | 	public SimpleSnmpClient(String address) {
 40 | 		super();
 41 | 		this.address = address;
 42 | 		try {
 43 | 			start();
 44 | 		} catch (IOException e) {
 45 | 			throw new RuntimeException(e);
 46 | 		}
 47 | 	}
 48 | 
 49 | 	// Since snmp4j relies on asynch req/resp we need a listener
 50 | 	// for responses which should be closed
 51 | 	public void stop() throws IOException {
 52 | 		snmp.close();
 53 | 	}
 54 | 
 55 | 	private void start() throws IOException {
 56 | 		TransportMapping transport = new DefaultUdpTransportMapping();
 57 | 		snmp = new Snmp(transport);
 58 | 		// Do not forget this line!
 59 | 		transport.listen();
 60 | 	}
 61 | 	
 62 | 	public String getAsString(OID oid) throws IOException {
 63 | 		ResponseEvent event = get(new OID[]{oid});
 64 | 		return event.getResponse().get(0).getVariable().toString();
 65 | 	}
 66 | 	
 67 | 	
 68 | 	public void getAsString(OID oids,ResponseListener listener) {
 69 | 		try {
 70 | 			snmp.send(getPDU(new OID[]{oids}), getTarget(),null, listener);
 71 | 		} catch (IOException e) {
 72 | 			throw new RuntimeException(e);
 73 | 		}
 74 | 	}
 75 | 	
 76 | 	
 77 | 	private PDU getPDU(OID oids[]) {
 78 | 		PDU pdu = new PDU();
 79 | 		for (OID oid : oids) {
 80 | 			pdu.add(new VariableBinding(oid));
 81 | 		}
 82 | 	 	   
 83 | 		pdu.setType(PDU.GET);
 84 | 		return pdu;
 85 | 	}
 86 | 	
 87 | 	public ResponseEvent get(OID oids[]) throws IOException {
 88 | 	   ResponseEvent event = snmp.send(getPDU(oids), getTarget(), null);
 89 | 	   if(event != null) {
 90 | 		   return event;
 91 | 	   }
 92 | 	   throw new RuntimeException("GET timed out");	  
 93 | 	}
 94 | 	
 95 | 	private Target getTarget() {
 96 | 		Address targetAddress = GenericAddress.parse(address);
 97 | 		CommunityTarget target = new CommunityTarget();
 98 | 		target.setCommunity(new OctetString("public"));
 99 | 		target.setAddress(targetAddress);
100 | 		target.setRetries(2);
101 | 		target.setTimeout(1500);
102 | 		target.setVersion(SnmpConstants.version2c);
103 | 		return target;
104 | 	}
105 | 
106 | 	/**
107 | 	 * Normally this would return domain objects or something else than this...
108 | 	 */
109 | 	public List> getTableAsStrings(OID[] oids) {
110 | 		TableUtils tUtils = new TableUtils(snmp, new DefaultPDUFactory());
111 | 		
112 | 		@SuppressWarnings("unchecked") 
113 | 			List events = tUtils.getTable(getTarget(), oids, null, null);
114 | 		
115 | 		List> list = new ArrayList>();
116 | 		for (TableEvent event : events) {
117 | 			if(event.isError()) {
118 | 				throw new RuntimeException(event.getErrorMessage());
119 | 			}
120 | 			List strList = new ArrayList();
121 | 			list.add(strList);
122 | 			for(VariableBinding vb: event.getColumns()) {
123 | 				strList.add(vb.getVariable().toString());
124 | 			}
125 | 		}
126 | 		return list;
127 | 	}
128 | 	
129 | 	public static String extractSingleString(ResponseEvent event) {
130 | 		return event.getResponse().get(0).getVariable().toString();
131 | 	}
132 | }
133 | 


--------------------------------------------------------------------------------
/src/main/log4j.xml:
--------------------------------------------------------------------------------
 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 | 


--------------------------------------------------------------------------------
/src/main/resources/log4j.xml:
--------------------------------------------------------------------------------
 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 | 


--------------------------------------------------------------------------------
/src/test/java/com/jayway/snmpblogg/SnmpAgentAndClientTest.java:
--------------------------------------------------------------------------------
  1 | package com.jayway.snmpblogg;
  2 | 
  3 | import static java.util.concurrent.TimeUnit.SECONDS;
  4 | import static junit.framework.Assert.assertEquals;
  5 | import static org.hamcrest.Matchers.equalTo;
  6 | import static org.hamcrest.Matchers.is;
  7 | import static com.jayway.awaitility.Awaitility.*;
  8 | import java.io.IOException;
  9 | import java.util.List;
 10 | import java.util.concurrent.Callable;
 11 | 
 12 | import org.junit.AfterClass;
 13 | import org.junit.BeforeClass;
 14 | import org.junit.Test;
 15 | import org.snmp4j.agent.mo.MOAccessImpl;
 16 | import org.snmp4j.event.ResponseEvent;
 17 | import org.snmp4j.event.ResponseListener;
 18 | import org.snmp4j.smi.Gauge32;
 19 | import org.snmp4j.smi.Integer32;
 20 | import org.snmp4j.smi.OID;
 21 | import org.snmp4j.smi.OctetString;
 22 | import org.snmp4j.smi.SMIConstants;
 23 | 
 24 | import com.jayway.awaitility.Duration;
 25 | 
 26 | public class SnmpAgentAndClientTest {
 27 | 	
 28 | 	// These are both standard in RFC-1213
 29 | 	static final OID sysDescr = new OID(".1.3.6.1.2.1.1.1.0");
 30 | 	static final OID interfacesTable = new OID(".1.3.6.1.2.1.2.2.1");
 31 | 		
 32 | 	static Agent agent;
 33 | 	static SimpleSnmpClient client;
 34 | 	
 35 | 	@BeforeClass
 36 | 	public static void setUp() throws Exception {
 37 | 		agent = new Agent("0.0.0.0/2001");
 38 | 		agent.start();
 39 | 		
 40 | 		// Since BaseAgent registers some mibs by default we need to unregister
 41 | 		// one before we register our own sysDescr. Normally you would
 42 | 		// override that method and register the mibs that you need
 43 | 		agent.unregisterManagedObject(agent.getSnmpv2MIB());
 44 | 		
 45 | 		// Register a system description, use one from you product environment
 46 | 		// to test with
 47 | 		agent.registerManagedObject(
 48 | 				MOScalarFactory.createReadOnly(sysDescr,"MySystemDescr"));
 49 | 		
 50 | 		// Build a table. This example is taken from TestAgent and sets up
 51 | 		// two physical interfaces 
 52 | 		MOTableBuilder builder = new MOTableBuilder(interfacesTable)
 53 | 			.addColumnType(SMIConstants.SYNTAX_INTEGER,MOAccessImpl.ACCESS_READ_ONLY)
 54 | 			.addColumnType(SMIConstants.SYNTAX_OCTET_STRING,MOAccessImpl.ACCESS_READ_ONLY)
 55 | 			.addColumnType(SMIConstants.SYNTAX_INTEGER,MOAccessImpl.ACCESS_READ_ONLY)
 56 | 			.addColumnType(SMIConstants.SYNTAX_INTEGER,MOAccessImpl.ACCESS_READ_ONLY)
 57 | 			.addColumnType(SMIConstants.SYNTAX_GAUGE32,MOAccessImpl.ACCESS_READ_ONLY)
 58 | 			.addColumnType(SMIConstants.SYNTAX_OCTET_STRING,MOAccessImpl.ACCESS_READ_ONLY)
 59 | 			.addColumnType(SMIConstants.SYNTAX_INTEGER,MOAccessImpl.ACCESS_READ_ONLY)
 60 | 			.addColumnType(SMIConstants.SYNTAX_INTEGER,MOAccessImpl.ACCESS_READ_ONLY)
 61 | 			// Normally you would begin loop over you two domain objects here
 62 | 			.addRowValue(new Integer32(1))
 63 | 			.addRowValue(new OctetString("loopback"))
 64 | 			.addRowValue(new Integer32(24))
 65 | 			.addRowValue(new Integer32(1500))
 66 | 			.addRowValue(new Gauge32(10000000))
 67 | 			.addRowValue(new OctetString("00:00:00:00:01"))
 68 | 			.addRowValue(new Integer32(1500))
 69 | 			.addRowValue(new Integer32(1500))
 70 | 			//next row
 71 | 			.addRowValue(new Integer32(2))
 72 | 			.addRowValue(new OctetString("eth0"))
 73 | 			.addRowValue(new Integer32(24))
 74 | 			.addRowValue(new Integer32(1500))
 75 | 			.addRowValue(new Gauge32(10000000))
 76 | 			.addRowValue(new OctetString("00:00:00:00:02"))
 77 | 			.addRowValue(new Integer32(1500))
 78 | 			.addRowValue(new Integer32(1500));
 79 | 
 80 | 		agent.registerManagedObject(builder.build());
 81 | 		
 82 | 		// Setup the client to use our newly started agent
 83 | 		client = new SimpleSnmpClient("udp:127.0.0.1/2001");
 84 | 
 85 | 	}
 86 | 	
 87 | 	@AfterClass
 88 | 	public static void tearDown() throws Exception {
 89 | 		agent.stop();
 90 | 		client.stop();
 91 | 	}
 92 | 	
 93 |     
 94 | 	/**
 95 | 	 * Simply verifies that we get the same sysDescr as we have registered in our agent
 96 | 	 */
 97 | 	@Test
 98 |     public void verifySysDescr() throws IOException
 99 |     {
100 |     	assertEquals("MySystemDescr", client.getAsString(sysDescr));
101 |     }
102 | 
103 | 	
104 | 	/**
105 | 	 * Uses asynchronous fetch and test it with Awaitility.
106 | 	 */
107 | 	@Test
108 | 	public void verifySysDescrAsynch() throws Exception {
109 | 		final StringResponseListener listener = new StringResponseListener();
110 | 		client.getAsString(sysDescr, listener);
111 | 		await().until(callTo(listener).getValue(),equalTo("MySystemDescr"));
112 | 	}
113 | 	
114 | 	/**
115 | 	 * Verify that the table contents is ok.
116 | 	 */
117 | 	@Test
118 |     public void verifyTableContents() {
119 | 
120 | 		// You retreive a table by suppling the columns of the table that
121 | 		// you need, here we use column 2,6 and 8 so we do not verify the complete
122 | 		// table
123 | 		List> tableContents = client.getTableAsStrings(new OID[]{
124 |     			new OID(interfacesTable.toString() + ".2"),
125 |     			new OID(interfacesTable.toString() + ".6"),
126 |     			new OID(interfacesTable.toString() + ".8")});
127 | 		
128 | 		//and validate here
129 | 		assertEquals(2, tableContents.size());
130 | 		assertEquals(3, tableContents.get(0).size());
131 | 		assertEquals(3, tableContents.get(1).size());
132 | 
133 | 		// Row 1
134 | 		assertEquals("loopback", tableContents.get(0).get(0));
135 | 		assertEquals("00:00:00:00:01", tableContents.get(0).get(1));
136 | 		assertEquals("1500", tableContents.get(0).get(2));
137 | 		
138 | 		// Row 2
139 | 		assertEquals("eth0", tableContents.get(1).get(0));
140 | 		assertEquals("00:00:00:00:02", tableContents.get(1).get(1));
141 | 		assertEquals("1500", tableContents.get(1).get(2));
142 |     }
143 | 
144 | 	class StringResponseListener implements ResponseListener {
145 | 		
146 | 		private String value = null;
147 | 		
148 | 		@Override
149 | 		public void onResponse(ResponseEvent event) {
150 | 			System.out.println(event.getResponse());
151 | 			if(event.getResponse() != null) {
152 | 				value = SimpleSnmpClient.extractSingleString(event);
153 | 			}
154 | 		}
155 | 
156 | 		public String getValue() {
157 | 			System.out.println(value);
158 | 			return value;
159 | 		}
160 | 		
161 | 	}
162 | }
163 | 
164 | 
165 | 


--------------------------------------------------------------------------------