@Nullable @Override public VSphereHost apply(@Nullable String dataCenter) { try (VSphereServiceInstance instance = serviceInstance.get();) { ManagedEntity[] clusterEntities = new InventoryNavigator(instance.getInstance().getRootFolder()).searchManagedEntities("ClusterComputeResource"); Iterable<ClusterComputeResource> clusterComputeResources = Iterables.transform(Arrays.asList(clusterEntities), new Function<ManagedEntity, ClusterComputeResource>() { public ClusterComputeResource apply(ManagedEntity input) { return (ClusterComputeResource) input; } }); HostSystem curHostSystem = null; for (ClusterComputeResource cluster : clusterComputeResources) { if (cluster.getName().equals(dataCenter) || dataCenter.equals("default")) { HostSystem[] hostSystems = cluster.getHosts(); long maxMemory = Integer.MIN_VALUE; for (HostSystem hostSystem : hostSystems) { int currentMemory = hostSystem.getSummary().getQuickStats().getOverallMemoryUsage(); long currentTotalMemory = hostSystem.getConfig().getSystemResources().getConfig().getMemoryAllocation().getLimit(); if (currentTotalMemory - currentMemory > maxMemory) { curHostSystem = hostSystem; maxMemory = currentTotalMemory - currentMemory; } } break; } } return new VSphereHost(curHostSystem.getName(), serviceInstance.get()); // return this.systemHostToVSphereHost.apply(curHostSystem); } catch (Exception e) { logger.error("Problem in finding a valid host: " + e.toString(), e); } return null; }
public boolean isVmEventOnMonitoredCluster(Event event, String hostName) throws RemoteException { String dcName; Datacenter dc; // If contrailClusterName is null, all clusters under datacenter // are monitored by vcenter plugin. if (contrailClusterName == null) return true; if (event.getHost() != null) { hostName = event.getHost().getName(); HostSystem host = getVmwareHost(hostName, contrailDC, contrailDataCenterName); if (host != null) { ClusterComputeResource cluster = (ClusterComputeResource) host.getParent(); if (cluster != null) { String clstrName = cluster.getName(); if (clstrName != null && clstrName.equals(contrailClusterName)) { return true; } } } } return false; }
private void findHostsInFolder(Folder hostsFolder, List<HostSystem> hostsList) throws IOException, Exception { for (ManagedEntity e : hostsFolder.getChildEntity()) { if (e instanceof HostSystem) { hostsList.add((HostSystem)e); } // This is a cluster resource. Delve deeper to // find more hosts. if (e instanceof ComputeResource) { ComputeResource cr = (ComputeResource) e; if (e instanceof ClusterComputeResource) { ClusterComputeResource cluster = (ClusterComputeResource) e; if ((contrailClusterName != null) && (cluster.getName().equals(contrailClusterName) != true)) { continue; } } for(HostSystem host : cr.getHosts()) { hostsList.add((HostSystem)host); } } if (e instanceof Folder) { findHostsInFolder((Folder)e, hostsList); } } }
public static void main(String[] args) throws Exception { if(args.length!=4) { System.out.println("Usage: DrsModeChanger url username password mode"); System.exit(-1); } URL url = new URL(args[0]); String username = args[1]; String password = args[2]; String mode = args[3]; String drs_obj_id = "domain-c5"; ServiceInstance si = new ServiceInstance(url, username, password, true); ManagedObjectReference mref_drs = new ManagedObjectReference(); mref_drs.set_value(drs_obj_id); mref_drs.setType("ClusterComputeResource"); ClusterComputeResource ccr = (ClusterComputeResource )MorUtil.createExactManagedEntity(si.getServerConnection(), mref_drs); ClusterConfigSpec ccs = new ClusterConfigSpec(); ClusterDrsConfigInfo cdci = new ClusterDrsConfigInfo(); if("manual".equals(mode)) cdci.setDefaultVmBehavior(DrsBehavior.manual); else cdci.setDefaultVmBehavior(DrsBehavior.fullyAutomated); cdci.setVmotionRate(new Integer(5)); ccs.setDrsConfig(cdci); ccr.reconfigureCluster_Task(ccs, true); si.getServerConnection().logout(); System.out.println("End of changing DRS config to " + args[3]); }
public static void main(String[] args) throws Exception { if(args.length!=3) { System.out.println("Usage: DrsAffRule url username password"); System.exit(-1); } URL url = null; try { url = new URL(args[0]); } catch ( MalformedURLException urlE) { System.out.println("The URL provided is NOT valid. Please check it."); System.exit(-1); } String username = args[1]; String password = args[2]; String drs_obj_id = "domain-c5"; // The reference ID for cluster String vm1_oid = "vm-26"; // The reference ID for VM 1 String vm2_oid = "vm-28"; // The reference ID for VM 2 // initialize the system, set up web services ServiceInstance si = new ServiceInstance(url, username, password, true); //create the MOR object for DRS cluster ManagedObjectReference mref_drs = createMOR("ClusterComputeResource", drs_obj_id); ClusterComputeResource ccr = (ClusterComputeResource ) MorUtil.createExactManagedEntity(si.getServerConnection(), mref_drs); // create a new ClusterConfigSpec and populate it with related data for affinity rule ClusterConfigSpec ccs = new ClusterConfigSpec(); ClusterAffinityRuleSpec cars = new ClusterAffinityRuleSpec(); cars.setName("App and DB Appliance Bundle"); cars.setEnabled(Boolean.TRUE); ManagedObjectReference vm1 = createMOR("VirtualMachine", vm1_oid); ManagedObjectReference vm2 = createMOR("VirtualMachine", vm2_oid); cars.setVm(new ManagedObjectReference[] {vm1, vm2}); ClusterRuleSpec crs = new ClusterRuleSpec(); //*NOTE*: the following setOperation has to be called since operation must be set. crs.setOperation(ArrayUpdateOperation.add); crs.setInfo(cars); ccs.setRulesSpec(new ClusterRuleSpec[] {crs}); // make a call to set the configuration. ccr.reconfigureCluster_Task(ccs, true); si.getServerConnection().logout(); System.out.println("Done with setting affinity rule for DRS cluster."); }