use of org.opennms.protocols.wmi.WmiResult in project opennms by OpenNMS.
the class WmiCollector method isGroupAvailable.
private boolean isGroupAvailable(final WmiAgentState agentState, final Wpm wpm) {
LOG.debug("Checking availability of group {} via object {} of class {} in namespace {}", wpm.getName(), wpm.getKeyvalue(), wpm.getWmiClass(), wpm.getWmiNamespace());
WmiManager manager = null;
/*
* We provide a bogus comparison value and use an operator of "NOOP"
* to ensure that, regardless of results, we receive a result and perform
* no logic. We're only validating that the agent is reachable and gathering
* the result objects.
*/
try {
// Get and initialize the WmiManager
manager = agentState.getManager();
manager.setNamespace(wpm.getWmiNamespace());
manager.init();
final WmiParams params = new WmiParams(WmiParams.WMI_OPERATION_INSTANCEOF, "not-applicable", "NOOP", wpm.getWmiClass(), wpm.getKeyvalue());
final WmiResult result = manager.performOp(params);
final boolean isAvailable = (result.getResultCode() == WmiResult.RES_STATE_OK);
agentState.setGroupIsAvailable(wpm.getName(), isAvailable);
LOG.debug("Group {} is {}{}.", wpm.getName(), (isAvailable ? "" : "not "), "available");
} catch (final WmiException e) {
// Log a warning signifying that this group is unavailable.
LOG.warn("Error checking group ({}) availability.", wpm.getName(), e);
// Set the group as unavailable.
agentState.setGroupIsAvailable(wpm.getName(), false);
// And then continue on to check the next wpm entry.
return false;
} finally {
if (manager != null) {
try {
manager.close();
} catch (WmiException e) {
LOG.warn("An error occurred closing the WMI Manager", e);
}
}
}
return true;
}
use of org.opennms.protocols.wmi.WmiResult in project opennms by OpenNMS.
the class WmiDetector method isServiceDetected.
@Override
public boolean isServiceDetected(final InetAddress address, final WmiAgentConfig agentConfig) {
WmiParams clientParams = null;
if (getWmiWqlStr().equals(DEFAULT_WMI_WQL)) {
// Create the check parameters holder.
clientParams = new WmiParams(WmiParams.WMI_OPERATION_INSTANCEOF, getCompVal(), getCompOp(), getWmiClass(), getWmiObject());
} else {
// Define the WQL Query.
clientParams = new WmiParams(WmiParams.WMI_OPERATION_WQL, getCompVal(), getCompOp(), getWmiWqlStr(), getWmiObject());
}
// Use WMI credentials from configuration files, and override values with the detector parameters if they exists.
if (getUsername() != null)
agentConfig.setUsername(getUsername());
if (getPassword() != null)
agentConfig.setPassword(getPassword());
if (getDomain() != null)
agentConfig.setDomain(getDomain());
if (getRetries() > 0)
agentConfig.setRetries(getRetries());
if (getTimeout() > 0)
agentConfig.setTimeout(getTimeout());
// Perform the operation specified in the parameters.
WmiResult result = isServer(address, agentConfig.getUsername(), agentConfig.getPassword(), agentConfig.getDomain(), getNamespace(), getMatchType(), agentConfig.getRetries(), agentConfig.getTimeout(), clientParams);
// Only fail on critical and unknown returns.
return (result != null && result.getResultCode() != WmiResult.RES_STATE_CRIT && result.getResultCode() != WmiResult.RES_STATE_UNKNOWN);
}
use of org.opennms.protocols.wmi.WmiResult in project opennms by OpenNMS.
the class WmiMonitor method poll.
/**
* {@inheritDoc}
*
* Poll the specified address for service availability. During the poll an
* attempt is made to connect the WMI agent on the specified host. If the
* connection request is successful, the parameters are parsed and turned
* into <code>WmiParams</code> and a check is performed against the
* remote WMI service. If the <code>WmiManager</code> responds
* with a <code>WmiResult</code> containing a result code of
* <code>WmiResult.RES_STATE_OK</code> then we have determined that
* we are talking to a valid service and we set the service status to
* SERVICE_AVAILABLE and return.
*/
@Override
public PollStatus poll(final MonitoredService svc, final Map<String, Object> parameters) {
// Holds the response reason.
String reason = null;
// Used to exit the retry loop early, if possible.
int serviceStatus = PollStatus.SERVICE_UNAVAILABLE;
// This will hold the data the server sends back.
WmiResult response = null;
// Used to track how long the request took.
Double responseTime = null;
final InetAddress ipAddr = svc.getAddress();
final WmiAgentConfig agentConfig = WmiPeerFactory.getInstance().getAgentConfig(ipAddr);
String matchType = DEFAULT_WMI_MATCH_TYPE;
String compVal = DEFAULT_WMI_COMP_VAL;
String compOp = DEFAULT_WMI_COMP_OP;
String wmiClass = DEFAULT_WMI_CLASS;
String wmiObject = DEFAULT_WMI_OBJECT;
String wmiWqlStr = DEFAULT_WMI_WQL;
String wmiNamespace = DEFAULT_WMI_NAMESPACE;
if (parameters != null) {
if (parameters.get("timeout") != null) {
int timeout = ParameterMap.getKeyedInteger(parameters, "timeout", agentConfig.getTimeout());
agentConfig.setTimeout(timeout);
}
if (parameters.get("retry") != null) {
int retries = ParameterMap.getKeyedInteger(parameters, "retry", agentConfig.getRetries());
agentConfig.setRetries(retries);
}
if (parameters.get("username") != null) {
String user = ParameterMap.getKeyedString(parameters, "username", agentConfig.getUsername());
agentConfig.setUsername(user);
}
if (parameters.get("password") != null) {
String pass = ParameterMap.getKeyedString(parameters, "password", agentConfig.getPassword());
agentConfig.setUsername(pass);
}
if (parameters.get("domain") != null) {
String domain = ParameterMap.getKeyedString(parameters, "domain", agentConfig.getDomain());
agentConfig.setUsername(domain);
}
if (parameters.get("namespace") != null) {
wmiNamespace = ParameterMap.getKeyedString(parameters, "wmiNamespace", ParameterMap.getKeyedString(parameters, "namespace", DEFAULT_WMI_NAMESPACE));
}
matchType = ParameterMap.getKeyedString(parameters, "matchType", DEFAULT_WMI_MATCH_TYPE);
compVal = ParameterMap.getKeyedString(parameters, "compareValue", DEFAULT_WMI_COMP_VAL);
compOp = ParameterMap.getKeyedString(parameters, "compareOp", DEFAULT_WMI_COMP_OP);
wmiWqlStr = ParameterMap.getKeyedString(parameters, "wql", DEFAULT_WMI_WQL);
wmiClass = ParameterMap.getKeyedString(parameters, "wmiClass", DEFAULT_WMI_CLASS);
wmiObject = ParameterMap.getKeyedString(parameters, "wmiObject", DEFAULT_WMI_OBJECT);
}
final TimeoutTracker tracker = new TimeoutTracker(parameters, agentConfig.getRetries(), agentConfig.getTimeout());
final String hostAddress = InetAddressUtils.str(ipAddr);
LOG.debug("poll: address = {}, user = {}, {}", hostAddress, agentConfig.getUsername(), tracker);
WmiManager mgr = null;
for (tracker.reset(); tracker.shouldRetry() && serviceStatus != PollStatus.SERVICE_AVAILABLE; tracker.nextAttempt()) {
try {
tracker.startAttempt();
LOG.debug("poll: creating WmiManager object.");
// Create a client, set up details and connect.
mgr = new WmiManager(hostAddress, agentConfig.getUsername(), agentConfig.getPassword(), agentConfig.getDomain(), matchType);
mgr.setTimeout(tracker.getSoTimeout());
mgr.setNamespace(wmiNamespace);
mgr.init();
LOG.debug("Completed initializing WmiManager object.");
// We are connected, so upgrade status to unresponsive
serviceStatus = PollStatus.SERVICE_UNRESPONSIVE;
// Set up the parameters the client will use to validate the response.
// Note: We will check and see if we were provided with a WQL string.
WmiParams clientParams = null;
if (DEFAULT_WMI_WQL.equals(wmiWqlStr)) {
clientParams = new WmiParams(WmiParams.WMI_OPERATION_INSTANCEOF, compVal, compOp, wmiClass, wmiObject);
LOG.debug("Attempting to perform operation: \\\\{}\\{}", wmiClass, wmiObject);
} else {
// Create parameters to run a WQL query.
clientParams = new WmiParams(WmiParams.WMI_OPERATION_WQL, compVal, compOp, wmiWqlStr, wmiObject);
LOG.debug("Attempting to perform operation: {}", wmiWqlStr);
}
// Send the request to the server and receive the response..
response = mgr.performOp(clientParams);
LOG.debug("Received result: {}", response);
// Now save the time it took to process the check command.
responseTime = tracker.elapsedTimeInMillis();
if (response == null) {
continue;
}
final List<Object> wmiObjects = response.getResponse();
final StringBuilder reasonBuffer = new StringBuilder();
reasonBuffer.append("Constraint '").append(matchType).append(" ").append(clientParams.getCompareOperation()).append(" ").append(clientParams.getCompareValue()).append("' failed for value of ");
// If there's no WQL string then use the class\object name as the result message
if (DEFAULT_WMI_WQL.equals(wmiWqlStr)) {
reasonBuffer.append(wmiClass).append("\\").append(wmiObject);
} else {
// Otherwise, print the WQL statement in the result message
reasonBuffer.append("\"").append(wmiWqlStr).append("\"");
}
if (response.getResultCode() == WmiResult.RES_STATE_OK) {
serviceStatus = PollStatus.SERVICE_AVAILABLE;
reasonBuffer.append(": ").append(wmiObjects.get(0));
} else if (response.getResultCode() == WmiResult.RES_STATE_CRIT) {
serviceStatus = PollStatus.SERVICE_UNAVAILABLE;
// set this to null so we don't try to save data when the node is down
responseTime = null;
}
reason = reasonBuffer.toString();
} catch (final WmiException e) {
LOG.debug("WMI Poller received exception from client.", e);
reason = "WmiException: " + e.getMessage();
} finally {
if (mgr != null) {
try {
mgr.close();
} catch (WmiException e) {
LOG.warn("An error occurred closing the WMI Manager.", e);
}
}
}
}
// end for(;;)
return PollStatus.get(serviceStatus, reason, responseTime);
}
use of org.opennms.protocols.wmi.WmiResult in project opennms by OpenNMS.
the class WmiDetector method isServer.
private WmiResult isServer(InetAddress host, String user, String pass, String domain, String namespace, String matchType, int retries, int timeout, WmiParams params) {
boolean isAServer = false;
WmiResult result = null;
for (int attempts = 0; attempts <= retries && !isAServer; attempts++) {
WmiManager mgr = null;
try {
mgr = new WmiManager(InetAddressUtils.str(host), user, pass, domain, matchType);
mgr.setNamespace(namespace);
// Connect to the WMI server.
mgr.init();
// Perform the operation specified in the parameters.
result = mgr.performOp(params);
if (params.getWmiOperation().equals(WmiParams.WMI_OPERATION_WQL)) {
LOG.debug("WmiPlugin: {} : {}", params.getWql(), WmiResult.convertStateToString(result.getResultCode()));
} else {
LOG.debug("WmiPlugin: \\\\{}\\{} : {}", params.getWmiClass(), params.getWmiObject(), WmiResult.convertStateToString(result.getResultCode()));
}
isAServer = true;
} catch (WmiException e) {
final StringBuilder message = new StringBuilder();
message.append("WmiPlugin: Check failed... : ");
message.append(e.getMessage());
message.append(" : ");
message.append((e.getCause() == null ? "" : e.getCause().getMessage()));
LOG.info(message.toString());
isAServer = false;
} finally {
if (mgr != null) {
try {
mgr.close();
} catch (WmiException e) {
LOG.warn("an error occurred closing the WMI Manager", e);
}
}
}
}
return result;
}
Aggregations