Search in sources :

Example 1 with FilterParseException

use of org.opennms.netmgt.filter.api.FilterParseException in project opennms by OpenNMS.

the class JdbcFilterDao method getNodeMap.

/**
 * {@inheritDoc}
 *
 * This method returns a map of all nodeids and nodelabels that match
 * the rule that is passed in, sorted by nodeid.
 * @exception FilterParseException
 *                if a rule is syntactically incorrect or failed in
 *                executing the SQL statement
 */
@Override
public SortedMap<Integer, String> getNodeMap(final String rule) throws FilterParseException {
    final SortedMap<Integer, String> resultMap = new TreeMap<Integer, String>();
    String sqlString;
    LOG.debug("Filter.getNodeMap({})", rule);
    // get the database connection
    Connection conn = null;
    final DBUtils d = new DBUtils(getClass());
    try {
        conn = getDataSource().getConnection();
        d.watch(conn);
        // parse the rule and get the sql select statement
        sqlString = getNodeMappingStatement(rule);
        LOG.debug("Filter.getNodeMap({}): SQL statement: {}", rule, sqlString);
        // execute query
        final Statement stmt = conn.createStatement();
        d.watch(stmt);
        final ResultSet rset = stmt.executeQuery(sqlString);
        d.watch(rset);
        if (rset != null) {
            // Iterate through the result and build the map
            while (rset.next()) {
                resultMap.put(Integer.valueOf(rset.getInt(1)), rset.getString(2));
            }
        }
    } catch (final FilterParseException e) {
        LOG.warn("Filter Parse Exception occurred getting node map.", e);
        throw new FilterParseException("Filter Parse Exception occurred getting node map: " + e.getLocalizedMessage(), e);
    } catch (final SQLException e) {
        LOG.warn("SQL Exception occurred getting node map.", e);
        throw new FilterParseException("SQL Exception occurred getting node map: " + e.getLocalizedMessage(), e);
    } catch (final Throwable e) {
        LOG.error("Exception getting database connection.", e);
        throw new UndeclaredThrowableException(e);
    } finally {
        d.cleanUp();
    }
    return Collections.unmodifiableSortedMap(resultMap);
}
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) Connection(java.sql.Connection) DBUtils(org.opennms.core.utils.DBUtils) ResultSet(java.sql.ResultSet) TreeMap(java.util.TreeMap)

Example 2 with FilterParseException

use of org.opennms.netmgt.filter.api.FilterParseException in project opennms by OpenNMS.

the class JdbcFilterDao method getIPAddressServiceMap.

/**
 * {@inheritDoc}
 */
@Override
public Map<InetAddress, Set<String>> getIPAddressServiceMap(final String rule) throws FilterParseException {
    final Map<InetAddress, Set<String>> ipServices = new TreeMap<InetAddress, Set<String>>(new InetAddressComparator());
    String sqlString;
    LOG.debug("Filter.getIPAddressServiceMap({})", rule);
    // get the database connection
    Connection conn = null;
    final DBUtils d = new DBUtils(getClass());
    try {
        conn = getDataSource().getConnection();
        d.watch(conn);
        // parse the rule and get the sql select statement
        sqlString = getIPServiceMappingStatement(rule);
        LOG.debug("Filter.getIPAddressServiceMap({}): SQL statement: {}", rule, sqlString);
        // execute query
        final Statement stmt = conn.createStatement();
        d.watch(stmt);
        final ResultSet 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()) {
                final InetAddress ipaddr = addr(rset.getString(1));
                if (ipaddr != null) {
                    if (!ipServices.containsKey(ipaddr)) {
                        ipServices.put(ipaddr, new TreeSet<String>());
                    }
                    ipServices.get(ipaddr).add(rset.getString(2));
                }
            }
        }
    } catch (final FilterParseException e) {
        LOG.warn("Filter Parse Exception occurred getting IP Service List.", e);
        throw new FilterParseException("Filter Parse Exception occurred getting IP Service List: " + e.getLocalizedMessage(), e);
    } catch (final SQLException e) {
        LOG.warn("SQL Exception occurred getting IP Service List.", e);
        throw new FilterParseException("SQL Exception occurred getting IP Service List: " + e.getLocalizedMessage(), e);
    } catch (final RuntimeException e) {
        LOG.error("Unexpected exception getting database connection.", e);
        throw e;
    } catch (final Error e) {
        LOG.error("Unexpected exception getting database connection.", e);
        throw e;
    } finally {
        d.cleanUp();
    }
    return ipServices;
}
Also used : TreeSet(java.util.TreeSet) ResultSet(java.sql.ResultSet) Set(java.util.Set) FilterParseException(org.opennms.netmgt.filter.api.FilterParseException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) TreeMap(java.util.TreeMap) InetAddressComparator(org.opennms.core.utils.InetAddressComparator) DBUtils(org.opennms.core.utils.DBUtils) ResultSet(java.sql.ResultSet) InetAddress(java.net.InetAddress)

Example 3 with FilterParseException

use of org.opennms.netmgt.filter.api.FilterParseException in project opennms by OpenNMS.

the class DataManager method nodeGainedService.

/**
 * Handles a node gained service event. Add a new entry to the map and the
 * categories on a 'serviceGained' event
 *
 * @param nodeid
 *            the node id
 * @param ip
 *            the IP address
 * @param svcName
 *            the service name
 */
public synchronized void nodeGainedService(int nodeid, InetAddress ip, String svcName) {
    // 
    // check the 'status' flag for the service
    // 
    String svcStatus = m_monitoredServiceDao.get((int) nodeid, ip, svcName).getStatus();
    // 
    if (!"A".equals(svcStatus)) {
        LOG.info("nodeGainedSvc: {}/{}/{} IGNORED because status is not active: {}", nodeid, ip, svcName, svcStatus);
    } else {
        LOG.debug("nodeGainedSvc: {}/{}/{}/{}", nodeid, ip, svcName, svcStatus);
        // I ran into problems with adding new services, so I just ripped
        // all that out and added
        // a call to the rescan method. -T
        // Hrm - since the rules can be based on things other than the
        // service name
        // we really need to rescan every time a new service is discovered.
        // For
        // example, if I have a category where the rule is "ipaddr =
        // 10.1.1.1 & isHTTP"
        // yet I only have ICMP in the service list, the node will not be
        // added when
        // HTTP is discovered, because it is not in the services list.
        // 
        // This is mainly useful when SNMP is discovered on a node.
        LOG.debug("rtcN : Rescanning services on : {}", ip);
        try {
            rtcNodeRescan(nodeid);
        } catch (FilterParseException ex) {
            LOG.warn("Failed to unmarshall database config", ex);
            throw new UndeclaredThrowableException(ex);
        } catch (SQLException ex) {
            LOG.warn("Failed to get database connection", ex);
            throw new UndeclaredThrowableException(ex);
        } catch (RTCException ex) {
            LOG.warn("Failed to get database connection", ex);
            throw new UndeclaredThrowableException(ex);
        }
    }
}
Also used : FilterParseException(org.opennms.netmgt.filter.api.FilterParseException) SQLException(java.sql.SQLException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException)

Example 4 with FilterParseException

use of org.opennms.netmgt.filter.api.FilterParseException in project opennms by OpenNMS.

the class NotificationWizardServlet method processRule.

private String processRule(final HttpServletRequest request, final HttpSession user) {
    String ruleString = request.getParameter("newRule");
    ruleString = toSingleQuote(ruleString);
    ruleString = stripExtraWhite(ruleString);
    ruleString = stripServices(ruleString);
    ruleString = checkParens(ruleString);
    final StringBuilder rule = new StringBuilder(ruleString);
    final String[] services = request.getParameterValues("services");
    if (services != null) {
        rule.append(" & ").append(" (");
        for (int i = 0; i < services.length; i++) {
            rule.append("is").append(services[i]);
            if (i < services.length - 1) {
                rule.append(" | ");
            }
        }
        rule.append(" )");
    }
    final String[] notServices = request.getParameterValues("notServices");
    if (notServices != null) {
        rule.append(" & ").append(" (");
        for (int i = 0; i < notServices.length; i++) {
            rule.append("!is").append(notServices[i]);
            if (i < notServices.length - 1) {
                rule.append(" & ");
            }
        }
        rule.append(" )");
    }
    final Map<String, Object> params = new HashMap<String, Object>();
    params.put("newRule", rule.toString());
    if (services != null) {
        params.put("services", services);
    }
    if (notServices != null) {
        params.put("notServices", notServices);
    }
    // page to redirect to, either validate or skip validation
    String redirectPage = request.getParameter("nextPage");
    // now lets see if the rule is syntactically valid
    try {
        getFilterDao().validateRule(rule.toString());
    } catch (final FilterParseException e) {
        // page to redirect to if the rule is invalid
        params.put("mode", "failed");
        redirectPage = SOURCE_PAGE_RULE;
    }
    // save the rule if we are bypassing validation
    if (redirectPage.equals(SOURCE_PAGE_PATH)) {
        final Notification newNotice = (Notification) user.getAttribute("newNotice");
        newNotice.setRule(rule.toString());
    }
    return redirectPage + makeQueryString(params);
}
Also used : FilterParseException(org.opennms.netmgt.filter.api.FilterParseException) HashMap(java.util.HashMap) Notification(org.opennms.netmgt.config.notifications.Notification)

Example 5 with FilterParseException

use of org.opennms.netmgt.filter.api.FilterParseException 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)

Aggregations

FilterParseException (org.opennms.netmgt.filter.api.FilterParseException)8 SQLException (java.sql.SQLException)6 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)4 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 Statement (java.sql.Statement)4 DBUtils (org.opennms.core.utils.DBUtils)4 InetAddress (java.net.InetAddress)2 ArrayList (java.util.ArrayList)2 TreeMap (java.util.TreeMap)2 HashMap (java.util.HashMap)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 Matcher (java.util.regex.Matcher)1 InetAddressComparator (org.opennms.core.utils.InetAddressComparator)1 Notification (org.opennms.netmgt.config.notifications.Notification)1 RTCHashMap (org.opennms.netmgt.rtc.datablock.RTCHashMap)1 TransactionStatus (org.springframework.transaction.TransactionStatus)1 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)1