Ejemplo n.º 1
0
  /**
   * Queries a host system for Cim data.
   *
   * @param hostSystem the host system to query
   * @param cimClass the class of Cim objects to retrieve
   * @param primaryIpAddress the Ip address to use
   * @return the list of Cim objects
   * @throws RemoteException
   * @throws CIMException
   */
  public List<CIMObject> queryCimObjects(
      HostSystem hostSystem, String cimClass, String primaryIpAddress)
      throws ConnectException, RemoteException, CIMException {
    List<CIMObject> cimObjects = new ArrayList<CIMObject>();

    if (!m_hostServiceTickets.containsKey(hostSystem)) {
      m_hostServiceTickets.put(hostSystem, hostSystem.acquireCimServicesTicket());
    }

    HostServiceTicket hostServiceTicket = m_hostServiceTickets.get(hostSystem);

    if (!m_hostSystemCimUrls.containsKey(hostSystem)) {
      String ipAddress = primaryIpAddress;

      if (ipAddress == null) {
        ipAddress = getPrimaryHostSystemIpAddress(hostSystem);
      }

      if (ipAddress == null) {
        logger.warn(
            "Cannot determine ip address for host system '{}'", hostSystem.getMOR().getVal());
        return cimObjects;
      }

      m_hostSystemCimUrls.put(hostSystem, "https://" + ipAddress + ":5989");
    }

    String cimAgentAddress = m_hostSystemCimUrls.get(hostSystem);

    String namespace = "root/cimv2";

    UserPrincipal userPr = new UserPrincipal(hostServiceTicket.getSessionId());
    PasswordCredential pwCred =
        new PasswordCredential(hostServiceTicket.getSessionId().toCharArray());

    CIMNameSpace ns = new CIMNameSpace(cimAgentAddress, namespace);
    CIMClient cimClient = new CIMClient(ns, userPr, pwCred);

    // very important to query esx5 hosts
    cimClient.useMPost(false);

    CIMObjectPath rpCOP = new CIMObjectPath(cimClass);

    Enumeration<?> rpEnm = cimClient.enumerateInstances(rpCOP);

    while (rpEnm.hasMoreElements()) {
      CIMObject rp = (CIMObject) rpEnm.nextElement();

      cimObjects.add(rp);
    }

    return cimObjects;
  }