broadcasteventprocessor.java
来自「opennms得相关源码 请大家看看」· Java 代码 · 共 857 行 · 第 1/3 页
JAVA
857 行
//// 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://// 2004 Nov 22: Fixed problem with notifications for threshold events on non-IP interfaces.// 2004 Aug 26: Added the ability to trigger notifications on an event and a parameter.// 2003 Sep 30: Added a change to support SNMP Thresholding notices.// 2003 Jan 31: Cleaned up some unused imports.// 2002 Nov 09: Added code to allow an event to match more than one notice.// 2002 Oct 29: Added the ability to include event files in eventconf.xml.// 2002 Oct 24: Option to auto-acknowledge "up" notifications as well as "down". Bug #544.// // 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.notifd;import java.io.IOException;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.Calendar;import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import org.apache.log4j.Category;import org.apache.regexp.RE;import org.apache.regexp.RESyntaxException;import org.exolab.castor.xml.MarshalException;import org.exolab.castor.xml.ValidationException;import org.opennms.core.utils.ThreadCategory;import org.opennms.core.utils.TimeConverter;import org.opennms.netmgt.EventConstants;import org.opennms.netmgt.config.NotifdConfigManager;import org.opennms.netmgt.config.NotificationManager;import org.opennms.netmgt.config.PollOutagesConfigManager;import org.opennms.netmgt.config.destinationPaths.Escalate;import org.opennms.netmgt.config.destinationPaths.Path;import org.opennms.netmgt.config.destinationPaths.Target;import org.opennms.netmgt.config.groups.Group;import org.opennms.netmgt.config.notifd.AutoAcknowledge;import org.opennms.netmgt.config.notificationCommands.Command;import org.opennms.netmgt.config.notifications.Notification;import org.opennms.netmgt.config.notifications.Parameter;import org.opennms.netmgt.config.users.Contact;import org.opennms.netmgt.config.users.User;import org.opennms.netmgt.eventd.EventIpcManager;import org.opennms.netmgt.eventd.EventListener;import org.opennms.netmgt.eventd.EventUtil;import org.opennms.netmgt.utils.RowProcessor;import org.opennms.netmgt.xml.event.Event;import org.opennms.netmgt.xml.event.Logmsg;/** * * @author <a href="mailto:weave@oculan.com">Brian Weaver </a> * @author <a href="http://www.opennms.org/">OpenNMS </a> */public final class BroadcastEventProcessor implements EventListener { /** */ private Map m_noticeQueues; /** * A regular expression for matching an expansion parameter delimited by * percent signs. */ private static final String NOTIFD_EXPANSION_PARM = "%(noticeid)%"; private static RE notifdExpandRE; private Notifd m_notifd; /** * Initializes the expansion regular expression. The exception is going to * be thrown away if the RE can't be compiled, thus the complilation should * be tested prior to runtime. */ static { try { notifdExpandRE = new RE(NOTIFD_EXPANSION_PARM); } catch (RESyntaxException e) { // this shouldn't throw an exception, should be tested prior to // runtime ThreadCategory.getInstance(BroadcastEventProcessor.class).error("failed to compile RE " + NOTIFD_EXPANSION_PARM, e); } } /** * This constructor is called to initilize the event receiver. A connection * to the message server is opened and this instance is setup as the * endpoint for broadcast events. When a new event arrives it is processed * and the appropriate action is taken. * */ BroadcastEventProcessor(Notifd notifd, Map noticeQueues) { // set up the exectuable queue first // m_notifd = notifd; m_noticeQueues = noticeQueues; // start to listen for events getEventManager().addEventListener(this); } /** * Unsubscribe from eventd */ public void close() { getEventManager().removeEventListener(this); } /** * @return */ private EventIpcManager getEventManager() { return m_notifd.getEventManager(); } /** * This method is invoked by the EventIpcManager when a new event is * available for processing. * * @param event * The event . */ public void onEvent(Event event) { if (event == null) return; String status = "off"; try { status = getConfigManager().getNotificationStatus(); } catch (Exception e) { ThreadCategory.getInstance(getClass()).error("error getting notifd status, assuming status = 'off' for now: ", e); } if (status.equals("on")) { scheduleNoticesForEvent(event); } else { Category log = ThreadCategory.getInstance(getClass()); if (log.isDebugEnabled()) log.debug("discarding event " + event.getUei() + ", notifd status = " + status); } automaticAcknowledge(event); } // end onEvent() /** * */ private void automaticAcknowledge(Event event) { try { Collection ueis = getConfigManager().getConfiguration().getAutoAcknowledgeCollection(); // see if this event has an auto acknowledge for a notice Iterator i = ueis.iterator(); while (i.hasNext()) { AutoAcknowledge curAck = (AutoAcknowledge) i.next(); if (curAck.getUei().equals(event.getUei())) { try { ThreadCategory.getInstance(getClass()).debug("Acknowledging event " + curAck.getAcknowledge() + " " + event.getNodeid() + ":" + event.getInterface() + ":" + event.getService()); Collection notifIDs = getNotificationManager().acknowledgeNotice(event, curAck.getAcknowledge(), curAck.getMatch()); try { if (curAck.getNotify()) { sendResolvedNotifications(notifIDs, event, curAck.getAcknowledge(), curAck.getMatch(), curAck.getResolutionPrefix()); } } catch (Exception e) { ThreadCategory.getInstance(getClass()).error("Failed to send resolution notifications.", e); } } catch (SQLException e) { ThreadCategory.getInstance(getClass()).error("Failed to auto acknowledge notice.", e); } } } } catch (Exception e) { ThreadCategory.getInstance(getClass()).error("Unable to auto acknowledge notice due to exception.", e); } } private void sendResolvedNotifications(Collection notifIDs, Event event, String acknowledge, String[] match, String resolutionPrefix) throws Exception { Category log = ThreadCategory.getInstance(getClass()); for (Iterator it = notifIDs.iterator(); it.hasNext();) { int notifId = ((Integer) it.next()).intValue(); boolean wa = false; if(notifId < 0) { notifId *= -1; wa = true; log.debug("Conditional autoNotify for notifId " + notifId); } final boolean wasAcked = wa; final Map parmMap = rebuildParameterMap(notifId, resolutionPrefix); String queueID = getNotificationManager().getQueueForNotification(notifId); final Map userNotifitcations = new HashMap(); RowProcessor acknowledgeNotification = new RowProcessor() { public void processRow(ResultSet rs) throws SQLException { String userID = rs.getString("userID"); String contactInfo = rs.getString("contactinfo"); String autoNotifyChar = rs.getString("autonotify"); if(userID.equals("email-address")) { userID = contactInfo; } String cmd = rs.getString("media"); if(autoNotifyChar == null) { autoNotifyChar = "C"; } if(autoNotifyChar.equals("Y") || (autoNotifyChar.equals("C") && !wasAcked)) { List cmdList = (List) userNotifitcations.get(userID); if (cmdList == null) { cmdList = new ArrayList(); userNotifitcations.put(userID, cmdList); } cmdList.add(cmd); } } }; getNotificationManager().forEachUserNotification(notifId, acknowledgeNotification); for (Iterator userIt = userNotifitcations.keySet().iterator(); userIt.hasNext();) { String userID = (String) userIt.next(); List cmdList = (List) userNotifitcations.get(userID); String[] cmds = (String[]) cmdList.toArray(new String[cmdList.size()]); log.debug("Sending " + resolutionPrefix + " notification to userID = " + userID + " for notice ID " + notifId); sendResolvedNotificationsToUser(queueID, userID, cmds, parmMap); } } } protected void sendResolvedNotificationsToUser(String queueID, String targetName, String[] commands, Map params) throws Exception { int noticeId = -1; NoticeQueue noticeQueue = (NoticeQueue) m_noticeQueues.get(queueID); long now = System.currentTimeMillis(); if (m_notifd.getUserManager().hasUser(targetName)) { NotificationTask newTask = makeUserTask(now, params, noticeId, targetName, commands, null, null); if (newTask != null) { noticeQueue.put(new Long(now), newTask); } } else if (targetName.indexOf("@") > -1) { NotificationTask newTask = makeEmailTask(now, params, noticeId, targetName, commands, null, null); if (newTask != null) { noticeQueue.put(new Long(now), newTask); } } else { Category log = ThreadCategory.getInstance(getClass()); log.warn("Unrecognized target '" + targetName + "' contained in destinationPaths.xml. Please check the configuration."); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?