1 2 23 24 package net.fenyo.gnetwatch; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import java.net.*; 30 import java.io.*; 31 32 import org.snmp4j.*; 33 import org.snmp4j.smi.*; 34 import org.snmp4j.mp.*; 35 import org.snmp4j.transport.*; 36 import org.snmp4j.util.*; 37 import org.snmp4j.event.*; 38 import org.snmp4j.security.*; 39 40 45 46 public class SNMPQuerier { 49 private static Log log = LogFactory.getLog(SNMPQuerier.class); 50 51 private final SNMPManager snmp_manager; 52 private Target snmp_target; 53 54 private final InetAddress address; 55 56 private int version = 0; private int sec = SecurityLevel.AUTH_PRIV; 58 private int retries = 3; 59 private int timeout = 1500; private int port = 161; 61 private String community = "public"; 62 private String username = ""; 63 private String password_auth = ""; 64 private String password_priv = ""; 65 private int pdu_max_size = 1000; 66 67 private boolean snmp_capable = false; 68 private String last_sysdescr = null; 69 70 73 public interface QuerierListener { 74 79 public void onResponse(final ResponseEvent event); 80 81 86 public void onTimeout(final ResponseEvent event); 87 } 88 89 94 protected SNMPQuerier(final InetAddress address, final SNMPManager snmp_manager) { 95 this.snmp_manager = snmp_manager; 96 this.address = address; 97 parametersHaveChanged(); 98 } 99 100 105 public InetAddress getAddress() { 106 return address; 107 } 108 109 114 public boolean isSNMPCapable() { 115 return snmp_capable; 116 } 117 118 123 public String getLastSysdescr() { 124 return last_sysdescr; 125 } 126 127 132 public void update() { 133 parametersHaveChanged(); 134 } 135 136 141 private final void parametersHaveChanged() { 143 if (version == 0 || version == 1) { 144 snmp_target = new CommunityTarget(); 145 snmp_target.setAddress(new UdpAddress(address, port)); 146 snmp_target.setVersion((version == 0) ? SnmpConstants.version1 : SnmpConstants.version2c); 147 ((CommunityTarget) snmp_target).setCommunity(new OctetString(community)); 148 149 } else { 150 try { 151 UsmUserEntry entry = snmp_manager.getSNMP().getUSM().getUserTable().getUser(new OctetString(username)); 152 if (entry != null && snmp_manager.getSNMP().getUSM().removeUser(entry.getEngineID(), entry.getUserName()) == null) 153 log.error("USM user not found"); 154 snmp_manager.getSNMP().getUSM().addUser(new OctetString(username), 155 new UsmUser(new OctetString(username), 156 sec != SecurityLevel.NOAUTH_NOPRIV ? AuthMD5.ID : null, 157 sec != SecurityLevel.NOAUTH_NOPRIV ? new OctetString(password_auth) : null, 158 sec == SecurityLevel.AUTH_PRIV ? PrivDES.ID : null, 159 sec == SecurityLevel.AUTH_PRIV ? new OctetString(password_priv) : null)); 160 snmp_target = new UserTarget(new UdpAddress(address, port), new OctetString(username), 161 new byte [] {}, sec); 162 snmp_target.setVersion(SnmpConstants.version3); 163 } catch (final IllegalArgumentException ex) { 164 log.error("Exception", ex); 165 } 166 } 167 168 snmp_target.setRetries(retries); 169 snmp_target.setTimeout(timeout); 170 snmp_target.setMaxSizeRequestPDU(pdu_max_size); 171 } 172 173 178 public int getVersion() { 179 return version; 180 } 181 182 187 public int getSec() { 188 return sec; 189 } 190 191 196 public int getRetries() { 197 return retries; 198 } 199 200 205 public int getTimeout() { 206 return timeout; 207 } 208 209 214 public int getPort() { 215 return port; 216 } 217 218 223 public String getCommunity() { 224 return community; 225 } 226 227 232 public String getUsername() { 233 return username; 234 } 235 236 241 public String getPasswordAuth() { 242 return password_auth; 243 } 244 245 250 public String getPasswordPriv() { 251 return password_priv; 252 } 253 254 259 public int getPDUMaxSize() { 260 return pdu_max_size; 261 } 262 263 268 public void setVersion(final int version) { 269 this.version = version; 270 } 271 272 277 public void setSec(final int sec) { 278 this.sec = sec; 279 } 280 281 286 public void setRetries(final int retries) { 287 this.retries = retries; 288 } 289 290 295 public void setTimeout(final int timeout) { 296 this.timeout = timeout; 297 } 298 299 304 public void setPort(final int port) { 305 this.port = port; 306 } 307 308 313 public void setCommunity(final String community) { 314 this.community = community; 315 } 316 317 322 public void setUsername(final String username) { 323 this.username = username; 324 } 325 326 331 public void setPasswordAuth(final String password_auth) { 332 this.password_auth = password_auth; 333 } 334 335 340 public void setPasswordPriv(final String password_priv) { 341 this.password_priv = password_priv; 342 } 343 344 349 public void setPDUMaxSize(final int pdu_max_size) { 350 this.pdu_max_size = pdu_max_size; 351 } 352 353 358 private Snmp getSNMP() { 359 return snmp_manager.getSNMP(); 360 } 361 362 367 private PDU getPDU() { 368 if (version == 0) return new PDUv1(); 369 if (version == 1) return new PDU(); 370 return new ScopedPDU(); 371 } 372 373 378 public String getSysDescr() { 381 final PDU pdu = getPDU(); 382 pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.1"))); 383 pdu.setType(PDU.GETNEXT); 384 try { 385 final ResponseEvent response = getSNMP().send(pdu, snmp_target); 386 387 if (response.getResponse() != null) { 388 389 if (response.getResponse().get(0) != null && 390 response.getResponse().get(0).getVariable() != null) { 391 snmp_capable = true; 392 last_sysdescr = response.getResponse().get(0).getVariable().toString(); 393 } 394 395 return response.getResponse().toString(); 396 } 397 } catch (final IOException ex) { 398 log.error("Exception", ex); 399 } 400 return null; 401 } 402 403 408 public void getSysDescr(final QuerierListener listener) { 409 final PDU pdu = getPDU(); 410 pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.1"))); 411 pdu.setType(PDU.GETNEXT); 412 try { 413 getSNMP().send(pdu, snmp_target, null, new ResponseListener() { 414 private Boolean invoked = false; 415 416 public void onResponse(final ResponseEvent event) { 417 if (event.getResponse() == null) { 418 synchronized(invoked) { 419 if (invoked == true) return; 420 invoked = true; 421 } 422 listener.onTimeout(event); 423 } 424 else { 425 synchronized(invoked) { 426 if (invoked == true) return; 427 invoked = true; 428 } 429 ((Snmp) event.getSource()).cancel(event.getRequest(), this); 430 431 snmp_capable = true; 432 if (event.getResponse() != null && event.getResponse().get(0) != null && 433 event.getResponse().get(0).getVariable() != null) 434 last_sysdescr = event.getResponse().get(0).getVariable().toString(); 435 436 listener.onResponse(event); 437 } 438 } 439 }); 440 } catch (final IOException ex) { 441 log.error("Exception", ex); 442 } 443 } 444 445 450 public java.util.List <TableEvent> getInterfaces() { 451 final PDUFactory pdu_factory = new DefaultPDUFactory(PDU.GETNEXT); 452 453 TableUtils table_utils = new TableUtils(getSNMP(), pdu_factory); 454 455 final OID[] cols = new OID[] { 456 new OID("1.3.6.1.2.1.2.2.1.1"), new OID("1.3.6.1.2.1.2.2.1.2"), new OID("1.3.6.1.2.1.2.2.1.3"), new OID("1.3.6.1.2.1.2.2.1.4"), new OID("1.3.6.1.2.1.2.2.1.5"), new OID("1.3.6.1.2.1.2.2.1.6"), new OID("1.3.6.1.2.1.2.2.1.7"), new OID("1.3.6.1.2.1.2.2.1.8"), new OID("1.3.6.1.2.1.2.2.1.10"), new OID("1.3.6.1.2.1.2.2.1.16"), }; 467 468 java.util.List <TableEvent> table = table_utils.getTable(snmp_target, cols, null, null); 469 if (table != null && table.get(0) != null && table.get(0).getColumns() != null) snmp_capable = true; 470 return table; 471 } 472 } 473 | Popular Tags |