コード例 #1
0
 private int getAdjustment(VHost host) throws Exception {
   int adm = 0;
   for (VM vm : host.getVMs()) {
     adm += vm.cpuUsageMhz();
   }
   return adm;
 }
コード例 #2
0
  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();
      }
    }
  }