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;
  }
Ejemplo n.º 2
0
  @Override
  public void run() {

    try {

      while (true) {

        URL url = new URL(SJSULabConfig.getvCenterURL());
        try {

          ServiceInstance si =
              new ServiceInstance(
                  url, SJSULabConfig.getvCenterUsername(), SJSULabConfig.getPassword(), true);
          HostSystem host =
              (HostSystem)
                  new InventoryNavigator(si.getRootFolder())
                      .searchManagedEntity("HostSystem", ip); //
          System.out.println("host value" + host);
          PerformanceManager perfMgr = si.getPerformanceManager();

          PerfProviderSummary summary = perfMgr.queryPerfProviderSummary(host); // vm

          int perfInterval = summary.getRefreshRate();

          PerfMetricId[] queryAvailablePerfMetric =
              perfMgr.queryAvailablePerfMetric(host, null, null, perfInterval);
          PerfCounterInfo[] pci = perfMgr.getPerfCounter();
          ArrayList<PerfMetricId> list = new ArrayList<PerfMetricId>();
          for (int i2 = 0; i2 < queryAvailablePerfMetric.length; i2++) {
            PerfMetricId perfMetricId = queryAvailablePerfMetric[i2];
            if (SELECTED_COUNTER_ID == perfMetricId.getCounterId()) {
              list.add(perfMetricId);
            }
          }
          PerfMetricId[] pmis = list.toArray(new PerfMetricId[list.size()]);
          PerfQuerySpec qSpec = new PerfQuerySpec();
          qSpec.setEntity(host.getMOR());
          qSpec.setMetricId(pmis);

          qSpec.intervalId = perfInterval;
          PerfEntityMetricBase[] pembs = perfMgr.queryPerf(new PerfQuerySpec[] {qSpec});

          for (int i = 0; pembs != null && i < pembs.length; i++) {

            PerfEntityMetricBase val = pembs[i];
            PerfEntityMetric pem = (PerfEntityMetric) val;
            PerfMetricSeries[] vals = pem.getValue();
            PerfSampleInfo[] infos = pem.getSampleInfo();

            Date date1 = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy h:mm:ss");
            String formattedDate = sdf.format(date1);

            infoList.put(aName[counter], formattedDate);

            for (int j = 0; vals != null && j < vals.length; ++j) {
              PerfMetricIntSeries val1 = (PerfMetricIntSeries) vals[j];
              long[] longs = val1.getValue();
              System.out.println("Host CPU :" + longs[5]);
              infoList.put(aName[0], String.valueOf(longs[5]));
              /*
              for (int k : a) {

              	infoList.put(aName[counter+1],
              			String.valueOf(longs[k]));
              	counter++;
              }
              counter = 0;
              */
            }
          }
          si.getServerConnection().logout();
        } catch (InvalidProperty e) {
          e.printStackTrace();
        } catch (RuntimeFault e) {
          e.printStackTrace();
        } catch (RemoteException e) {
          e.printStackTrace();
        } catch (MalformedURLException e) {
          e.printStackTrace();
        }

        //	infoList.put("vmIP", ip);

        counter = 0;

        try {

          StringBuilder sb = new StringBuilder();
          sb.append("vmname:" + ip + ",");
          int countTest = 0;
          for (String str : aName) {
            sb.append(aName[countTest] + ":" + infoList.get(str).toString() + ",");
            countTest++;
          }
          String out = sb.toString();
          out = out.substring(0, out.length() - 1);
          writer.write(out + "\n");
          System.out.println(out);
        } catch (Exception e) {
          e.printStackTrace();
        }

        Thread.sleep(5000);
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }