public String computeUniqueId(ManagedObjectReference mor) { Preconditions .checkNotNull(mor, "ManagedObjectReference cannot be null"); Preconditions.checkArgument( !ManagedObjectTypes.VIRTUAL_MACHINE.equals(mor.getType()), "cannot call computeMacId() with mor.type=VirtualMachine"); return Hashing .sha1() .hashString(getVCenterId() + mor.getType() + mor.getVal(), Charsets.UTF_8) .toString(); }
ObjectNode toObjectNode(HostSystem host) { ManagedObjectReference mor = host.getMOR(); HostHardwareInfo hh = host.getHardware(); ObjectNode n = mapper.createObjectNode().put("id", getUniqueId(host)) .put("name", host.getName()).put("vmw_morType", mor.getType()) .put("vmw_morVal", mor.getVal()).put("vmw_hardwareModel", hh.getSystemInfo().getModel()) .put("vmw_cpuCoreCount", hh.getCpuInfo().getNumCpuCores()) .put("vmw_memorySize", hh.getMemorySize()); return n; }
.forEach(hostSystem -> { synchronized (m_hostSystemMap) { m_hostSystemMap.put(hostSystem.getMOR().getVal(), hostSystem.getName()); .filter(hostSystem -> checkForAttribute(hostSystem)) .map(hostSystem -> CompletableFuture.supplyAsync(() -> { logger.debug("Adding Host System '{}' (ID: {})", hostSystem.getName(), hostSystem.getMOR().getVal()); logger.debug("Found {} IP addresses for Host System '{}' (ID: {}): {}", ipAddresses.size(), hostSystem.getName(), hostSystem.getMOR().getVal(), ipAddresses); node.putAsset(new RequisitionAsset("cpu", hostSystem.getHardware().getCpuInfo().getNumCpuCores() + " cores")); } catch (Exception e) { logger.debug("Can't find CPU information for '{}' (ID: {})", hostSystem.getName(), hostSystem.getMOR().getVal()); node.putAsset(new RequisitionAsset("ram", Math.round(hostSystem.getHardware().getMemorySize() / 1000000f) + " MB")); } catch (Exception e) { logger.debug("Can't find Memory information for '{}' (ID: {})", hostSystem.getName(), hostSystem.getMOR().getVal());
/** * Checks whether the virtual machine should be imported into the requisition. * * @param virtualMachine the system to check * @return true for import, false otherwise */ private boolean checkVMPowerState(VirtualMachine virtualMachine) { logger.debug("Checking power state for VM {} (ID: {})", virtualMachine.getName(), virtualMachine.getMOR().getVal()); String powerState = virtualMachine.getRuntime().getPowerState().toString(); if ("poweredOn".equals(powerState) && request.isImportVMPoweredOn()) { return true; } if ("poweredOff".equals(powerState) && request.isImportVMPoweredOff()) { return true; } if ("suspended".equals(powerState) && request.isImportVMSuspended()) { return true; } return false; }
/** * Checks whether the host system should be imported into the requisition. * * @param hostSystem the system to check * @return true for import, false otherwise */ private boolean checkHostPowerState(HostSystem hostSystem) { logger.debug("Checking power state for host system {} (ID {})", hostSystem.getName(), hostSystem.getMOR().getVal()); String powerState = hostSystem.getRuntime().getPowerState().toString(); if ("poweredOn".equals(powerState) && request.isImportHostPoweredOn()) { return true; } if ("poweredOff".equals(powerState) && request.isImportHostPoweredOff()) { return true; } if ("standBy".equals(powerState) && request.isImportHostStandBy()) { return true; } if ("unknown".equals(powerState) && request.isImportHostUnknown()) { return true; } return false; }
/** * Checks whether the virtual machine should be imported into the requisition. * * @param virtualMachine the system to check * @return true for import, false otherwise */ private boolean checkVMPowerState(VirtualMachine virtualMachine) { logger.debug("Checking power state for VM {} (ID: {})", virtualMachine.getName(), virtualMachine.getMOR().getVal()); String powerState = virtualMachine.getRuntime().getPowerState().toString(); if ("poweredOn".equals(powerState) && request.isImportVMPoweredOn()) { return true; } if ("poweredOff".equals(powerState) && request.isImportVMPoweredOff()) { return true; } if ("suspended".equals(powerState) && request.isImportVMSuspended()) { return true; } return false; }
/** * Checks whether the host system should be imported into the requisition. * * @param hostSystem the system to check * @return true for import, false otherwise */ private boolean checkHostPowerState(HostSystem hostSystem) { logger.debug("Checking power state for host system {} (ID {})", hostSystem.getName(), hostSystem.getMOR().getVal()); String powerState = hostSystem.getRuntime().getPowerState().toString(); if ("poweredOn".equals(powerState) && request.isImportHostPoweredOn()) { return true; } if ("poweredOff".equals(powerState) && request.isImportHostPoweredOff()) { return true; } if ("standBy".equals(powerState) && request.isImportHostStandBy()) { return true; } if ("unknown".equals(powerState) && request.isImportHostUnknown()) { return true; } return false; }
/** * Gets the custom attributes. * * @param entity the entity * @return the custom attributes */ private Map<String,String> getCustomAttributes(ManagedEntity entity) { final Map<String,String> attributes = new TreeMap<String,String>(); logger.debug("Getting custom attributes from VMware management server {} : ManagedEntity {} (ID: {})", request.getHostname(), entity.getName(), entity.getMOR().getVal()); CustomFieldDef[] defs = new CustomFieldDef[0]; try { defs = entity.getAvailableField(); } catch (RemoteException e) { logger.warn("Cannot fetch attributes for entity '{}' (ID: {})", entity.getName(), entity.getMOR().getVal()); logger.warn("Exception thrown while fetching attributes: {}", e); return attributes; } CustomFieldValue[] values = entity.getCustomValue(); for (int i = 0; defs != null && i < defs.length; i++) { String key = defs[i].getName(); int targetIndex = defs[i].getKey(); for (int j = 0; values != null && j < values.length; j++) { if (targetIndex == values[j].getKey()) { attributes.put(key, ((CustomFieldStringValue) values[j]).getValue()); } } } return attributes; }
/** * Gets the custom attributes. * * @param entity the entity * @return the custom attributes */ private Map<String,String> getCustomAttributes(ManagedEntity entity) { final Map<String,String> attributes = new TreeMap<String,String>(); logger.debug("Getting custom attributes from VMware management server {} : ManagedEntity {} (ID: {})", request.getHostname(), entity.getName(), entity.getMOR().getVal()); CustomFieldDef[] defs = new CustomFieldDef[0]; try { defs = entity.getAvailableField(); } catch (RemoteException e) { logger.warn("Cannot fetch attributes for entity '{}' (ID: {})", entity.getName(), entity.getMOR().getVal()); logger.warn("Exception thrown while fetching attributes: {}", e); return attributes; } CustomFieldValue[] values = entity.getCustomValue(); for (int i = 0; defs != null && i < defs.length; i++) { String key = defs[i].getName(); int targetIndex = defs[i].getKey(); for (int j = 0; values != null && j < values.length; j++) { if (targetIndex == values[j].getKey()) { attributes.put(key, ((CustomFieldStringValue) values[j]).getValue()); } } } return attributes; }
logger.debug("Getting Managed entity custom attributes from VMware management server {} : ManagedEntity {} (ID: {})", request.getHostname(), managedEntity.getName(), managedEntity.getMOR().getVal()); Map<String,String> attribMap = getCustomAttributes(managedEntity);
.filter(virtualMachine -> checkForAttribute(virtualMachine)) .map(virtualMachine -> CompletableFuture.supplyAsync(() -> { logger.debug("Adding Virtual Machine '{}' (ID: {})", virtualMachine.getName(), virtualMachine.getMOR().getVal()); logger.debug("Found {} IP addresses for Virtual Machine '{}' (ID: {}): {}", ipAddresses.size(), virtualMachine.getName(), virtualMachine.getMOR().getVal(), ipAddresses); node.putAsset(new RequisitionAsset("cpu", virtualMachine.getConfig().getHardware().getNumCPU() + " vCPU")); } catch (Exception e) { logger.debug("Can't find CPU information for {} (ID: {})", virtualMachine.getName(), virtualMachine.getMOR().getVal()); node.putAsset(new RequisitionAsset("ram", virtualMachine.getConfig().getHardware().getMemoryMB() + " MB")); } catch (Exception e) { logger.debug("Can't find Memory information for {} (ID: {})", virtualMachine.getName(), virtualMachine.getMOR().getVal());
logger.warn("Cannot determine ip address for host system '{}'", hostSystem.getMOR().getVal()); return cimObjects;
logger.warn("Cannot determine ip address for host system '{}'", hostSystem.getMOR().getVal()); return cimObjects;
.filter(virtualMachine -> checkForAttribute(virtualMachine)) .map(virtualMachine -> CompletableFuture.supplyAsync(() -> { logger.debug("Adding Virtual Machine '{}' (ID: {})", virtualMachine.getName(), virtualMachine.getMOR().getVal()); logger.debug("Found {} IP addresses for Virtual Machine '{}' (ID: {}): {}", ipAddresses.size(), virtualMachine.getName(), virtualMachine.getMOR().getVal(), ipAddresses); node.putAsset(new RequisitionAsset("cpu", virtualMachine.getConfig().getHardware().getNumCPU() + " vCPU")); } catch (Exception e) { logger.debug("Can't find CPU information for {} (ID: {})", virtualMachine.getName(), virtualMachine.getMOR().getVal()); node.putAsset(new RequisitionAsset("ram", virtualMachine.getConfig().getHardware().getMemoryMB() + " MB")); } catch (Exception e) { logger.debug("Can't find Memory information for {} (ID: {})", virtualMachine.getName(), virtualMachine.getMOR().getVal());
public static void main(String[] args) throws Exception { ServiceInstance si = SampleUtil.createServiceInstance(); Folder rootFolder = si.getRootFolder(); Datacenter dc = (Datacenter) new InventoryNavigator(rootFolder).searchManagedEntities("Datacenter")[0]; ManagedEntity[] entities = new InventoryNavigator(dc).searchManagedEntities("ResourcePool"); System.out.println("============ Resource Pools ============"); for (ManagedEntity entity : entities) { ResourcePool rp = (ResourcePool) entity; System.out.println("Name: " + rp.getName()); System.out.println("-- Moid: " + rp.getMOR().getVal()); ResourcePoolSummary sum = rp.getSummary(); ResourceAllocationInfo info; info = sum.getConfig().getCpuAllocation(); System.out.println("-- CPU reservation: " + info.getReservation()); System.out.println("-- CPU limit: " + info.getLimit()); info = sum.getConfig().getMemoryAllocation(); System.out.println("-- Mem reservation: " + info.getReservation()); System.out.println("-- Mem limit: " + info.getLimit()); } }
hostNetworkSystem = hostSystem.getHostNetworkSystem(); } catch (RemoteException e) { logger.warn("Error fetching network information for Host System '{}' (ID: {})", hostSystem.getName(), hostSystem.getMOR().getVal()); logger.warn("Exception thrown while fetching network information: {}", e); return ipAddresses;
hostNetworkSystem = hostSystem.getHostNetworkSystem(); } catch (RemoteException e) { logger.warn("Error fetching network information for Host System '{}' (ID: {})", hostSystem.getName(), hostSystem.getMOR().getVal()); logger.warn("Exception thrown while fetching network information: {}", e); return ipAddresses;
for (ManagedEntity managedEntity : virtualMachines) { if ("poweredOn".equals(((VirtualMachine) managedEntity).getRuntime().getPowerState().toString())) { lookupMetrics("default-VirtualMachine" + apiVersion, managedEntity.getMOR().getVal()); break; for (ManagedEntity managedEntity : hostSystems) { if ("poweredOn".equals(((HostSystem) managedEntity).getRuntime().getPowerState().toString())) { lookupMetrics("default-HostSystem" + apiVersion, managedEntity.getMOR().getVal()); break;
String moVal = mor.getVal(); GuestInfo g = vm.getGuest(); setVal(n, "name", vm.getName());