Пример #1
0
  /**
   * mergeSnmpInterfaces
   *
   * @param scannedNode a {@link org.opennms.netmgt.model.OnmsNode} object.
   * @param deleteMissing a boolean.
   */
  public void mergeSnmpInterfaces(OnmsNode scannedNode, boolean deleteMissing) {

    // we need to skip this step if there is an indication that snmp data collection failed
    if (scannedNode.getSnmpInterfaces().size() == 0) {
      // we assume here that snmp collection failed and we don't update the snmp data
      return;
    }

    // Build map of ifIndices to scanned SnmpInterfaces
    Map<Integer, OnmsSnmpInterface> scannedInterfaceMap = new HashMap<Integer, OnmsSnmpInterface>();
    for (OnmsSnmpInterface snmpIface : scannedNode.getSnmpInterfaces()) {
      if (snmpIface.getIfIndex() != null) {
        scannedInterfaceMap.put(snmpIface.getIfIndex(), snmpIface);
      }
    }

    // for each interface on existing node...
    for (Iterator<OnmsSnmpInterface> it = getSnmpInterfaces().iterator(); it.hasNext(); ) {

      OnmsSnmpInterface iface = it.next();
      OnmsSnmpInterface imported = scannedInterfaceMap.get(iface.getIfIndex());

      // remove it since there is no corresponding scanned interface
      if (imported == null) {
        if (deleteMissing) {
          it.remove();
          scannedInterfaceMap.remove(iface.getIfIndex());
        }
      } else {
        // merge the data from the corresponding scanned interface
        iface.mergeSnmpInterfaceAttributes(imported);
        scannedInterfaceMap.remove(iface.getIfIndex());
      }
    }

    // for any scanned interface that was not found on the node add it the database
    for (OnmsSnmpInterface snmpIface : scannedInterfaceMap.values()) {
      addSnmpInterface(snmpIface);
    }
  }
  private ArrayList<OnmsResource> populateResourceList(
      File parent, File relPath, OnmsNode node, Boolean isForeign) {

    ArrayList<OnmsResource> resources = new ArrayList<OnmsResource>();

    File[] intfDirs = parent.listFiles(RrdFileConstants.INTERFACE_DIRECTORY_FILTER);

    Set<OnmsSnmpInterface> snmpInterfaces = node.getSnmpInterfaces();
    Map<String, OnmsSnmpInterface> intfMap = new HashMap<String, OnmsSnmpInterface>();

    for (OnmsSnmpInterface snmpInterface : snmpInterfaces) {
      /*
       * When Cisco Express Forwarding (CEF) or some ATM encapsulations
       * (AAL5) are used on Cisco routers, an additional entry might be
       * in the ifTable for these sub-interfaces, but there is no
       * performance data available for collection.  This check excludes
       * ifTable entries where ifDescr contains "-cef".  See bug #803.
       */
      if (snmpInterface.getIfDescr() != null) {
        if (Pattern.matches(".*-cef.*", snmpInterface.getIfDescr())) {
          continue;
        }
      }

      String replacedIfName = AlphaNumeric.parseAndReplace(snmpInterface.getIfName(), '_');
      String replacedIfDescr = AlphaNumeric.parseAndReplace(snmpInterface.getIfDescr(), '_');

      String[] keys =
          new String[] {
            replacedIfName + "-",
            replacedIfDescr + "-",
            replacedIfName + "-" + snmpInterface.getPhysAddr(),
            replacedIfDescr + "-" + snmpInterface.getPhysAddr()
          };

      for (String key : keys) {
        if (!intfMap.containsKey(key)) {
          intfMap.put(key, snmpInterface);
        }
      }
    }

    for (File intfDir : intfDirs) {
      String name = intfDir.getName();

      String desc = name;
      String mac = "";

      // Strip off the MAC address from the end, if there is one
      int dashIndex = name.lastIndexOf('-');

      if (dashIndex >= 0) {
        desc = name.substring(0, dashIndex);
        mac = name.substring(dashIndex + 1, name.length());
      }

      String key = desc + "-" + mac;
      OnmsSnmpInterface snmpInterface = intfMap.get(key);

      String label;
      Long ifSpeed = null;
      String ifSpeedFriendly = null;
      if (snmpInterface == null) {
        label = name + " (*)";
      } else {
        StringBuffer descr = new StringBuffer();
        StringBuffer parenString = new StringBuffer();

        if (snmpInterface.getIfAlias() != null) {
          parenString.append(snmpInterface.getIfAlias());
        }
        // Append all of the IP addresses on this ifindex
        for (OnmsIpInterface ipif : snmpInterface.getIpInterfaces()) {
          String ipaddr = InetAddressUtils.str(ipif.getIpAddress());
          if (!"0.0.0.0".equals(ipaddr)) {
            if (parenString.length() > 0) {
              parenString.append(", ");
            }
            parenString.append(ipaddr);
          }
        }
        if ((snmpInterface.getIfSpeed() != null) && (snmpInterface.getIfSpeed() != 0)) {
          ifSpeed = snmpInterface.getIfSpeed();
          ifSpeedFriendly = SIUtils.getHumanReadableIfSpeed(ifSpeed);
          if (parenString.length() > 0) {
            parenString.append(", ");
          }
          parenString.append(ifSpeedFriendly);
        }

        if (snmpInterface.getIfName() != null) {
          descr.append(snmpInterface.getIfName());
        } else if (snmpInterface.getIfDescr() != null) {
          descr.append(snmpInterface.getIfDescr());
        } else {
          /*
           * Should never reach this point, since ifLabel is based on
           * the values of ifName and ifDescr but better safe than sorry.
           */
          descr.append(name);
        }

        /* Add the extended information in parenthesis after the ifLabel,
         * if such information was found.
         */
        if (parenString.length() > 0) {
          descr.append(" (");
          descr.append(parenString);
          descr.append(")");
        }

        label = descr.toString();
      }

      OnmsResource resource = null;
      if (isForeign) {
        resource =
            getResourceByNodeSourceAndInterface(
                relPath.toString(), intfDir.getName(), label, ifSpeed, ifSpeedFriendly);
      } else {
        resource =
            getResourceByNodeAndInterface(
                node.getId(), intfDir.getName(), label, ifSpeed, ifSpeedFriendly);
      }
      if (snmpInterface != null) {
        Set<OnmsIpInterface> ipInterfaces = snmpInterface.getIpInterfaces();
        if (ipInterfaces.size() > 0) {
          int id = ipInterfaces.iterator().next().getId();
          resource.setLink("element/interface.jsp?ipinterfaceid=" + id);
        } else {
          int ifIndex = snmpInterface.getIfIndex();
          if (ifIndex > -1) {
            resource.setLink(
                "element/snmpinterface.jsp?node=" + node.getNodeId() + "&ifindex=" + ifIndex);
          }
        }

        resource.setEntity(snmpInterface);
      } else {
        LOG.debug("populateResourceList: snmpInterface is null");
      }
      LOG.debug("populateResourceList: adding resource toString {}", resource.toString());
      resources.add(resource);
    }

    return resources;
  }