use of org.krupczak.xmp.XmpSession 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.XmpSession 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);
}
use of org.krupczak.xmp.XmpSession in project opennms by OpenNMS.
the class XmpMonitor method poll.
/**
* {@inheritDoc}
*/
@Override
public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) {
PollStatus status = PollStatus.unavailable();
InetAddress ipaddr = svc.getAddress();
XmpConfig protoConfig = XmpConfigFactory.getInstance().getXmpConfig();
XmpSession session;
SocketOpts sockopts = new SocketOpts();
// TODO how to apply timeout and retry to XMP operations?
int retry = protoConfig.hasRetry() ? protoConfig.getRetry() : DEFAULT_RETRY;
int timeout = protoConfig.hasTimeout() ? protoConfig.getTimeout() : DEFAULT_TIMEOUT;
int port = DEFAULT_PORT;
String authenUser = DEFAULT_AUTHEN_USER;
String requestType = DEFAULT_REQUEST_TYPE;
String mib = DEFAULT_REQUEST_MIB;
String table = DEFAULT_REQUEST_TABLE;
String object = DEFAULT_REQUEST_OBJECT;
String instance = DEFAULT_REQUEST_INSTANCE;
String instanceMatch = null;
String valueOperator = XmpUtil.EQUALS;
String valueOperand = null;
int minMatches = DEFAULT_MIN_MATCHES;
int maxMatches = DEFAULT_MAX_MATCHES;
boolean maxMatchesUnbounded = DEFAULT_MAX_MATCHES_UNBOUNDED;
boolean valueCaseSensitive = DEFAULT_VALUE_CASE_SENSITIVE;
if (parameters != null) {
retry = ParameterMap.getKeyedInteger(parameters, "retry", protoConfig.hasRetry() ? protoConfig.getRetry() : DEFAULT_RETRY);
timeout = ParameterMap.getKeyedInteger(parameters, "timeout", protoConfig.hasTimeout() ? protoConfig.getTimeout() : DEFAULT_TIMEOUT);
port = ParameterMap.getKeyedInteger(parameters, "port", protoConfig.hasPort() ? protoConfig.getPort() : DEFAULT_PORT);
authenUser = ParameterMap.getKeyedString(parameters, "authenUser", (protoConfig.getAuthenUser() != null) ? protoConfig.getAuthenUser() : DEFAULT_AUTHEN_USER);
requestType = ParameterMap.getKeyedString(parameters, "request-type", DEFAULT_REQUEST_TYPE);
mib = ParameterMap.getKeyedString(parameters, "mib", DEFAULT_REQUEST_MIB);
table = ParameterMap.getKeyedString(parameters, "table", DEFAULT_REQUEST_TABLE);
object = ParameterMap.getKeyedString(parameters, "object", DEFAULT_REQUEST_OBJECT);
instance = ParameterMap.getKeyedString(parameters, "instance", DEFAULT_REQUEST_INSTANCE);
instanceMatch = ParameterMap.getKeyedString(parameters, "instance-match", DEFAULT_INSTANCE_MATCH);
valueOperator = ParameterMap.getKeyedString(parameters, "value-operator", "==");
valueOperand = ParameterMap.getKeyedString(parameters, "value-match", DEFAULT_VALUE_MATCH);
valueCaseSensitive = ParameterMap.getKeyedBoolean(parameters, "value-case-sensitive", DEFAULT_VALUE_CASE_SENSITIVE);
minMatches = ParameterMap.getKeyedInteger(parameters, "min-matches", DEFAULT_MIN_MATCHES);
maxMatches = ParameterMap.getKeyedInteger(parameters, "max-matches", DEFAULT_MAX_MATCHES);
String maxMatchesUnboundedStr = ParameterMap.getKeyedString(parameters, "max-matches", "unbounded");
maxMatchesUnbounded = (maxMatchesUnboundedStr.equalsIgnoreCase("unbounded"));
}
// for Table and Object.
if (requestType.equalsIgnoreCase("SelectTableRequest")) {
if (table.equals(DEFAULT_REQUEST_TABLE)) {
throw new IllegalArgumentException("When performing a SelectTableRequest, table must be specified");
}
if (object.equals(DEFAULT_REQUEST_OBJECT)) {
throw new IllegalArgumentException("When performing a SelectTableRequest, object must be specified and must be tabular");
}
} else // an instance
if (requestType.equalsIgnoreCase("GetRequest")) {
if (!table.equals(DEFAULT_REQUEST_TABLE)) {
throw new IllegalArgumentException("When performing a GetRequest, table must not be specified");
}
if (!instance.equals(DEFAULT_REQUEST_INSTANCE)) {
throw new IllegalArgumentException("When performing a GetRequest, instance must not be specified");
}
} else {
throw new IllegalArgumentException("Unknown request type " + requestType + ", only GetRequest and SelectTableRequest are supported");
}
Pattern instanceRegex = null;
try {
if (instanceMatch != null) {
instanceRegex = Pattern.compile(instanceMatch);
}
} catch (final PatternSyntaxException e) {
throw new java.lang.reflect.UndeclaredThrowableException(e);
}
long startTime = System.currentTimeMillis();
// Set the SO_TIMEOUT. What a concept!
sockopts.setConnectTimeout(timeout);
session = new XmpSession(sockopts, ipaddr, port, authenUser);
boolean result = false;
if (requestType.equalsIgnoreCase("SelectTableRequest")) {
try {
result = XmpUtil.handleTableQuery(session, mib, table, object, instance, instanceRegex, valueOperator, valueOperand, minMatches, maxMatches, maxMatchesUnbounded, valueCaseSensitive);
} catch (XmpUtilException e) {
status = PollStatus.unavailable(e.getMessage());
}
} else if (requestType.equalsIgnoreCase("GetRequest")) {
try {
result = XmpUtil.handleScalarQuery(session, mib, object, valueOperator, valueOperand, valueCaseSensitive);
} catch (XmpUtilException e) {
status = PollStatus.unavailable(e.getMessage());
}
}
if (result == true) {
Double responseTime = new Double(System.currentTimeMillis() - startTime);
status = PollStatus.available(responseTime);
}
return status;
}
Aggregations