use of org.opennms.protocols.wmi.WmiManager 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.WmiManager in project opennms by OpenNMS.
the class WmiManagerTest method testPerformOpValidObject.
/**
* Test the performOp method with a valid WMI class and valid WMI object.
*
* Test method for
* {@link org.opennms.protocols.wmi.WmiManager#performOp(org.opennms.protocols.wmi.WmiParams)}.
*
* @throws WmiException if there is a problem with the mock object.
*/
public final void testPerformOpValidObject() throws WmiException {
//
// Create parameter holder.
WmiParams params = new WmiParams(WmiParams.WMI_OPERATION_INSTANCEOF, "2/12/2004 00:00:00", "EQ", "Win32_BIOS", "ReleaseDate");
// Set up WMI mock client.
// 1) Expect a call to connect() with a bad hostname.
// 2) Throw a new WmiException indictating a bad hostname.
OnmsWbemObjectSet wos = new OnmsWbemObjectSetBiosStub(new OnmsWbemObjectBiosStub(new OnmsWbemPropSetBiosStub(new OnmsWbemPropBiosStub())));
m_WmiMock.connect("127.0.0.1", "Administrator", "password", WmiParams.WMI_DEFAULT_NAMESPACE);
expect(m_WmiMock.performInstanceOf("Win32_BIOS")).andReturn(wos);
replay(m_WmiMock);
try {
// Create a manager.
WmiManager wmiManager = new WmiManager("127.0.0.1", "Administrator", "password");
// Initialize
wmiManager.init(m_WmiMock);
// Perform an operation.
// WmiResult res =
wmiManager.performOp(params);
// assertTrue(res)
} catch (WmiException e) {
// assertTrue("Exception missing message: Unknown name: " + e, e
// .getMessage().contains("Unknown name"));
}
verify(m_WmiMock);
reset(m_WmiMock);
}
use of org.opennms.protocols.wmi.WmiManager in project opennms by OpenNMS.
the class WmiManagerTest method testInitBadUserPass.
/**
* Test that the WMI manager handles invalid username or password exceptions
* properly from the WMI client.
*
* Test method for {@link org.opennms.protocols.wmi.WmiManager#init()}.
*
* @throws WmiException if there is a problem with the mock object.
*/
public final void testInitBadUserPass() throws WmiException {
// Set up WMI mock client.
// 1) Expect a call to connect() with a bad hostname.
// 2) Throw a new WmiException indictating a user or password.
m_WmiMock.connect("127.0.0.1", "Administrator", "wrongpassword", WmiParams.WMI_DEFAULT_NAMESPACE);
expectLastCall().andThrow(new WmiException("Failed to connect to host '127.0.0.1': The attempted logon is invalid. This is either due to a bad username or authentication information. [0xC000006D]"));
replay(m_WmiMock);
try {
// Create a manager.
WmiManager wmiManager = new WmiManager("127.0.0.1", "Administrator", "wrongpassword");
// Initialize
wmiManager.init(m_WmiMock);
} catch (WmiException e) {
assertTrue("Exception missing message: The attempted logon is invalid: " + e, e.getMessage().contains("The attempted logon is invalid"));
}
verify(m_WmiMock);
reset(m_WmiMock);
}
use of org.opennms.protocols.wmi.WmiManager in project opennms by OpenNMS.
the class WmiManagerTest method testInitBadHostname.
/**
* Test that the WMI manager properly handles invalid host exceptions
* from the WMI client.
*
* Test method for {@link org.opennms.protocols.wmi.WmiManager#init()}.
*
* @throws WmiException if there is a problem with the mock object.
*/
public final void testInitBadHostname() throws WmiException {
// Set up WMI mock client.
// 1) Expect a call to connect() with a bad hostname.
// 2) Throw a new WmiException indictating a bad hostname.
m_WmiMock.connect("bad-hostname", "Administrator", "password", WmiParams.WMI_DEFAULT_NAMESPACE);
expectLastCall().andThrow(new WmiException("Unknown host 'bad-hostname'. Failed to connect to WMI agent."));
replay(m_WmiMock);
try {
// Create a manager.
WmiManager wmiManager = new WmiManager("bad-hostname", "Administrator", "password");
// Initialize
wmiManager.init(m_WmiMock);
} catch (WmiException e) {
assertTrue("Exception missing message: Unknown host: " + e, e.getMessage().contains("Unknown host"));
}
verify(m_WmiMock);
reset(m_WmiMock);
}
use of org.opennms.protocols.wmi.WmiManager in project opennms by OpenNMS.
the class WmiManagerTest method testPerformOpInvalidObject.
/**
* Test the performOp method with an valid WMI class and invalid WMI object.
*
* Test method for
* {@link org.opennms.protocols.wmi.WmiManager#performOp(org.opennms.protocols.wmi.WmiParams)}.
*
* @throws WmiException if there is a problem with the mock object.
*/
public final void testPerformOpInvalidObject() throws WmiException {
//
// Create parameter holder.
WmiParams params = new WmiParams(WmiParams.WMI_OPERATION_INSTANCEOF, "2/12/2004 00:00:00", "EQ", "Win32_BIOS", "RelDate");
// Set up WMI mock client.
// 1) Expect a call to connect() with a bad hostname.
// 2) Throw a new WmiException indictating a bad hostname.
OnmsWbemObjectSet wos = new OnmsWbemObjectSetBiosStub(new OnmsWbemObjectBiosStub(new OnmsWbemPropSetBiosStub(new OnmsWbemPropBiosStub())));
m_WmiMock.connect("127.0.0.1", "Administrator", "password", WmiParams.WMI_DEFAULT_NAMESPACE);
expect(m_WmiMock.performInstanceOf("Win32_BIOS")).andReturn(wos);
replay(m_WmiMock);
try {
// Create a manager.
WmiManager wmiManager = new WmiManager("127.0.0.1", "Administrator", "password");
// Initialize
wmiManager.init(m_WmiMock);
// Perform an operation.
wmiManager.performOp(params);
} catch (WmiException e) {
assertTrue("Exception missing message: Unknown name: " + e, e.getMessage().contains("Unknown name"));
}
verify(m_WmiMock);
reset(m_WmiMock);
}
Aggregations