1 20 package org.jboss.jmx.adaptor.snmp.agent; 21 22 import java.net.InetAddress ; 23 import java.net.SocketException ; 24 25 import org.jboss.logging.Logger; 26 import org.opennms.protocols.snmp.SnmpHandler; 27 import org.opennms.protocols.snmp.SnmpParameters; 28 import org.opennms.protocols.snmp.SnmpPduPacket; 29 import org.opennms.protocols.snmp.SnmpPeer; 30 import org.opennms.protocols.snmp.SnmpSMI; 31 import org.opennms.protocols.snmp.SnmpSession; 32 import org.opennms.protocols.snmp.SnmpSyntax; 33 34 43 class ManagerRecord implements SnmpHandler 44 { 45 46 private static final Logger log = Logger.getLogger(ManagerRecord.class); 47 48 49 private int retries = 10; 50 51 52 private int timeout = 5000; 53 54 55 private InetAddress address; 56 57 58 private int port; 59 60 61 private InetAddress localAddress; 62 63 64 private int localPort; 65 66 67 private int version; 68 69 70 private final String readCommunity = "public"; 71 72 73 private transient SnmpSession session; 74 75 84 public ManagerRecord(InetAddress address, int port, 85 InetAddress localAddress, int localPort, int version) 86 throws BadSnmpVersionException 87 { 88 this.address = address; 89 this.port = port; 90 this.localAddress = localAddress; 91 this.localPort = localPort; 92 93 switch (version) { 94 case SnmpAgentService.SNMPV1: 95 case SnmpAgentService.SNMPV2: 96 this.version = version; 97 break; 98 99 default: 100 throw new BadSnmpVersionException("Bad SNMP Version: " + version); 101 } 102 } 103 104 107 public InetAddress getAddress() 108 { 109 return this.address; 110 } 111 112 115 public int getPort() 116 { 117 return this.port; 118 } 119 120 123 public InetAddress getLocalAddress() 124 { 125 return this.localAddress; 126 } 127 128 131 public int getLocalPort() 132 { 133 return this.localPort; 134 } 135 136 139 public int getVersion() 140 { 141 return this.version; 142 } 143 144 public void openSession() 145 throws SocketException 146 { 147 SnmpPeer peer = new SnmpPeer(this.address, this.port, this.localAddress, this.localPort); 149 peer.setRetries(this.retries); 150 peer.setTimeout(this.timeout); 151 152 SnmpParameters parameters = peer.getParameters(); 153 154 switch(this.version) { 155 case SnmpAgentService.SNMPV1: 156 parameters.setVersion(SnmpSMI.SNMPV1); 157 break; 158 159 case SnmpAgentService.SNMPV2: 160 parameters.setVersion(SnmpSMI.SNMPV2); 161 break; 162 default: 163 parameters.setVersion(SnmpSMI.SNMPV1); 164 } 165 166 parameters.setReadCommunity(this.readCommunity); 167 peer.setParameters(parameters); 168 169 this.session = new SnmpSession(peer); 170 this.session.setDefaultHandler(this); 171 } 172 173 176 public void closeSession() 177 { 178 this.session.close(); 179 } 180 181 184 public SnmpSession getSession() 185 { 186 return this.session; 187 } 188 189 195 public boolean equals(Object o) 196 { 197 if (!(o instanceof ManagerRecord)) 198 return false; 199 200 ManagerRecord other = (ManagerRecord)o; 201 202 return (this.port == other.port && 203 this.address.equals(other.address) && 204 this.version == other.version); 205 } 206 207 210 public int hashCode() 211 { 212 return toString().hashCode(); 213 } 214 215 218 public String toString() 219 { 220 return new String (this.address + ":" + this.port + 221 " (" + this.version + ")" ); 222 } 223 224 227 public void snmpInternalError(SnmpSession session, int err, SnmpSyntax pdu) 228 { 229 log.error("ManagerRecord::snmpInternalError, code: " + err); 230 } 231 232 public void snmpTimeoutError(SnmpSession session, SnmpSyntax pdu) 233 { 234 log.error("ManagerRecord::snmpTimeoutError"); 235 } 236 237 public void snmpReceivedPdu(SnmpSession session, int cmd, SnmpPduPacket pdu) 238 { 239 log.error("ManagerRecord::snmpReceivedPdu"); 240 } 241 242 } | Popular Tags |