📄 capsdconfigfactory.java
字号:
* 1. All 'outage' table entries which refer to the service are deleted. 2. * All 'ifServices' table entries which refer to the service are deleted. * * Note that the 'service' table entry will remain in the database since * events most likely exist which refer to the service. */ public void syncServices(Connection conn) throws SQLException { Category log = ThreadCategory.getInstance(); if (conn == null) { log.error("CapsdConfigFactory.syncServices: Sync failed...must have valid database connection."); return; } // synchronize configured services list with // the database // PreparedStatement insStmt = conn.prepareStatement(SVCTBL_ADD_SQL); PreparedStatement nxtStmt = conn.prepareStatement(NEXT_SVC_ID_SQL); PreparedStatement loadStmt = conn.prepareStatement(SVCTBL_LOAD_SQL); PreparedStatement delFromOutagesStmt = null; PreparedStatement delFromIfServicesStmt = null; ResultSet rs = null; try { // go ahead and load the table first if it // can be loaded. // List serviceNames = new ArrayList(); rs = loadStmt.executeQuery(); while (rs.next()) { Integer id = new Integer(rs.getInt(1)); String name = rs.getString(2); m_serviceIds.put(id, name); m_serviceIds.put(name, id); serviceNames.add(name); } rs.close(); // Build list of configured protocols from the loaded // configuration List protocols = new ArrayList(); Enumeration eplugin = m_config.enumerateProtocolPlugin(); while (eplugin.hasMoreElements()) { ProtocolPlugin plugin = (ProtocolPlugin) eplugin.nextElement(); protocols.add(plugin.getProtocol()); } // now iterate over the configured protocols // and make sure that each is represented in the database. // Iterator protos = protocols.iterator(); while (protos.hasNext()) { String protocol = (String) protos.next(); if (!serviceNames.contains(protocol)) { // get the next identifier // int id = -1; rs = nxtStmt.executeQuery(); rs.next(); id = rs.getInt(1); rs.close(); insStmt.setInt(1, id); insStmt.setString(2, protocol); insStmt.executeUpdate(); Integer xid = new Integer(id); m_serviceIds.put(xid, protocol); m_serviceIds.put(protocol, xid); serviceNames.add(protocol); } } // now iterate over the services from the 'service' table // and determine if any no longer exist in the list of // configured protocols // Iterator s = serviceNames.iterator(); while (s.hasNext()) { String service = (String) s.next(); if (!protocols.contains(service)) { if (log.isDebugEnabled()) log.debug("syncServices: service " + service + " exists in the database but not in the Capsd config file."); // Delete 'outage' table entries which refer to the service Integer id = (Integer) m_serviceIds.get(service); if (log.isDebugEnabled()) log.debug("syncServices: deleting all references to service id " + id + " from the Outages table."); delFromOutagesStmt = conn.prepareStatement(DELETE_OUTAGES_SQL); delFromOutagesStmt.setInt(1, id.intValue()); delFromOutagesStmt.executeUpdate(); // Delete 'ifServices' table entries which refer to the // service if (log.isDebugEnabled()) log.debug("syncServices: deleting all references to service id " + id + " from the IfServices table."); delFromIfServicesStmt = conn.prepareStatement(DELETE_IFSERVICES_SQL); delFromIfServicesStmt.setInt(1, id.intValue()); delFromIfServicesStmt.executeUpdate(); } } } finally { if (rs != null) rs.close(); if (insStmt != null) insStmt.close(); if (nxtStmt != null) nxtStmt.close(); if (loadStmt != null) loadStmt.close(); if (delFromOutagesStmt != null) delFromOutagesStmt.close(); if (delFromIfServicesStmt != null) delFromIfServicesStmt.close(); } } /** * Responsible for syncing up the 'isManaged' field of the ipInterface table * and the 'status' field of the ifServices table based on the capsd and * poller configurations. Note that the 'sync' only takes place for * interfaces and services that are not deleted or force unmanaged. * * <pre> * Here is how the statuses are set: * If an interface is 'unmanaged' based on the capsd configuration, * ipManaged='U' and status='U' * * If an interface is 'managed' based on the capsd configuration, * 1. If the interface is not in any pacakge, ipManaged='N' and status ='N' * 2. If the interface in atleast one package but the service is not polled by * by any of the packages, ipManaged='M' and status='N' * 3. If the interface in atleast one package and the service is polled by a * package that this interface belongs to, ipManaged='M' and status'=A' * * </pre> * * @param conn * Connection to the database. * * @exception SQLException * Thrown if an error occurs while syncing the database. */ public void syncManagementState(Connection conn) throws SQLException { Category log = ThreadCategory.getInstance(); boolean verifyServer = OpennmsServerConfigFactory.getInstance().verifyServer(); String localServer = OpennmsServerConfigFactory.getInstance().getServerName(); if (log.isDebugEnabled()) log.debug("syncManagementState: local server: " + localServer + " verify server: " + verifyServer); if (conn == null) { log.error("CapsdConfigFactory.syncManagementState: Sync failed...must have valid database connection."); return; } // Get default management state. // String temp = m_config.getManagementPolicy(); boolean managed_by_default = (temp == null || temp.equalsIgnoreCase("managed")); if (log.isDebugEnabled()) log.debug("syncManagementState: managed_by_default: " + managed_by_default); // // Retrieve list of interfaces and their managed status from the // database // NOTE: Interfaces with an 'isManaged' field equal to 'D' (Deleted) or // 'F' (Forced Unmanaged) are // not eligible to be managed and will not be included in the interfaces // retrieved from the database. Likewise, interfaces with IP address of // '0.0.0.0' will also be excluded by the SQL query. // // prepare the SQL statement to query the database PreparedStatement ipRetStmt = null; ResultSet result = null; ArrayList ifList = new ArrayList(); try { if (verifyServer) { ipRetStmt = conn.prepareStatement(SQL_DB_RETRIEVE_IP_INTERFACE_IN_LOCAL_SERVER); ipRetStmt.setString(1, localServer); } else ipRetStmt = conn.prepareStatement(SQL_DB_RETRIEVE_IP_INTERFACE); // run the statement result = ipRetStmt.executeQuery(); // Build array list of CapsdInterface objects representing each // of the interfaces retrieved from the database while (result.next()) { // Node Id int nodeId = result.getInt(1); // IP address String address = result.getString(2); if (address == null) { log.warn("invalid ipInterface table entry, no IP address, skipping..."); continue; } // Management State char managedState = DbIpInterfaceEntry.STATE_UNKNOWN; String str = result.getString(3); if (str != null) managedState = str.charAt(0); ifList.add(new LightWeightIfEntry(nodeId, LightWeightIfEntry.NULL_IFINDEX, address, managedState, DbIpInterfaceEntry.SNMP_UNKNOWN, LightWeightIfEntry.NULL_IFTYPE)); } } finally { try { if (result != null) result.close(); } finally { if (ipRetStmt != null) ipRetStmt.close(); } } // For efficiency, prepare the SQL statements in advance PreparedStatement ifUpdateStmt = null; PreparedStatement allSvcUpdateStmt = null; PreparedStatement svcRetStmt = null; PreparedStatement svcUpdateStmt = null; // get a handle to the PollerConfigFactory PollerConfigFactory pollerCfgFactory = null; try { ifUpdateStmt = conn.prepareStatement(SQL_DB_UPDATE_IP_INTERFACE); allSvcUpdateStmt = conn.prepareStatement(SQL_DB_UPDATE_ALL_SERVICES_FOR_NIP); svcRetStmt = conn.prepareStatement(SQL_DB_RETRIEVE_IF_SERVICES); svcUpdateStmt = conn.prepareStatement(SQL_DB_UPDATE_SERVICE_FOR_NIP); // get a handle to the PollerConfigFactory pollerCfgFactory = PollerConfigFactory.getInstance(); // Loop through interface list and determine if there has been a // change in // the managed status of the interface based on the newly loaded // package // configuration data. Iterator iter = ifList.iterator(); while (iter.hasNext()) { LightWeightIfEntry ifEntry = (LightWeightIfEntry) iter.next(); String ipaddress = ifEntry.getAddress(); // Convert to InetAddress object // InetAddress ifAddress = null; try { ifAddress = InetAddress.getByName(ipaddress); } catch (UnknownHostException uhE) { log.warn("Failed converting ip address " + ipaddress + " to InetAddress."); continue; } // Check interface address against Capsd config information to // determine // if interface management state should be managed or unmanaged. boolean address_is_unmanaged = this.isAddressUnmanaged(ifAddress); if (log.isDebugEnabled()) { log.debug("syncManagementState: " + ipaddress + " unmanaged based on capsd config?: " + address_is_unmanaged); } if (address_is_unmanaged) { // Interface not managed, check current // management state for this interface. if (ifEntry.getManagementState() != DbIpInterfaceEntry.STATE_UNMANAGED) { // Update management state to unmanaged for the // interface as well as for its services. // Update the 'ipInterface' table ifUpdateStmt.setString(1, new String(new char[] { DbIpInterfaceEntry.STATE_UNMANAGED })); ifUpdateStmt.setInt(2, ifEntry.getNodeId()); ifUpdateStmt.setString(3, ipaddress); ifUpdateStmt.executeUpdate(); // Update the 'ifServices' table allSvcUpdateStmt.setString(1, new String(new char[] { DbIfServiceEntry.STATUS_UNMANAGED })); allSvcUpdateStmt.setInt(2, ifEntry.getNodeId()); allSvcUpdateStmt.setString(3, ipaddress); allSvcUpdateStmt.executeUpdate(); if (log.isDebugEnabled()) { log.debug("syncManagementState: update completed for node/interface: " + ifEntry.getNodeId() + "/" + ipaddress + " to unmanaged"); } } } else { // Interface should be managed - check the status against // poller config to see if interface will be polled // // NOTE: Try to avoid re-evaluating the ip against filters // for // each service, try to get the first package here and use // that for service evaluation // org.opennms.netmgt.config.poller.Package ipPkg = pollerCfgFactory.getFirstPackageMatch(ipaddress); boolean ipToBePolled = false; if (ipPkg != null) ipToBePolled = true; if (log.isDebugEnabled()) log.debug("syncManagementState: " + ipaddress + " to be polled based on poller config?: " + ipToBePolled); if ((ifEntry.getManagementState() == DbIpInterfaceEntry.STATE_MANAGED && ipToBePolled) || (ifEntry.getManagementState() == DbIpInterfaceEntry.STATE_NOT_POLLED && !ipToBePolled)) { // current status is right if (log.isDebugEnabled()) log.debug("syncManagementState: " + ipaddress + " - no change in status"); } else { if (ipToBePolled) ifUpdateStmt.setString(1, new String(new char[] { DbIpInterfaceEntry.STATE_MANAGED })); else ifUpdateStmt.setString(1, new String(new char[] { DbIpInterfaceEntry.STATE_NOT_POLLED }));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -