private PerfMetricId[] getHostPerformanceManager(HostSystem host, int interval) throws RemoteException { PerfMetricId[] queryAvailablePerfMetric = null; String name = host.getName(); queryAvailablePerfMetric = hostPerfCache.getIfPresent(name); if (queryAvailablePerfMetric != null) { LOG.debug("CacheHIT: PerfID name: " + name); return queryAvailablePerfMetric; } queryAvailablePerfMetric = performanceManager.queryAvailablePerfMetric(host, null, null, interval); hostPerfCache.put(name, queryAvailablePerfMetric); LOG.debug("CacheMISS: PerfID name: " + name); return queryAvailablePerfMetric; }
/** * Searches for the primary ip address of a host system. * <p>The idea is to resolve the HostSystem's name and use the resulting IP if the IP is listed on the available addresses list, * otherwise, use the first ip listed on the available list.</p> * * @param hostSystem the host system to query * @return the primary ip address */ // TODO We should use the IP of the "Management Network" (i.e. the port that has enabled "Management Traffic" on the available vSwitches). // Resolving the name of the HostSystem as the FQDN is the most closest thing for that. public String getPrimaryHostSystemIpAddress(HostSystem hostSystem) { TreeSet<String> addresses = getHostSystemIpAddresses(hostSystem); String ipAddress = null; try { ipAddress = InetAddress.getByName(hostSystem.getName()).getHostAddress(); } catch (Exception e) { logger.debug("Can't resolve the IP address from {}.", hostSystem.getName()); } if (ipAddress == null) { return addresses.first(); } return addresses.contains(ipAddress) ? ipAddress : addresses.first(); }
/** * Searches for the primary ip address of a host system. * <p>The idea is to resolve the HostSystem's name and use the resulting IP if the IP is listed on the available addresses list, * otherwise, use the first ip listed on the available list.</p> * * @param hostSystem the host system to query * @return the primary ip address */ // TODO We should use the IP of the "Management Network" (i.e. the port that has enabled "Management Traffic" on the available vSwitches). // Resolving the name of the HostSystem as the FQDN is the most closest thing for that. public String getPrimaryHostSystemIpAddress(HostSystem hostSystem) { TreeSet<String> addresses = getHostSystemIpAddresses(hostSystem); String ipAddress = null; try { ipAddress = InetAddress.getByName(hostSystem.getName()).getHostAddress(); } catch (Exception e) { logger.debug("Can't resolve the IP address from {}.", hostSystem.getName()); } if (ipAddress == null) { return addresses.first(); } return addresses.contains(ipAddress) ? ipAddress : addresses.first(); }
/** * Returns the display name of a host */ private void getHostName(String hostName, PrintWriter out) throws IOException { try { HostSystem host = (HostSystem) getManagedEntity(hostName, "HostSystem"); String name = ""; if (host == null) { LOG.warn("No host named '" + hostName + "' found"); } else { name = host.getName(); } out.print(name); out.flush(); } catch (Exception ex) { LOG.error("An error occurred : " + ex.toString()); } }
private boolean reachableCimService(VmwareViJavaAccess vmwareViJavaAccess, HostSystem hostSystem, String ipAddress) { int oldTimeout = vmwareViJavaAccess.getTimeout(); if (!vmwareViJavaAccess.setTimeout(request.getCimTimeout())) { logger.warn("Error setting CIM connection timeout"); } logger.debug("Probing address {} for Host System '{}'", ipAddress, hostSystem.getName()); List<CIMObject> cimObjects = null; try { cimObjects = vmwareViJavaAccess.queryCimObjects(hostSystem, "CIM_NumericSensor", ipAddress); } catch (ConnectException | RemoteException | CIMException e) { logger.debug("Error while probing for address {} for Host System '{}'", ipAddress, hostSystem.getName()); logger.debug("Exception thrown while probing IP address: {}", e); return false; } finally { if (!vmwareViJavaAccess.setTimeout(oldTimeout)) { logger.warn("Error restoring connection timeout"); } } return cimObjects != null; }
private boolean reachableCimService(VmwareViJavaAccess vmwareViJavaAccess, HostSystem hostSystem, String ipAddress) { int oldTimeout = vmwareViJavaAccess.getTimeout(); if (!vmwareViJavaAccess.setTimeout(request.getCimTimeout())) { logger.warn("Error setting CIM connection timeout"); } logger.debug("Probing address {} for Host System '{}'", ipAddress, hostSystem.getName()); List<CIMObject> cimObjects = null; try { cimObjects = vmwareViJavaAccess.queryCimObjects(hostSystem, "CIM_NumericSensor", ipAddress); } catch (ConnectException | RemoteException | CIMException e) { logger.debug("Error while probing for address {} for Host System '{}'", ipAddress, hostSystem.getName()); logger.debug("Exception thrown while probing IP address: {}", e); return false; } finally { if (!vmwareViJavaAccess.setTimeout(oldTimeout)) { logger.warn("Error restoring connection timeout"); } } return cimObjects != null; }
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; }
if (hsum != null && hd != null) { JsonObject jObject = new JsonObject(); jObject.addProperty("{#ESXHOST}", h.getName()); jObject.addProperty("{#UUID}", hd.getUuid()); jObject.addProperty("{#CLUSTER}", h.getParent().getName());
public void scanAllHosts() { VMWareQueryTemplate t = new VMWareQueryTemplate(getServiceInstance()); for (HostSystem host : t.findAllHostSystems()) { try { scanHost(host, true); } catch (RuntimeException e) { logger.warn("scan host failed: {}", host.getName()); } } }
/** * Returns the running host of a virtual machine */ private void getVmHost(String vmName, PrintWriter out) throws IOException { try { VirtualMachine vm = (VirtualMachine) getManagedEntity(vmName, "VirtualMachine"); String vmHost = ""; if (vm == null) { LOG.warn("No vm named '" + vmName + "' found"); } else { VirtualMachineRuntimeInfo vmRuntimeInfo = vm.getRuntime(); ManagedObjectReference hmor = vmRuntimeInfo.getHost(); HostSystem host = new HostSystem(vm.getServerConnection(), hmor); vmHost = host.getName(); } out.print(vmHost); out.flush(); } catch (Exception ex) { LOG.error("An error occurred : " + ex.toString()); } }
protected void scanHost(HostSystem host, boolean scanGuests) { try { logger.info("scanning esxi host={}", host.getName()); ObjectNode n = toObjectNode(host); logger.info("scanning guests on esxi host={}",host.getName()); VirtualMachine[] vms = host.getVms(); if (vms != null) { logger.info("scanned {} guests on host={}",count,host.getName()); logger.info("scanned {} guests on host={}",count,host.getName());
/** * 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; }
protected void updateClusterHostRelationship(ClusterComputeResource cluster, HostSystem host) { logger.debug("updating relationship between cluster={} and host={}", cluster.getName(), host.getName()); String cypher = "match (h {id:{hostId} }), (c:VMWareCluster {id: {clusterId}}) MERGE (c)-[r:CONTAINS]->(h) ON CREATE SET r.updateTs=timestamp(),r.createTs=timestamp() ON MATCH SET r.updateTs=timestamp() return r"; getProjector().getNeoRxClient().execCypher(cypher, "hostId", getUniqueId(host), "clusterId", getUniqueId(cluster)); }
protected void updateHostVmRelationship(HostSystem h, VirtualMachine vm) { logger.debug("updating relationship between host={} and vm={}", h.getName(), vm.getName()); String cypher = "match (h:ComputeHost {id:{hostId} }), (c:ComputeInstance {id: {computeId}}) MERGE (h)-[r:HOSTS]->(c) ON CREATE SET r.updateTs=timestamp(),r.createTs=timestamp() ON MATCH SET r.updateTs=timestamp() return r"; getProjector().getNeoRxClient().execCypher(cypher, "hostId", getUniqueId(h), "computeId", getUniqueId(vm)); }
/** * 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; }
protected void clearStaleRelationships(HostSystem host, long ts) { logger.info( "clearing stale ComputeHost->ComputeInstance relationships for host: {}", host.getName()); String cypher = "match (h:ComputeHost {id:{id}})-[r:HOSTS]-(c:ComputeInstance) where r.updateTs<{ts} delete r"; // this is for logging only for (JsonNode n : getProjector().getNeoRxClient() .execCypher(cypher.replace("delete r", "return r"), "id", getUniqueId(host), "ts", ts) .toList().blockingGet()) { logger.info("clearing stale relationship: {}", n); } // end of logging section getProjector().getNeoRxClient().execCypher(cypher, "id", getUniqueId(host), "ts", ts); }
public static void main(String[] args) throws Exception { ServiceInstance si = new ServiceInstance(new URL("https://10.20.143.209/sdk"), "Administrator", "password", true); ClusterComputeResource drsCluster = (ClusterComputeResource) new InventoryNavigator( si.getRootFolder()).searchManagedEntity("ClusterComputeResource", "myDrs"); if(drsCluster==null) { System.out.println("There is no cluster called myDrs. Exiting..."); } ClusterDrsMigration[] histories = drsCluster.getMigrationHistory(); if(histories==null) { System.out.println("There is no migration history set. Please check whether it's running in automatic mode!\nExiting..."); } System.out.println("There were totally " + histories.length + " migration(s)."); for(int i=0; i < histories.length; i++) { ServerConnection sc = si.getServerConnection(); VirtualMachine vm = (VirtualMachine) MorUtil.createExactManagedEntity(sc, histories[i].getVm()); HostSystem srcHost = (HostSystem) MorUtil.createExactManagedEntity(sc, histories[i].getSource()); HostSystem dstHost = (HostSystem) MorUtil.createExactManagedEntity(sc, histories[i].getDestination()); System.out.println("VM: " + vm.getName() + " migrated from " + srcHost.getName() + " to " + dstHost.getName()); } si.getServerConnection().logout(); } }
public static void main(String[] args) throws Exception { ServiceInstance si = new ServiceInstance(new URL("https://10.20.143.209/sdk"), "Administrator", "password", true); ClusterComputeResource drsCluster = (ClusterComputeResource) new InventoryNavigator( si.getRootFolder()).searchManagedEntity("ClusterComputeResource", "myDrs"); if(drsCluster==null) { System.out.println("There is no cluster called myDrs. Exiting..."); } ClusterDrsMigration[] histories = drsCluster.getMigrationHistory(); if(histories==null) { System.out.println("There is no migration history set. Please check whether it's running in automatic mode!\nExiting..."); } System.out.println("There were totally " + histories.length + " migration(s)."); for(int i=0; i < histories.length; i++) { ServerConnection sc = si.getServerConnection(); VirtualMachine vm = (VirtualMachine) MorUtil.createExactManagedEntity(sc, histories[i].getVm()); HostSystem srcHost = (HostSystem) MorUtil.createExactManagedEntity(sc, histories[i].getSource()); HostSystem dstHost = (HostSystem) MorUtil.createExactManagedEntity(sc, histories[i].getDestination()); System.out.println("VM: " + vm.getName() + " migrated from " + srcHost.getName() + " to " + dstHost.getName()); } si.getServerConnection().logout(); } }
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;