Index: capsd/RescanProcessor.java =================================================================== RCS file: /cvsroot/opennms/opennms/src/services/org/opennms/netmgt/capsd/RescanProcessor.java,v retrieving revision 1.58 diff -u -r1.58 RescanProcessor.java --- capsd/RescanProcessor.java 10 Feb 2005 20:21:18 -0000 1.58 +++ capsd/RescanProcessor.java 26 Mar 2005 18:22:53 -0000 @@ -10,6 +10,9 @@ // // Modifications: // +// 2005 Mar 25: Fixed bug 1178 regarding designation of secondary SNMP +// interfaces, as well as a few other minor bugs discovered +// in testing the bug fix. // 2005 Jan 03: Changed the way a primary SNMP interface is determined // such that most or all nodes with SNMP will have a // primary SNMP interface. @@ -128,7 +131,7 @@ * SQL statement for retrieving the nodeids that have the same ipaddr as the * updating node no matter what ifindex they have. */ - final static String SQL_DB_RETRIEVE_DUPLICATE_NODEIDS = "SELECT nodeid FROM ipinterface WHERE ipaddr = ? AND nodeid !=? "; + final static String SQL_DB_RETRIEVE_DUPLICATE_NODEIDS = "SELECT nodeid FROM ipinterface WHERE ismanaged != 'D' AND ipaddr = ? AND nodeid !=? "; /** * SQL statements used to reparent an interface and its associated services @@ -172,6 +175,12 @@ */ private final static String SQL_RETRIEVE_SERVICE_IDS = "SELECT serviceid,servicename FROM service"; + /** + * SQL statement used to update ipinterface.ismanaged + * when rescan discovers new interface + */ + private final static String SQL_DB_UPDATE_ISMANAGED = "UPDATE ipinterface SET ismanaged=? WHERE nodeID=? AND ipaddr=? AND isManaged!='D'"; + final static Map m_serviceNames = new HashMap(); /** @@ -421,7 +430,10 @@ // make sure we have a current PackageIpListMap // this was getting done once for each managed ip // interface in updateInterfaceInfo. Seems more - // efficient to just do it once here. + // efficient to just do it once here, and then later + // for new interfaces (which aren't in the DB yet at + // this point) in updateInterfaceInfo. + if (log.isDebugEnabled()) log.debug("updateInterfaces: Rebuilding PackageIpListMap"); PollerConfigFactory pollerCfgFactory = PollerConfigFactory.getInstance(); @@ -586,14 +598,6 @@ if (status != -1) dbIpIfEntry.setStatus(status); - // Removed the following update to insure that the issnmpprimary field - // was not - // over-written during a rescan. Since it should already be set, there - // is no - // need to re-set it. - - // dbIpIfEntry.setPrimaryState(DbIpInterfaceEntry.SNMP_NOT_ELIGIBLE); - // Update the database dbIpIfEntry.store(dbc); @@ -914,11 +918,13 @@ // Interface not found with this nodeId so create new interface // entry if (log.isDebugEnabled()) - log.debug("updateInterface: interface " + ifaddr + " not in database under nodeid " + node.getNodeId() + ", creating new interface object."); + log.debug("updateInterface: interface " + ifaddr + " ifIndex " + ifIndex + " not in database under nodeid " + node.getNodeId() + ", creating new interface object."); if (ifIndex == -1 && !doesSnmp) { dbIpIfEntry = DbIpInterfaceEntry.create(node.getNodeId(), ifaddr); } else { dbIpIfEntry = DbIpInterfaceEntry.create(node.getNodeId(), ifaddr, ifIndex); + // This wasn't getting done for some reason, so do it explicitly + dbIpIfEntry.setIfIndex(ifIndex); } if (isDuplicateInterface(dbc, ifaddr, node.getNodeId())) { @@ -1088,11 +1094,6 @@ // Hostname currIpIfEntry.setHostname(ifaddr.getHostName()); - // set ifIndex for lame SNMP host - // if(ifIndex == CapsdConfigFactory.LAME_SNMP_HOST_IFINDEX) { - // currIpIfEntry.setIfIndex(ifIndex); - // } - // Managed state // NOTE: (reference internal bug# 201) // If the ip is 'managed', it might still be 'not polled' based @@ -1102,17 +1103,15 @@ // each service, try to get the first package here and use // that for service evaluation // - // At this point the ip is already/ in the database, so package filter - // evaluation should go through OK + // At this point IF the ip is already in the database, package filter + // evaluation should go through OK. New interfaces will be dealt with + // later // org.opennms.netmgt.config.poller.Package ipPkg = null; if (cFactory.isAddressUnmanaged(ifaddr)) currIpIfEntry.setManagedState(DbIpInterfaceEntry.STATE_UNMANAGED); else { - // rebuildPackageIpListMap now done in updateInterfaces - // PollerConfigFactory.getInstance().rebuildPackageIpListMap(); - boolean ipToBePolled = false; ipPkg = pollerCfgFactory.getFirstPackageMatch(ifaddr.getHostAddress()); if (ipPkg != null) @@ -1128,38 +1127,29 @@ } // If SNMP data collection is available set SNMP Primary state - // as well as ifIndex and ifStatus. It appears ifType is no - // longer used here, so it is commented out. - // - // int ifType = -1; + // as well as ifIndex and ifStatus. + + // For all interfaces simply set 'isSnmpPrimary' field to + // not eligible for now. Following the interface updates + // the primary and secondary SNMP interfaces will be + // determined and the value of the 'isSnmpPrimary' field + // set accordingly for each interface. The old primary + // interface should have already been saved for future + // reference. + if (doesSnmp && snmpc != null && snmpc.hasIpAddrTable()) { if (ifIndex != -1) { if (snmpc.hasIfTable()) { int status = snmpc.getAdminStatus(ifIndex); currIpIfEntry.setStatus(status); } - // ifType = snmpc.getIfType(ifIndex); - - // For new interfaces simply set 'isSnmpPrimary' field to - // secondary for now. - // Following the interface updates the primary SNMP interface - // will be determined - // and the value of the 'isSnmpPrimary' field set to 'P' - // (primary) for that - // interface. - if (isNewIpEntry) - currIpIfEntry.setPrimaryState(DbIpInterfaceEntry.SNMP_SECONDARY); - else - currIpIfEntry.setPrimaryState(originalIpIfEntry.getPrimaryState()); } else { - // No ifIndex found so set primary state to NOT_ELIGIBLE - currIpIfEntry.setPrimaryState(DbIpInterfaceEntry.SNMP_NOT_ELIGIBLE); + // No ifIndex found + log.debug("updateInterfaceInfo: No ifIndex found for " + ifaddr.getHostAddress() + ". Not eligible for primary SNMP interface"); } + currIpIfEntry.setPrimaryState(DbIpInterfaceEntry.SNMP_NOT_ELIGIBLE); } else if (doesSnmp) { - if (isNewIpEntry) - currIpIfEntry.setPrimaryState(DbIpInterfaceEntry.SNMP_SECONDARY); - else - currIpIfEntry.setPrimaryState(originalIpIfEntry.getPrimaryState()); + currIpIfEntry.setPrimaryState(DbIpInterfaceEntry.SNMP_NOT_ELIGIBLE); } // Update any fields which have changed @@ -1209,6 +1199,35 @@ if (ipHostnameChangedFlag) { createIpHostNameChangedEvent(dbIpIfEntry, originalIpIfEntry); } + if(isNewIpEntry) { + // if its new, the packageIpListMap needs to be rebuilt, + // polling status rechecked, and ismanaged updated if necessary + boolean ipToBePolled = false; + log.debug("updateInterfaceInfo: rebuilding PackageIpListMap for new interface " + ifaddr.getHostAddress()); + PollerConfigFactory.getInstance().rebuildPackageIpListMap(); + ipPkg = pollerCfgFactory.getFirstPackageMatch(ifaddr.getHostAddress()); + if (ipPkg != null) + ipToBePolled = true; + log.debug("updateInterfaceInfo: interface " + ifaddr.getHostAddress() + " to be polled: " + ipToBePolled); + if (ipToBePolled) { + PreparedStatement stmt = dbc.prepareStatement(SQL_DB_UPDATE_ISMANAGED); + stmt.setString(1, new String(new char[] { DbIpInterfaceEntry.STATE_MANAGED })); + stmt.setInt(2, dbIpIfEntry.getNodeId()); + stmt.setString(3, ifaddr.getHostAddress()); + try { + stmt.executeUpdate(); + log.debug("updateInterfaceInfo: updated managed state for new interface " + ifaddr.getHostAddress() + " on node " + dbIpIfEntry.getNodeId() + " to managed"); + } catch (SQLException sqlE) { + throw sqlE; + } finally { + try { + stmt.close(); + } catch (Exception e) { + // Ignore + } + } + } + } } /** @@ -1357,8 +1376,7 @@ // Now process previously existing protocols to update polling status. // Additional checks on forced rescan for existing services go here. // Specifically, has service been forced managed/unmanaged or has - // polling - // status changed? + // polling status changed? Category log = ThreadCategory.getInstance(getClass()); PollerConfigFactory pollerCfgFactory = PollerConfigFactory.getInstance(); @@ -2713,7 +2731,11 @@ Map nonSnmpCollectorMap = new HashMap(); Set probedAddrs = new HashSet(); - boolean gotSnmpCollection = scanPrimarySnmpInterface(log, dbInterfaces, collectorMap, probedAddrs); + boolean gotSnmpCollection = false; + DbIpInterfaceEntry oldPrimarySnmpInterface = DbNodeEntry.getPrimarySnmpInterface(dbInterfaces); + if(oldPrimarySnmpInterface != null) { + gotSnmpCollection = scanPrimarySnmpInterface(log, oldPrimarySnmpInterface, collectorMap, probedAddrs); + } if(!gotSnmpCollection) { @@ -2885,7 +2907,11 @@ updateInterfaces(dbc, now, dbNodeEntry, collectorMap, doesSnmp); if (doesSnmp) { - InetAddress newSnmpPrimaryIf = updatePrimarySnmpInterface(dbc, dbNodeEntry, collectorMap); + InetAddress oldPriIf = null; + if(oldPrimarySnmpInterface != null) { + oldPriIf = oldPrimarySnmpInterface.getIfAddress(); + } + InetAddress newSnmpPrimaryIf = updatePrimarySnmpInterface(dbc, dbNodeEntry, collectorMap, oldPriIf); DbNodeEntry updatedNodeEntry = updateNode(dbc, now, dbNodeEntry, newSnmpPrimaryIf, dbInterfaces, collectorMap); } @@ -2931,50 +2957,40 @@ log.debug("Rescan for node w/ nodeid " + m_scheduledNode.getNodeId() + " completed."); } // end run - private boolean scanPrimarySnmpInterface(Category log, DbIpInterfaceEntry[] dbInterfaces, Map collectorMap, Set probedAddrs) { + private boolean scanPrimarySnmpInterface(Category log, DbIpInterfaceEntry oldPrimarySnmpInterface, Map collectorMap, Set probedAddrs) { boolean gotSnmpCollection = false; - DbIpInterfaceEntry oldPrimarySnmpInterface = null; - - // Get primary snmp interface from database - oldPrimarySnmpInterface = DbNodeEntry.getPrimarySnmpInterface(dbInterfaces); - if(oldPrimarySnmpInterface != null) { - // Run collector for DB primary snmp interface and add result - // to a collector map. - InetAddress ifaddr = oldPrimarySnmpInterface.getIfAddress(); + // Run collector for DB primary snmp interface and add result + // to a collector map. + InetAddress ifaddr = oldPrimarySnmpInterface.getIfAddress(); + if (log.isDebugEnabled()) + log.debug("running collection for DB primary snmp interface " + ifaddr.getHostAddress()); + IfCollector collector = new IfCollector(ifaddr, true, probedAddrs); + collector.run(); + IfSnmpCollector snmpc = collector.getSnmpCollector(); + if (snmpc == null) { + if (log.isDebugEnabled()) + log.debug("SNMP Collector from DB primary snmp interface is null"); + } else { + gotSnmpCollection = true; + collectorMap.put(ifaddr.getHostAddress(), collector); if (log.isDebugEnabled()) - log.debug("running collection for DB primary snmp interface " + ifaddr.getHostAddress()); - IfCollector collector = new IfCollector(ifaddr, true, probedAddrs); - collector.run(); - IfSnmpCollector snmpc = collector.getSnmpCollector(); - if (snmpc == null) { + log.debug("SNMP data collected from DB primary snmp interface" + ifaddr.getHostAddress()); + if (!snmpc.hasIfTable()) { if (log.isDebugEnabled()) - log.debug("SNMP Collector from DB primary snmp interface is null"); - } else { - gotSnmpCollection = true; - collectorMap.put(ifaddr.getHostAddress(), collector); + log.debug("SNMP Collector has no IfTable"); + } + if (!snmpc.hasIpAddrTable() || snmpc.getIfIndex(snmpc.getTarget()) == -1) { if (log.isDebugEnabled()) - log.debug("SNMP data collected from DB primary snmp interface" + ifaddr.getHostAddress()); - if (!snmpc.hasIfTable()) { - if (log.isDebugEnabled()) - log.debug("SNMP Collector has no IfTable"); - } - if (!snmpc.hasIpAddrTable() || snmpc.getIfIndex(snmpc.getTarget()) == -1) { - if (log.isDebugEnabled()) - log.debug("SNMP Collector has no IpAddrTable. Assume its a lame SNMP host."); - } + log.debug("SNMP Collector has no IpAddrTable. Assume its a lame SNMP host."); } } return gotSnmpCollection; } - private InetAddress updatePrimarySnmpInterface(Connection dbc, DbNodeEntry dbNodeEntry, Map collectorMap) throws SQLException { + private InetAddress updatePrimarySnmpInterface(Connection dbc, DbNodeEntry dbNodeEntry, Map collectorMap, InetAddress oldPriIf) throws SQLException { Category log = ThreadCategory.getInstance(getClass()); - // Get old primary SNMP interface(s) (if one or more exists) - // - List oldPriIfs = SuspectEventProcessor.getPrimarySnmpInterfaceFromDb(dbc, dbNodeEntry); - // Now that all interfaces have been added to the // database we can update the 'primarySnmpInterface' // field of the ipInterface table. Necessary because @@ -2990,9 +3006,35 @@ // all eligible interfaces. // boolean strict = true; + CollectdConfigFactory.getInstance().rebuildPackageIpListMap(); IfSnmpCollector snmpc = findSnmpCollector(collectorMap); List snmpLBAddresses = buildLBSnmpAddressList(collectorMap, snmpc); List snmpAddresses = buildSnmpAddressList(collectorMap, snmpc); + // first set the value of issnmpprimary for secondaries + Iterator iter = snmpAddresses.iterator(); + while(iter.hasNext()) { + InetAddress addr = (InetAddress) iter.next(); + if (CollectdConfigFactory.getInstance().lookupInterfaceServicePair(addr.getHostAddress(), "SNMP")) { + PreparedStatement stmt = dbc.prepareStatement("UPDATE ipInterface SET isSnmpPrimary='S' WHERE nodeId=? AND ipAddr=? AND isManaged!='D'"); + stmt.setInt(1, dbNodeEntry.getNodeId()); + stmt.setString(2, addr.getHostAddress()); + + // Execute statement + try { + stmt.executeUpdate(); + log.debug("updatePrimarySnmpInterface: updated " + addr.getHostAddress() + " to secondary."); + } catch (SQLException sqlE) { + throw sqlE; + } finally { + try { + stmt.close(); + } catch (Exception e) { + // Ignore + } + } + } + } + InetAddress newSnmpPrimaryIf = CollectdConfigFactory.getInstance().determinePrimarySnmpInterface(snmpLBAddresses, strict); String psiType = ConfigFileConstants.getFileName(ConfigFileConstants.COLLECTD_CONFIG_FILE_NAME) + " loopback addresses"; @@ -3019,18 +3061,9 @@ if (newSnmpPrimaryIf != null) { if(log.isDebugEnabled()) { - log.debug("primary SNMP interface is: " + newSnmpPrimaryIf + ", selected from " + psiType); - } - // iterate over list of old primaries. There should only be one, - // but in case there are more, this should clear out the extras. - Iterator opiter = oldPriIfs.iterator(); - if(opiter.hasNext()) { - while (opiter.hasNext()) { - SuspectEventProcessor.setPrimarySnmpInterface(dbc, dbNodeEntry, newSnmpPrimaryIf, (InetAddress) opiter.next()); - } - } else { - SuspectEventProcessor.setPrimarySnmpInterface(dbc, dbNodeEntry, newSnmpPrimaryIf, null); + log.debug("updatePrimarySnmpInterface: primary SNMP interface is: " + newSnmpPrimaryIf + ", selected from " + psiType); } + SuspectEventProcessor.setPrimarySnmpInterface(dbc, dbNodeEntry, newSnmpPrimaryIf, oldPriIf); } else { if(log.isDebugEnabled()) @@ -3041,7 +3074,7 @@ // generate certain SNMP data collection related // events // - generateSnmpDataCollectionEvents(dbNodeEntry, oldPriIfs, newSnmpPrimaryIf); + generateSnmpDataCollectionEvents(dbNodeEntry, oldPriIf, newSnmpPrimaryIf); return newSnmpPrimaryIf; } @@ -3109,42 +3142,36 @@ * * @param nodeEntry * DbNodeEntry object of the node being rescanned. - * @param oldPriIfs - * List of Previous primary SNMP interface(s) (from the DB). + * @param oldPriIf + * Previous primary SNMP interface (from the DB). * @param primarySnmpIf * Primary SNMP interface as determined by the current rescan. */ - private void generateSnmpDataCollectionEvents(DbNodeEntry nodeEntry, List oldPriIfs, InetAddress primarySnmpIf) { + private void generateSnmpDataCollectionEvents(DbNodeEntry nodeEntry, InetAddress oldPriIf, InetAddress primarySnmpIf) { Category log = ThreadCategory.getInstance(getClass()); - // THE FOLLOWING LOGIC IS QUESTIONABLE!!! // NOTE: If SNMP service was not previously supported on this node - // then the 'oldPriIfs' list will be empty. If this is the case + // then oldPriIf will be null. If this is the case // then no need to generate primarySnmpInterfaceChanged event, // the nodeGainedService event generated due to the addition of // SNMP is sufficient. boolean reInit = true; - Iterator opiter = oldPriIfs.iterator(); - if (!opiter.hasNext() && primarySnmpIf != null) { + if (oldPriIf == null && primarySnmpIf != null) { reInit = false; if (log.isDebugEnabled()) log.debug("generateSnmpDataCollectionEvents: Either SNMP support was recently enabled on this node, or node doesn't support ipAddrTable MIB."); createPrimarySnmpInterfaceChangedEvent(nodeEntry.getNodeId(), primarySnmpIf, null); } else { - InetAddress op = null; - while (opiter.hasNext()) { - op = (InetAddress) opiter.next(); - // A PrimarySnmpInterfaceChanged event is generated if the scan - // found a different primary SNMP interface than what is stored - // in the database. - // - if (primarySnmpIf != null && !op.equals(primarySnmpIf)) { - if (log.isDebugEnabled()) { - log.debug("generateSnmpDataCollectionEvents: primary SNMP interface has changed. Was: " + op.getHostAddress() + " Is: " + primarySnmpIf.getHostAddress()); - } - createPrimarySnmpInterfaceChangedEvent(nodeEntry.getNodeId(), primarySnmpIf, op); - reInit = false; + // A PrimarySnmpInterfaceChanged event is generated if the scan + // found a different primary SNMP interface than what is stored + // in the database. + // + if (primarySnmpIf != null && !oldPriIf.equals(primarySnmpIf)) { + if (log.isDebugEnabled()) { + log.debug("generateSnmpDataCollectionEvents: primary SNMP interface has changed. Was: " + oldPriIf.getHostAddress() + " Is: " + primarySnmpIf.getHostAddress()); } + createPrimarySnmpInterfaceChangedEvent(nodeEntry.getNodeId(), primarySnmpIf, oldPriIf); + reInit = false; } } Index: capsd/SuspectEventProcessor.java =================================================================== RCS file: /cvsroot/opennms/opennms/src/services/org/opennms/netmgt/capsd/SuspectEventProcessor.java,v retrieving revision 1.27 diff -u -r1.27 SuspectEventProcessor.java --- capsd/SuspectEventProcessor.java 10 Feb 2005 18:27:25 -0000 1.27 +++ capsd/SuspectEventProcessor.java 26 Mar 2005 18:22:54 -0000 @@ -10,6 +10,9 @@ // // Modifications: // +// 2005 Mar 25: Fixed bug 1178 regarding designation of secondary SNMP +// interfaces, as well as a few other minor bugs discovered +// in testing the bug fix. // 2004 Dec 27: Updated code to determine primary SNMP interface to select // an interface from collectd-configuration.xml first, and if // none found, then from all interfaces on the node. In either @@ -108,7 +111,7 @@ /** * SQL statement to retrieve the ipaddresses for a given node ID */ - private final static String SQL_RETRIEVE_IPINTERFACES_ON_NODEID = "SELECT ipaddr FROM ipinterface WHERE nodeid = ?"; + private final static String SQL_RETRIEVE_IPINTERFACES_ON_NODEID = "SELECT ipaddr FROM ipinterface WHERE nodeid = ? and ismanaged != 'D'"; private final static String SELECT_METHOD_MIN = "min"; @@ -498,7 +501,9 @@ Date now = new Date(); - DbIpInterfaceEntry ipIfEntry = DbIpInterfaceEntry.create(node.getNodeId(), ifaddr); + int nodeId = node.getNodeId(); + + DbIpInterfaceEntry ipIfEntry = DbIpInterfaceEntry.create(nodeId, ifaddr); ipIfEntry.setLastPoll(now); ipIfEntry.setHostname(ifaddr.getHostName()); @@ -516,10 +521,13 @@ // boolean addrUnmanaged = cFactory.isAddressUnmanaged(ifaddr); - if (addrUnmanaged) + if (addrUnmanaged) { + log.debug("addInterfaces: " + ifaddr + " is unmanaged"); ipIfEntry.setManagedState(DbIpInterfaceEntry.STATE_UNMANAGED); - else + } else { + log.debug("addInterfaces: " + ifaddr + " is managed"); ipIfEntry.setManagedState(DbIpInterfaceEntry.STATE_MANAGED); + } ipIfEntry.setPrimaryState(DbIpInterfaceEntry.SNMP_NOT_ELIGIBLE); @@ -538,6 +546,8 @@ if (ipPkg != null) ipToBePolled = true; + log.debug("addInterfaces: " + ifaddr + " is to be polled = " + ipToBePolled); + if (!ipToBePolled) { // update ismanaged to 'N' in ipinterface ipIfEntry.setManagedState(DbIpInterfaceEntry.STATE_NOT_POLLED); @@ -549,13 +559,14 @@ IfSnmpCollector snmpc = null; if (collector.hasSnmpCollection()) { snmpc = collector.getSnmpCollector(); - if (snmpc.hasIpAddrTable() && (ifIndex = snmpc.getIfIndex(ifaddr)) != -1) { - // Just set primary state to secondary for now. The primary SNMP - // interface won't be selected until after all interfaces have - // been inserted into the database. This is because the interface - // must already be in the database for filter rule evaluation to - // succeed. + // Just set primary state to not eligible for now. The primary SNMP + // interface won't be selected until after all interfaces have + // been inserted into the database. This is because the interface + // must already be in the database for filter rule evaluation to + // succeed. + ipIfEntry.setPrimaryState(DbIpInterfaceEntry.SNMP_NOT_ELIGIBLE); + if (snmpc.hasIpAddrTable() && (ifIndex = snmpc.getIfIndex(ifaddr)) != -1) { if (snmpc.hasIfTable()) { int status = snmpc.getAdminStatus(ifIndex); if (status != -1) @@ -569,7 +580,6 @@ if (log.isDebugEnabled()) log.debug("SuspectEventProcessor: no valid ifIndex for " + ifaddr + " Assume this is a lame SNMP host"); } - ipIfEntry.setPrimaryState(DbIpInterfaceEntry.SNMP_SECONDARY); if (ifIndex != -1) if (log.isDebugEnabled()) log.debug("SuspectEventProcessor: setting ifindex for " + ifaddr + " to " + ifIndex); @@ -602,7 +612,7 @@ if (log.isDebugEnabled()) log.debug("addInterfaces: adding interface " + xifaddr.getHostAddress()); - DbIpInterfaceEntry xipIfEntry = DbIpInterfaceEntry.create(node.getNodeId(), xifaddr); + DbIpInterfaceEntry xipIfEntry = DbIpInterfaceEntry.create(nodeId, xifaddr); xipIfEntry.setLastPoll(now); xipIfEntry.setHostname(xifaddr.getHostName()); @@ -624,7 +634,13 @@ xipIfEntry.setManagedState(DbIpInterfaceEntry.STATE_UNMANAGED); else xipIfEntry.setManagedState(DbIpInterfaceEntry.STATE_MANAGED); + // Just set primary state to not eligible for now. The + // primary SNMP interface won't be selected until after + // all interfaces have been inserted into the database. + // This is because the interface must already be in + // the database for filter rule evaluation to succeed. + xipIfEntry.setPrimaryState(DbIpInterfaceEntry.SNMP_NOT_ELIGIBLE); int xifIndex = -1; if ((xifIndex = snmpc.getIfIndex(xifaddr)) != -1) { xipIfEntry.setIfIndex(xifIndex); @@ -632,21 +648,12 @@ if (status != -1) xipIfEntry.setStatus(status); - if (supportsSnmp((List) extraTargets.get(xifaddr))) { - // Just set primary state to secondary for now. The - // primary SNMP interface - // won't be selected until after all interfaces have - // been inserted - // into the database. This is because the interface must - // already be in - // the database for filter rule evaluation to succeed. - xipIfEntry.setPrimaryState(DbIpInterfaceEntry.SNMP_SECONDARY); - } else { - xipIfEntry.setPrimaryState(DbIpInterfaceEntry.SNMP_NOT_ELIGIBLE); + if (!supportsSnmp((List) extraTargets.get(xifaddr))) { + log.debug("addInterfaces: Interface doesn't support SNMP. " + xifaddr.getHostAddress() + " set to not eligible"); } } else { // No ifIndex found so set primary state to NOT_ELIGIBLE - xipIfEntry.setPrimaryState(DbIpInterfaceEntry.SNMP_NOT_ELIGIBLE); + log.debug("addInterfaces: No ifIndex found. " + xifaddr.getHostAddress() + " set to not eligible"); } xipIfEntry.store(dbc); @@ -684,7 +691,7 @@ DbIpInterfaceEntry xipIfEntry = null; try { - xipIfEntry = DbIpInterfaceEntry.create(node.getNodeId(), InetAddress.getByName("0.0.0.0")); + xipIfEntry = DbIpInterfaceEntry.create(nodeId, InetAddress.getByName("0.0.0.0")); } catch (UnknownHostException e) { continue; } @@ -753,7 +760,7 @@ if (aaddrs[0].getHostAddress().startsWith("127.")) continue; - DbSnmpInterfaceEntry snmpEntry = DbSnmpInterfaceEntry.create(node.getNodeId(), xifIndex); + DbSnmpInterfaceEntry snmpEntry = DbSnmpInterfaceEntry.create(nodeId, xifIndex); // IP address snmpEntry.setIfAddress(aaddrs[0]); @@ -839,7 +846,7 @@ } } if(ifIndex == CapsdConfigFactory.LAME_SNMP_HOST_IFINDEX || forceSnmpInterfaceEntry) { - DbSnmpInterfaceEntry snmpEntry = DbSnmpInterfaceEntry.create(node.getNodeId(), ifIndex); + DbSnmpInterfaceEntry snmpEntry = DbSnmpInterfaceEntry.create(nodeId, ifIndex); // IP address snmpEntry.setIfAddress(ifaddr); snmpEntry.store(dbc); @@ -1311,6 +1318,30 @@ CollectdConfigFactory.getInstance().rebuildPackageIpListMap(); List lbAddressList = buildLBSnmpAddressList(collector); List addressList = buildSnmpAddressList(collector); + // first set the value of issnmpprimary for secondaries + Iterator iter = addressList.iterator(); + while(iter.hasNext()) { + InetAddress addr = (InetAddress) iter.next(); + if (CollectdConfigFactory.getInstance().lookupInterfaceServicePair(addr.getHostAddress(), "SNMP")) { + PreparedStatement stmt = dbc.prepareStatement("UPDATE ipInterface SET isSnmpPrimary='S' WHERE nodeId=? AND ipAddr=? AND isManaged!='D'"); + stmt.setInt(1, entryNode.getNodeId()); + stmt.setString(2, addr.getHostAddress()); + + // Execute statement + try { + stmt.executeUpdate(); + log.debug("updated " + addr.getHostAddress() + " to secondary."); + } catch (SQLException sqlE) { + throw sqlE; + } finally { + try { + stmt.close(); + } catch (Exception e) { + // Ignore + } + } + } + } String psiType = null; if (lbAddressList != null) { newSnmpPrimaryIf = CollectdConfigFactory.getInstance().determinePrimarySnmpInterface(lbAddressList, strict); @@ -1336,9 +1367,9 @@ if (log.isDebugEnabled()) { if(newSnmpPrimaryIf == null) { - log.debug("SuspectEventProcessor: No primary SNMP interface found"); + log.debug("No primary SNMP interface found"); } else { - log.debug("SuspectEventProcessor: primary SNMP interface is: " + newSnmpPrimaryIf + ", selected from " + psiType); + log.debug("primary SNMP interface is: " + newSnmpPrimaryIf + ", selected from " + psiType); } } // iterate over list of old primaries. There should only be @@ -1410,7 +1441,7 @@ List priSnmpAddrs = new ArrayList(); - log.debug("getPrimarySnmpInterfaceFromDb: retrieving primary snmp interface(s) from DB..."); + log.debug("getPrimarySnmpInterfaceFromDb: retrieving primary snmp interface(s) from DB for node " + node.getNodeId()); InetAddress oldPrimarySnmpIf = null; // Prepare SQL statement @@ -1422,7 +1453,8 @@ try { rs = stmt.executeQuery(); while (rs.next()) { - String oldPrimaryAddr = rs.getString("ipAddr"); + String oldPrimaryAddr = rs.getString(1); + log.debug("getPrimarySnmpInterfaceFromDb: String oldPrimaryAddr = " + oldPrimaryAddr); if (oldPrimaryAddr != null) { try { oldPrimarySnmpIf = InetAddress.getByName(oldPrimaryAddr); @@ -1434,6 +1466,7 @@ } } } catch (SQLException sqlE) { + log.warn("getPrimarySnmpInterfaceFromDb: Exception: " + sqlE); throw sqlE; } finally { try { @@ -1474,46 +1507,23 @@ return; } else { if (log.isDebugEnabled()) - log.debug("setPrimarySnmpInterface: newSnmpPrimary=" + newPrimarySnmpIf); + log.debug("setPrimarySnmpInterface: newSnmpPrimary = " + newPrimarySnmpIf); } // Verify that old and new primary interfaces are different // if (oldPrimarySnmpIf != null && oldPrimarySnmpIf.equals(newPrimarySnmpIf)) { - // Old and new primary interfaces are the same, just return + // Old and new primary interfaces are the same if (log.isDebugEnabled()) - log.debug("setPrimarySnmpInterface: Old and new primary interfaces are the same, just return"); - return; + log.debug("setPrimarySnmpInterface: Old and new primary interfaces are the same"); } - // They are different so mark the old primary as a secondary interface - // - if (oldPrimarySnmpIf != null) { - // Prepare SQL statement - PreparedStatement stmt = dbc.prepareStatement("UPDATE ipInterface SET isSnmpPrimary='S' WHERE nodeId=? AND ipAddr=? AND isManaged!='D'"); - stmt.setInt(1, node.getNodeId()); - stmt.setString(2, oldPrimarySnmpIf.getHostAddress()); - - // Execute statement - try { - stmt.executeUpdate(); - log.debug("setPrimarySnmpInterface: completed update of old primary interface to SECONDARY."); - } catch (SQLException sqlE) { - throw sqlE; - } finally { - try { - stmt.close(); - } catch (Exception e) { - // Ignore - } - } - } // Set primary SNMP interface 'isSnmpPrimary' field to 'P' for primary // if (newPrimarySnmpIf != null) { if (log.isDebugEnabled()) - log.debug("setPrimarySnmpInterface: primary SNMP interface=" + newPrimarySnmpIf.getHostAddress()); + log.debug("setPrimarySnmpInterface: Updating primary SNMP interface " + newPrimarySnmpIf.getHostAddress()); // Update the appropriate entry in the 'ipInterface' table // Index: config/CapsdConfigFactory.java =================================================================== RCS file: /cvsroot/opennms/opennms/src/services/org/opennms/netmgt/config/CapsdConfigFactory.java,v retrieving revision 1.17 diff -u -r1.17 CapsdConfigFactory.java --- config/CapsdConfigFactory.java 6 Jan 2005 16:03:17 -0000 1.17 +++ config/CapsdConfigFactory.java 26 Mar 2005 18:22:55 -0000 @@ -10,6 +10,9 @@ // // Modifications: // +// 2005 Mar 25: Fixed bug 1178 regarding designation of secondary SNMP +// interfaces, as well as a few other minor bugs discovered +// in testing the bug fix. // 2004 Dec 27: Updated code to determine primary SNMP interface to select // an interface from collectd-configuration.xml first, and if // none found, then from all interfaces on the node. In either @@ -152,7 +155,7 @@ * SQL statement to retrieve all non-deleted IP addresses from the * ipInterface table which support SNMP. */ - private static String SQL_DB_RETRIEVE_SNMP_IP_INTERFACES = "SELECT DISTINCT ipinterface.nodeid,ipinterface.ipaddr,ipinterface.ifindex,ipinterface.issnmpprimary,snmpinterface.snmpiftype,snmpinterface.snmpifindex FROM ipinterface,ifservices,service,snmpinterface WHERE ipinterface.ismanaged!='D' AND ipinterface.ipaddr=ifservices.ipaddr AND ipinterface.ipaddr=snmpinterface.ipaddr AND ifservices.serviceid=service.serviceid AND service.servicename='SNMP' AND ipinterface.nodeid=snmpinterface.nodeid"; + private static String SQL_DB_RETRIEVE_SNMP_IP_INTERFACES = "SELECT DISTINCT ipinterface.nodeid,ipinterface.ipaddr,ipinterface.ifindex,ipinterface.issnmpprimary,snmpinterface.snmpiftype,snmpinterface.snmpifindex FROM ipinterface,ifservices,service,snmpinterface WHERE ipinterface.ismanaged!='D' AND ifservices.status!='D' AND ipinterface.ipaddr=ifservices.ipaddr AND ipinterface.ipaddr=snmpinterface.ipaddr AND ifservices.serviceid=service.serviceid AND service.servicename='SNMP' AND ipinterface.nodeid=snmpinterface.nodeid"; /** * SQL statement used to update the 'isSnmpPrimary' field of the ipInterface @@ -1207,7 +1210,10 @@ if (lwIf.getIfIndex() == LightWeightIfEntry.NULL_IFINDEX) { lwIf.setSnmpPrimaryState(DbIpInterfaceEntry.SNMP_NOT_ELIGIBLE); } else if (primarySnmpIf == null || !lwIf.getAddress().equals(primarySnmpIf.getHostAddress())) { - lwIf.setSnmpPrimaryState(DbIpInterfaceEntry.SNMP_SECONDARY); + if (CollectdConfigFactory.getInstance().lookupInterfaceServicePair(lwIf.getAddress(), "SNMP")) + lwIf.setSnmpPrimaryState(DbIpInterfaceEntry.SNMP_SECONDARY); + else + lwIf.setSnmpPrimaryState(DbIpInterfaceEntry.SNMP_NOT_ELIGIBLE); } else { lwIf.setSnmpPrimaryState(DbIpInterfaceEntry.SNMP_PRIMARY); } Index: config/CollectdConfigFactory.java =================================================================== RCS file: /cvsroot/opennms/opennms/src/services/org/opennms/netmgt/config/CollectdConfigFactory.java,v retrieving revision 1.15 diff -u -r1.15 CollectdConfigFactory.java --- config/CollectdConfigFactory.java 6 Jan 2005 16:03:17 -0000 1.15 +++ config/CollectdConfigFactory.java 26 Mar 2005 18:22:55 -0000 @@ -380,7 +380,9 @@ has_specific = hasSpecificUrl(iface, pkg, has_specific); boolean has_range_exclude = hasExcludeRange(pkg, addr, has_specific); - return has_specific || (has_range_include && !has_range_exclude); + boolean packagePassed = has_specific || (has_range_include && !has_range_exclude); + log.debug("interfaceInPackage: Interface " + iface + " passed filter and specific/range for package " + pkg.getName() + "?: " + packagePassed); + return packagePassed; } @@ -488,9 +490,9 @@ if (log.isDebugEnabled()) if (primaryIf != null) - log.debug("determinePrimarySnmpInterface: selected primary SNMP interface: " + primaryIf.getHostAddress()); + log.debug("determinePrimarySnmpInterface: candidate primary SNMP interface: " + primaryIf.getHostAddress()); else - log.debug("determinePrimarySnmpInterface: no primary SNMP interface found"); + log.debug("determinePrimarySnmpInterface: no candidate primary SNMP interface found"); return primaryIf; } @@ -632,12 +634,12 @@ java.util.List ipList = (java.util.List) m_pkgIpMap.get(pkg); if (ipList != null && ipList.size() > 0) { filterPassed = ipList.contains(iface); + } else { + log.debug("interfaceInFilter: ipList contains no data"); } - if (log.isDebugEnabled()) - log.debug("interfaceInPackage: Interface " + iface - + " passed filter for package " + pkg.getName() + "?: " - + filterPassed); + if (!filterPassed) + log.debug("interfaceInFilter: Interface " + iface + " passed filter for package " + pkg.getName() + "?: false"); return filterPassed; } }