📄 outagewriter.java
字号:
//// This file is part of the OpenNMS(R) Application.//// OpenNMS(R) is Copyright (C) 2002-2003 The OpenNMS Group, Inc. All rights reserved.// OpenNMS(R) is a derivative work, containing both original code, included code and modified// code that was published under the GNU General Public License. Copyrights for modified // and included code are below.//// OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.//// Modifications://// 2003 Nov 11: Merged changes from Rackspace project// 2003 Nov 10: Removed event cache calls - too many issues - set outage writer threads to 1// 2003 Jan 31: Cleaned up some unused imports. // 2003 Jan 08: Changed SQL "= null" to "is null" to work with Postgres 7.2// // Original code base Copyright (C) 1999-2001 Oculan Corp. All rights reserved.//// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details. //// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.// // For more information contact: // OpenNMS Licensing <license@opennms.org>// http://www.opennms.org/// http://www.opennms.com///// Tab Size = 8//package org.opennms.netmgt.outage;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.Enumeration;import org.apache.log4j.Category;import org.opennms.core.utils.ThreadCategory;import org.opennms.netmgt.EventConstants;import org.opennms.netmgt.xml.event.Event;import org.opennms.netmgt.xml.event.Parm;import org.opennms.netmgt.xml.event.Parms;import org.opennms.netmgt.xml.event.Value;/** * When a 'nodeLostService' is received, it is made sure that there is no 'open' * outage record in the 'outages' table for this nodeid/ipaddr/serviceid - i.e * that there is not already a record for this n/i/s where the 'lostService' * time is known and the 'regainedService' time is NULL - if there is, the * current 'lostService' event is ignored else a new outage is created. * * The 'interfaceDown' is similar to the 'nodeLostService' except that it acts * relevant to a nodeid/ipaddr combination and a 'nodeDown' acts on a nodeid. * * When a 'nodeRegainedService' is received and there is an 'open' outage for * the nodeid/ipaddr/serviceid, the outage is cleared. If not, the event is * placed in the event cache in case a race condition has occurred that puts the * "up" event in before the "down" event. (currently inactive). * * The 'interfaceUp' is similar to the 'nodeRegainedService' except that it acts * relevant to a nodeid/ipaddr combination and a 'nodeUp' acts on a nodeid. * * When a 'deleteService' is received, the appropriate entry is marked for * deletion is the 'ifservices' table - if this entry is the only entry for a * node/ip combination, the corresponding entry in the 'ipinterface' table is * marked for deletion and this is then cascaded to the node table All deletions * are followed by an appropriate event(serviceDeleted or interfaceDeleted or..) * being generated and sent to eventd. * * When an 'interfaceReparented' event is received, 'outages' table entries * associated with the old nodeid/interface pairing are changed so that those * outage entries will be associated with the new nodeid/interface pairing. * * The nodeLostService, interfaceDown, nodeDown, nodeUp, interfaceUp, * nodeRegainedService, deleteService events update the svcLostEventID and the * svcRegainedEventID fields as approppriate. The interfaceReparented event has * no impact on these eventid reference fields. * * @author <A HREF="mailto:sowmya@opennms.org">Sowmya Nataraj </A> * @author <A HREF="mailto:mike@opennms.org">Mike Davidson </A> * @author <A HREF="http://www.opennms.org">OpenNMS.org </A> */public final class OutageWriter implements Runnable { private static final String SNMP_SVC = "SNMP"; private static final String SNMPV2_SVC = "SNMPv2"; /** * The event from which data is to be read. */ private Event m_event; // Control whether or not an event is generated following // database modifications to notify other OpenNMS processes private boolean m_generateNodeDeletedEvent; private OutageManager m_outageMgr; private BasicNetwork m_network; /** * <P> * This method is used to convert the service name into a service id. It * first looks up the information from a service map in OutagesManager and * if no match is found, by performing a lookup in the database. If the * conversion is successful then the corresponding integer identifier will * be returned to the caller. * </P> * * @param name * The name of the service * * @return The integer identifier for the service name. * * @throws java.sql.SQLException * if there is an error accessing the stored data, the SQL text * is malformed, or the result cannot be obtained. * * @see org.opennms.netmgt.outage.OutageConstants#DB_GET_SVC_ID * DB_GET_SVC_ID */ private long getServiceID(String name) throws SQLException { // // Check the name to make sure that it is not null // if (name == null) throw new NullPointerException("The service name was null"); // ask OutageManager // long id = m_outageMgr.getServiceID(name); if (id != -1) return id; // // talk to the database and get the identifer // Connection dbConn = null; try { dbConn = getConnection(); // SQL statement to get service id for a servicename from the // service table PreparedStatement serviceStmt = dbConn.prepareStatement(OutageConstants.DB_GET_SVC_ID); serviceStmt.setString(1, name); ResultSet rset = serviceStmt.executeQuery(); if (rset.next()) { id = rset.getLong(1); } // close result set rset.close(); // close statement if (serviceStmt != null) serviceStmt.close(); } finally { try { if (dbConn != null) dbConn.close(); } catch (SQLException e) { ThreadCategory.getInstance(getClass()).warn("Exception closing JDBC connection", e); } } // Record the new find // if (id != -1) m_outageMgr.addServiceMapping(name, id); // // return the id to the caller // return id; } /** * Handles node lost service events. Record the 'nodeLostService' event in * the outages table - create a new outage entry if the service is not * already down. */ private void handleNodeLostService(long eventID, String eventTime, BasicService svc) { Category log = ThreadCategory.getInstance(OutageWriter.class); if (eventID == -1 || !svc.isValid()) { log.warn(EventConstants.NODE_LOST_SERVICE_EVENT_UEI + " ignored - info incomplete - eventid/nodeid/ip/svc: " + eventID + "/" + svc); return; } // check that there is no 'open' entry already Connection dbConn = null; try { dbConn = getConnection(); if (!svc.openOutage(dbConn, eventID, eventTime)) { log.warn("\'" + EventConstants.NODE_LOST_SERVICE_EVENT_UEI + "\' for " + svc + " ignored - table already has an open record "); } // commit work DbUtil.commit(dbConn, "nodeLostService : " + svc + " recorded in DB", "outage could not be created for nodeid/ipAddr/service: " + svc); } catch (SQLException sqle) { DbUtil.rollback(dbConn, "SQL exception while handling \'nodeLostService\'", sqle); } finally { DbUtil.close(dbConn); } } /** * Handles interface down events. Record the 'interfaceDown' event in the * outages table - create a new outage entry for each active service of the * nodeid/ip if service not already down. */ private void handleInterfaceDown(long eventID, String eventTime, BasicInterface iface) { if (eventID == -1 || !iface.isValid()) { ThreadCategory.getInstance(getClass()).warn(EventConstants.INTERFACE_DOWN_EVENT_UEI + " ignored - info incomplete - eventid/nodeid/ip: " + eventID + "/" + iface); return; } Connection dbConn = null; try { dbConn = getConnection(); iface.openOutages(dbConn, eventID, eventTime); // commit work DbUtil.commit(dbConn, "Outage recorded for all active services for " + iface, "interfaceDown could not be recorded for nodeid/ipAddr: " + iface); } catch (SQLException sqle) { DbUtil.rollback(dbConn, "SQL exception while handling \'interfaceDown\'", sqle); } finally { DbUtil.close(dbConn); } } /** * Handles node down events. Record the 'nodeDown' event in the outages * table - create a new outage entry for each active service of the nodeid * if service is not already down. */ private void handleNodeDown(long eventID, String eventTime, BasicNode node) { if (eventID == -1 || !node.isValid()) { ThreadCategory.getInstance(getClass()).warn(EventConstants.NODE_DOWN_EVENT_UEI + " ignored - info incomplete - eventid/nodeid: " + eventID + "/" + node); return; } Connection dbConn = null; try { dbConn = getConnection(); node.openOutages(dbConn, eventID, eventTime); // commit work DbUtil.commit(dbConn, "Outage recorded for all active services for " + node, "nodeDown could not be recorded for nodeId: " + node); } catch (SQLException sqle) { DbUtil.rollback(dbConn, "SQL exception while handling \'nodeDown\'", sqle); } finally { DbUtil.close(dbConn); } } /** * Handle node up events. Record the 'nodeUp' event in the outages table - * close all open outage entries for the nodeid in the outages table. */ private void handleNodeUp(long eventID, String eventTime, BasicNode node) { Category log = ThreadCategory.getInstance(OutageWriter.class); if (eventID == -1 || !node.isValid()) { log.warn(EventConstants.NODE_UP_EVENT_UEI + " ignored - info incomplete - eventid/nodeid: " + eventID + "/" + node); return; } Connection dbConn = null; try { dbConn = getConnection(); int count = 0; if (node.openOutageExists(dbConn)) { count = node.closeOutages(dbConn, eventID, eventTime); } else { // Outage table does not have an open record. log.warn("\'" + EventConstants.NODE_UP_EVENT_UEI + "\' for " + node + " no open record."); } // commit work DbUtil.commit(dbConn, "nodeUp closed " + count + " outages for nodeid " + node + " in DB", "nodeUp could not be recorded for nodeId: " + node); } catch (SQLException se) { DbUtil.rollback(dbConn, "SQL exception while handling \'nodeRegainedService\'", se); } finally { DbUtil.close(dbConn); } } /** * Handles interface up events. Record the 'interfaceUp' event in the * outages table - close all open outage entries for the nodeid/ip in the * outages table. */ private void handleInterfaceUp(long eventID, String eventTime, BasicInterface iface) { Category log = ThreadCategory.getInstance(OutageWriter.class); if (eventID == -1 || !iface.isValid()) { log.warn(EventConstants.INTERFACE_UP_EVENT_UEI + " ignored - info incomplete - eventid/nodeid/ipAddr: " + eventID + "/" + iface); return; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -