use of org.krupczak.xmp.XmpMessage in project opennms by OpenNMS.
the class XmpUtil method handleTableQuery.
/**
* <p>handleTableQuery</p>
*
* @param session a {@link org.krupczak.xmp.XmpSession} object.
* @param mib a {@link java.lang.String} object.
* @param table a {@link java.lang.String} object.
* @param object a {@link java.lang.String} object.
* @param instance a {@link java.lang.String} object.
* @param instanceRegex a {@link java.util.regex.Pattern} object.
* @param valueOperator a {@link java.lang.String} object.
* @param valueOperand a {@link java.lang.String} object.
* @param minMatches a int.
* @param maxMatches a int.
* @param maxMatchesUnbounded a boolean.
* @param caseSensitive a boolean.
* @return a boolean.
* @throws org.opennms.netmgt.protocols.xmp.XmpUtilException if any.
*/
public static boolean handleTableQuery(XmpSession session, String mib, String table, String object, String instance, Pattern instanceRegex, String valueOperator, String valueOperand, int minMatches, int maxMatches, boolean maxMatchesUnbounded, boolean caseSensitive) throws XmpUtilException {
XmpMessage reply;
String[] tableInfo = new String[3];
XmpVar[] queryVars = new XmpVar[1];
XmpVar[] replyVars;
int numMatches = 0;
queryVars[0] = new XmpVar(mib, object, Xmp.SYNTAX_NULLSYNTAX);
tableInfo[0] = mib;
tableInfo[1] = object;
tableInfo[2] = instance;
reply = session.queryTableVars(tableInfo, 0, queryVars);
if (reply == null) {
LOG.warn("handleTableQuery: query for object {} from MIB {} failed, {}", object, mib, Xmp.errorStatusToString(session.getErrorStatus()));
throw new XmpUtilException("XMP query failed (MIB " + mib + ", object " + object + "): " + Xmp.errorStatusToString(session.getErrorStatus()));
}
replyVars = reply.getMIBVars();
LOG.debug("handleTableQuery: Got reply with {} variables", replyVars.length);
/* Since we're constrained to a single object, we know that there's
* exactly one column in the result set and so can use a Java 5
* for() loop. If there were multiple columns, we'd have to break the
* flat array into a two-dimensional matrix using a pair of old-style
* for() loops.
*/
for (XmpVar thisVar : replyVars) {
String rowInstance = thisVar.getKey();
if ((instanceRegex != null) && (!instanceRegex.matcher(rowInstance).matches())) {
LOG.debug("handleTableQuery: instance {} does not match, skipping this row.", rowInstance);
// to next var
continue;
} else if (instanceRegex == null) {
LOG.debug("handleTableQuery: instance match not specified, evaluating value of instance {}", rowInstance);
} else {
LOG.debug("handleTableQuery: instance {} matches, evaluating value", rowInstance);
}
if (valueMeetsCriteria(thisVar, valueOperator, valueOperand, caseSensitive)) {
numMatches++;
}
}
if (numMatches >= minMatches) {
LOG.debug("handleTableQuery: Found {} matches, meets specified minimum of {}", numMatches, minMatches);
if (maxMatchesUnbounded) {
LOG.debug("handleTableQuery: Maximum matches unbounded, returning true");
return true;
} else if (numMatches <= maxMatches) {
LOG.debug("handleTableQuery: Found {} matches, meets specified maximum of {}, returning true", numMatches, maxMatches);
return true;
} else {
LOG.debug("handleTableQuery: Found {} matches, exceeds specified maximum of {}, returning false", numMatches, maxMatches);
throw new XmpUtilException("Found too many value matches (" + numMatches + " > " + maxMatches + ") for condition " + mib + "." + object + " " + valueOperator + " " + valueOperand);
}
} else {
LOG.debug("Found only {} matches, too few to meet specified minimum of {}", numMatches, minMatches);
throw new XmpUtilException("Found too few value matches (" + numMatches + " < " + minMatches + ") for condition " + mib + "." + object + " " + valueOperator + " " + valueOperand);
}
}
use of org.krupczak.xmp.XmpMessage in project opennms by OpenNMS.
the class XmpUtil method handleScalarQuery.
/**
* <p>handleScalarQuery</p>
*
* @param session a {@link org.krupczak.xmp.XmpSession} object.
* @param mib a {@link java.lang.String} object.
* @param object a {@link java.lang.String} object.
* @param valueOperator a {@link java.lang.String} object.
* @param valueOperand a {@link java.lang.String} object.
* @param caseSensitive a boolean.
* @return a boolean.
* @throws org.opennms.netmgt.protocols.xmp.XmpUtilException if any.
*/
public static boolean handleScalarQuery(XmpSession session, String mib, String object, String valueOperator, String valueOperand, boolean caseSensitive) throws XmpUtilException {
XmpMessage reply;
XmpVar[] queryVars = new XmpVar[1];
XmpVar[] replyVars;
queryVars[0] = new XmpVar(mib, object, Xmp.SYNTAX_NULLSYNTAX);
reply = session.queryVars(queryVars);
if (reply == null) {
LOG.warn("handleScalarQuery: query for object {} from MIB {} failed, {}", object, mib, Xmp.errorStatusToString(session.getErrorStatus()));
return false;
} else {
LOG.debug("handleScalarQuery: query for object {} from MIB {} succeeded.", object, mib);
}
replyVars = reply.getMIBVars();
if (replyVars[0].getMibName().equals(mib) && replyVars[0].getObjName().equals(object)) {
return valueMeetsCriteria(replyVars[0], valueOperator, valueOperand, caseSensitive);
} else {
LOG.error("Observed MIB name ({}) or object name ({}) does not match specified MIB name ({}) or object name ({}), giving up", replyVars[0].getMibName(), replyVars[0].getObjName(), mib, object);
throw new XmpUtilException("Received unexpected response (MIB: " + replyVars[0].getMibName() + " Object: " + replyVars[0].getObjName());
}
}
use of org.krupczak.xmp.XmpMessage in project opennms by OpenNMS.
the class XmpCollector method handleScalarQuery.
/* private methods *********************************** */
// handle scalar query and put in a single collection resource
// devoted to scalars; check sysUptime if its present
// and indicate if data should be persisted
private boolean handleScalarQuery(String groupName, CollectionAgent agent, CollectionSetBuilder collectionSetBuilder, long oldUptime, XmpSession session, NodeLevelResource nodeLevelResource, XmpVar[] queryVars) {
XmpMessage reply;
long newUptime;
int i;
XmpVar[] vars;
//log().debug("sending scalar query");
reply = session.queryVars(queryVars);
if (reply == null) {
LOG.warn("collect: query to {} failed, {}", agent, Xmp.errorStatusToString(session.getErrorStatus()));
return false;
}
// for each variable in reply, store it in collectionSet
// hack alert: somewhere in some query, we asked for
// sysUptime; find it save value for later
// vars[i] should match up with mibObjects[i] !!!
vars = reply.getMIBVars();
newUptime = 0;
for (i = 0; i < vars.length; i++) {
if (vars[i].getMibName().equals("core") && vars[i].getObjName().equals("sysUpTime")) {
newUptime = vars[i].getValueLong();
}
// put in collectionSet via this attribute group
final XmpVar xmpVar = vars[i];
collectionSetBuilder.withAttribute(nodeLevelResource, groupName, xmpVar.getObjName(), xmpVar.getValue(), getType(xmpVar));
}
if (newUptime > oldUptime) {
collectionSetBuilder.disableCounterPersistence(false);
}
if (newUptime > 0) {
// save the agent's sysUpTime in the CollectionAgent
agent.setSavedSysUpTime(newUptime);
}
return true;
}
use of org.krupczak.xmp.XmpMessage in project opennms by OpenNMS.
the class XmpCollector method handleTableQuery.
/* handleScalarQuery */
// handle a tabular query, save each row in its own
// collection resource
private boolean handleTableQuery(String groupName, String resourceType, CollectionAgent agent, CollectionSetBuilder collectionSetBuilder, String[] tableInfo, XmpSession session, NodeLevelResource nodeLevelResource, XmpVar[] queryVars) throws CollectionException {
int numColumns, numRows;
XmpMessage reply;
int i, j;
XmpVar[] vars;
String targetInstance;
numColumns = queryVars.length;
// make sure we have an instance or * for all rows; preserve
// passed in value as targetInstance so we know if we are
// are going to use targetInstance for saving results or
// use returned instance for saving values
// if resourceType is present, we use it as a subDir in
// our RRD dir
targetInstance = tableInfo[2];
if ((tableInfo[2] == null) || (tableInfo[2].length() == 0)) {
tableInfo[2] = new String("*");
targetInstance = null;
}
LOG.debug("sending table query {},{},{} target: {}", tableInfo[0], tableInfo[1], tableInfo[2], targetInstance);
reply = session.queryTableVars(tableInfo, 0, queryVars);
if (reply == null) {
LOG.warn("collect: query to {} failed, {}", agent, Xmp.errorStatusToString(session.getErrorStatus()));
return false;
}
vars = reply.getMIBVars();
// we have to go through the reply and find out how
// many rows we have
// for each row: create a CollectionResource of
// appropriate type, instance, etc.
// create AttributeGroup to put
// the values in
numRows = vars.length / numColumns;
LOG.info("query returned valid table data for {} numRows={} numColumns={}", groupName, numRows, numColumns);
for (i = 0; i < numRows; i++) {
String rowInstance;
// determine instance for this row
// we use either the rowInstance or targetInstance for
// naming the instance for saving RRD file; if user
// wanted all rows (blank instance), then we will use
// the returned instance; if user specified an instance
// we use that instance for specifying the RRD file
// and collection resource
rowInstance = vars[i * numColumns].getKey();
// instead of using '*' for the nodeTypeName, use the
// table name so that the proper rrd file is spec'd
final String instanceName;
if (targetInstance != null) {
instanceName = targetInstance;
} else {
instanceName = rowInstance;
}
// node type can be "node" for scalars or
// "if" for network interface resources and
// "*" for all other resource types
final String nodeTypeName = tableInfo[1];
final Resource resource = getResource(nodeLevelResource, nodeTypeName, resourceType, instanceName);
LOG.debug("queryTable instance={}", rowInstance);
for (j = 0; j < numColumns; j++) {
final XmpVar var = vars[i * numColumns + j];
collectionSetBuilder.withAttribute(resource, groupName, var.getObjName(), var.getValue(), getType(var));
}
/* for each column */
}
return true;
}
use of org.krupczak.xmp.XmpMessage in project opennms by OpenNMS.
the class XmpDetector method detect.
@Override
public final DetectResults detect(DetectRequest request) {
final InetAddress address = request.getAddress();
XmpSession aSession;
XmpMessage aReply;
XmpVar[] vars, replyVars;
LOG.debug("XmpDetector: isServiceDetected starting to query " + address);
// try to establish session
aSession = new XmpSession(sockopts, address, xmpPort, xmpAuthenUser);
if (aSession.isClosed()) {
LOG.debug("XmpDetector: null session to " + address);
return new DetectResultsImpl(false);
}
LOG.debug("XmpDetector: isServiceDetected session established with " + address);
// query for core.sysName, core.sysDescr,
// core.sysUpTime, core.xmpdVersion
vars = new XmpVar[] { new XmpVar("core", "sysName", "", "", Xmp.SYNTAX_NULLSYNTAX), new XmpVar("core", "sysDescr", "", "", Xmp.SYNTAX_NULLSYNTAX), new XmpVar("core", "sysUpTime", "", "", Xmp.SYNTAX_NULLSYNTAX), new XmpVar("core", "xmpdVersion", "", "", Xmp.SYNTAX_NULLSYNTAX) };
if ((aReply = aSession.queryVars(vars)) == null) {
LOG.debug("XmpDetector: isServiceDetected no vars from " + address);
aSession.closeSession();
return new DetectResultsImpl(false);
}
aSession.closeSession();
// log what we retrieved
if ((replyVars = aReply.getMIBVars()) == null) {
LOG.debug("XmpDetector: isServiceDetected no replyVars for " + address);
return new DetectResultsImpl(false);
}
/* if replyVars == null */
LOG.debug("XmpDetector: isServiceDetected " + address + " reports " + replyVars[0].getValue() + "," + replyVars[1].getValue());
LOG.debug("XmpDetector: isServiceDetected true for " + address);
return new DetectResultsImpl(true);
}
Aggregations