📄 defaultquerymanager.java
字号:
stmt = dbConn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT nodelabel FROM node WHERE nodeid=" + String.valueOf(nodeId)); if (rs.next()) { nodeLabel = (String) rs.getString("nodelabel"); if (log.isDebugEnabled()) log.debug("getNodeLabel: nodeid=" + nodeId + " nodelabel=" + nodeLabel); } } finally { // Close the statement if (stmt != null) { try { stmt.close(); } catch (Exception e) { if (log.isDebugEnabled()) log.debug("getNodeLabel: an exception occured closing the SQL statement", e); } } // Close the database connection if (dbConn != null) { try { dbConn.close(); } catch (Throwable t) { if (log.isDebugEnabled()) log.debug("getNodeLabel: an exception occured closing the SQL connection", t); } } } return nodeLabel; } /** * @param ipaddr * @return * @throws SQLException */ public int getServiceCountForInterface(String ipaddr) throws SQLException { Category log = log(); java.sql.Connection dbConn = getConnection(); try { int count = -1; // Count active services to poll PreparedStatement stmt = dbConn.prepareStatement(DefaultQueryManager.SQL_COUNT_IFSERVICES_TO_POLL); stmt.setString(1, ipaddr); ResultSet rs = stmt.executeQuery(); while (rs.next()) { count = rs.getInt(1); if (log.isDebugEnabled()) log.debug("restartPollingInterfaceHandler: count active ifservices to poll for interface: " + ipaddr); } stmt.close(); return count; } finally { dbConn.close(); } } /** * @param svcName * @return * @throws SQLException */ public List getInterfacesWithService(String svcName) throws SQLException { List ifkeys; Category log = log(); java.sql.Connection dbConn = getConnection(); if (log.isDebugEnabled()) log.debug("scheduleExistingInterfaces: dbConn = " + dbConn + ", svcName = " + svcName); PreparedStatement stmt = dbConn.prepareStatement(DefaultQueryManager.SQL_RETRIEVE_INTERFACES); stmt.setString(1, svcName); // Service name ResultSet rs = stmt.executeQuery(); // Iterate over result set and schedule each // interface/service // pair which passes the criteria // ifkeys = new ArrayList(); while (rs.next()) { IfKey key = new IfKey(rs.getInt(1), rs.getString(2)); ifkeys.add(key); } rs.close(); return ifkeys; } /** * @param poller * @param nodeId * @param ipAddr * @param svcName * @return */ public Date getServiceLostDate(int nodeId, String ipAddr, String svcName, int serviceId) { Category log = ThreadCategory.getInstance(Poller.class); log.debug("getting last known status for address: " + ipAddr + " service: " + svcName); Date svcLostDate = null; // Convert service name to service identifier // if (serviceId < 0) { log.warn("Failed to retrieve service identifier for interface " + ipAddr + " and service '" + svcName + "'"); return svcLostDate; } ResultSet outagesResult = null; Timestamp regainedDate = null; Timestamp lostDate = null; Connection dbConn = null; try { dbConn = getConnection(); // get the outage information for this service on this ip address PreparedStatement outagesQuery = dbConn.prepareStatement(DefaultQueryManager.SQL_RETRIEVE_SERVICE_STATUS); // add the values for the main query outagesQuery.setInt(1, nodeId); outagesQuery.setString(2, ipAddr); outagesQuery.setInt(3, serviceId); // add the values for the subquery outagesQuery.setInt(4, nodeId); outagesQuery.setString(5, ipAddr); outagesQuery.setInt(6, serviceId); outagesResult = outagesQuery.executeQuery(); // if there was a result then the service has been down before, if (outagesResult.next()) { regainedDate = outagesResult.getTimestamp(1); lostDate = outagesResult.getTimestamp(2); log.debug("getServiceLastKnownStatus: lostDate: " + lostDate); } // the service has never been down, need to use current date for // both else { Date currentDate = new Date(System.currentTimeMillis()); regainedDate = new Timestamp(currentDate.getTime()); lostDate = lostDate = new Timestamp(currentDate.getTime()); } } catch (SQLException sqlE) { log.error("SQL exception while retrieving last known service status for " + ipAddr + "/" + svcName); } finally { if (outagesResult != null) { try { outagesResult.close(); if (dbConn != null) dbConn.close(); } catch (SQLException slqE) { // Do nothing } } } // Now use retrieved outage times to determine current status // of the service. If there was an error and we were unable // to retrieve the outage times the default of AVAILABLE will // be returned. // if (lostDate != null) { // If the service was never regained then simply // assign the svc lost date. if (regainedDate == null) { svcLostDate = new Date(lostDate.getTime()); log.debug("getServiceLastKnownStatus: svcLostDate: " + svcLostDate); } } return svcLostDate; } public void setDbConnectionFactory(DbConnectionFactory dbConnectionFactory) { m_dbConnectionFactory = dbConnectionFactory; } public Timestamp convertEventTimeToTimeStamp(String time) { try { Date date = EventConstants.parseToDate(time); Timestamp eventTime = new Timestamp(date.getTime()); return eventTime; } catch (ParseException e) { throw new RuntimeException("Invalid date format "+time, e); } } public void openOutage(String outageIdSQL, int nodeId, String ipAddr, int serviceId, int dbId, String time) { int attempt = 0; boolean notUpdated = true; while (attempt < 2 && notUpdated) { try { log().info("opening outage for "+nodeId+":"+ipAddr+":"+serviceId+" with cause "+dbId+":"+time); String sql = "insert into outages (outageId, svcLostEventId, nodeId, ipAddr, serviceId, ifLostService) values (" + "("+outageIdSQL+"), " + "?, ?, ?, ?, ?)"; Object values[] = { new Integer(dbId), new Integer(nodeId), ipAddr, new Integer(serviceId), convertEventTimeToTimeStamp(time), }; Updater updater = new Updater(m_dbConnectionFactory, sql); updater.execute(values); notUpdated = false; } catch (Exception e) { if (attempt > 1) { log().fatal("openOutage: Second and final attempt failed opening outage for "+nodeId+":"+ipAddr+":"+serviceId, e); } else { log().info("openOutage: First attempt failed opening outage for "+nodeId+":"+ipAddr+":"+serviceId, e); } } attempt++; } } private Category log() { return ThreadCategory.getInstance(getClass()); } public void resolveOutage(int nodeId, String ipAddr, int serviceId, int dbId, String time) { int attempt = 0; boolean notUpdated = true; while (attempt < 2 && notUpdated) { try { log().info("resolving outage for "+nodeId+":"+ipAddr+":"+serviceId+" with resolution "+dbId+":"+time); String sql = "update outages set svcRegainedEventId=?, ifRegainedService=? where nodeId = ? and ipAddr = ? and serviceId = ? and ifRegainedService is null"; Object values[] = { new Integer(dbId), convertEventTimeToTimeStamp(time), new Integer(nodeId), ipAddr, new Integer(serviceId), }; Updater updater = new Updater(m_dbConnectionFactory, sql); updater.execute(values); notUpdated = false; } catch (Exception e) { if (attempt > 1) { log().fatal("openOutage: Second and final attempt failed opening outage for "+nodeId+":"+ipAddr+":"+serviceId, e); } else { log().info("openOutage: First attempt failed opening outage for "+nodeId+":"+ipAddr+":"+serviceId, e); } } attempt++; } } public void reparentOutages(String ipAddr, int oldNodeId, int newNodeId) { try { log().info("reparenting outages for "+oldNodeId+":"+ipAddr+" to new node "+newNodeId); String sql = "update outages set nodeId = ? where nodeId = ? and ipaddr = ?"; Object[] values = { new Integer(newNodeId), new Integer(oldNodeId), ipAddr, }; Updater updater = new Updater(m_dbConnectionFactory, sql); updater.execute(values); } catch (Exception e) { log().fatal(" Error reparenting outage for "+oldNodeId+":"+ipAddr+" to "+newNodeId, e); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -