use of com.vmware.vim25.mo.HostSystem in project opennms by OpenNMS.
the class VmwareViJavaAccessTest method setUp.
@Before
public void setUp() throws Exception {
// setup required objects
managedObjectReferenceManagedEntity = new ManagedObjectReference();
managedObjectReferenceManagedEntity.setType("ManagedEntity");
managedObjectReferenceManagedEntity.setVal("moIdMe");
managedObjectReferenceVirtualMachine = new ManagedObjectReference();
managedObjectReferenceVirtualMachine.setType("VirtualMachine");
managedObjectReferenceVirtualMachine.setVal("moIdVm");
managedObjectReferenceHostSystem = new ManagedObjectReference();
managedObjectReferenceHostSystem.setType("HostSystem");
managedObjectReferenceHostSystem.setVal("moIdHs");
// setup VmwareViJavaAccess
vmwareViJavaAccess = new VmwareViJavaAccess("hostname", "username", "password") {
@Override
protected void relax() {
}
};
// setup PerformanceManager
mockPerformanceManager = createMock(PerformanceManager.class);
// setup ServiceInstance mock
mockServiceInstance = createMock(ServiceInstance.class);
// setup ServerConnection
mockServerConnection = new ServerConnection(new URL("https://hostname/sdk"), new VimPortType(new WSClient("https://hostname/sdk") {
@Override
protected SSLSocketFactory getTrustAllSocketFactory(boolean ignoreCert) throws RemoteException {
return null;
}
}), mockServiceInstance);
// setup AboutInfo
mockAboutInfo = createMock(AboutInfo.class);
expectNew(ServiceInstance.class, new Class<?>[] { URL.class, String.class, String.class }, new URL("https://hostname/sdk"), "username", "password").andReturn(mockServiceInstance).anyTimes();
expect(mockServiceInstance.getServerConnection()).andReturn(mockServerConnection).anyTimes();
expect(mockServiceInstance.getPerformanceManager()).andReturn(mockPerformanceManager).anyTimes();
expect(mockServiceInstance.getAboutInfo()).andReturn(mockAboutInfo).anyTimes();
managedEntity = new ManagedEntity(null, managedObjectReferenceManagedEntity);
virtualMachine = new VirtualMachine(null, managedObjectReferenceVirtualMachine);
hostSystem = new HostSystem(null, managedObjectReferenceHostSystem);
// setup MorUtil
mockStatic(MorUtil.class);
expect(MorUtil.createExactManagedEntity(mockServerConnection, managedObjectReferenceManagedEntity)).andReturn(managedEntity).anyTimes();
expect(MorUtil.createExactManagedEntity(mockServerConnection, managedObjectReferenceVirtualMachine)).andReturn(virtualMachine).anyTimes();
expect(MorUtil.createExactManagedEntity(mockServerConnection, managedObjectReferenceHostSystem)).andReturn(hostSystem).anyTimes();
// setup about info
expect(mockAboutInfo.getApiVersion()).andReturn("2.x");
expect(mockAboutInfo.getApiVersion()).andReturn("3.x");
expect(mockAboutInfo.getApiVersion()).andReturn("4.x");
expect(mockAboutInfo.getApiVersion()).andReturn("5.x");
expect(mockAboutInfo.getApiVersion()).andReturn("6.x");
expect(mockAboutInfo.getApiVersion()).andReturn("x");
// setup performance data
int refreshRate = 100;
int metricCount = 15;
int instanceModifier = 5;
perfQuerySpec = new PerfQuerySpec();
perfQuerySpec.setEntity(managedEntity.getMOR());
perfQuerySpec.setMaxSample(new Integer(1));
perfQuerySpec.setIntervalId(refreshRate);
perfEntityMetricBases = new PerfEntityMetricBase[metricCount];
perfCounterInfos = new PerfCounterInfo[metricCount];
for (int i = 0; i < metricCount; i++) {
ElementDescription groupInfo = new ElementDescription();
groupInfo.setKey("key" + i);
ElementDescription nameInfo = new ElementDescription();
nameInfo.setKey("name" + i);
perfCounterInfos[i] = new PerfCounterInfo();
perfCounterInfos[i].setKey(i);
perfCounterInfos[i].setGroupInfo(groupInfo);
perfCounterInfos[i].setNameInfo(nameInfo);
perfCounterInfos[i].setRollupType(PerfSummaryType.average);
perfEntityMetricBases[i] = new PerfEntityMetric();
PerfMetricIntSeries[] perfMetricIntSeries;
int instanceCount = (i % instanceModifier) + 1;
perfMetricIntSeries = new PerfMetricIntSeries[instanceCount];
for (int b = 0; b < instanceCount; b++) {
PerfMetricId perfMetricId = new PerfMetricId();
perfMetricId.setCounterId(i);
if (instanceCount == 1) {
perfMetricId.setInstance(null);
} else {
perfMetricId.setInstance("instance" + b);
}
perfMetricIntSeries[b] = new PerfMetricIntSeries();
perfMetricIntSeries[b].setValue(new long[] { (long) 42 });
perfMetricIntSeries[b].setId(perfMetricId);
}
((PerfEntityMetric) perfEntityMetricBases[i]).setValue(perfMetricIntSeries);
}
// setup PerfProviderSummary
mockPerfProviderSummary = createMock(PerfProviderSummary.class);
expect(mockPerformanceManager.queryPerfProviderSummary(managedEntity)).andReturn(mockPerfProviderSummary).anyTimes();
expect(mockPerfProviderSummary.getRefreshRate()).andReturn(refreshRate).anyTimes();
expect(mockPerformanceManager.getPerfCounter()).andReturn(perfCounterInfos).anyTimes();
expect(mockPerformanceManager.queryPerf(anyObject(PerfQuerySpec[].class))).andReturn(perfEntityMetricBases).anyTimes();
// setup network info
HostNetworkInfo hostNetworkInfo = new HostNetworkInfo();
int numberOfVnics = 3;
HostVirtualNic[] hostVirtualNics = new HostVirtualNic[numberOfVnics];
for (int i = 0; i < numberOfVnics; i++) {
HostVirtualNicSpec hostVirtualNicSpec = new HostVirtualNicSpec();
HostIpConfig hostIpConfig = new HostIpConfig();
hostIpConfig.setIpAddress("192.168.1." + (i + 1));
hostVirtualNicSpec.setIp(hostIpConfig);
hostVirtualNics[i] = new HostVirtualNic();
hostVirtualNics[i].setSpec(hostVirtualNicSpec);
}
hostNetworkInfo.setVnic(hostVirtualNics);
HostVirtualNic[] hostVirtualConsoleNics = new HostVirtualNic[numberOfVnics];
for (int i = 0; i < numberOfVnics; i++) {
HostVirtualNicSpec hostVirtualNicSpec = new HostVirtualNicSpec();
HostIpConfig hostIpConfig = new HostIpConfig();
hostIpConfig.setIpAddress("192.168.2." + (i + 1));
hostVirtualNicSpec.setIp(hostIpConfig);
hostVirtualConsoleNics[i] = new HostVirtualNic();
hostVirtualConsoleNics[i].setSpec(hostVirtualNicSpec);
}
hostNetworkInfo.setConsoleVnic(hostVirtualConsoleNics);
HostServiceTicket hostServiceTicket = new HostServiceTicket();
hostServiceTicket.setSessionId("sessionId");
// setup HostSystem
mockHostSystem = createMock(HostSystem.class);
// setup HostNetworkSystem
mockHostNetworkSystem = createMock(HostNetworkSystem.class);
// setup CIMClient
mockCIMClient = createPartialMock(CIMClient.class, "enumerateInstances");
// setup the cim objects
cimObjects = new ArrayList<CIMObject>();
int cimObjectCount = 5;
for (int i = 0; i < cimObjectCount; i++) {
CIMInstance cimInstance = new CIMInstance();
cimInstance.setName("cimInstance" + i);
cimObjects.add(cimInstance);
}
expect(mockHostSystem.getName()).andReturn("mockesxi01.local").anyTimes();
expect(mockHostSystem.getHostNetworkSystem()).andReturn(mockHostNetworkSystem).anyTimes();
expect(mockHostSystem.acquireCimServicesTicket()).andReturn(hostServiceTicket).anyTimes();
expect(mockHostNetworkSystem.getNetworkInfo()).andReturn(hostNetworkInfo).anyTimes();
expectNew(CIMClient.class, new Class<?>[] { CIMNameSpace.class, Principal.class, Object.class }, anyObject(), anyObject(), anyObject()).andReturn(mockCIMClient).anyTimes();
suppress(method(CIMClient.class, "useMPost"));
expect(mockCIMClient.enumerateInstances(new CIMObjectPath("cimClass"))).andReturn(Collections.enumeration(cimObjects)).anyTimes();
}
use of com.vmware.vim25.mo.HostSystem in project opennms by OpenNMS.
the class VmwareViJavaAccessTest method testGetHostSystemByManagedObjectId.
@Test
public void testGetHostSystemByManagedObjectId() throws Exception {
replay(mockServiceInstance, MorUtil.class, ServiceInstance.class);
try {
vmwareViJavaAccess.connect();
} catch (MalformedURLException e) {
Assert.fail(e.getMessage());
} catch (RemoteException e) {
Assert.fail(e.getMessage());
}
HostSystem returnedHostSystem = vmwareViJavaAccess.getHostSystemByManagedObjectId("moIdHs");
Assert.assertNotNull(returnedHostSystem);
Assert.assertEquals(hostSystem.getMOR().getVal(), returnedHostSystem.getMOR().getVal());
verify(mockServiceInstance, MorUtil.class, ServiceInstance.class);
}
use of com.vmware.vim25.mo.HostSystem 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 com.vmware.vim25.mo.HostSystem in project CloudStack-archive by CloudStack-extras.
the class HostMO method getDatastorePropertiesOnHyperHost.
@Override
public ObjectContent[] getDatastorePropertiesOnHyperHost(String[] propertyPaths) throws Exception {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - retrieveProperties() on Datastore properties. target MOR: " + _mor.get_value() + ", properties: " + new Gson().toJson(propertyPaths));
PropertySpec pSpec = new PropertySpec();
pSpec.setType("Datastore");
pSpec.setPathSet(propertyPaths);
TraversalSpec host2DatastoreTraversal = new TraversalSpec();
host2DatastoreTraversal.setType("HostSystem");
host2DatastoreTraversal.setPath("datastore");
host2DatastoreTraversal.setName("host2DatastoreTraversal");
ObjectSpec oSpec = new ObjectSpec();
oSpec.setObj(_mor);
oSpec.setSkip(Boolean.TRUE);
oSpec.setSelectSet(new SelectionSpec[] { host2DatastoreTraversal });
PropertyFilterSpec pfSpec = new PropertyFilterSpec();
pfSpec.setPropSet(new PropertySpec[] { pSpec });
pfSpec.setObjectSet(new ObjectSpec[] { oSpec });
ObjectContent[] properties = _context.getService().retrieveProperties(_context.getServiceContent().getPropertyCollector(), new PropertyFilterSpec[] { pfSpec });
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - retrieveProperties() done");
return properties;
}
use of com.vmware.vim25.mo.HostSystem in project CloudStack-archive by CloudStack-extras.
the class ClusterMO method getVmPropertiesOnHyperHost.
@Override
public ObjectContent[] getVmPropertiesOnHyperHost(String[] propertyPaths) throws Exception {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - retrieveProperties() for VM properties. target MOR: " + _mor.get_value() + ", properties: " + new Gson().toJson(propertyPaths));
PropertySpec pSpec = new PropertySpec();
pSpec.setType("VirtualMachine");
pSpec.setPathSet(propertyPaths);
TraversalSpec host2VmFolderTraversal = new TraversalSpec();
host2VmFolderTraversal.setType("HostSystem");
host2VmFolderTraversal.setPath("vm");
host2VmFolderTraversal.setName("host2VmFolderTraversal");
TraversalSpec cluster2HostFolderTraversal = new TraversalSpec();
cluster2HostFolderTraversal.setType("ClusterComputeResource");
cluster2HostFolderTraversal.setPath("host");
cluster2HostFolderTraversal.setName("cluster2HostFolderTraversal");
cluster2HostFolderTraversal.setSelectSet(new SelectionSpec[] { host2VmFolderTraversal });
ObjectSpec oSpec = new ObjectSpec();
oSpec.setObj(getMor());
oSpec.setSkip(Boolean.TRUE);
oSpec.setSelectSet(new SelectionSpec[] { cluster2HostFolderTraversal });
PropertyFilterSpec pfSpec = new PropertyFilterSpec();
pfSpec.setPropSet(new PropertySpec[] { pSpec });
pfSpec.setObjectSet(new ObjectSpec[] { oSpec });
ObjectContent[] properties = _context.getService().retrieveProperties(_context.getServiceContent().getPropertyCollector(), new PropertyFilterSpec[] { pfSpec });
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - retrieveProperties() done");
return properties;
}
Aggregations