📄 eventwriter.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.//// 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.eventd;import java.io.IOException;import java.lang.reflect.UndeclaredThrowableException;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Timestamp;import java.sql.Types;import java.util.ArrayList;import java.util.Enumeration;import java.util.List;import org.apache.log4j.Category;import org.exolab.castor.xml.MarshalException;import org.exolab.castor.xml.ValidationException;import org.opennms.core.utils.ThreadCategory;import org.opennms.netmgt.EventConstants;import org.opennms.netmgt.config.DatabaseConnectionFactory;import org.opennms.netmgt.eventd.db.AutoAction;import org.opennms.netmgt.eventd.db.Constants;import org.opennms.netmgt.eventd.db.OperatorAction;import org.opennms.netmgt.eventd.db.Parameter;import org.opennms.netmgt.eventd.db.SnmpInfo;import org.opennms.netmgt.xml.event.Event;import org.opennms.netmgt.xml.event.Header;import org.opennms.netmgt.xml.event.Operaction;/** * EventWriter loads the information in each 'Event' into the database. * * While loading mutiple values of the same element into a single DB column, the * mutiple values are delimited by MULTIPLE_VAL_DELIM. * * When an element and its attribute are loaded into a single DB column, the * value and the attribute are separated by a DB_ATTRIB_DELIM. * * When using delimiters to append values, if the values already have the * delimiter, the delimiter in the value is escaped as in URLs. * * Values for the ' <parms>' block are loaded with each parm name and parm value * delimited with the NAME_VAL_DELIM. * * @see org.opennms.netmgt.eventd.db.Constants#MULTIPLE_VAL_DELIM * @see org.opennms.netmgt.eventd.db.Constants#DB_ATTRIB_DELIM * @see org.opennms.netmgt.eventd.db.Constants#NAME_VAL_DELIM * * @author <A HREF="mailto:sowmya@opennms.org">Sowmya Nataraj </A> * @author <A HREF="http://www.opennms.org">OpenNMS.org </A> */final class EventWriter { // // Field sizes in the events table // private static final int EVENT_UEI_FIELD_SIZE = 256; private static final int EVENT_HOST_FIELD_SIZE = 256; private static final int EVENT_INTERFACE_FIELD_SIZE = 16; private static final int EVENT_DPNAME_FIELD_SIZE = 12; private static final int EVENT_SNMPHOST_FIELD_SIZE = 256; private static final int EVENT_SNMP_FIELD_SIZE = 256; private static final int EVENT_DESCR_FIELD_SIZE = 4000; private static final int EVENT_LOGGRP_FIELD_SIZE = 32; private static final int EVENT_LOGMSG_FIELD_SIZE = 256; private static final int EVENT_PATHOUTAGE_FIELD_SIZE = 1024; private static final int EVENT_CORRELATION_FIELD_SIZE = 1024; private static final int EVENT_OPERINSTRUCT_FIELD_SIZE = 1024; private static final int EVENT_AUTOACTION_FIELD_SIZE = 256; private static final int EVENT_OPERACTION_FIELD_SIZE = 256; private static final int EVENT_OPERACTION_MENU_FIELD_SIZE = 64; private static final int EVENT_NOTIFICATION_FIELD_SIZE = 128; private static final int EVENT_TTICKET_FIELD_SIZE = 128; private static final int EVENT_FORWARD_FIELD_SIZE = 256; private static final int EVENT_MOUSEOVERTEXT_FIELD_SIZE = 64; private static final int EVENT_ACKUSER_FIELD_SIZE = 256; private static final int EVENT_SOURCE_FIELD_SIZE = 128; /** * The character to put in if the log or display is to be set to yes */ private static char MSG_YES = 'Y'; /** * The character to put in if the log or display is to be set to no */ private static char MSG_NO = 'N'; /** * The database connection */ private Connection m_dbConn; /** * SQL statement to get service id for a service name */ private PreparedStatement m_getSvcIdStmt; /** * SQL statement to get hostname for an ip from the ipinterface table */ private PreparedStatement m_getHostNameStmt; /** * SQL statement to get next event id from sequence */ private PreparedStatement m_getNextEventIdStmt; /** * SQL statement to get insert an event into the db */ private PreparedStatement m_eventInsStmt; /** * Sets the statement up for a String value. * * @param stmt * The statement to add the value to. * @param ndx * The ndx for the value. * @param value * The value to add to the statement. * * @exception java.sql.SQLException * Thrown if there is an error adding the value to the * statement. */ private void set(PreparedStatement stmt, int ndx, String value) throws SQLException { if (value == null || value.length() == 0) { stmt.setNull(ndx, Types.VARCHAR); } else { stmt.setString(ndx, value); } } /** * Sets the statement up for an integer type. If the integer type is less * than zero, then it is set to null! * * @param stmt * The statement to add the value to. * @param ndx * The ndx for the value. * @param value * The value to add to the statement. * * @exception java.sql.SQLException * Thrown if there is an error adding the value to the * statement. */ private void set(PreparedStatement stmt, int ndx, int value) throws SQLException { if (value < 0) { stmt.setNull(ndx, Types.INTEGER); } else { stmt.setInt(ndx, value); } } /** * Sets the statement up for a timestamp type. * * @param stmt * The statement to add the value to. * @param ndx * The ndx for the value. * @param value * The value to add to the statement. * * @exception java.sql.SQLException * Thrown if there is an error adding the value to the * statement. */ private void set(PreparedStatement stmt, int ndx, Timestamp value) throws SQLException { if (value == null) { stmt.setNull(ndx, Types.TIMESTAMP); } else { stmt.setTimestamp(ndx, value); } } /** * Sets the statement up for a character value. * * @param stmt * The statement to add the value to. * @param ndx * The ndx for the value. * @param value * The value to add to the statement. * * @exception java.sql.SQLException * Thrown if there is an error adding the value to the * statement. */ private void set(PreparedStatement stmt, int ndx, char value) throws SQLException { stmt.setString(ndx, String.valueOf(value)); } /** * This method is used to convert the service name into a service id. It * first looks up the information from a service map of Eventd 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. * * @param name * The name of the service * * @return The integer identifier for the service name. * * @exception java.sql.SQLException * Thrown if there is an error accessing the stored data or * the SQL text is malformed. This will also be thrown if the * result cannot be obtained. * * @see EventdConstants#SQL_DB_SVCNAME_TO_SVCID * */ private int 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 persistd // int id = Eventd.getServiceID(name); if (id != -1) return id; // // talk to the database and get the identifer // m_getSvcIdStmt.setString(1, name); ResultSet rset = m_getSvcIdStmt.executeQuery(); if (rset.next()) { id = rset.getInt(1); } // close result set rset.close(); // inform persistd about the new find // if (id != -1) Eventd.addServiceMapping(name, id); // // return the id to the caller // return id; } /** * This method is used to convert the event host into a hostname id by * performing a lookup in the database. If the conversion is successful then * the corresponding hosname will be returned to the caller. * * @param hostip * The event host * * @return The hostname * * @exception java.sql.SQLException * Thrown if there is an error accessing the stored data or * the SQL text is malformed. * * @see EventdConstants#SQL_DB_HOSTIP_TO_HOSTNAME * */ private String getHostName(String hostip) throws SQLException { // // talk to the database and get the identifer // String hostname = hostip; m_getHostNameStmt.setString(1, hostip); ResultSet rset = m_getHostNameStmt.executeQuery(); if (rset.next()) { hostname = rset.getString(1); } // close and free the result set // rset.close(); // hostname can be null - if it is, return the ip // if (hostname == null) hostname = hostip; // // return the hostname to the caller // return hostname; } /** * Insert values into the EVENTS table *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -