⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 outagewriter.java

📁 opennms得相关源码 请大家看看
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        Connection dbConn = null;        try {            dbConn = getConnection();            if (iface.openOutageExists(dbConn)) {                int count = iface.closeOutages(dbConn, eventID, eventTime);                // commit work                DbUtil.commit(dbConn, "handleInterfaceUp: interfaceUp closed " + count + " outages for nodeid/ip " + iface + " in DB", "interfaceUp could not be recorded for nodeId/ipaddr: " + iface);            } else {                log.warn("\'" + EventConstants.INTERFACE_UP_EVENT_UEI + "\' for " + iface + " ignored.");            }        } catch (SQLException se) {            DbUtil.rollback(dbConn, "SQL exception while handling \'interfaceUp\'", se);        } finally {            DbUtil.close(dbConn);        }    }    /**     * Hanlde node regained service events. Record the 'nodeRegainedService'     * event in the outages table - close the outage entry in the table if the     * service is currently down.     * @param svc TODO     */    private void handleNodeRegainedService(long eventID, String eventTime, BasicService svc) {        Category log = ThreadCategory.getInstance(OutageWriter.class);        if (eventID == -1 || !svc.isValid()) {            log.warn(EventConstants.NODE_REGAINED_SERVICE_EVENT_UEI + " ignored - info incomplete - eventid/nodeid/ip/svc: " + eventID + "/" + svc);            return;        }        Connection dbConn = null;        try {            dbConn = getConnection();            if (svc.openOutageExists(dbConn)) {                svc.closeOutage(dbConn, eventID, eventTime);                // commit work                DbUtil.commit(dbConn, "nodeRegainedService: closed outage for nodeid/ip/service " + svc + " in DB", "nodeRegainedService could not be recorded  for nodeId/ipAddr/service: " + svc);            } else {                // Outage table does not have an open record.                log.warn("\'" + EventConstants.NODE_REGAINED_SERVICE_EVENT_UEI + "\' for " + svc + " does not have open record.");            }        } catch (SQLException se) {            DbUtil.rollback(dbConn, "SQL exception while handling \'nodeRegainedService\'", se);        } finally {            DbUtil.close(dbConn);        }    }    /**     * <p>     * Record the 'interfaceReparented' event in the outages table.     * Change'outages' table entries associated with the old nodeid/interface     * pairing so that those outage entries will be associated with the new     * nodeid/interface pairing.     * </p>     *      * <p>     * <strong>Note: </strong>This event has no impact on the event id reference     * fields     * </p>     */    private void handleInterfaceReparented(String ipAddr, Parms eventParms) {        Category log = ThreadCategory.getInstance(OutageWriter.class);        if (log.isDebugEnabled())            log.debug("interfaceReparented event received...");        if (ipAddr == null || eventParms == null) {            log.warn(EventConstants.INTERFACE_REPARENTED_EVENT_UEI + " ignored - info incomplete - ip/parms: " + ipAddr + "/" + eventParms);            return;        }        long oldNodeId = -1;        long newNodeId = -1;        String parmName = null;        Value parmValue = null;        String parmContent = null;        Enumeration parmEnum = eventParms.enumerateParm();        while (parmEnum.hasMoreElements()) {            Parm parm = (Parm) parmEnum.nextElement();            parmName = parm.getParmName();            parmValue = parm.getValue();            if (parmValue == null)                continue;            else                parmContent = parmValue.getContent();            // old nodeid            if (parmName.equals(EventConstants.PARM_OLD_NODEID)) {                try {                    oldNodeId = Integer.valueOf(parmContent).intValue();                } catch (NumberFormatException nfe) {                    log.warn("Parameter " + EventConstants.PARM_OLD_NODEID + " cannot be non-numeric");                    oldNodeId = -1;                }            }            // new nodeid            else if (parmName.equals(EventConstants.PARM_NEW_NODEID)) {                try {                    newNodeId = Integer.valueOf(parmContent).intValue();                } catch (NumberFormatException nfe) {                    log.warn("Parameter " + EventConstants.PARM_NEW_NODEID + " cannot be non-numeric");                    newNodeId = -1;                }            }        }        if (newNodeId == -1 || oldNodeId == -1) {            log.warn("Unable to process 'interfaceReparented' event, invalid event parm.");            return;        }        BasicInterface iface = getInterface(oldNodeId, ipAddr);        Connection dbConn = null;        try {            dbConn = getConnection();            // Set the database commit mode            dbConn.setAutoCommit(false);            // Issue SQL update to change the 'outages' table entries            // associated with the old nodeid/interface pairing            // so that those outage entries will be associated with            // the new nodeid/interface pairing.            // Prepare SQL statement used to reparent outage table entries -            // used when a 'interfaceReparented' event is received            PreparedStatement reparentOutagesStmt = dbConn.prepareStatement(OutageConstants.DB_REPARENT_OUTAGES);            reparentOutagesStmt.setLong(1, newNodeId);            reparentOutagesStmt.setLong(2, iface.getNodeId());            reparentOutagesStmt.setString(3, iface.getIpAddr());            int count = reparentOutagesStmt.executeUpdate();            // commit work            String s = "Reparented " + count + " outages - ip: " + iface.getIpAddr() + " reparented from " + iface.getNodeId() + " to " + newNodeId;            String f = "reparent outages failed for newNodeId/ipAddr: " + newNodeId + "/" + iface.getIpAddr();            DbUtil.commit(dbConn, s, f);            // close statement            reparentOutagesStmt.close();        } catch (SQLException se) {            DbUtil.rollback(dbConn, "SQL exception while handling \'interfaceReparented\'", se);        } finally {            DbUtil.close(dbConn);        }    }    private Connection getConnection() throws SQLException {        return m_outageMgr.getConnection();    }        private BasicNetwork getNetwork() {        return m_outageMgr.getNetwork();    }    /**     * This method creates an event for the passed parameters.     *      * @param uei     *            Event to generate and send     * @param eventDate     *            Time to be set for the event     * @param nodeID     *            Node identifier associated with this event     * @param ipAddr     *            Interface address associated with this event     * @param serviceName     *            Service name associated with this event     */    private Event createEvent(String uei, java.util.Date eventDate, long nodeID, String ipAddr, String serviceName) {        // build event to send        Event newEvent = new Event();        newEvent.setUei(uei);        newEvent.setSource("OutageManager");        // Convert integer nodeID to String        newEvent.setNodeid(nodeID);        if (ipAddr != null)            newEvent.setInterface(ipAddr);        if (serviceName != null)            newEvent.setService(serviceName);        newEvent.setTime(EventConstants.formatToString(eventDate));        return newEvent;    }    /**     * Process an event. Read the event UEI, nodeid, interface and service -     * depending on the UEI, read event parms, if necessary, and process as     * appropriate.     */    private void processEvent() {        Category log = ThreadCategory.getInstance(OutageWriter.class);        if (m_event == null) {            if (log.isDebugEnabled())                log.debug("Event is null, nothing to process");            return;        }        if (log.isDebugEnabled())            log.debug("About to process event: " + m_event.getUei());        //        // Check to make sure the event has a uei        //        String uei = m_event.getUei();        if (uei == null) {            // should only get registered events            if (log.isDebugEnabled())                log.debug("Event received with null UEI, ignoring event");            return;        }        // get eventid        long eventID = -1;        if (m_event.hasDbid())            eventID = m_event.getDbid();        // convert the node id        long nodeID = -1;        if (m_event.hasNodeid())            nodeID = m_event.getNodeid();        String ipAddr = m_event.getInterface();        String service = m_event.getService();        String eventTime = m_event.getTime();        if (log.isDebugEnabled())            log.debug("processEvent: Event\nuei\t\t" + uei + "\neventid\t\t" + eventID + "\nnodeid\t\t" + nodeID + "\nipaddr\t\t" + ipAddr + "\nservice\t\t" + service + "\neventtime\t" + (eventTime != null ? eventTime : "<null>"));        // get service id for the service name        long serviceID = -1;        if (service != null) {            try {                serviceID = getServiceID(service);            } catch (SQLException sqlE) {                log.warn("Error converting service name \"" + service + "\" to an integer identifier, storing -1", sqlE);            }        }        //        // Check for any of the following UEIs:        //        // nodeLostService        // interfaceDown        // nodeDown        // nodeUp        // interfaceUp        // nodeRegainedService        // deleteService        // interfaceReparented        //        if (uei.equals(EventConstants.NODE_LOST_SERVICE_EVENT_UEI)) {            handleNodeLostService(eventID, eventTime, getService(nodeID, ipAddr, serviceID));        } else if (uei.equals(EventConstants.INTERFACE_DOWN_EVENT_UEI)) {            handleInterfaceDown(eventID, eventTime, getInterface(nodeID, ipAddr));        } else if (uei.equals(EventConstants.NODE_DOWN_EVENT_UEI)) {            handleNodeDown(eventID, eventTime, getNode(nodeID));        } else if (uei.equals(EventConstants.NODE_UP_EVENT_UEI)) {            handleNodeUp(eventID, eventTime, getNode(nodeID));        } else if (uei.equals(EventConstants.INTERFACE_UP_EVENT_UEI)) {            handleInterfaceUp(eventID, eventTime, getInterface(nodeID, ipAddr));        } else if (uei.equals(EventConstants.NODE_REGAINED_SERVICE_EVENT_UEI)) {            handleNodeRegainedService(eventID, eventTime, getService(nodeID, ipAddr, serviceID));        } else if (uei.equals(EventConstants.INTERFACE_REPARENTED_EVENT_UEI)) {            handleInterfaceReparented(ipAddr, m_event.getParms());        }    }    private BasicNode getNode(long nodeID) {        return getNetwork().getNode(nodeID);    }    private BasicInterface getInterface(long nodeID, String ipAddr) {        return getNetwork().getInterface(nodeID, ipAddr);    }    private BasicService getService(long nodeID, String ipAddr, long serviceID) {        return getNetwork().getService(nodeID, ipAddr, serviceID);    }        private BasicService getService(BasicInterface iface, long serviceID) {        return getNetwork().getService(iface, serviceID);    }    /**     * The constructor.     * @param mgr     *      * @param event     *            the event for this outage writer.     */    public OutageWriter(OutageManager mgr, Event event) {        m_outageMgr = mgr;        m_network = mgr.getNetwork();        m_event = event;    }    /**     * Process the event depending on the UEI.     */    public void run() {        try {            processEvent();        } catch (Throwable t) {            Category log = ThreadCategory.getInstance(OutageWriter.class);            log.warn("Unexpected exception processing event", t);        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -