use of org.sblim.wbem.cim.CIMObject in project opennms by OpenNMS.
the class VmwareViJavaAccessTest method testQueryCimObjects.
@Test
public void testQueryCimObjects() {
replay(mockPerformanceManager, mockHostSystem, mockHostNetworkSystem, mockCIMClient, CIMClient.class, mockServiceInstance, ServiceInstance.class);
try {
vmwareViJavaAccess.connect();
} catch (MalformedURLException e) {
Assert.fail(e.getMessage());
} catch (RemoteException e) {
Assert.fail(e.getMessage());
}
List<CIMObject> returnedCimObjects = null;
try {
returnedCimObjects = vmwareViJavaAccess.queryCimObjects(mockHostSystem, "cimClass");
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
Assert.assertNotNull(returnedCimObjects);
Assert.assertEquals(returnedCimObjects.size(), cimObjects.size());
for (CIMObject cimObject : cimObjects) {
Assert.assertTrue(returnedCimObjects.contains(cimObject));
}
verify(mockPerformanceManager, mockHostSystem, mockHostNetworkSystem, mockCIMClient, CIMClient.class, mockServiceInstance, ServiceInstance.class);
}
use of org.sblim.wbem.cim.CIMObject in project opennms by OpenNMS.
the class VmwareCimCollector method collect.
/**
* This method collect the data for a given collection agent.
*
* @param agent the collection agent
* @param parameters the parameters map
* @return the generated collection set
* @throws CollectionException
*/
@Override
public CollectionSet collect(CollectionAgent agent, Map<String, Object> parameters) throws CollectionException {
final VmwareCimCollection collection = (VmwareCimCollection) parameters.get(VMWARE_COLLECTION_KEY);
final String vmwareManagementServer = (String) parameters.get(VMWARE_MGMT_SERVER_KEY);
final String vmwareManagedObjectId = (String) parameters.get(VMWARE_MGED_OBJECT_ID_KEY);
final VmwareServer vmwareServer = (VmwareServer) parameters.get(VMWARE_SERVER_KEY);
CollectionSetBuilder builder = new CollectionSetBuilder(agent);
builder.withStatus(CollectionStatus.FAILED);
VmwareViJavaAccess vmwareViJavaAccess = new VmwareViJavaAccess(vmwareServer);
int timeout = ParameterMap.getKeyedInteger(parameters, "timeout", -1);
if (timeout > 0) {
if (!vmwareViJavaAccess.setTimeout(timeout)) {
logger.warn("Error setting connection timeout for VMware management server '{}'", vmwareManagementServer);
}
}
if (collection.getVmwareCimGroup().length < 1) {
logger.info("No groups to collect. Returning empty collection set.");
builder.withStatus(CollectionStatus.SUCCEEDED);
return builder.build();
}
try {
vmwareViJavaAccess.connect();
} catch (MalformedURLException e) {
logger.warn("Error connecting VMware management server '{}': '{}' exception: {} cause: '{}'", vmwareManagementServer, e.getMessage(), e.getClass().getName(), e.getCause());
return builder.build();
} catch (RemoteException e) {
logger.warn("Error connecting VMware management server '{}': '{}' exception: {} cause: '{}'", vmwareManagementServer, e.getMessage(), e.getClass().getName(), e.getCause());
return builder.build();
}
HostSystem hostSystem = vmwareViJavaAccess.getHostSystemByManagedObjectId(vmwareManagedObjectId);
String powerState = null;
if (hostSystem == null) {
logger.debug("hostSystem=null");
} else {
HostRuntimeInfo hostRuntimeInfo = hostSystem.getRuntime();
if (hostRuntimeInfo == null) {
logger.debug("hostRuntimeInfo=null");
} else {
HostSystemPowerState hostSystemPowerState = hostRuntimeInfo.getPowerState();
if (hostSystemPowerState == null) {
logger.debug("hostSystemPowerState=null");
} else {
powerState = hostSystemPowerState.toString();
}
}
}
logger.debug("The power state for host system '{}' is '{}'", vmwareManagedObjectId, powerState);
if ("poweredOn".equals(powerState)) {
HashMap<String, List<CIMObject>> cimObjects = new HashMap<String, List<CIMObject>>();
for (final VmwareCimGroup vmwareCimGroup : collection.getVmwareCimGroup()) {
String cimClass = vmwareCimGroup.getCimClass();
if (!cimObjects.containsKey(cimClass)) {
List<CIMObject> cimList = null;
try {
cimList = vmwareViJavaAccess.queryCimObjects(hostSystem, cimClass, InetAddressUtils.str(agent.getAddress()));
} catch (Exception e) {
logger.warn("Error retrieving CIM values from host system '{}'. Error message: '{}'", vmwareManagedObjectId, e.getMessage());
return builder.build();
} finally {
vmwareViJavaAccess.disconnect();
}
cimObjects.put(cimClass, cimList);
}
final List<CIMObject> cimList = cimObjects.get(cimClass);
if (cimList == null) {
logger.warn("Error getting objects of CIM class '{}' from host system '{}'", cimClass, vmwareManagedObjectId);
continue;
}
String keyAttribute = vmwareCimGroup.getKey();
String attributeValue = vmwareCimGroup.getValue();
String instanceAttribute = vmwareCimGroup.getInstance();
for (CIMObject cimObject : cimList) {
boolean addObject = false;
if (keyAttribute != null && attributeValue != null) {
String cimObjectValue = vmwareViJavaAccess.getPropertyOfCimObject(cimObject, keyAttribute);
if (attributeValue.equals(cimObjectValue)) {
addObject = true;
} else {
addObject = false;
}
} else {
addObject = true;
}
if (addObject) {
final String instance = vmwareViJavaAccess.getPropertyOfCimObject(cimObject, instanceAttribute);
final NodeLevelResource nodeResource = new NodeLevelResource(agent.getNodeId());
final Resource resource = new DeferredGenericTypeResource(nodeResource, vmwareCimGroup.getResourceType(), instance);
for (Attrib attrib : vmwareCimGroup.getAttrib()) {
final AttributeType type = attrib.getType();
String value = vmwareViJavaAccess.getPropertyOfCimObject(cimObject, attrib.getName());
if (valueModifiers.containsKey(attrib.getName())) {
String modifiedValue = valueModifiers.get(attrib.getName()).modifyValue(attrib.getName(), value, cimObject, vmwareViJavaAccess);
logger.debug("Applying value modifier for instance value " + attrib.getName() + "[" + instance + "]='" + value + "' => '" + modifiedValue + "' for node " + agent.getNodeId());
value = modifiedValue;
}
builder.withAttribute(resource, vmwareCimGroup.getName(), attrib.getAlias(), value, type);
}
}
}
}
builder.withStatus(CollectionStatus.SUCCEEDED);
}
vmwareViJavaAccess.disconnect();
return builder.build();
}
use of org.sblim.wbem.cim.CIMObject in project opennms by OpenNMS.
the class VmwareViJavaAccessTest method testGetPropertyOfCimObject.
@Test
public void testGetPropertyOfCimObject() throws Exception {
CIMObject obj1 = new CIMInstance();
CIMProperty cimProperty1 = new CIMProperty();
cimProperty1.setName("theKey");
cimProperty1.setValue(new CIMValue("theValue", CIMDataType.getPredefinedType(CIMDataType.STRING)));
obj1.addProperty(cimProperty1);
CIMObject obj2 = new CIMInstance();
CIMProperty cimProperty2 = new CIMProperty();
cimProperty2.setName("theKey");
cimProperty2.setValue(new CIMValue(null, CIMDataType.getPredefinedType(CIMDataType.STRING)));
obj2.addProperty(cimProperty2);
CIMObject obj3 = new CIMInstance();
CIMProperty cimProperty3 = new CIMProperty();
obj3.addProperty(cimProperty3);
Assert.assertEquals(vmwareViJavaAccess.getPropertyOfCimObject(obj1, "theKey"), "theValue");
Assert.assertNull(vmwareViJavaAccess.getPropertyOfCimObject(obj2, "theKey"));
Assert.assertNull(vmwareViJavaAccess.getPropertyOfCimObject(obj3, "theKey"));
}
use of org.sblim.wbem.cim.CIMObject in project opennms by OpenNMS.
the class VmwareCimMonitor method poll.
/**
* This method queries the Vmware hypervisor for sensor data.
*
* @param svc the monitored service
* @param parameters the parameter map
* @return the poll status for this system
*/
@Override
public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) {
final boolean ignoreStandBy = getKeyedBoolean(parameters, "ignoreStandBy", false);
final String vmwareManagementServer = getKeyedString(parameters, VMWARE_MANAGEMENT_SERVER_KEY, null);
final String vmwareManagedObjectId = getKeyedString(parameters, VMWARE_MANAGED_OBJECT_ID_KEY, null);
final String vmwareMangementServerUsername = getKeyedString(parameters, VMWARE_MANAGEMENT_SERVER_USERNAME_KEY, null);
final String vmwareMangementServerPassword = getKeyedString(parameters, VMWARE_MANAGEMENT_SERVER_PASSWORD_KEY, null);
TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT);
PollStatus serviceStatus = PollStatus.unknown();
for (tracker.reset(); tracker.shouldRetry() && !serviceStatus.isAvailable(); tracker.nextAttempt()) {
final VmwareViJavaAccess vmwareViJavaAccess = new VmwareViJavaAccess(vmwareManagementServer, vmwareMangementServerUsername, vmwareMangementServerPassword);
try {
vmwareViJavaAccess.connect();
} catch (MalformedURLException e) {
logger.warn("Error connecting VMware management server '{}': '{}' exception: {} cause: '{}'", vmwareManagementServer, e.getMessage(), e.getClass().getName(), e.getCause());
return PollStatus.unavailable("Error connecting VMware management server '" + vmwareManagementServer + "'");
} catch (RemoteException e) {
logger.warn("Error connecting VMware management server '{}': '{}' exception: {} cause: '{}'", vmwareManagementServer, e.getMessage(), e.getClass().getName(), e.getCause());
return PollStatus.unavailable("Error connecting VMware management server '" + vmwareManagementServer + "'");
}
if (!vmwareViJavaAccess.setTimeout(tracker.getConnectionTimeout())) {
logger.warn("Error setting connection timeout for VMware management server '{}'", vmwareManagementServer);
}
HostSystem hostSystem = vmwareViJavaAccess.getHostSystemByManagedObjectId(vmwareManagedObjectId);
String powerState = null;
if (hostSystem == null) {
return PollStatus.unknown("hostSystem=null");
} else {
HostRuntimeInfo hostRuntimeInfo = hostSystem.getRuntime();
if (hostRuntimeInfo == null) {
return PollStatus.unknown("hostRuntimeInfo=null");
} else {
HostSystemPowerState hostSystemPowerState = hostRuntimeInfo.getPowerState();
if (hostSystemPowerState == null) {
return PollStatus.unknown("hostSystemPowerState=null");
} else {
powerState = hostSystemPowerState.toString();
}
}
}
if ("poweredOn".equals(powerState)) {
List<CIMObject> cimObjects = null;
try {
cimObjects = vmwareViJavaAccess.queryCimObjects(hostSystem, "CIM_NumericSensor", svc.getIpAddr());
} catch (Exception e) {
logger.warn("Error retrieving CIM values from host system '{}'", vmwareManagedObjectId, e.getMessage());
vmwareViJavaAccess.disconnect();
return PollStatus.unavailable("Error retrieving cim values from host system '" + vmwareManagedObjectId + "'");
}
boolean success = true;
final StringBuilder reason = new StringBuilder("VMware CIM query returned: ");
for (CIMObject cimObject : cimObjects) {
String healthState = vmwareViJavaAccess.getPropertyOfCimObject(cimObject, "HealthState");
String cimObjectName = vmwareViJavaAccess.getPropertyOfCimObject(cimObject, "Name");
if (healthState != null) {
int healthStateInt = Integer.valueOf(healthState).intValue();
if (healthStateInt != 5) {
if (!success) {
reason.append(", ");
}
success = false;
reason.append(cimObjectName + " ");
if (m_healthStates.containsKey(healthStateInt)) {
reason.append("(" + m_healthStates.get(healthStateInt) + ")");
} else {
reason.append("(" + healthStateInt + ")");
}
}
}
}
if (success) {
serviceStatus = PollStatus.available();
} else {
serviceStatus = PollStatus.unavailable(reason.toString());
}
} else {
if (ignoreStandBy && "standBy".equals(powerState)) {
serviceStatus = PollStatus.up();
} else {
serviceStatus = PollStatus.unresponsive("Host system's power state is '" + powerState + "'");
}
}
vmwareViJavaAccess.disconnect();
}
return serviceStatus;
}
use of org.sblim.wbem.cim.CIMObject in project opennms by OpenNMS.
the class VmwareCimQuery method cimQuery.
private static void cimQuery(String hostname, String username, String password) {
System.out.print("Trying to connect to " + hostname + "... ");
VmwareViJavaAccess vmwareViJavaAccess = new VmwareViJavaAccess(hostname, username, password);
try {
vmwareViJavaAccess.connect();
} catch (MalformedURLException e) {
System.out.println("Exception:");
e.printStackTrace();
return;
} catch (RemoteException e) {
System.out.println("Exception:");
e.printStackTrace();
return;
}
System.out.println("Success!");
ManagedEntity[] hostSystems;
System.out.print(" Querying " + hostname + " for host systems... ");
try {
hostSystems = vmwareViJavaAccess.searchManagedEntities("HostSystem");
} catch (RemoteException remoteException) {
remoteException.printStackTrace();
vmwareViJavaAccess.disconnect();
return;
}
if (hostSystems != null) {
System.out.println(hostSystems.length + " host system(s) found!");
for (ManagedEntity managedEntity : hostSystems) {
HostSystem hostSystem = (HostSystem) managedEntity;
if (hostSystem.getSummary() != null) {
if (hostSystem.getRuntime() != null) {
String powerState = hostSystem.getRuntime().getPowerState().toString();
if (!"poweredOn".equals(powerState)) {
System.out.println(" Ignoring host system " + hostSystem.getName() + " (powerState=" + powerState + ")... ");
continue;
} else {
System.out.print(" Determining primary Ip address of host system " + hostSystem.getName() + " (powerState=" + powerState + ")... ");
}
} else {
System.out.println(" Ignoring host system " + hostSystem.getName() + " (powerState=unknown)... ");
continue;
}
} else {
System.out.println(" Ignoring host system " + hostSystem.getName() + " (powerState=unknown)... ");
continue;
}
String ipAddress;
ipAddress = vmwareViJavaAccess.getPrimaryHostSystemIpAddress(hostSystem);
if (ipAddress != null) {
System.out.print(ipAddress + "\n Querying host system " + hostSystem.getName() + " for numeric sensors... ");
List<CIMObject> cimObjects;
try {
cimObjects = vmwareViJavaAccess.queryCimObjects(hostSystem, "CIM_NumericSensor");
} catch (Exception e) {
System.out.println("Exception:");
e.printStackTrace();
continue;
}
if (cimObjects != null) {
// FIXME queryCimObjects returns an empty list or a filled list, but never null
System.out.println(cimObjects.size() + " sensor(s) found!");
for (CIMObject cimObject : cimObjects) {
String healthState = vmwareViJavaAccess.getPropertyOfCimObject(cimObject, "HealthState");
String cimObjectName = vmwareViJavaAccess.getPropertyOfCimObject(cimObject, "Name");
System.out.print(" " + cimObjectName);
if (healthState != null) {
System.out.println(" " + m_healthStates.get(Integer.valueOf(healthState)));
} else {
System.out.println();
}
}
} else {
System.out.println("NULL - aborting...");
continue;
}
} else {
System.out.println("NULL - aborting...");
continue;
}
}
} else {
System.out.println("NULL - aborting...");
}
System.out.println("Cleaning up...");
vmwareViJavaAccess.disconnect();
}
Aggregations