Retrieving Statistics

The following code fragment calls the
QueryPerf
method to retrieve statistics. It performs these tasks:
  1. Create a list of qualified performance counter names for retrieval. The name is a path consisting of
    group-name.counter-name.ROLLUP-TYPE
    , for example
    mem.granted.AVERAGE
    . The rollup type must be coded in uppercase letters to match the character case of the rollup type in the performance counter metadata (
    PerfCounterInfo.rollupType
    ). See the
    vSphere API Reference
    for tables of available counters. The
    vSphere API Reference
    page for the
    PerformanceManager
    managed object contains links to the tables.
  2. Create a list of
    PerfMetricId
    objects, one for each counter to be retrieved. The metric ID is a combination of the counter ID and the instance. To fill in the
    PerfMetricId
    properties, the example does the following:
    • Use the
      countersIdMap
      to translate a full counter name into a counter ID.
    • Specify an asterisk (*) for the
      PerfMetricId.instance
      property. The asterisk is the system-defined instance specification for combined instance and rollup retrieval.
  3. Build a query specification for the method call. This query specifies the following:
    • Virtual machine for which performance data is being retrieved (
      entityMor
      );
    • Interval ID of 300 to collect 5-minute rollup data.
    • Comma-separated value (CSV) format for the retrieved data.
  4. Call the
    QueryPerf
    method.
/* * Use <group>.<name>.<ROLLUP-TYPE> path specification to identify counters. */ String[] counterNames = new String[] {"disk.provisioned.LATEST", "mem.granted.AVERAGE", "power.power.AVERAGE"}; /* * Create the list of PerfMetricIds, one for each counter. */ List<PerfMetricId> perfMetricIds = new ArrayList<PerfMetricId>(); for(int i = 0; i < counterNames.length; i++) { /* * Create the PerfMetricId object for the counterName. * Use an asterisk to select all metrics associated with counterId (instances and rollup). */ PerfMetricId metricId = new PerfMetricId(); /* Get the ID for this counter. */ metricId.setCounterId(countersIdMap.get(counterNames[i])); metricId.setInstance("*"); perfMetricIds.add(metricId); } /* * Create the query specification for queryPerf(). * Specify 5 minute rollup interval and CSV output format. */ int intervalId = 300; PerfQuerySpec querySpecification = new PerfQuerySpec(); querySpecification.setEntity( querySpecification.setIntervalId(intervalId); querySpecification.setFormat("csv"); querySpecification.getMetricId().addAll(perfMetricIds); List<PerfQuerySpec> pqsList = new ArrayList<PerfQuerySpec>(); pqsList.add(querySpecification); /* * Call queryPerf() * * QueryPerf() returns the statistics specified by the provided * PerfQuerySpec objects. When specified statistics are unavailable - * for example, when the counter doesn't exist on the target * ManagedEntity - QueryPerf() returns null for that counter. */ List<PerfEntityMetricBase> retrievedStats = apiMethods.queryPerf(performanceMgrRef, pqsList);