📄 notificationmanager.java
字号:
//// This file is part of the OpenNMS(R) Application.//// OpenNMS(R) is Copyright (C) 2006 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.//// 2006 Apr 7: Changed replaceNotification to handle preserving the order.//// 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 = 8package org.opennms.netmgt.config;import java.io.IOException;import java.io.Reader;import java.io.StringWriter;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.sql.Timestamp;import java.sql.Types;import java.util.ArrayList;import java.util.Collection;import java.util.Date;import java.util.Enumeration;import java.util.HashMap;import java.util.LinkedList;import java.util.List;import java.util.Map;import org.apache.log4j.Category;import org.exolab.castor.xml.MarshalException;import org.exolab.castor.xml.Marshaller;import org.exolab.castor.xml.Unmarshaller;import org.exolab.castor.xml.ValidationException;import org.opennms.core.utils.ThreadCategory;import org.opennms.netmgt.EventConstants;import org.opennms.netmgt.config.notifications.Header;import org.opennms.netmgt.config.notifications.Notification;import org.opennms.netmgt.config.notifications.Notifications;import org.opennms.netmgt.config.notifications.Parameter;import org.opennms.netmgt.filter.Filter;import org.opennms.netmgt.filter.FilterParseException;import org.opennms.netmgt.utils.Querier;import org.opennms.netmgt.utils.RowProcessor;import org.opennms.netmgt.utils.SingleResultQuerier;import org.opennms.netmgt.xml.event.Event;/** * @author David Hustace <david@opennms.org> * This base class was refactored from NotificationFactory to support non-global * references during JUnit testing and later to support distributed processes. */public abstract class NotificationManager { /** * Object containing all Notification objects parsed from the xml file */ public Notifications m_notifications; /** * */ private Header oldHeader; public static final String PARAM_TYPE = "-t"; public static final String PARAM_DESTINATION = "-d"; public static final String PARAM_TEXT_MSG = "-tm"; public static final String PARAM_NUM_MSG = "-nm"; public static final String PARAM_RESPONSE = "-r"; public static final String PARAM_NODE = "-nodeid"; public static final String PARAM_INTERFACE = "-interface"; public static final String PARAM_SERVICE = "-service"; public static final String PARAM_SUBJECT = "-subject"; public static final String PARAM_EMAIL = "-email"; public static final String PARAM_PAGER_EMAIL = "-pemail"; public static final String PARAM_XMPP_ADDRESS = "-xmpp"; public static final String PARAM_TEXT_PAGER_PIN = "-tp"; public static final String PARAM_NUM_PAGER_PIN = "-np"; NotifdConfigManager m_configManager; private DbConnectionFactory m_dbConnectionFactory; /** * @param configIn * @throws MarshalException * @throws ValidationException */ protected NotificationManager(NotifdConfigManager configManager, DbConnectionFactory dcf) { m_configManager = configManager; m_dbConnectionFactory = dcf; } public synchronized void parseXML(Reader reader) throws MarshalException, ValidationException { m_notifications = (Notifications) Unmarshaller.unmarshal(Notifications.class, reader); oldHeader = m_notifications.getHeader(); } public boolean hasUei(String uei) throws IOException, MarshalException, ValidationException { update(); for (Enumeration e = m_notifications.enumerateNotification(); e.hasMoreElements();) { Notification notif = (Notification) e.nextElement(); if (uei.equals(notif.getUei()) || "MATCH-ANY-UEI".equals(notif.getUei())) { return true; } } return false; } public Notification[] getNotifForEvent(Event event) throws IOException, MarshalException, ValidationException { update(); Category log = ThreadCategory.getInstance(getClass()); ArrayList notifList = new ArrayList(); Notification[] notif = null; boolean matchAll = getConfigManager().getNotificationMatch(); for (Enumeration e = m_notifications.enumerateNotification(); e.hasMoreElements();) { Notification curNotif = (Notification) e.nextElement(); if (curNotif.getStatus().equals("on") && (event.getUei().equals(curNotif.getUei()) || "MATCH-ANY-UEI".equals(curNotif.getUei())) && nodeInterfaceServiceValid(curNotif, event)) { boolean parmsmatched = getConfigManager().matchNotificationParameters(event, curNotif); if (!parmsmatched) { log.debug("Event " + event.getUei() + " did not match parameters for notice " + curNotif.getName()); continue; } notifList.add(curNotif); log.debug("Event " + event.getUei() + " matched notice " + curNotif.getName()); if (!matchAll) break; } } if (!notifList.isEmpty()) { notif = (Notification[]) notifList.toArray(new Notification[0]); } return notif; } /** * @return */ protected NotifdConfigManager getConfigManager() { return m_configManager; } private boolean nodeInterfaceServiceValid(Notification notif, Event event) { boolean result = false; Connection connection = null; try { // Get the Interface and Service from the Event long eventNode = event.getNodeid(); String eventIf = (String) event.getInterface(); String eventSrv = (String) event.getService(); if (eventNode == 0 && eventIf == null && eventSrv == null) { return true; } // ThreadCategory.getInstance(getClass()).debug("Notification Event // Interface: " + eventIf + " Service: " + eventSrv); // Get the Notification Rule String sql = getInterfaceFilter(notif.getRule()); // ThreadCategory.getInstance(getClass()).debug("getSQL Returned SQL for Notification: " + notif.getName() + ": " + sql); connection = getConnection(); Statement stmt = connection.createStatement(); ResultSet rows = stmt.executeQuery(sql); // Loop through the rows returned from the SQL query and return true // if they match event while (rows.next()) { String notifIf = rows.getString(1); String notifSrv = rows.getString(2); long notifNode = rows.getLong(3); // ThreadCategory.getInstance(getClass()).debug("Notification // Notif Interface: " + notifIf + " Service: " + notifSrv); // if there is no If with the event, there can be no service, // thus check only if the node matches if (eventIf == null || eventIf.equals("0.0.0.0")) { if (eventNode == notifNode) { result = true; break; } } // If there is no Srv with the event, check and see if the If // matches else if (eventSrv == null) { if (eventIf.equals(notifIf)) { result = true; break; } } // Otherwise, insure that both the Srv and If match else if (eventSrv.equals(notifSrv) && eventIf.equals(notifIf)) { result = true; break; } } try { rows.close(); } catch (SQLException e) { } try { stmt.close(); } catch (SQLException e) { } } catch (SQLException e) { ThreadCategory.getInstance(getClass()).error("Filter query threw exception: " + notif.getName() + ": " + notif.getRule(), e); return true; } catch (FilterParseException e) { ThreadCategory.getInstance(getClass()).error("Invalid filter rule for notification " + notif.getName() + ": " + notif.getRule(), e); // go ahead and send the notification return true; } finally { if (connection != null) { try { connection.close(); } catch (SQLException e) { } } } return result; } /** * @param rule * @return */ protected String getInterfaceFilter(String rule) { Filter filter = new Filter(rule); // Select the Interfaces and Services that match the rule String sql = filter.getInterfaceWithServiceStatement(); return sql; } /** * @return * @throws SQLException */ private Connection getConnection() throws SQLException { return m_dbConnectionFactory.getConnection(); } /** * This method wraps the call to the database to get a sequence notice ID * from the database. * * @return int, the sequence id from the database, 0 by default if there is * database trouble */ public synchronized int getNoticeId() throws SQLException, IOException, MarshalException, ValidationException {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -