private InetAddress getIpForNode(final OnmsNode node) {
   LogUtils.debugf(
       this,
       "getIpForNode: node: %s Foreign Source: %s",
       node.getNodeId(),
       node.getForeignSource());
   final OnmsIpInterface primaryInterface = node.getPrimaryInterface();
   InetAddress ipaddr = InetAddressUtils.getLocalHostAddress();
   if (primaryInterface == null) {
     log().debug("getIpForNode: found null SNMP Primary Interface, getting interfaces");
     final Set<OnmsIpInterface> ipInterfaces = node.getIpInterfaces();
     for (final OnmsIpInterface onmsIpInterface : ipInterfaces) {
       log().debug("getIpForNode: trying Interface with id: " + onmsIpInterface.getId());
       if (InetAddressUtils.str(onmsIpInterface.getIpAddress()) != null)
         ipaddr = onmsIpInterface.getIpAddress();
       else
         log()
             .debug(
                 "getIpForNode: found null ip address on Interface with id: "
                     + onmsIpInterface.getId());
     }
   } else {
     log().debug("getIpForNode: found SNMP Primary Interface");
     if (InetAddressUtils.str(primaryInterface.getIpAddress()) != null)
       ipaddr = primaryInterface.getIpAddress();
     else log().debug("getIpForNode: found null ip address on Primary Interface");
   }
   return ipaddr;
 }
 /**
  * getIpAddrToInterfaceMap
  *
  * @param imported a {@link org.opennms.netmgt.model.OnmsNode} object.
  * @return a {@link java.util.Map} object.
  */
 protected Map<String, OnmsIpInterface> getIpAddrToInterfaceMap(OnmsNode imported) {
   Map<String, OnmsIpInterface> ipAddrToIface = new HashMap<String, OnmsIpInterface>();
   for (final OnmsIpInterface iface : imported.getIpInterfaces()) {
     final String ipAddress = InetAddressUtils.str(iface.getIpAddress());
     ipAddrToIface.put(ipAddress, iface);
   }
   return ipAddrToIface;
 }
 /** {@inheritDoc} */
 @Transactional
 public OnmsNode getNode(final Integer nodeId) {
   final OnmsNode node = m_nodeDao.get(nodeId);
   m_nodeDao.initialize(node);
   m_nodeDao.initialize(node.getCategories());
   m_nodeDao.initialize(node.getIpInterfaces());
   return node;
 }
  /** updateSnmpData */
  protected void updateSnmpData() {
    if (m_collector != null) {
      m_collector.run();
    }

    updateSnmpDataForNode();

    updateSnmpDataForSnmpInterfaces();

    for (OnmsIpInterface ipIf : m_node.getIpInterfaces()) {
      resolveIpHostname(ipIf);
      updateSnmpDataForInterface(ipIf);
    }
  }
Exemple #5
0
 /**
  * loadNode
  *
  * @param loadNode a {@link org.opennms.core.tasks.BatchTask} object.
  */
 public void loadNode(final BatchTask loadNode) {
   if (getForeignSource() != null) {
     m_node = m_provisionService.getRequisitionedNode(getForeignSource(), getForeignId());
     if (m_node == null) {
       abort(
           String.format(
               "Unable to get requisitioned node (%s/%s): aborted", m_foreignSource, m_foreignId));
     } else {
       for (final OnmsIpInterface iface : m_node.getIpInterfaces()) {
         loadNode.add(
             new IpInterfaceScan(
                 getNodeId(), iface.getIpAddress(), getForeignSource(), getProvisionService()));
       }
     }
   } else {
     m_node = m_provisionService.getNode(m_nodeId);
   }
 }
  /** {@inheritDoc} */
  public List<OnmsResource> getResourcesForNode(final int nodeId) {
    final LinkedList<OnmsResource> resources = new LinkedList<OnmsResource>();

    final OnmsNode node = m_nodeDao.get(nodeId);
    if (node == null) {
      throw new ObjectRetrievalFailureException(
          OnmsNode.class, nodeId, "Could not find node for node Id " + nodeId, null);
    }

    for (final OnmsIpInterface i : node.getIpInterfaces()) {
      String ipAddr = InetAddressUtils.str(i.getIpAddress());

      final File iface = getInterfaceDirectory(ipAddr, false);

      if (iface.isDirectory()) {
        resources.add(createResource(i));
      }
    }

    return resources;
  }
  @Test
  @Transactional
  public void testNotificationSave() {
    OnmsEvent event = new OnmsEvent();
    event.setDistPoller(m_distPollerDao.load("localhost"));
    event.setEventCreateTime(new Date());
    event.setEventDescr("event dao test");
    event.setEventHost("localhost");
    event.setEventLog("Y");
    event.setEventDisplay("Y");
    event.setEventLogGroup("event dao test log group");
    event.setEventLogMsg("event dao test log msg");
    event.setEventSeverity(7);
    event.setEventSource("EventDaoTest");
    event.setEventTime(new Date());
    event.setEventUei("uei://org/opennms/test/NotificationDaoTest");
    //        OnmsAlarm alarm = new OnmsAlarm();
    //        event.setAlarm(alarm);

    OnmsNode node = m_nodeDao.findAll().iterator().next();
    OnmsIpInterface iface = node.getIpInterfaces().iterator().next();
    OnmsMonitoredService service = iface.getMonitoredServices().iterator().next();
    event.setNode(node);
    event.setServiceType(service.getServiceType());
    event.setIpAddr(iface.getIpAddress());
    m_eventDao.save(event);
    OnmsEvent newEvent = m_eventDao.load(event.getId());
    assertEquals("uei://org/opennms/test/NotificationDaoTest", newEvent.getEventUei());

    OnmsNotification notification = new OnmsNotification();
    notification.setEvent(newEvent);
    notification.setTextMsg("Tests are fun!");
    m_notificationDao.save(notification);

    OnmsNotification newNotification = m_notificationDao.load(notification.getNotifyId());
    assertEquals(
        "uei://org/opennms/test/NotificationDaoTest", newNotification.getEvent().getEventUei());
  }
Exemple #8
0
    public void detectIpInterfaceTable(final BatchTask currentPhase) {
      final OnmsNode node = getNode();

      LOG.debug("Attempting to scan the IPInterface table for node {}", node);

      // mark all provisioned interfaces as 'in need of scanning' so we can mark them
      // as scanned during ipAddrTable processing
      final Set<InetAddress> provisionedIps = new HashSet<InetAddress>();
      if (getForeignSource() != null) {
        for (final OnmsIpInterface provisioned : node.getIpInterfaces()) {
          provisionedIps.add(provisioned.getIpAddress());
        }
      }

      final IPInterfaceTableTracker ipIfTracker =
          new IPInterfaceTableTracker() {
            @Override
            public void processIPInterfaceRow(final IPInterfaceRow row) {
              final String ipAddress = row.getIpAddress();
              LOG.info(
                  "Processing IPInterface table row with ipAddr {} for node {}/{}/{}",
                  ipAddress,
                  node.getId(),
                  node.getForeignSource(),
                  node.getForeignId());

              final InetAddress address = addr(ipAddress);

              // skip if it's any number of unusual/local address types
              if (address == null) return;
              if (address.isAnyLocalAddress()) {
                LOG.debug("{}.isAnyLocalAddress() == true, Skipping.", ipAddress);
                return;
              }
              if (address.isLinkLocalAddress()) {
                LOG.debug("{}.isLinkLocalAddress() == true, Skipping.", ipAddress);
                return;
              }
              if (address.isLoopbackAddress()) {
                LOG.debug("{}.isLoopbackAddress() == true, Skipping.", ipAddress);
                return;
              }
              if (address.isMulticastAddress()) {
                LOG.debug("{}.isMulticastAddress() == true, Skipping.", ipAddress);
                return;
              }

              // mark any provisioned interface as scanned
              provisionedIps.remove(ipAddress);

              // save the interface
              OnmsIpInterface iface = row.createInterfaceFromRow();

              if (iface != null) {
                iface.setIpLastCapsdPoll(getScanStamp());

                // add call to the ip interface is managed policies
                iface.setIsManaged("M");

                final List<IpInterfacePolicy> policies =
                    getProvisionService()
                        .getIpInterfacePoliciesForForeignSource(
                            getForeignSource() == null ? "default" : getForeignSource());
                for (final IpInterfacePolicy policy : policies) {
                  if (iface != null) {
                    iface = policy.apply(iface);
                  }
                }

                if (iface != null) {
                  currentPhase.add(ipUpdater(currentPhase, iface), "write");
                }
              }
            }
          };

      walkTable(currentPhase, provisionedIps, ipIfTracker);
    }
Exemple #9
0
  /**
   * mergeIpInterfaces
   *
   * @param scannedNode a {@link org.opennms.netmgt.model.OnmsNode} object.
   * @param eventForwarder a {@link org.opennms.netmgt.model.events.EventForwarder} object.
   * @param deleteMissing a boolean.
   */
  public void mergeIpInterfaces(
      OnmsNode scannedNode, EventForwarder eventForwarder, boolean deleteMissing) {
    OnmsIpInterface oldPrimaryInterface = null;
    OnmsIpInterface scannedPrimaryIf = null;
    // build a map of ipAddrs to ipInterfaces for the scanned node
    Map<InetAddress, OnmsIpInterface> ipInterfaceMap = new HashMap<InetAddress, OnmsIpInterface>();
    for (OnmsIpInterface iface : scannedNode.getIpInterfaces()) {
      if (scannedPrimaryIf == null && iface.isPrimary()) {
        scannedPrimaryIf = iface;
      } else if (iface.isPrimary()) {
        iface.setIsSnmpPrimary(PrimaryType.SECONDARY);
      }

      ipInterfaceMap.put(iface.getIpAddress(), iface);
    }

    // for each ipInterface from the database
    for (Iterator<OnmsIpInterface> it = getIpInterfaces().iterator(); it.hasNext(); ) {
      OnmsIpInterface dbIface = it.next();
      // find the corresponding scanned Interface
      OnmsIpInterface scannedIface = ipInterfaceMap.get(dbIface.getIpAddress());

      // if we can't find a scanned interface remove from the database
      if (scannedIface == null) {
        if (deleteMissing) {
          it.remove();
          dbIface.visit(new DeleteEventVisitor(eventForwarder));
        } else if (scannedPrimaryIf != null && dbIface.isPrimary()) {
          dbIface.setIsSnmpPrimary(PrimaryType.SECONDARY);
          oldPrimaryInterface = dbIface;
        }
      } else {
        // else update the database with scanned info
        dbIface.mergeInterface(scannedIface, eventForwarder, deleteMissing);
        if (scannedPrimaryIf != null && dbIface.isPrimary() && scannedPrimaryIf != scannedIface) {
          dbIface.setIsSnmpPrimary(PrimaryType.SECONDARY);
          oldPrimaryInterface = dbIface;
        }
      }

      // now remove the interface from the map to indicate it was processed
      ipInterfaceMap.remove(dbIface.getIpAddress());
    }

    // for any remaining scanned interfaces, add them to the database
    for (OnmsIpInterface iface : ipInterfaceMap.values()) {
      addIpInterface(iface);
      if (iface.getIfIndex() != null) {
        iface.setSnmpInterface(getSnmpInterfaceWithIfIndex(iface.getIfIndex()));
      }
      iface.visit(new AddEventVisitor(eventForwarder));
    }

    if (oldPrimaryInterface != null && scannedPrimaryIf != null) {
      EventBuilder bldr =
          new EventBuilder(EventConstants.PRIMARY_SNMP_INTERFACE_CHANGED_EVENT_UEI, "Provisiond");
      bldr.setIpInterface(scannedPrimaryIf);
      bldr.setService("SNMP");
      bldr.addParam(
          EventConstants.PARM_OLD_PRIMARY_SNMP_ADDRESS,
          InetAddressUtils.str(oldPrimaryInterface.getIpAddress()));
      bldr.addParam(
          EventConstants.PARM_NEW_PRIMARY_SNMP_ADDRESS,
          InetAddressUtils.str(scannedPrimaryIf.getIpAddress()));

      eventForwarder.sendNow(bldr.getEvent());
    }
  }