Search in sources :

Example 51 with DBUtils

use of org.opennms.core.utils.DBUtils in project opennms by OpenNMS.

the class DbIpInterfaceEntry method load.

/**
 * Load the current interface from the database. If the interface was
 * modified, the modifications are lost. The nodeid and ip address must be
 * set prior to this call.
 *
 * @param c
 *            The connection used to load the data.
 *
 * @throws java.sql.SQLException
 *             Thrown if an error occurs with the connection
 */
private boolean load(Connection c) throws SQLException {
    if (!m_fromDb) {
        throw new IllegalStateException("The record does not exists in the database");
    }
    // create the Prepared statement and then start setting the query values
    PreparedStatement stmt = null;
    ResultSet rset = null;
    final DBUtils d = new DBUtils(getClass());
    try {
        if (m_useIfIndexAsKey) {
            stmt = c.prepareStatement(SQL_LOAD_REC_IFINDEX);
            d.watch(stmt);
            stmt.setLong(1, m_nodeId);
            stmt.setString(2, InetAddressUtils.str(m_ipAddr));
            stmt.setInt(3, m_ifIndex);
        } else {
            stmt = c.prepareStatement(SQL_LOAD_REC);
            d.watch(stmt);
            stmt.setLong(1, m_nodeId);
            stmt.setString(2, InetAddressUtils.str(m_ipAddr));
        }
        // Execute the query
        rset = stmt.executeQuery();
        d.watch(rset);
        if (!rset.next()) {
            return false;
        }
        // extract the values
        int ndx = 1;
        // get the ifIndex
        m_ifIndex = rset.getInt(ndx++);
        if (rset.wasNull()) {
            m_ifIndex = -1;
        }
        // get the host name
        m_hostname = rset.getString(ndx++);
        if (rset.wasNull()) {
            m_hostname = null;
        }
        // get the managed status
        String str = rset.getString(ndx++);
        if (str != null && rset.wasNull() == false) {
            m_managedState = str.charAt(0);
        } else {
            m_managedState = STATE_UNKNOWN;
        }
        // get the status
        m_status = rset.getInt(ndx++);
        if (rset.wasNull()) {
            m_status = -1;
        }
        // get the time
        m_lastPoll = rset.getTimestamp(ndx++);
        // get the SNMP primary state
        str = rset.getString(ndx++);
        if (str != null && rset.wasNull() == false) {
            m_primaryState = str.charAt(0);
        } else {
            m_primaryState = STATE_UNKNOWN;
        }
    } finally {
        d.cleanUp();
    }
    // clear the mask and mark as backed by the database
    m_changed = 0;
    return true;
}
Also used : ResultSet(java.sql.ResultSet) DBUtils(org.opennms.core.utils.DBUtils) PreparedStatement(java.sql.PreparedStatement)

Example 52 with DBUtils

use of org.opennms.core.utils.DBUtils in project opennms by OpenNMS.

the class DbSnmpInterfaceEntry method load.

/**
 * Load the current interface from the database. If the interface was
 * modified, the modifications are lost. The nodeid and ip address must be
 * set prior to this call.
 *
 * @param c
 *            The connection used to load the data.
 *
 * @throws java.sql.SQLException
 *             Thrown if an error occurs with the connection
 */
private boolean load(Connection c) throws SQLException {
    if (!m_fromDb) {
        throw new IllegalStateException("The record does not exists in the database");
    }
    // create the Prepared statement and then start setting the query values
    PreparedStatement stmt = null;
    ResultSet rset = null;
    final DBUtils d = new DBUtils(getClass());
    try {
        stmt = c.prepareStatement(SQL_LOAD_REC);
        d.watch(stmt);
        stmt.setLong(1, m_nodeId);
        stmt.setInt(2, m_ifIndex);
        // Run the query
        rset = stmt.executeQuery();
        d.watch(rset);
        if (!rset.next()) {
            return false;
        }
        // extract the values
        int ndx = 1;
        // get the physical address
        m_physAddr = rset.getString(ndx++);
        if (rset.wasNull()) {
            m_physAddr = null;
        }
        // get the description
        m_ifDescription = rset.getString(ndx++);
        if (rset.wasNull()) {
            m_ifDescription = null;
        }
        // get the type
        m_ifType = rset.getInt(ndx++);
        if (rset.wasNull()) {
            m_ifIndex = -1;
        }
        // get the name
        m_ifName = rset.getString(ndx++);
        if (rset.wasNull()) {
            m_ifName = null;
        }
        // get the speed
        m_ifSpeed = rset.getLong(ndx++);
        if (rset.wasNull()) {
            m_ifSpeed = -1L;
        }
        // get the admin status
        m_ifAdminStatus = rset.getInt(ndx++);
        if (rset.wasNull()) {
            m_ifAdminStatus = -1;
        }
        // get the operational status
        m_ifOperStatus = rset.getInt(ndx++);
        if (rset.wasNull()) {
            m_ifOperStatus = -1;
        }
        // get the alias
        m_ifAlias = rset.getString(ndx++);
        if (rset.wasNull()) {
            m_ifAlias = null;
        }
        // get the collect flag
        m_collect = rset.getString(ndx++);
        if (rset.wasNull()) {
            m_collect = null;
        }
    } finally {
        d.cleanUp();
    }
    return true;
}
Also used : ResultSet(java.sql.ResultSet) DBUtils(org.opennms.core.utils.DBUtils) PreparedStatement(java.sql.PreparedStatement)

Example 53 with DBUtils

use of org.opennms.core.utils.DBUtils in project opennms by OpenNMS.

the class NotificationManager method noticeOutstanding.

/**
 * This method returns a boolean indicating if the page has been responded
 * to by any member of the group the page was sent to.
 *
 * @param noticeId a int.
 * @return a boolean.
 * @throws java.io.IOException if any.
 */
public boolean noticeOutstanding(final int noticeId) throws IOException {
    boolean outstanding = false;
    Connection connection = null;
    final DBUtils d = new DBUtils(getClass());
    try {
        connection = getConnection();
        d.watch(connection);
        final PreparedStatement statement = connection.prepareStatement(getConfigManager().getConfiguration().getOutstandingNoticesSql());
        d.watch(statement);
        statement.setInt(1, noticeId);
        ResultSet results = statement.executeQuery();
        d.watch(results);
        // count how many rows were returned, if there is even one then the
        // page
        // has been responded too.
        int count = 0;
        while (results.next()) {
            count++;
        }
        if (count == 0) {
            outstanding = true;
        }
    } catch (SQLException e) {
        LOG.error("Error getting notice status", e);
    } finally {
        d.cleanUp();
    }
    return outstanding;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) DBUtils(org.opennms.core.utils.DBUtils) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 54 with DBUtils

use of org.opennms.core.utils.DBUtils in project opennms by OpenNMS.

the class JdbcFilterDao method isRuleMatching.

/**
 * {@inheritDoc}
 */
@Override
public boolean isRuleMatching(final String rule) throws FilterParseException {
    boolean matches = false;
    String sqlString;
    LOG.debug("Filter.isRuleMatching({})", rule);
    final DBUtils d = new DBUtils(getClass());
    // get the database connection
    Connection conn = null;
    try {
        conn = getDataSource().getConnection();
        d.watch(conn);
        // parse the rule and get the sql select statement
        sqlString = getSQLStatement(rule) + " LIMIT 1";
        LOG.debug("Filter.isRuleMatching({}): SQL statement: {}", rule, sqlString);
        // execute query and return the list of ip addresses
        final Statement stmt = conn.createStatement();
        d.watch(stmt);
        final ResultSet rset = stmt.executeQuery(sqlString);
        d.watch(rset);
        // we only want to check if zero or one rows were fetched, so just
        // return the output from rset.next()
        matches = rset.next();
        LOG.debug("isRuleMatching: rule \"{}\" {} an entry in the database", rule, matches ? "matches" : "does not match");
    } catch (final FilterParseException e) {
        LOG.warn("Filter Parse Exception occurred testing rule \"{}\" for matching results.", rule, e);
        throw new FilterParseException("Filter Parse Exception occurred testing rule \"" + rule + "\" for matching results: " + e.getLocalizedMessage(), e);
    } catch (final SQLException e) {
        LOG.warn("SQL Exception occurred testing rule \"{}\" for matching results.", e);
        throw new FilterParseException("SQL Exception occurred testing rule \"" + rule + "\" for matching results: " + e.getLocalizedMessage(), e);
    } catch (final Throwable e) {
        LOG.error("Exception getting database connection.", e);
        throw new UndeclaredThrowableException(e);
    } finally {
        d.cleanUp();
    }
    return matches;
}
Also used : FilterParseException(org.opennms.netmgt.filter.api.FilterParseException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) DBUtils(org.opennms.core.utils.DBUtils) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Example 55 with DBUtils

use of org.opennms.core.utils.DBUtils in project opennms by OpenNMS.

the class JdbcFilterDao method getIPAddressList.

private List<InetAddress> getIPAddressList(final String rule, final boolean filterDeleted, final String address) throws FilterParseException {
    final List<InetAddress> resultList = new ArrayList<>();
    final boolean filterByAddress = address != null && address.length() > 0;
    String sqlString;
    LOG.debug("Filter.getIPAddressList({})", rule);
    // get the database connection
    Connection conn = null;
    final DBUtils d = new DBUtils(getClass());
    try {
        // parse the rule and get the sql select statement
        sqlString = getSQLStatement(rule);
        if (filterDeleted) {
            if (!sqlString.contains("isManaged")) {
                sqlString += " AND (ipInterface.isManaged != 'D' or ipInterface.isManaged IS NULL)";
            }
        }
        if (filterByAddress) {
            sqlString += " AND ipInterface.ipaddr = ?";
        }
        conn = getDataSource().getConnection();
        d.watch(conn);
        LOG.debug("Filter.getIPAddressList({}): SQL statement: {}", rule, sqlString);
        // execute query and return the list of ip addresses
        final ResultSet rset;
        if (filterByAddress) {
            final PreparedStatement preparedStatement = conn.prepareStatement(sqlString);
            preparedStatement.setString(1, address);
            d.watch(preparedStatement);
            rset = preparedStatement.executeQuery();
        } else {
            final Statement stmt = conn.createStatement();
            d.watch(stmt);
            rset = stmt.executeQuery(sqlString);
        }
        d.watch(rset);
        // fill up the array list if the result set has values
        if (rset != null) {
            // Iterate through the result and build the array list
            while (rset.next()) {
                resultList.add(addr(rset.getString(1)));
            }
        }
    } catch (final FilterParseException e) {
        LOG.warn("Filter Parse Exception occurred getting IP List.", e);
        throw new FilterParseException("Filter Parse Exception occurred getting IP List: " + e.getLocalizedMessage(), e);
    } catch (final SQLException e) {
        LOG.warn("SQL Exception occurred getting IP List.", e);
        throw new FilterParseException("SQL Exception occurred getting IP List: " + e.getLocalizedMessage(), e);
    } catch (final Throwable e) {
        LOG.error("Exception getting database connection.", e);
        throw new UndeclaredThrowableException(e);
    } finally {
        d.cleanUp();
    }
    LOG.debug("Filter.getIPAddressList({}): resultList = {}", rule, resultList);
    return resultList;
}
Also used : FilterParseException(org.opennms.netmgt.filter.api.FilterParseException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) DBUtils(org.opennms.core.utils.DBUtils) ResultSet(java.sql.ResultSet) InetAddress(java.net.InetAddress)

Aggregations

DBUtils (org.opennms.core.utils.DBUtils)79 Connection (java.sql.Connection)70 PreparedStatement (java.sql.PreparedStatement)70 ResultSet (java.sql.ResultSet)51 SQLException (java.sql.SQLException)17 ArrayList (java.util.ArrayList)14 Timestamp (java.sql.Timestamp)12 Statement (java.sql.Statement)10 Filter (org.opennms.web.filter.Filter)9 ServletException (javax.servlet.ServletException)5 IfIndexFilter (org.opennms.web.event.filter.IfIndexFilter)5 InterfaceFilter (org.opennms.web.event.filter.InterfaceFilter)5 NodeFilter (org.opennms.web.event.filter.NodeFilter)5 ServiceFilter (org.opennms.web.event.filter.ServiceFilter)5 SeverityFilter (org.opennms.web.event.filter.SeverityFilter)5 Date (java.util.Date)4 FilterParseException (org.opennms.netmgt.filter.api.FilterParseException)4 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)3 InetAddress (java.net.InetAddress)3 HashMap (java.util.HashMap)3