Retrieving
Statistics
The following code fragment calls the
QueryPerf
method to retrieve
statistics. It performs these tasks:
- Create a list of qualified performance counter names for retrieval. The name is a path consisting ofgroup-name.counter-name.ROLLUP-TYPE, for examplemem.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 thevSphere API Referencefor tables of available counters. ThevSphere API Referencepage for thePerformanceManagermanaged object contains links to the tables.
- Create a list ofPerfMetricIdobjects, one for each counter to be retrieved. The metric ID is a combination of the counter ID and the instance. To fill in thePerfMetricIdproperties, the example does the following:
- Use thecountersIdMapto translate a full counter name into a counter ID.
- Specify an asterisk (*) for thePerfMetricId.instanceproperty. The asterisk is the system-defined instance specification for combined instance and rollup retrieval.
- 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.
- Call theQueryPerfmethod.
/* * 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);