use of org.opennms.protocols.wmi.wbem.OnmsWbemObjectSet in project opennms by OpenNMS.
the class WmiCollector method collect.
/** {@inheritDoc} */
@Override
public CollectionSet collect(final CollectionAgent agent, final Map<String, Object> parameters) {
// Find attributes to collect - check groups in configuration. For each,
// check scheduled nodes to see if that group should be collected
final WmiCollection collection = (WmiCollection) parameters.get(WMI_COLLECTION_KEY);
final WmiAgentConfig agentConfig = (WmiAgentConfig) parameters.get(WMI_AGENT_CONFIG_KEY);
final WmiAgentState agentState = new WmiAgentState(agent.getAddress(), agentConfig, parameters);
// Create a new collection set.
CollectionSetBuilder builder = new CollectionSetBuilder(agent).withStatus(CollectionStatus.FAILED);
if (collection.getWpms().size() < 1) {
LOG.info("No groups to collect.");
return builder.withStatus(CollectionStatus.SUCCEEDED).build();
}
final NodeLevelResource nodeResource = new NodeLevelResource(agent.getNodeId());
// Iterate through the WMI collection groups.
for (final Wpm wpm : collection.getWpms()) {
// A wpm consists of a list of attributes, identified by name
if (agentState.shouldCheckAvailability(wpm.getName(), wpm.getRecheckInterval())) {
if (!isGroupAvailable(agentState, wpm)) {
continue;
}
}
if (agentState.groupIsAvailable(wpm.getName())) {
WmiClient client = null;
// Collect the data
try {
// Tell the agent to connect
agentState.connect(wpm.getWmiNamespace());
// And retrieve the client object for working.
client = (WmiClient) agentState.getWmiClient();
// Retrieve the WbemObjectSet from the class defined on the group.
final OnmsWbemObjectSet wOS = client.performInstanceOf(wpm.getWmiClass());
// If we received a WbemObjectSet result, lets go through it and collect it.
if (wOS != null) {
// Go through each object (class instance) in the object set.
for (int i = 0; i < wOS.count(); i++) {
// Create a new collection resource.
Resource resource = null;
// Fetch our WBEM Object
final OnmsWbemObject obj = wOS.get(i);
// If this is multi-instance, fetch the instance name and store it.
if (wOS.count() > 1) {
// Fetch the value of the key value. e.g. Name.
final OnmsWbemProperty prop = obj.getWmiProperties().getByName(wpm.getKeyvalue());
final Object propVal = prop.getWmiValue();
String instance = null;
if (propVal instanceof String) {
instance = (String) propVal;
} else {
instance = propVal.toString();
}
resource = getWmiResource(agent, wpm.getResourceType(), nodeResource, instance);
} else {
resource = nodeResource;
}
for (final Attrib attrib : wpm.getAttribs()) {
final OnmsWbemProperty prop = obj.getWmiProperties().getByName(attrib.getWmiObject());
final AttributeType type = attrib.getType();
final String stringValue = prop.getWmiValue().toString();
if (type.isNumeric()) {
Double numericValue = Double.NaN;
try {
numericValue = Double.parseDouble(stringValue);
} catch (NumberFormatException e) {
LOG.warn("Value '{}' for attribute named '{}' cannot be converted to a number. Skipping.", prop.getWmiValue(), attrib.getName());
continue;
}
builder.withNumericAttribute(resource, wpm.getName(), attrib.getAlias(), numericValue, type);
} else {
builder.withStringAttribute(resource, wpm.getName(), attrib.getAlias(), stringValue);
}
}
}
}
builder.withStatus(CollectionStatus.SUCCEEDED);
} catch (final WmiException e) {
LOG.info("unable to collect params for wpm '{}'", wpm.getName(), e);
} finally {
if (client != null) {
try {
client.disconnect();
} catch (final WmiException e) {
LOG.warn("An error occurred disconnecting while collecting from WMI.", e);
}
}
}
}
}
return builder.build();
}
use of org.opennms.protocols.wmi.wbem.OnmsWbemObjectSet in project opennms by OpenNMS.
the class TestingNewStructure method main.
/**
* <p>main</p>
*
* @param args an array of {@link java.lang.String} objects.
*/
public static void main(final String[] args) {
try {
// Connect to the WMI server.
final WmiClient wmiClient = new WmiClient("localhost");
wmiClient.connect("CHILDRENSNT", "CE136452", "aj7162007", WmiParams.WMI_DEFAULT_NAMESPACE);
final OnmsWbemObjectSet wos = wmiClient.performExecQuery("Select * From Win32_NTLogEvent");
System.out.println("The number of objects retrieved: " + wos.count());
for (int i = 0; i < wos.count(); i++) {
final OnmsWbemObject obj = wos.get(i);
if (obj == null)
System.out.println("Received null object.");
//System.out.println("+++++ Testing Object PropertySet +++++");
final OnmsWbemPropertySet propSet = obj.getWmiProperties();
//System.out.println("Object has " + propSet.count() + " properties available.");
System.out.println("The service (" + propSet.getByName("EventIdentifier").getWmiValue() + ") is: " + propSet.getByName("Message").getWmiValue());
//for(int b=0; b<propSet.count(); b++) {
// OnmsWbemProperty prop = propSet.get(b);
// System.out.println("Property named: " + prop.getWmiName());
// System.out.println("Property origin: " + prop.getWmiOrigin());
// System.out.println("Property is array: " + prop.getWmiIsArray());
// System.out.println("Property is local: " + prop.getWmiIsLocal());
// System.out.println("Property value: " + prop.getWmiValue());
// System.out.println("Property CIMType: " + OnmsWbemCimTypeEnum.get(prop.getWmiCIMType()).getCimName() );
//}
}
// lets do some logic here....
// OnmsWbemObjectSet wos = wmiClient.performInstanceOf("Win32_Keyboard");
//
// System.out.println("The number of objects retrieved: " + wos.count());
// for(int i=0; i<wos.count(); i++) {
// OnmsWbemObject obj = wos.get(0);
// if(obj == null)
// System.out.println("Received null object.");
//
// System.out.println("+++++ Testing OnmsWbemMethodSet and Mehod +++++");
// OnmsWbemMethodSet methodSet = obj.getWmiMethods();
// System.out.println("object has " + methodSet.getCount() + " methods available.");
// for(int a=0;a<methodSet.getCount(); a++) {
// OnmsWbemMethod method = methodSet.get(a);
// System.out.println("Object method Name: " + method.getWmiName());
// System.out.println("Object method Origin: " + method.getWmiOrigin());
// }
//
// System.out.println("+++++ Testing OnmsWbemObjectPath +++++");
// OnmsWbemObjectPath objPath = obj.getWmiPath();
// System.out.println("Object Path is: " + objPath.getWmiPath());
// System.out.println("Object ParentNamespace is: " + objPath.getWmiParentNamespace());
// System.out.println("Object Namespace is: " + objPath.getWmiNamespace());
// System.out.println("Object Locale is: " + objPath.getWmiLocale());
// System.out.println("Object DisplayName is: " + objPath.getWmiDisplayName());
// System.out.println("Object Class is: " + objPath.getWmiClass());
// System.out.println("Object Authority is: " + objPath.getWmiAuthority());
// System.out.println("Object RelPath is: " + objPath.getWmiRelPath());
// System.out.println("Object Server is: " + objPath.getWmiServer());
// System.out.println("Object IsClass is: " + objPath.getWmiIsClass());
// System.out.println("Object IsSingleton is: " + objPath.getWmiIsSingleton());
//
// System.out.println("+++++ Testing Getting Object Text +++++");
// System.out.println(obj.getWmiObjectText());
//
// System.out.println("+++++ Testing Object PropertySet +++++");
// OnmsWbemPropertySet propSet = obj.getWmiProperties();
// System.out.println("Object has " + propSet.count() + " properties available.");
// for(int b=0; b<propSet.count(); b++) {
// OnmsWbemProperty prop = propSet.get(b);
// System.out.println("Property named: " + prop.getWmiName());
// System.out.println("Property origin: " + prop.getWmiOrigin());
// System.out.println("Property is array: " + prop.getWmiIsArray());
// System.out.println("Property is local: " + prop.getWmiIsLocal());
// System.out.println("Property value: " + prop.getWmiValue());
// System.out.println("Property CIMType: " + OnmsWbemCimTypeEnum.get(prop.getWmiCIMType()).getCimName() );
//
//
// }
//
// }
// Disconenct from the WMI server.
wmiClient.disconnect();
} catch (final WmiException e) {
System.out.println("Caught exception: " + e.getMessage() + "\n");
e.printStackTrace();
}
}
use of org.opennms.protocols.wmi.wbem.OnmsWbemObjectSet in project opennms by OpenNMS.
the class WmiManager method performExecQuery.
/**
* <p>performExecQuery</p>
*
* @param params a {@link org.opennms.protocols.wmi.WmiParams} object.
* @return a {@link org.opennms.protocols.wmi.WmiResult} object.
* @throws org.opennms.protocols.wmi.WmiException if any.
*/
public WmiResult performExecQuery(final WmiParams params) throws WmiException {
final ArrayList<Object> wmiObjects = new ArrayList<Object>();
final OnmsWbemObjectSet wos = m_WmiClient.performExecQuery(params.getWql());
for (int i = 0; i < wos.count(); i++) {
wmiObjects.add(wos.get(i).getWmiProperties().getByName(params.getWmiObject()).getWmiValue());
}
final WmiResult result = new WmiResult(wmiObjects);
if (params.getCompareOperation().equals("NOOP")) {
result.setResultCode(WmiResult.RES_STATE_OK);
} else if (params.getCompareOperation().equals("EQ") || params.getCompareOperation().equals("NEQ") || params.getCompareOperation().equals("GT") || params.getCompareOperation().equals("LT")) {
performResultCheck(result, params);
} else {
result.setResultCode(WmiResult.RES_STATE_UNKNOWN);
}
return result;
}
use of org.opennms.protocols.wmi.wbem.OnmsWbemObjectSet in project opennms by OpenNMS.
the class WmiManager method performInstanceOf.
/**
* <p>performInstanceOf</p>
*
* @param params a {@link org.opennms.protocols.wmi.WmiParams} object.
* @return a {@link org.opennms.protocols.wmi.WmiResult} object.
* @throws org.opennms.protocols.wmi.WmiException if any.
*/
public WmiResult performInstanceOf(final WmiParams params) throws WmiException {
final ArrayList<Object> wmiObjects = new ArrayList<Object>();
final OnmsWbemObjectSet wos = m_WmiClient.performInstanceOf(params.getWmiClass());
for (int i = 0; i < wos.count(); i++) {
wmiObjects.add(wos.get(i).getWmiProperties().getByName(params.getWmiObject()).getWmiValue());
}
final WmiResult result = new WmiResult(wmiObjects);
if (params.getCompareOperation().equals("NOOP")) {
result.setResultCode(WmiResult.RES_STATE_OK);
} else if (params.getCompareOperation().equals("EQ") || params.getCompareOperation().equals("NEQ") || params.getCompareOperation().equals("GT") || params.getCompareOperation().equals("LT")) {
performResultCheck(result, params);
} else {
result.setResultCode(WmiResult.RES_STATE_UNKNOWN);
}
return result;
}
use of org.opennms.protocols.wmi.wbem.OnmsWbemObjectSet 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