001: package com.dwipal;
002:
003: import java.util.*;
004: import org.snmp4j.*;
005: import org.snmp4j.event.*;
006: import org.snmp4j.mp.*;
007: import org.snmp4j.smi.*;
008: import org.snmp4j.transport.*;
009:
010: public class SnmpLib implements ISnmpLib {
011:
012: private String m_host;
013: private int m_port;
014: private String m_readCommunity;
015: private String m_writeCommunity;
016: private ISnmpResponseHandler m_snmpResponseHandler;
017: private boolean m_sessionDestroyed;
018: private CommunityTarget m_target;
019:
020: public SnmpLib() {
021: m_target = null;
022: }
023:
024: public String getHost() {
025: return m_host;
026: }
027:
028: public int getPort() {
029: return m_port;
030: }
031:
032: public String getReadCommunity() {
033: return m_readCommunity;
034: }
035:
036: public String getWriteCommunity() {
037: return m_writeCommunity;
038: }
039:
040: public void setCommunity(String readCommunity, String writeCommunity) {
041: m_readCommunity = readCommunity;
042: m_writeCommunity = writeCommunity;
043: }
044:
045: public void setHost(String host) {
046: m_target = null;
047: m_host = host;
048: }
049:
050: public void setPort(int port) {
051: m_target = null;
052: m_port = port;
053: }
054:
055: public void setSnmpResponseHandler(
056: ISnmpResponseHandler snmpResponseHandler) {
057: m_snmpResponseHandler = snmpResponseHandler;
058: }
059:
060: public ISnmpResponseHandler getSnmpResponseHandler() {
061: return m_snmpResponseHandler;
062: }
063:
064: public void destroySession() {
065: m_sessionDestroyed = true;
066: }
067:
068: private Target getTarget(String strCommunity) {
069: if (m_target == null) {
070: Address addr = GenericAddress.parse("udp:" + getHost()
071: + "/" + getPort());
072: m_target = new CommunityTarget();
073: m_target.setCommunity(new OctetString(strCommunity));
074: m_target.setAddress(addr);
075: m_target.setVersion(SnmpConstants.version1);
076: m_target.setRetries(3);
077: }
078: return m_target;
079: }
080:
081: public void snmpWalk(String oidFrom, String oidTo)
082: throws SnmpException {
083: snmpWalk(oidFrom);
084: }
085:
086: public void snmpWalk(String oidFrom) throws SnmpException {
087: PDU request = new PDU();
088: request.setType(PDU.GETNEXT);
089: request.add(new VariableBinding(new OID(oidFrom)));
090: request.setNonRepeaters(0);
091: OID rootOID = request.get(0).getOid();
092: PDU response = null;
093:
094: int objects = 0;
095: int requests = 0;
096: long startTime = System.currentTimeMillis();
097:
098: try {
099: Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
100: snmp.listen();
101: m_sessionDestroyed = false;
102: do {
103: requests++;
104: ResponseEvent responseEvent = snmp.send(request,
105: getTarget(getReadCommunity()));
106: response = responseEvent.getResponse();
107: if (response != null) {
108: objects += response.size();
109: }
110: } while (!processWalk(response, request, rootOID)
111: && !m_sessionDestroyed);
112:
113: } catch (SnmpException e) {
114: throw e;
115: } catch (Exception e) {
116: e.printStackTrace();
117: throw new SnmpException(e.getMessage());
118: }
119: if (m_snmpResponseHandler != null) {
120: m_snmpResponseHandler.requestStats(requests, objects,
121: System.currentTimeMillis() - startTime);
122: }
123: }
124:
125: private boolean processWalk(PDU response, PDU request, OID rootOID)
126: throws SnmpException {
127: if ((response == null) || (response.getErrorStatus() != 0)
128: || (response.getType() == PDU.REPORT)) {
129: return true;
130: }
131: boolean finished = false;
132: OID lastOID = request.get(0).getOid();
133: for (int i = 0; (!finished) && (i < response.size()); i++) {
134: VariableBinding vb = response.get(i);
135: if ((vb.getOid() == null)
136: || (vb.getOid().size() < rootOID.size())
137: || (rootOID.leftMostCompare(rootOID.size(), vb
138: .getOid()) != 0)) {
139: finished = true;
140: } else if (Null.isExceptionSyntax(vb.getVariable()
141: .getSyntax())) {
142: outputResponse(vb);
143: finished = true;
144: } else if (vb.getOid().compareTo(lastOID) <= 0) {
145: throw new SnmpException(
146: "Variable received is not lexicographic successor of requested one:"
147: + vb.toString() + " <= " + lastOID);
148: } else {
149: outputResponse(vb);
150: lastOID = vb.getOid();
151: }
152: }
153: if (response.size() == 0) {
154: finished = true;
155: }
156: if (!finished) {
157: VariableBinding next = response.get(response.size() - 1);
158: next.setVariable(new Null());
159: request.set(0, next);
160: request.setRequestID(new Integer32(0));
161: }
162: return finished;
163: }
164:
165: private SnmpOidValuePair outputResponse(VariableBinding vb) {
166: SnmpOidValuePair oidval = new SnmpOidValuePair();
167: oidval.oid = vb.getOid().toString();
168: oidval.value_str = vb.getVariable().toString();
169: if (m_snmpResponseHandler != null) {
170: m_snmpResponseHandler.responseReceived(oidval);
171: }
172: return oidval;
173: }
174:
175: void snmpSetValue(String oid, int syntax, String value)
176: throws SnmpException {
177: VariableBinding varbind = getVarBindForSetRequest(oid, syntax,
178: value);
179:
180: PDU request = new PDU();
181: request.setType(PDU.SET);
182: request.add(varbind);
183: PDU response = null;
184:
185: long startTime = System.currentTimeMillis();
186:
187: try {
188: Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
189: snmp.listen();
190: ResponseEvent responseEvent = snmp.send(request,
191: getTarget(getWriteCommunity()));
192: response = responseEvent.getResponse();
193: System.out.println(response);
194: } catch (Exception e) {
195: e.printStackTrace();
196: throw new SnmpException(e.getMessage());
197: }
198: }
199:
200: VariableBinding getVarBindForSetRequest(String oid, int type,
201: String value) {
202: VariableBinding vb = new VariableBinding(new OID(oid));
203:
204: if (value != null) {
205: Variable variable;
206: switch (type) {
207: case DwSnmpMibRecord.VALUE_TYPE_INTEGER32:
208: variable = new Integer32(Integer.parseInt(value));
209: break;
210: case DwSnmpMibRecord.VALUE_TYPE_UNSIGNED_INTEGER32:
211: variable = new UnsignedInteger32(Long.parseLong(value));
212: break;
213: case DwSnmpMibRecord.VALUE_TYPE_OCTET_STRING:
214: variable = new OctetString(value);
215: break;
216: case DwSnmpMibRecord.VALUE_TYPE_NULL:
217: variable = new Null();
218: break;
219: case DwSnmpMibRecord.VALUE_TYPE_OID:
220: variable = new OID(value);
221: break;
222: case DwSnmpMibRecord.VALUE_TYPE_TIMETICKS:
223: variable = new TimeTicks(Long.parseLong(value));
224: break;
225: case DwSnmpMibRecord.VALUE_TYPE_IP_ADDRESS:
226: variable = new IpAddress(value);
227: break;
228: default:
229: throw new IllegalArgumentException("Variable type "
230: + type + " not supported");
231: }
232: vb.setVariable(variable);
233: }
234: return vb;
235: }
236:
237: }
|