use of org.krupczak.xmp.XmpVar 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.XmpVar 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.XmpVar 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.XmpVar in project opennms by OpenNMS.
the class XmpCollector method collect.
/**
* {@inheritDoc}
*
* Collect data, via XMP, from a particular agent EventProxy is
* used to send opennms events into the system in case a
* collection fails or if a system is back working again after a
* failure (suceed event). But otherwise, no events sent if
* collection succeeds. Collect is called once per agent per
* collection cycle. Parameters are a map of String Key/String
* Value passed in. Keys come from collectd config
* @throws CollectionException
*/
@Override
public CollectionSet collect(CollectionAgent agent, Map<String, Object> parameters) throws CollectionException {
CollectionSetBuilder collectionSetBuilder;
XmpSession session;
long oldUptime;
int i;
XmpCollection collection;
LOG.debug("collect agent {}", agent);
oldUptime = 0;
// First go to the peer factory
XmpAgentConfig peerConfig = XmpPeerFactory.getInstance().getAgentConfig(agent.getAddress());
authenUser = peerConfig.getAuthenUser();
timeout = (int) peerConfig.getTimeout();
retries = peerConfig.getRetry();
xmpPort = peerConfig.getPort();
if (parameters.get("authenUser") != null)
authenUser = ParameterMap.getKeyedString(parameters, "authenUser", null);
if (parameters.get("timeout") != null) {
timeout = ParameterMap.getKeyedInteger(parameters, "timeout", 3000);
}
if (parameters.get("retry") != null) {
retries = ParameterMap.getKeyedInteger(parameters, "retries", 0);
}
parameters.get("collection");
if (parameters.get("port") != null) {
xmpPort = Integer.valueOf((String) parameters.get("port"));
}
// log().debug("collect got parameters for "+agent);
String collectionName = ParameterMap.getKeyedString(parameters, "collection", null);
// this would/will come from xmp-datacollection.xml
if (collectionName == null) {
// log this!
LOG.warn("collect found no collectionName for {}", agent);
return null;
}
// log().debug("collect got collectionName for "+agent);
LOG.debug("XmpCollector: collect {} from {}", collectionName, agent);
// get/create our collections set
collectionSetBuilder = new CollectionSetBuilder(agent).withStatus(// default to failed
CollectionStatus.FAILED).disableCounterPersistence(// don't persist counters by default
true);
// default collection resource for putting scalars in
final NodeLevelResource nodeLevelResource = new NodeLevelResource(agent.getNodeId());
// get the collection, again, from the data config file factory
// because it could have changed; its not necessarily re-parsed,
// but we are getting another copy of it for each agent
// that we are queried each time we are invoked
collection = XmpCollectionFactory.getInstance().getXmpCollection(collectionName);
if (collection == null) {
LOG.warn("collect found no matching collection for {}", agent);
return collectionSetBuilder.build();
}
if (collection.getGroups().getGroup().length < 1) {
LOG.info("No groups to collect.");
return collectionSetBuilder.withStatus(CollectionStatus.SUCCEEDED).build();
}
oldUptime = agent.getSavedSysUpTime();
// open/get a session with the target agent
LOG.debug("collect: attempting to open XMP session with {}:{},{}", agent.getAddress(), xmpPort, authenUser);
// Set the SO_TIMEOUT, why don't we...
sockopts.setConnectTimeout(timeout);
session = new XmpSession(sockopts, agent.getAddress(), xmpPort, authenUser);
if (session.isClosed()) {
LOG.warn("collect unable to open XMP session with {}", agent);
return collectionSetBuilder.build();
}
LOG.debug("collect: successfully opened XMP session with{}", agent);
for (Group group : collection.getGroups().getGroup()) {
// get name of group and MIB objects in group
String groupName = group.getName();
MibObj[] mibObjects = group.getMibObj();
XmpVar[] vars = new XmpVar[mibObjects.length];
LOG.debug("collecting XMP group {} with {} mib objects", groupName, mibObjects.length);
// prepare the query vars
for (i = 0; i < mibObjects.length; i++) {
vars[i] = new XmpVar(mibObjects[i].getMib(), mibObjects[i].getVar(), mibObjects[i].getInstance(), "", Xmp.SYNTAX_NULLSYNTAX);
}
if ((mibObjects[0].getTable() != null) && (mibObjects[0].getTable().length() != 0)) {
String[] tableInfo = new String[3];
tableInfo[0] = mibObjects[0].getMib();
tableInfo[1] = mibObjects[0].getTable();
tableInfo[2] = mibObjects[0].getInstance();
// tabular query
if (handleTableQuery(group.getName(), group.getResourceType(), agent, collectionSetBuilder, tableInfo, session, nodeLevelResource, vars) == false) {
session.closeSession();
return collectionSetBuilder.build();
}
} else {
// scalar query
if (handleScalarQuery(group.getName(), agent, collectionSetBuilder, oldUptime, session, nodeLevelResource, vars) == false) {
session.closeSession();
return collectionSetBuilder.build();
}
}
}
/* for each Group in collection Group list */
// done talking to this agent; close session
session.closeSession();
// Did agent restart since last query? If so, set
// ignorePersist to true; our scalar
// query will have handled this by searching returned
// MIB objects for sysUpTime
// WARNING, EACH COLLECTION SHOULD HAVE A SCALAR QUERY THAT
// INCLUDES Core.sysUpTime
collectionSetBuilder.withStatus(CollectionStatus.SUCCEEDED);
LOG.debug("XMP collect finished for {}, uptime for {} is {}", collectionName, agent, agent.getSavedSysUpTime());
return collectionSetBuilder.build();
}
use of org.krupczak.xmp.XmpVar 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;
}
Aggregations