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);
}
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;
}
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);
}
}
}
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);
}
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;
}
Aggregations