1 package com.dwipal; 2 3 import java.util.*; 4 import java.net.*; 5 import org.snmp4j.*; 6 import org.snmp4j.event.*; 7 import org.snmp4j.transport.*; 8 import org.snmp4j.smi.*; 9 import org.snmp4j.mp.*; 10 11 public class DwSnmpMibBrowserFunctions { 12 private DwSnmpMibOutputHandler output = null; 13 private DwSnmpOidSupport oidSupport = null; 14 15 private String agentIP = "192.168.2.9"; 16 private int agentPort = 161; 17 private String setCommunity = "public"; 18 private String getCommunity = "public"; 19 20 private SnmpLib m_snmpLib; 21 22 public DwSnmpMibBrowserFunctions() { 23 } 24 25 public void setOutput(DwSnmpMibOutputHandler output) { 26 this.output = output; 27 } 28 29 public void setOidSupport(DwSnmpOidSupport oidSupport) { 30 this.oidSupport = oidSupport; 31 } 32 33 public void setIP(String s) { 34 m_snmpLib=null; 35 this.agentIP = s; 36 } 37 38 public void setPort(int p) { 39 m_snmpLib=null; 40 this.agentPort = p; 41 } 42 43 public void setCommunity(String get, String set) { 44 m_snmpLib=null; 45 this.getCommunity = get; 46 this.setCommunity = set; 47 } 48 49 public String getIP() { 50 return agentIP; 51 } 52 53 public int getPort() { 54 return agentPort; 55 } 56 57 public String getReadCommunity() { 58 return getCommunity; 59 } 60 61 public String getWriteCommunity() { 62 return setCommunity; 63 } 64 65 void destroySession() { 66 getSnmpLib().destroySession(); 67 } 68 69 public void outputRecord(SnmpOidValuePair oidval) { 70 try { 71 if (oidSupport != null) { 72 outputText("Oid : " + oidval.oid + " (" + 73 oidSupport.resolveOidName(oidval.oid) + " )"); 74 } 75 else { 76 outputText("Oid : " + oidval.oid); 77 } 78 } 79 catch (Exception e) { 80 outputError("Cannot resolve Name from OID..\n" + e.toString()); 81 } 82 outputText("Value: " + oidval.value_str); 83 84 } 85 86 void outputError(String s) { 87 try { 88 output.printError(s); 89 } 90 catch (Exception e) { 91 System.out.println(s); 92 } 93 94 } 95 96 void outputText(String s) { 97 try { 98 output.println(s); 99 } 100 catch (Exception e) { 101 System.out.println(s); 102 } 103 } 104 105 public void snmpRequestGet(String strVar, String strTo) { 106 try { 107 getSnmpLib().snmpWalk(strVar); 108 } catch (Exception e) { 109 outputError("\nError in executing GET request : \n" + e.toString()); 110 e.printStackTrace(); 111 } 112 } 113 114 public DwSnmpRequest snmpRequestGet(String strVar) { 115 snmpRequestGet(strVar, null); 116 return null; 117 } 118 119 String processSetRequest(DwSnmpMibRecord setRec, String oid, String setVal) { 120 try { 121 getSnmpLib().snmpSetValue(oid, setRec.getSyntaxID(), setVal); 122 } catch (Exception e) { 123 outputError("\nError in executing SET request : \n" + e.toString()); 124 e.printStackTrace(); 125 } 126 return ""; 127 } 128 129 private SnmpLib getSnmpLib() { 130 if(m_snmpLib==null) { 131 m_snmpLib = new SnmpLib(); 132 m_snmpLib.setHost(getIP()); 133 m_snmpLib.setPort(getPort()); 134 m_snmpLib.setCommunity(getReadCommunity(), getWriteCommunity()); 135 136 m_snmpLib.setSnmpResponseHandler(new ISnmpResponseHandler() { 137 public void responseReceived(SnmpOidValuePair resp_values) { 138 outputRecord(resp_values); 139 } 140 141 public void requestStats(int totalRequests, int totalResponses, 142 long timeInMillis) { 143 if (totalResponses == 0) totalResponses = 1; 144 outputText("\nReceived " + (totalResponses - 1) + " record(s) in " + 145 timeInMillis + " milliseconds."); 146 } 147 }); 148 } 149 return m_snmpLib; 150 } 151 } 152
| Popular Tags
|