Exemplo n.º 1
0
  /**
   * @param midList
   * @param compMetric
   * @return
   * @throws RuntimeException
   * @throws RemoteException
   * @throws InvalidPropertyFaultMsg
   * @throws RuntimeFaultFaultMsg
   */
  private static XMLGregorianCalendar displayStats(
      List<PerfMetricId> midList, PerfCompositeMetric compMetric)
      throws RuntimeException, RemoteException, InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
    if (compMetric == null || (compMetric.getEntity() == null)) {
      return null;
    }

    List<Object[]> data = new ArrayList<Object[]>();
    PerfEntityMetric entityMetric = (PerfEntityMetric) compMetric.getEntity();
    PerfMetricIntSeries intSeries = (PerfMetricIntSeries) entityMetric.getValue().get(0);
    int numSamples = entityMetric.getSampleInfo().size();

    XMLGregorianCalendar timeStamp =
        entityMetric.getSampleInfo().get(numSamples - 1).getTimestamp();
    long overallUsage = intSeries.getValue().get(numSamples - 1);
    System.out.println("Info Updated");
    int numColumns = midList.size() + 1;
    List<PerfEntityMetricBase> listpemb = compMetric.getChildEntity();
    List<PerfEntityMetricBase> childEntityMetric = listpemb;
    for (int childNum = 0; childNum < childEntityMetric.size(); childNum++) {
      PerfEntityMetric childStats = (PerfEntityMetric) childEntityMetric.get(childNum);
      String childName = getEntityName(childStats.getEntity());
      int numChildSamples = childStats.getSampleInfo().size();
      Object[] tableData = new Object[numColumns];
      tableData[0] = childName;

      for (int i = 0; i < childStats.getValue().size(); i++) {
        PerfMetricIntSeries childSeries = (PerfMetricIntSeries) childStats.getValue().get(i);
        int col = findStatsIndex(midList, childSeries.getId());
        if (col >= 0) {
          long statsVal = childSeries.getValue().get(numChildSamples - 1);
          tableData[col + 1] = new Long(statsVal);
        }
      }
      data.add(tableData);
    }

    if (statsTable != null) {
      statsTable.setData(timeStamp.toGregorianCalendar(), overallUsage, "Mhz", data);
    }
    return timeStamp;
  }
Exemplo n.º 2
0
  private String[] getStats(Object[] info) {
    ManagedEntity managedEntity = (ManagedEntity) info[0];
    String cluster = (String) info[1];
    if (cluster.trim().equals("")) cluster = "none";

    final ArrayList<String> temp_results = new ArrayList<String>();
    final String TAG_NS =
        appConfig.get("graphiteTag") + "." + appConfig.get("vcsTag") + "." + cluster;

    PerfProviderSummary pps;
    try {
      String meName = managedEntity.getName();
      // TODO: get rid of other stuff here that won't translate well into graphite
      // TODO: This won't handle every bizarre thing that people want to do with VM naming.
      // get rid of spaces, since we never want them.
      meName = meName.replace(" ", "_");
      String meNameTag = "";
      String[] meNameParts = meName.split("[.]");
      Boolean use_fqdn = false;
      Boolean is_ip = false;
      Boolean all_periods = false;
      Boolean all_absolute = false;
      Boolean all_delta = false;

      // parse it if we're supposed to use the fully qualified name
      if (appConfig.get("USE_FQDN").equals("true")) {
        use_fqdn = true;
      }
      if (appConfig.get("SEND_ALL_PERIODS").equals("true")) {
        all_periods = true;
      }
      if (appConfig.get("SEND_ALL_ABSOLUTE").equals("true")) {
        all_absolute = true;
      }
      if (appConfig.get("SEND_ALL_DELTA").equals("true")) {
        all_delta = true;
      }

      // detect if the object name is an IP address, so that using short name
      // doesn't shorten it to the first octet.
      try {
        int a = Integer.parseInt(meNameParts[0]);
        int b = Integer.parseInt(meNameParts[1]);
        is_ip = true;
      } catch (NumberFormatException e) {
        is_ip = false;
      }

      if (use_fqdn) {
        meNameTag = meName;
      } else {
        if (is_ip) {
          // make sure the whole IP is displayed
          meNameTag = meName;
        } else {
          meNameTag = meNameParts[0];
        }
      }
      // replace all the . with _
      meNameTag = meNameTag.replace(".", "_");

      pps = this.perfMgr.queryPerfProviderSummary(managedEntity);
      // for VM's, this is likely always 20 seconds in this context.
      int refreshRate = pps.getRefreshRate().intValue();
      PerfMetricId[] pmis =
          this.perfMgr.queryAvailablePerfMetric(managedEntity, null, null, refreshRate);
      int perfEntries = Integer.parseInt(appConfig.get("PERIODS"));
      PerfQuerySpec qSpec = createPerfQuerySpec(managedEntity, pmis, perfEntries, refreshRate);
      // pValues always returns perfEntries results. this code hasn't been tested grabbing more than
      // 1
      // stat at a time.
      PerfEntityMetricBase[] pValues = perfMgr.queryPerf(new PerfQuerySpec[] {qSpec});

      if (pValues != null) {
        for (PerfEntityMetricBase pValue : pValues) {
          PerfEntityMetric pem = (PerfEntityMetric) pValue;
          PerfMetricSeries[] vals = pem.getValue();
          PerfSampleInfo[] infos = pem.getSampleInfo();

          for (int x = 0; vals != null && x < vals.length; x++) {
            int counterId = vals[x].getId().getCounterId();
            // create strings for the parts of the tag.
            Hashtable<String, String> perfKey = perfKeys.get("" + counterId);
            if (perfKey == null) continue;
            String key = perfKey.get("key");

            String instance = vals[x].getId().getInstance();
            // disks will be naa.12341234, change them to naa_12341234 instead
            instance = instance.replace(".", "_");
            // some other items are some/random/path, change them to dots
            instance = instance.replace("/", ".");
            // the 'none' rollup type is completely legitimate - it doesn't roll up, so it's live
            // data
            String rollup = perfKeys.get("" + counterId).get("rollup");
            String statstype = perfKeys.get("" + counterId).get("statstype");

            String graphiteTag;
            if (instance.equals("")) {
              // no instance, no period required
              graphiteTag = TAG_NS + "." + mobType + "." + meNameTag + "." + key + "." + rollup;
            } else {
              graphiteTag =
                  TAG_NS + "." + mobType + "." + meNameTag + "." + key + "." + instance + "."
                      + rollup;
            }
            // tag should be vmstats.VMTAG.hostname.cpu.whatever.whatever at this point

            long stat = 0;
            if (vals[x] instanceof PerfMetricIntSeries) {
              PerfMetricIntSeries val = (PerfMetricIntSeries) vals[x];
              long[] longs = val.getValue();
              long running_total = 0;
              for (int c = 0; c < longs.length; c++) {
                stat = longs[c];
                running_total += stat;
                // always create an average, even though it'll be wrong until the 3rd iteration
                long stat_avg = running_total / longs.length;
                // get the timestamp for the data
                long timestamp = infos[c].getTimestamp().getTimeInMillis() / 1000;
                String graphiteData = graphiteTag + " " + stat + " " + timestamp + "\n";
                if (all_periods) {
                  // send all the periods
                  temp_results.add(graphiteData);
                } else if (all_absolute && statstype.equals("absolute")) {
                  // send if we're sending all the absolute and is an absolute type
                  temp_results.add(graphiteData);
                } else if (all_delta && statstype.equals("delta")) {
                  // send if we're sending all delta and is a delta type
                  temp_results.add(graphiteData);
                } else {
                  // send only the last data
                  if (c == (longs.length - 1)) {
                    if (statstype.equals("absolute")) {
                      // if it's an absolute, average the data and send it
                      // TODO: turn this into a flag?
                      graphiteData = graphiteTag + " " + stat_avg + " " + timestamp + "\n";
                      temp_results.add(graphiteData);
                    } else {
                      // otherwise, don't average it
                      graphiteData = graphiteTag + " " + stat + " " + timestamp + "\n";
                      temp_results.add(graphiteData);
                    }
                  }
                }
              }
            }
          }
        }
      }

    } catch (RuntimeFault e) {
      logger.info(
          "statsGrabber: Thread: "
              + Thread.currentThread().getName()
              + " +  Interrupted: "
              + e.getMessage());
      e.printStackTrace();
      System.exit(100);
    } catch (RemoteException e) {
      logger.info(
          "statsGrabber: Thread: "
              + Thread.currentThread().getName()
              + " +  Interrupted: "
              + e.getMessage());
      e.printStackTrace();
      System.exit(101);
    }
    return temp_results.toArray(new String[temp_results.size()]);
  }
Exemplo n.º 3
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();
    }
  }