Search in sources :

Example 1 with WmiManager

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;
}
Also used : WmiResult(org.opennms.protocols.wmi.WmiResult) WmiParams(org.opennms.protocols.wmi.WmiParams) WmiException(org.opennms.protocols.wmi.WmiException) WmiManager(org.opennms.protocols.wmi.WmiManager)

Example 2 with WmiManager

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);
}
Also used : OnmsWbemObjectSetBiosStub(org.opennms.protocols.wmi.test.stubs.OnmsWbemObjectSetBiosStub) OnmsWbemPropBiosStub(org.opennms.protocols.wmi.test.stubs.OnmsWbemPropBiosStub) OnmsWbemObjectBiosStub(org.opennms.protocols.wmi.test.stubs.OnmsWbemObjectBiosStub) WmiParams(org.opennms.protocols.wmi.WmiParams) OnmsWbemPropSetBiosStub(org.opennms.protocols.wmi.test.stubs.OnmsWbemPropSetBiosStub) OnmsWbemObjectSet(org.opennms.protocols.wmi.wbem.OnmsWbemObjectSet) WmiException(org.opennms.protocols.wmi.WmiException) WmiManager(org.opennms.protocols.wmi.WmiManager)

Example 3 with WmiManager

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);
}
Also used : WmiException(org.opennms.protocols.wmi.WmiException) WmiManager(org.opennms.protocols.wmi.WmiManager)

Example 4 with WmiManager

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);
}
Also used : WmiException(org.opennms.protocols.wmi.WmiException) WmiManager(org.opennms.protocols.wmi.WmiManager)

Example 5 with WmiManager

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);
}
Also used : OnmsWbemObjectSetBiosStub(org.opennms.protocols.wmi.test.stubs.OnmsWbemObjectSetBiosStub) OnmsWbemPropBiosStub(org.opennms.protocols.wmi.test.stubs.OnmsWbemPropBiosStub) OnmsWbemObjectBiosStub(org.opennms.protocols.wmi.test.stubs.OnmsWbemObjectBiosStub) WmiParams(org.opennms.protocols.wmi.WmiParams) OnmsWbemPropSetBiosStub(org.opennms.protocols.wmi.test.stubs.OnmsWbemPropSetBiosStub) OnmsWbemObjectSet(org.opennms.protocols.wmi.wbem.OnmsWbemObjectSet) WmiException(org.opennms.protocols.wmi.WmiException) WmiManager(org.opennms.protocols.wmi.WmiManager)

Aggregations

WmiManager (org.opennms.protocols.wmi.WmiManager)11 WmiException (org.opennms.protocols.wmi.WmiException)9 WmiParams (org.opennms.protocols.wmi.WmiParams)5 WmiResult (org.opennms.protocols.wmi.WmiResult)3 OnmsWbemObjectBiosStub (org.opennms.protocols.wmi.test.stubs.OnmsWbemObjectBiosStub)2 OnmsWbemObjectSetBiosStub (org.opennms.protocols.wmi.test.stubs.OnmsWbemObjectSetBiosStub)2 OnmsWbemPropBiosStub (org.opennms.protocols.wmi.test.stubs.OnmsWbemPropBiosStub)2 OnmsWbemPropSetBiosStub (org.opennms.protocols.wmi.test.stubs.OnmsWbemPropSetBiosStub)2 OnmsWbemObjectSet (org.opennms.protocols.wmi.wbem.OnmsWbemObjectSet)2 InetAddress (java.net.InetAddress)1 TimeoutTracker (org.opennms.core.utils.TimeoutTracker)1 WmiAgentConfig (org.opennms.netmgt.config.wmi.WmiAgentConfig)1