Пример #1
0
  public static void main(String[] args) throws Exception {
    ServiceInstance si = new ServiceInstance(new URL(VCENTERURL), USERNAME, PASSWORD, true);
    Folder rootFolder = si.getRootFolder();
    ManagedEntity[] hosts = new InventoryNavigator(rootFolder).searchManagedEntities("HostSystem");
    if (hosts == null || hosts.length == 0) {
      return;
    }
    for (int i = 0; i < hosts.length; i++) { // get all vm in Vhost
      System.out.println("vHost loop ******************");
      System.out.println("HostName: " + hosts[i].getName());
      ManagedEntity[] mes =
          new InventoryNavigator(rootFolder).searchManagedEntities("VirtualMachine");
      for (int j = 0; j < mes.length; j++) {
        VirtualMachine vm = (VirtualMachine) mes[j];
        VirtualMachineConfigInfo vminfo = vm.getConfig();
        vm.getResourcePool();
        System.out.println("vHost: " + vm.getParent().getName());
        System.out.println("Guest: " + vm.getName());
        System.out.println("GuestOS: " + vminfo.getGuestFullName());
        System.out.println("GuestState: " + vm.getGuest().guestState);
        System.out.println("Guest Ip address: " + vm.getGuest().getIpAddress());

        pingByIP(vm.getGuest().getIpAddress()); // ping VM
      }

      HostSystem newHost = (HostSystem) hosts[i];
      String vHost_IP =
          newHost.getConfig().getNetwork().getVnic()[0].getSpec().getIp().getIpAddress();
      System.out.println("vHost IP address: " + vHost_IP);

      // test for ping vHost
      pingByIP(vHost_IP); // ping vHost
    }
  } // end of main
Пример #2
0
  /**
   * 获取虚拟机的摘要信息
   *
   * @param url
   * @param username
   * @param password
   * @param vmId
   * @return
   */
  public static Map<String, Object> getSummary(
      String url, String username, String password, String vmId) {

    HashMap<String, Object> resultMap = new HashMap<String, Object>();

    try {
      VIMCache cache = VIMCache.getInstance(url, username, password);
      ManagedObjectReference mor = cache.getVirtualMachine(vmId);
      //			System.out.println("vmId--:"+vmId+"===mor-是否存在---------------------------"+mor);

      if (mor != null) {
        ExtendedAppUtil ecb = getECB(url, username, password);

        // 配置
        VirtualMachineConfigInfo config =
            (VirtualMachineConfigInfo) getDynamicProperty(ecb, mor, DYNAMICPROPERTY_CONFIG);

        // 运行
        VirtualMachineRuntimeInfo runtime =
            (VirtualMachineRuntimeInfo) getDynamicProperty(ecb, mor, DYNAMICPROPERTY_RUNTIME);

        // 摘要
        VirtualMachineSummary summary =
            (VirtualMachineSummary) getDynamicProperty(ecb, mor, DYNAMICPROPERTY_SUMMARY);

        // 存储列表
        ManagedObjectReference[] dsList =
            (ManagedObjectReference[]) getDynamicProperty(ecb, mor, DYNAMICPROPERTY_DATASTORE);

        // 网络列表
        ManagedObjectReference[] netList =
            (ManagedObjectReference[]) getDynamicProperty(ecb, mor, DYNAMICPROPERTY_NETWORK);

        // List<Object> properties = getDynamicProperties(ecb, mor,
        // new String[] { DYNAMICPROPERTY_CONFIG,
        // DYNAMICPROPERTY_SUMMARY,
        // DYNAMICPROPERTY_DATASTORE,
        // DYNAMICPROPERTY_NETWORK });
        // VirtualMachineConfigInfo config = (VirtualMachineConfigInfo)
        // properties
        // .get(0);
        // VirtualMachineSummary summary = (VirtualMachineSummary)
        // properties
        // .get(1);
        // ManagedObjectReference[] dsList = (ManagedObjectReference[])
        // properties
        // .get(2);
        // ManagedObjectReference[] netList = (ManagedObjectReference[])
        // properties
        // .get(3);

        String hoId = "";
        if ((runtime != null) && (runtime.getHost() != null)) {
          hoId = runtime.getHost().get_value();
        }
        resultMap.put(SYNC_VM_HOID, hoId);
        resultMap.put(SYNC_COMMON_VID, mor.get_value());
        resultMap.put(SYNC_COMMON_NAME, config.getName());
        int numCore = config.getHardware().getNumCoresPerSocket();
        resultMap.put(SYNC_VM_NUMCORE, numCore);
        if ((config != null) && (summary != null)) {
          // 常规
          // 客户机操作系统
          resultMap.put(SUMMARY_COMMON_GUESTFULLNAME, config.getGuestFullName());

          // 虚拟机版本,样本数据vmx-08
          resultMap.put(SUMMARY_COMMON_VERSION, config.getVersion());
          // CPU
          resultMap.put(SUMMARY_COMMON_CPU, summary.getConfig().getNumCpu());
          // 内存,单位为MB
          resultMap.put(
              SUMMARY_COMMON_MEMORYSIZEMB,
              getByteSizeStrFromMB(summary.getConfig().getMemorySizeMB()));

          // 内存开销,current overhead reservation,单位为字节
          resultMap.put(
              SUMMARY_COMMON_MEMORYOVERHEAD,
              getByteSizeStrFromBytes(summary.getRuntime().getMemoryOverhead()));
          // VMware Tools:
          // 运行状态toolsRunningStatus
          // VirtualMachineToolsRunningStatus:guestToolsExecutingScripts,guestToolsNotRunning,guestToolsRunning
          // 状态(toolsStatus,toolsVersionStatus[Since4.0],toolsVersionStatus2[Since5.0])
          // VirtualMachineToolsVersionStatus:guestToolsBlacklisted[Since5.0],guestToolsCurrent,guestToolsNeedUpgrade,guestToolsNotInstalled,
          // guestToolsSupportedNew[Since5.0],guestToolsSupportedOld[Since5.0],guestToolsTooNew[Since5.0],guestToolsTooOld[Since5.0],guestToolsUnmanaged
          // 正在运行(当前版本)
          // http://pubs.vmware.com/vsphere-50/topic/com.vmware.wssdk.apiref.doc_50/vim.vm.Summary.GuestSummary.html
          // toolsStatus
          // toolsVersionStatus[Since4.0]
          // toolsVersionStatus2[Since5.0]
          // 暂时只获取toolsVersionStatus
          resultMap.put(
              SUMMARY_COMMON_VMWARETOOLS,
              cache.getEnum(summary.getGuest().getToolsRunningStatus())
                  + "("
                  + cache.getEnum(summary.getGuest().getToolsVersionStatus())
                  + ")");
          // IP地址
          resultMap.put(
              SUMMARY_COMMON_IPADDRESS, Util.normalizeString(summary.getGuest().getIpAddress()));
          // DNS名称
          resultMap.put(
              SUMMARY_COMMON_HOSTNAME, Util.normalizeString(summary.getGuest().getHostName()));
          // XXX EVC模式,暂时无法获取
          // String SUMMARY_COMMON_EVCMODEL = "evcmodel";
          // 状况,poweredOff,poweredOn,suspended
          // powerstate在alarmDesc中有翻译
          //					resultMap.put(SUMMARY_COMMON_POWERSTATE, cache
          //							.getAlarmDesc(summary.getRuntime().getPowerState()
          //									.getValue()));

          String powerState = "";
          if (runtime != null) {
            powerState = runtime.getPowerState().toString();
          }
          resultMap.put(SYNC_VM_POWERSTATE, powerState);
          // 主机
          resultMap.put(SUMMARY_COMMON_HOST, getEntityName(ecb, summary.getRuntime().getHost()));
          // XXX 活动任务,暂时无法获取
          // String SUMMARY_COMMON_ACTIVETASK = "activetask";
          // XXX vSphere HA保护,暂时无法获取
          // String SUMMARY_COMMON_VSPHEREHA = "vsphereha";

          // 资源
          // 已消耗的主机CPU,Basic CPU performance
          // statistics,单位MHz,大概是这个值此值变化比较快
          resultMap.put(
              SUMMARY_RESOURCE_OVERALLCPUUSAGE, summary.getQuickStats().getOverallCpuUsage() + MHZ);
          // 已消耗的主机内存:consumed host memory,单位为MB
          resultMap.put(
              SUMMARY_RESOURCE_HOSTMEMORYUSAGE,
              getByteSizeStrFromMB(summary.getQuickStats().getHostMemoryUsage()));
          // 活动客户机内存:active guest memory,单位为MB
          resultMap.put(
              SUMMARY_RESOURCE_GUESTMEMORYUSAGE,
              getByteSizeStrFromMB(summary.getQuickStats().getGuestMemoryUsage()));
          // 未共享的存储
          resultMap.put(
              SUMMARY_RESOURCE_STORAGE_UNSHARED,
              getByteSizeStrFromBytes(summary.getStorage().getUnshared()));
          // 已使用的存储
          resultMap.put(
              SUMMARY_RESOURCE_STORAGE_COMMITTED,
              getByteSizeStrFromBytes(summary.getStorage().getCommitted()));
          // 置备的存储
          resultMap.put(
              SUMMARY_RESOURCE_STORAGE_ALL,
              getByteSizeStrFromBytes(
                  summary.getStorage().getCommitted() + summary.getStorage().getUncommitted()));

          // 存储列表
          DatastoreMgr.recordResultMapDsList(resultMap, ecb, dsList);

          // 网络列表
          NetworkMgr.recordResultMapNetList(resultMap, ecb, netList);

          // 记录处理正确的标识
          recordResultMapSuccess(resultMap);
        } else {
          recordResultMapError(
              resultMap,
              "获取'" + DYNAMICPROPERTY_CONFIG + "'或'" + DYNAMICPROPERTY_SUMMARY + "'信息为空");
        }
      } else {
        recordResultMapError(resultMap, "虚拟机'" + vmId + "'不存在");
      }
    } catch (Exception e) {
      LOGGER.error("getSummary error, ", e);
      recordResultMapException(resultMap, e);
    }

    return resultMap;
  }