/** {@inheritDoc} */
  @Transactional
  public OnmsIpInterface updateIpInterfaceAttributes(
      final Integer nodeId, final OnmsIpInterface scannedIface) {
    final OnmsSnmpInterface snmpInterface = scannedIface.getSnmpInterface();
    if (snmpInterface != null && snmpInterface.getIfIndex() != null) {
      scannedIface.setSnmpInterface(updateSnmpInterfaceAttributes(nodeId, snmpInterface));
    }

    return new UpsertTemplate<OnmsIpInterface, IpInterfaceDao>(
        m_transactionManager, m_ipInterfaceDao) {

      @Override
      protected OnmsIpInterface query() {
        OnmsIpInterface dbIface =
            m_ipInterfaceDao.findByNodeIdAndIpAddress(nodeId, str(scannedIface.getIpAddress()));
        debugf(
            this,
            "Updating interface attributes for DB interface %s for node %d with ip %s",
            dbIface,
            nodeId,
            str(scannedIface.getIpAddress()));
        return dbIface;
      }

      @Override
      protected OnmsIpInterface doUpdate(final OnmsIpInterface dbIface) {
        if (dbIface.isManaged() && !scannedIface.isManaged()) {
          final Set<OnmsMonitoredService> monSvcs = dbIface.getMonitoredServices();

          for (final OnmsMonitoredService monSvc : monSvcs) {
            monSvc.visit(new DeleteEventVisitor(m_eventForwarder));
          }
          monSvcs.clear();
        }

        dbIface.mergeInterfaceAttributes(scannedIface);
        infof(this, "Updating IpInterface %s", dbIface);
        m_ipInterfaceDao.update(dbIface);
        m_ipInterfaceDao.flush();
        return dbIface;
      }

      @Override
      protected OnmsIpInterface doInsert() {
        final OnmsNode dbNode = m_nodeDao.load(nodeId);
        assertNotNull(dbNode, "no node found with nodeId %d", nodeId);
        // for performance reasons we don't add the ip interface to the node so we avoid loading all
        // the interfaces
        // setNode only sets the node in the interface
        scannedIface.setNode(dbNode);
        saveOrUpdate(scannedIface);
        final AddEventVisitor visitor = new AddEventVisitor(m_eventForwarder);
        scannedIface.visit(visitor);
        m_ipInterfaceDao.flush();
        return scannedIface;
      }
    }.execute();
  }
  private void updateSnmpDataForSnmpInterfaces() {
    if (m_collector != null && m_collector.hasIfTable()) {

      for (IfTableEntry entry : m_collector.getIfTable()) {

        Integer ifIndex = entry.getIfIndex();

        if (ifIndex == null) continue;

        LOG.debug("Updating SNMP Interface with ifIndex {}", ifIndex);

        // first look to see if an snmpIf was created already
        OnmsSnmpInterface snmpIf = m_node.getSnmpInterfaceWithIfIndex(ifIndex);

        if (snmpIf == null) {
          // if not then create one
          snmpIf = new OnmsSnmpInterface(m_node, ifIndex);
        }

        snmpIf.setIfAlias(m_collector.getIfAlias(ifIndex));
        snmpIf.setIfName(m_collector.getIfName(ifIndex));
        snmpIf.setIfType(getIfType(ifIndex));
        snmpIf.setNetMask(getNetMask(ifIndex));
        snmpIf.setIfAdminStatus(getAdminStatus(ifIndex));
        snmpIf.setIfDescr(m_collector.getIfDescr(ifIndex));
        snmpIf.setIfSpeed(m_collector.getInterfaceSpeed(ifIndex));
        snmpIf.setPhysAddr(m_collector.getPhysAddr(ifIndex));
      }
    }
  }
Example #3
0
 @Override
 public void run() {
   try {
     OnmsSnmpInterface snmpIface = new OnmsSnmpInterface();
     snmpIface.setIfIndex(m_ifIndex);
     snmpIface.setIfName(m_ifName);
     m_upsertService.upsert(m_nodeId, snmpIface, 1000);
   } catch (Throwable t) {
     t.printStackTrace();
     m_throwable.set(t);
   }
 }
 @Override
 public void setParentInterfaces(
     OnmsSnmpInterface sourceInterface, OnmsSnmpInterface targetInterface) {
   if (targetInterface != null && targetInterface.getIfOperStatus() != null) {
     if (sourceInterface != null) {
       if (sourceInterface.getIfOperStatus() == 1 && targetInterface.getIfOperStatus() == 1) {
         getLinkStateMachine().setState(getLinkStateMachine().getUpState());
       } else {
         getLinkStateMachine().setState(getLinkStateMachine().getDownState());
       }
     }
   }
 }
 public OnmsSnmpInterface createInterfaceFromRow() {
   final OnmsSnmpInterface snmpIface = new OnmsSnmpInterface(null, getIfIndex());
   snmpIface.setIfAdminStatus(getIfAdminStatus());
   snmpIface.setIfAlias(getIfAlias());
   snmpIface.setIfDescr(getIfDescr());
   snmpIface.setIfName(getIfName());
   snmpIface.setIfOperStatus(getIfOperStatus());
   snmpIface.setIfSpeed(getSpeed());
   snmpIface.setIfType(getIfType());
   snmpIface.setPhysAddr(getPhysAddr());
   return snmpIface;
 }
Example #6
0
  @Test
  @JUnitTemporaryDatabase
  public void testInsert() {
    String newIfName = "newIf0";
    assertEquals(0, countIfs(m_populator.getNode1().getId(), 1001, newIfName));

    // add non existent snmpiface
    OnmsSnmpInterface snmpIface = new OnmsSnmpInterface();
    snmpIface.setNode(m_populator.getNode1());
    snmpIface.setIfIndex(1001);
    snmpIface.setIfName(newIfName);

    m_upsertService.upsert(m_populator.getNode1().getId() /* nodeid */, snmpIface, 0);

    assertEquals(1, countIfs(m_populator.getNode1().getId(), 1001, newIfName));
  }
Example #7
0
  @Test
  @JUnitTemporaryDatabase
  public void testUpdate() {
    String oldIfName = "eth0";
    String newIfName = "newIf0";
    assertEquals(1, countIfs(m_populator.getNode1().getId(), 2, oldIfName));
    assertEquals(0, countIfs(m_populator.getNode1().getId(), 2, newIfName));

    // add non existent snmpiface
    OnmsSnmpInterface snmpIface = new OnmsSnmpInterface();
    snmpIface.setIfIndex(2);
    snmpIface.setIfName(newIfName);

    m_upsertService.upsert(m_populator.getNode1().getId(), snmpIface, 0);

    assertEquals(0, countIfs(m_populator.getNode1().getId(), 2, oldIfName));
    assertEquals(1, countIfs(m_populator.getNode1().getId(), 2, newIfName));
  }
  private void updateSnmpDataForInterface(OnmsIpInterface ipIf) {
    if (m_collector == null || !m_collector.hasIpAddrTable() || !m_collector.hasIfTable()) {
      return;
    }

    final InetAddress inetAddr = ipIf.getIpAddress();
    final String ipAddr = InetAddressUtils.str(inetAddr);

    LOG.debug("Creating SNMP info for interface {}", ipAddr);

    int ifIndex = m_collector.getIfIndex(inetAddr);
    if (ifIndex == -1) {
      return;
    }

    // first look to see if an snmpIf was created already
    OnmsSnmpInterface snmpIf = m_node.getSnmpInterfaceWithIfIndex(ifIndex);

    if (snmpIf == null) {
      // if not then create one
      snmpIf = new OnmsSnmpInterface(m_node, ifIndex);
      snmpIf.setIfAlias(m_collector.getIfAlias(ifIndex));
      snmpIf.setIfName(m_collector.getIfName(ifIndex));
      snmpIf.setIfType(getIfType(ifIndex));
      snmpIf.setNetMask(getNetMask(ifIndex));
      snmpIf.setIfAdminStatus(getAdminStatus(ifIndex));
      snmpIf.setIfDescr(m_collector.getIfDescr(ifIndex));
      snmpIf.setIfSpeed(m_collector.getInterfaceSpeed(ifIndex));
      snmpIf.setPhysAddr(m_collector.getPhysAddr(ifIndex));
    }

    snmpIf.setCollectionEnabled(true);

    ipIf.setSnmpInterface(snmpIf);

    // FIXME: Improve OpenNMS to provide these values
    // ifOperStatus

  }
  protected String getEdgeTooltipText(DataLinkInterface link, Vertex source, Vertex target) {
    StringBuffer tooltipText = new StringBuffer();

    OnmsSnmpInterface sourceInterface =
        getSnmpInterfaceDao()
            .findByNodeIdAndIfIndex(Integer.parseInt(source.getId()), link.getIfIndex());
    OnmsSnmpInterface targetInterface =
        getSnmpInterfaceDao()
            .findByNodeIdAndIfIndex(Integer.parseInt(target.getId()), link.getParentIfIndex());

    tooltipText.append(HTML_TOOLTIP_TAG_OPEN);
    if (sourceInterface != null
        && targetInterface != null
        && sourceInterface.getNetMask() != null
        && !sourceInterface.getNetMask().isLoopbackAddress()
        && targetInterface.getNetMask() != null
        && !targetInterface.getNetMask().isLoopbackAddress()) {
      tooltipText.append("Type of Link: Layer3/Layer2");
    } else {
      tooltipText.append("Type of Link: Layer2");
    }
    tooltipText.append(HTML_TOOLTIP_TAG_END);

    tooltipText.append(HTML_TOOLTIP_TAG_OPEN);
    tooltipText.append("Name: &lt;endpoint1 " + source.getLabel());
    if (sourceInterface != null) tooltipText.append(":" + sourceInterface.getIfName());
    tooltipText.append(" ---- endpoint2 " + target.getLabel());
    if (targetInterface != null) tooltipText.append(":" + targetInterface.getIfName());
    tooltipText.append("&gt;");
    tooltipText.append(HTML_TOOLTIP_TAG_END);

    LinkStateMachine stateMachine = new LinkStateMachine();
    stateMachine.setParentInterfaces(sourceInterface, targetInterface);
    tooltipText.append(HTML_TOOLTIP_TAG_OPEN);
    tooltipText.append("Link status: " + stateMachine.getLinkStatus());
    tooltipText.append(HTML_TOOLTIP_TAG_END);

    if (targetInterface != null) {
      if (targetInterface.getIfSpeed() != null) {
        tooltipText.append(HTML_TOOLTIP_TAG_OPEN);
        tooltipText.append("Bandwidth: " + getHumanReadableIfSpeed(targetInterface.getIfSpeed()));
        tooltipText.append(HTML_TOOLTIP_TAG_END);
      }
    } else if (sourceInterface != null) {
      if (sourceInterface.getIfSpeed() != null) {
        tooltipText.append(HTML_TOOLTIP_TAG_OPEN);
        tooltipText.append("Bandwidth: " + getHumanReadableIfSpeed(sourceInterface.getIfSpeed()));
        tooltipText.append(HTML_TOOLTIP_TAG_END);
      }
    }

    tooltipText.append(HTML_TOOLTIP_TAG_OPEN);
    tooltipText.append("End Point 1: " + source.getLabel() + ", " + source.getIpAddress());
    tooltipText.append(HTML_TOOLTIP_TAG_END);

    tooltipText.append(HTML_TOOLTIP_TAG_OPEN);
    tooltipText.append("End Point 2: " + target.getLabel() + ", " + target.getIpAddress());
    tooltipText.append(HTML_TOOLTIP_TAG_END);

    return tooltipText.toString();
  }
  @Test
  @JUnitSnmpAgents(
      value = {
        @JUnitSnmpAgent(
            host = ROUTER1_IP,
            port = 161,
            resource = "classpath:linkd/nms17216/router1-walk.txt")
      })
  @Transactional
  public final void testRouter1() throws MarshalException, ValidationException, IOException {
    m_capsd.init();
    m_capsd.start();
    m_capsd.scanSuspectInterface(ROUTER1_IP);

    List<OnmsIpInterface> ips = m_interfaceDao.findByIpAddress(ROUTER1_IP);
    assertTrue("Has only one ip interface", ips.size() == 1);

    OnmsIpInterface ip = ips.get(0);

    for (OnmsIpInterface ipinterface : ip.getNode().getIpInterfaces()) {
      if (ipinterface.getIfIndex() != null)
        System.out.println(
            "ROUTER1_IP_IF_MAP.put(InetAddress.getByName(\""
                + ipinterface.getIpHostName()
                + "\"), "
                + ipinterface.getIfIndex()
                + ");");
    }

    for (OnmsSnmpInterface snmpinterface : ip.getNode().getSnmpInterfaces()) {
      if (snmpinterface.getIfName() != null)
        System.out.println(
            "ROUTER1_IF_IFNAME_MAP.put("
                + snmpinterface.getIfIndex()
                + ", \""
                + snmpinterface.getIfName()
                + "\");");
      if (snmpinterface.getIfDescr() != null)
        System.out.println(
            "ROUTER1_IF_IFDESCR_MAP.put("
                + snmpinterface.getIfIndex()
                + ", \""
                + snmpinterface.getIfDescr()
                + "\");");
      if (snmpinterface.getPhysAddr() != null)
        System.out.println(
            "ROUTER1_IF_MAC_MAP.put("
                + snmpinterface.getIfIndex()
                + ", \""
                + snmpinterface.getPhysAddr()
                + "\");");
      if (snmpinterface.getIfAlias() != null)
        System.out.println(
            "ROUTER1_IF_IFALIAS_MAP.put("
                + snmpinterface.getIfIndex()
                + ", \""
                + snmpinterface.getIfAlias()
                + "\");");
    }

    m_capsd.stop();
  }
  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;
  }