001: package com.dwipal;
002:
003: import java.util.*;
004: import java.net.*;
005: import org.snmp4j.*;
006: import org.snmp4j.event.*;
007: import org.snmp4j.transport.*;
008: import org.snmp4j.smi.*;
009: import org.snmp4j.mp.*;
010:
011: public class DwSnmpMibBrowserFunctions {
012: private DwSnmpMibOutputHandler output = null;
013: private DwSnmpOidSupport oidSupport = null;
014:
015: private String agentIP = "192.168.2.9";
016: private int agentPort = 161;
017: private String setCommunity = "public";
018: private String getCommunity = "public";
019:
020: private SnmpLib m_snmpLib;
021:
022: public DwSnmpMibBrowserFunctions() {
023: }
024:
025: public void setOutput(DwSnmpMibOutputHandler output) {
026: this .output = output;
027: }
028:
029: public void setOidSupport(DwSnmpOidSupport oidSupport) {
030: this .oidSupport = oidSupport;
031: }
032:
033: public void setIP(String s) {
034: m_snmpLib = null;
035: this .agentIP = s;
036: }
037:
038: public void setPort(int p) {
039: m_snmpLib = null;
040: this .agentPort = p;
041: }
042:
043: public void setCommunity(String get, String set) {
044: m_snmpLib = null;
045: this .getCommunity = get;
046: this .setCommunity = set;
047: }
048:
049: public String getIP() {
050: return agentIP;
051: }
052:
053: public int getPort() {
054: return agentPort;
055: }
056:
057: public String getReadCommunity() {
058: return getCommunity;
059: }
060:
061: public String getWriteCommunity() {
062: return setCommunity;
063: }
064:
065: void destroySession() {
066: getSnmpLib().destroySession();
067: }
068:
069: public void outputRecord(SnmpOidValuePair oidval) {
070: try {
071: if (oidSupport != null) {
072: outputText("Oid : " + oidval.oid + " ("
073: + oidSupport.resolveOidName(oidval.oid) + " )");
074: } else {
075: outputText("Oid : " + oidval.oid);
076: }
077: } catch (Exception e) {
078: outputError("Cannot resolve Name from OID..\n"
079: + e.toString());
080: }
081: outputText("Value: " + oidval.value_str);
082:
083: }
084:
085: void outputError(String s) {
086: try {
087: output.printError(s);
088: } catch (Exception e) {
089: System.out.println(s);
090: }
091:
092: }
093:
094: void outputText(String s) {
095: try {
096: output.println(s);
097: } catch (Exception e) {
098: System.out.println(s);
099: }
100: }
101:
102: public void snmpRequestGet(String strVar, String strTo) {
103: try {
104: getSnmpLib().snmpWalk(strVar);
105: } catch (Exception e) {
106: outputError("\nError in executing GET request : \n"
107: + e.toString());
108: e.printStackTrace();
109: }
110: }
111:
112: public DwSnmpRequest snmpRequestGet(String strVar) {
113: snmpRequestGet(strVar, null);
114: return null;
115: }
116:
117: String processSetRequest(DwSnmpMibRecord setRec, String oid,
118: String setVal) {
119: try {
120: getSnmpLib()
121: .snmpSetValue(oid, setRec.getSyntaxID(), setVal);
122: } catch (Exception e) {
123: outputError("\nError in executing SET request : \n"
124: + e.toString());
125: e.printStackTrace();
126: }
127: return "";
128: }
129:
130: private SnmpLib getSnmpLib() {
131: if (m_snmpLib == null) {
132: m_snmpLib = new SnmpLib();
133: m_snmpLib.setHost(getIP());
134: m_snmpLib.setPort(getPort());
135: m_snmpLib.setCommunity(getReadCommunity(),
136: getWriteCommunity());
137:
138: m_snmpLib
139: .setSnmpResponseHandler(new ISnmpResponseHandler() {
140: public void responseReceived(
141: SnmpOidValuePair resp_values) {
142: outputRecord(resp_values);
143: }
144:
145: public void requestStats(int totalRequests,
146: int totalResponses, long timeInMillis) {
147: if (totalResponses == 0)
148: totalResponses = 1;
149: outputText("\nReceived "
150: + (totalResponses - 1)
151: + " record(s) in " + timeInMillis
152: + " milliseconds.");
153: }
154: });
155: }
156: return m_snmpLib;
157: }
158: }
|