Exemplo n.º 1
0
  public static String getPhysAddrFromValue(SnmpValue value) {
    String hexString = value.toHexString();
    if (hexString.length() != 12) {
      return null;
    }

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 12; i += 2) {
      sb.append(hexString.substring(i, i + 2));
      if (i < 10) {
        sb.append(':');
      }
    }

    return sb.toString().toUpperCase();
  }
 private String getPhysAddr() {
   final SnmpValue value = getValue(IF_PHYS_ADDR);
   String hexString = value == null ? null : value.toHexString();
   String displayString = value == null ? null : value.toDisplayString();
   // See ifTableEntry: NMS-4902 (revision cee964fe979e6465aeb4e2efd4772e50ebc54831)
   try {
     if (hexString != null && hexString.length() == 12) {
       // If the hex string is 12 characters long, than the agent is kinda weird and
       // is returning the value as a raw binary value that is 6 bytes in length.
       // But that's OK, as long as we can convert it into a string, that's fine.
       return hexString;
     } else {
       // This is the normal case that most agents conform to: the value is an ASCII
       // string representing the colon-separated MAC address. We just need to reformat
       // it to remove the colons and convert it into a 12-character string.
       return displayString == null || displayString.trim().isEmpty()
           ? null
           : InetAddressUtils.normalizeMacAddress(displayString);
     }
   } catch (IllegalArgumentException e) {
     LOG.warn(e.getMessage(), e);
     return displayString;
   }
 }
 private String getPhysAddr() {
   final SnmpValue value = getValue(IF_PHYS_ADDR);
   return value == null ? null : value.toHexString();
 }
Exemplo n.º 4
0
  public OnmsAccessPointCollection call() throws IOException {
    OnmsAccessPointCollection apsUp = new OnmsAccessPointCollection();
    InetAddress ipaddr = m_iface.getIpAddress();

    // Retrieve this interface's SNMP peer object
    SnmpAgentConfig agentConfig = SnmpPeerFactory.getInstance().getAgentConfig(ipaddr);
    if (agentConfig == null) {
      throw new IllegalStateException(
          "SnmpAgentConfig object not available for interface " + ipaddr);
    }
    final String hostAddress = InetAddressUtils.str(ipaddr);
    log().debug("poll: setting SNMP peer attribute for interface " + hostAddress);

    // Get configuration parameters
    String oid = ParameterMap.getKeyedString(m_parameters, "oid", null);
    if (oid == null) {
      throw new IllegalStateException("oid parameter is not set.");
    }

    agentConfig.hashCode();

    // Set timeout and retries on SNMP peer object
    agentConfig.setTimeout(
        ParameterMap.getKeyedInteger(m_parameters, "timeout", agentConfig.getTimeout()));
    agentConfig.setRetries(
        ParameterMap.getKeyedInteger(
            m_parameters,
            "retry",
            ParameterMap.getKeyedInteger(m_parameters, "retries", agentConfig.getRetries())));
    agentConfig.setPort(ParameterMap.getKeyedInteger(m_parameters, "port", agentConfig.getPort()));

    if (log().isDebugEnabled()) {
      log().debug("TableStrategy.poll: SnmpAgentConfig address= " + agentConfig);
    }

    // Establish SNMP session with interface
    try {
      SnmpObjId snmpObjectId = SnmpObjId.get(oid);

      Map<SnmpInstId, SnmpValue> map =
          SnmpUtils.getOidValues(agentConfig, "AccessPointMonitor::TableStrategy", snmpObjectId);

      if (map.size() <= 0) {
        throw new IOException("No entries found in table (possible timeout).");
      }

      for (Map.Entry<SnmpInstId, SnmpValue> entry : map.entrySet()) {
        SnmpValue value = entry.getValue();

        String physAddr = getPhysAddrFromValue(value);

        log()
            .debug(
                "AP at value '"
                    + value.toHexString()
                    + "' with MAC '"
                    + physAddr
                    + "' is considered to be ONLINE on controller '"
                    + m_iface.getIpAddress()
                    + "'");
        OnmsAccessPoint ap = m_accessPointDao.findByPhysAddr(physAddr);
        if (ap != null) {
          if (ap.getPollingPackage().compareToIgnoreCase(getPackage().getName()) == 0) {
            // Save the controller's IP address
            ap.setControllerIpAddress(ipaddr);
            apsUp.add(ap);
          } else {
            log().info("AP with MAC '" + physAddr + "' is in a different package.");
          }
        } else {
          log().info("No matching AP in database for value '" + value.toHexString() + "'.");
        }
      }
    } catch (InterruptedException e) {
      log().error("Interrupted while polling " + hostAddress, e);
    }

    return apsUp;
  }