Esempio n. 1
0
  /**
   * This method queries the Vmware vCenter server for sensor data.
   *
   * @param svc the monitored service
   * @param parameters the parameter map
   * @return the poll status for this system
   */
  @Override
  public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) {
    OnmsNode onmsNode = m_nodeDao.get(svc.getNodeId());

    // retrieve the assets and
    String vmwareManagementServer = onmsNode.getAssetRecord().getVmwareManagementServer();
    String vmwareManagedEntityType = onmsNode.getAssetRecord().getVmwareManagedEntityType();
    String vmwareManagedObjectId = onmsNode.getForeignId();

    TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT);

    PollStatus serviceStatus = PollStatus.unknown();

    for (tracker.reset();
        tracker.shouldRetry() && !serviceStatus.isAvailable();
        tracker.nextAttempt()) {

      VmwareViJavaAccess vmwareViJavaAccess = null;

      try {
        vmwareViJavaAccess = new VmwareViJavaAccess(vmwareManagementServer);
      } catch (MarshalException e) {
        logger.warn(
            "Error initialising VMware connection to '{}': '{}'",
            vmwareManagementServer,
            e.getMessage());
        return PollStatus.unavailable(
            "Error initialising VMware connection to '" + vmwareManagementServer + "'");
      } catch (ValidationException e) {
        logger.warn(
            "Error initialising VMware connection to '{}': '{}'",
            vmwareManagementServer,
            e.getMessage());
        return PollStatus.unavailable(
            "Error initialising VMware connection to '" + vmwareManagementServer + "'");
      } catch (IOException e) {
        logger.warn(
            "Error initialising VMware connection to '{}': '{}'",
            vmwareManagementServer,
            e.getMessage());
        return PollStatus.unavailable(
            "Error initialising VMware connection to '" + vmwareManagementServer + "'");
      }

      try {
        vmwareViJavaAccess.connect();
      } catch (MalformedURLException e) {
        logger.warn(
            "Error connecting VMware management server '{}': '{}'",
            vmwareManagementServer,
            e.getMessage());
        return PollStatus.unavailable(
            "Error connecting VMware management server '" + vmwareManagementServer + "'");
      } catch (RemoteException e) {
        logger.warn(
            "Error connecting VMware management server '{}': '{}'",
            vmwareManagementServer,
            e.getMessage());
        return PollStatus.unavailable(
            "Error connecting VMware management server '" + vmwareManagementServer + "'");
      }

      if (!vmwareViJavaAccess.setTimeout(tracker.getConnectionTimeout())) {
        logger.warn(
            "Error setting connection timeout for VMware management server '{}'",
            vmwareManagementServer);
      }

      String powerState = "unknown";

      if ("HostSystem".equals(vmwareManagedEntityType)) {
        HostSystem hostSystem =
            vmwareViJavaAccess.getHostSystemByManagedObjectId(vmwareManagedObjectId);
        if (hostSystem == null) {
          return PollStatus.unknown("hostSystem=null");
        } else {
          HostRuntimeInfo hostRuntimeInfo = hostSystem.getRuntime();
          if (hostRuntimeInfo == null) {
            return PollStatus.unknown("hostRuntimeInfo=null");
          } else {
            HostSystemPowerState hostSystemPowerState = hostRuntimeInfo.getPowerState();
            if (hostSystemPowerState == null) {
              return PollStatus.unknown("hostSystemPowerState=null");
            } else {
              powerState = hostSystemPowerState.toString();
            }
          }
        }
      } else {
        if ("VirtualMachine".equals(vmwareManagedEntityType)) {
          VirtualMachine virtualMachine =
              vmwareViJavaAccess.getVirtualMachineByManagedObjectId(vmwareManagedObjectId);
          if (virtualMachine == null) {
            return PollStatus.unknown("virtualMachine=null");
          } else {
            VirtualMachineRuntimeInfo virtualMachineRuntimeInfo = virtualMachine.getRuntime();
            if (virtualMachineRuntimeInfo == null) {
              return PollStatus.unknown("virtualMachineRuntimeInfo=null");
            } else {
              VirtualMachinePowerState virtualMachinePowerState =
                  virtualMachineRuntimeInfo.getPowerState();
              if (virtualMachinePowerState == null) {
                return PollStatus.unknown("virtualMachinePowerState=null");
              } else {
                powerState = virtualMachinePowerState.toString();
              }
            }
          }
        } else {
          logger.warn(
              "Error getting '{}' for '{}'", vmwareManagedEntityType, vmwareManagedObjectId);

          vmwareViJavaAccess.disconnect();

          return serviceStatus;
        }
      }

      if ("poweredOn".equals(powerState)) {
        serviceStatus = PollStatus.available();
      } else {
        serviceStatus = PollStatus.unavailable("The system's state is '" + powerState + "'");
      }

      vmwareViJavaAccess.disconnect();
    }

    return serviceStatus;
  }
Esempio n. 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;
  }