private int getAdjustment(VHost host) throws Exception { int adm = 0; for (VM vm : host.getVMs()) { adm += vm.cpuUsageMhz(); } return adm; }
public void start() throws Exception { while (true) { // find the vhost that cpu load is lower than the threshold VHost underloadHost = getUnderloadVhost(); if (underloadHost != null) { VHost targetVHost = null; // find the target vhost that can consolidate the vms on this vhost targetVHost = getTargetVHost(underloadHost, getAdjustment(underloadHost)); if (targetVHost != null) { // migrate all the vms on this vhost to the target vhost List<VM> vms = underloadHost.getVMs(); try { for (VM vm : vms) vm.migrate(targetVHost); } catch (Exception e) { e.printStackTrace(); log.warn("migration failed, please go to check your vCenter."); } // shut down this vhost powerOff(underloadHost); } } try { Thread.sleep(1000 * 3); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }