VMware Cloud Community
jcp0wermac
Enthusiast
Enthusiast

vCO Performance Query Example

Hello, I expanded on the example from http://mighty-virtualization.blogspot.ca/2010/11/vco-get-performance-data-from-vm-build.html.  Here is the JavaScript to retrieve the performance metrics from a resourcepool for a day, week and month.  Hopefully this will help someone out.

Joe

var querySpec = new Array();
querySpec.push(new VcPerfQuerySpec());
querySpec.push(new VcPerfQuerySpec());
querySpec.push(new VcPerfQuerySpec());
querySpec[0].entity = resourcepool.reference;
querySpec[1].entity = resourcepool.reference;
querySpec[2].entity = resourcepool.reference;
/* _vSphere_SDK_URL_
* The intervalId must equal to last arg of queryAvailablePerfMetric
*/
querySpec[0].intervalId = 300;
querySpec[1].intervalId = 1800;
querySpec[2].intervalId = 7200;
/* To find metricId - perfCounter PerfCounterInfo[]
* https://<vcenter_ip_address>/mob/?moid=PerfMgr
*/
querySpec[0].metricId = resourcepool.sdkConnection.perfManager.queryAvailablePerfMetric(resourcepool.reference,null,null,300);
querySpec[1].metricId = resourcepool.sdkConnection.perfManager.queryAvailablePerfMetric(resourcepool.reference,null,null,1800);
querySpec[2].metricId = resourcepool.sdkConnection.perfManager.queryAvailablePerfMetric(resourcepool.reference,null,null,7200);
querySpec[0].format = "normal";
querySpec[1].format = "normal";
querySpec[2].format = "normal";
/* Return Type: VcPerfEntityMetricBase[] */
var results = resourcepool.sdkConnection.perfManager.queryPerf(querySpec);
/* Create hash table */
var hashMaximums = new Object();
/* Keep the metricIds */
var metricIdArray = new Array();
for(var j in results) {
     var result = results[j]; // VcPerfEntityMetric
     var perfMetricSeries = result.value; //VcPerfMetricSeries[]
     for(var k in perfMetricSeries) { //VcPerfMetricIntSeries
          var average = AverageArray(perfMetricSeries[k].value); //Number[] (Int64)
          /* If j == 0 then we need to init values to the hashtable based on the counterId key */
          if( j == 0 ) {
               hashMaximums[perfMetricSeries[k].id.counterId] = average;
               metricIdArray.push(perfMetricSeries[k].id.counterId);
          } /* if the existing value is less the current, replace */
          else if( hashMaximums[perfMetricSeries[k].id.counterId] < average ) {
               hashMaximums[perfMetricSeries[k].id.counterId] = average;
          }
     }
}
for( var i in metricIdArray ) {
     var nameInfoLabel = resourcepool.sdkConnection.perfManager.perfCounter[metricIdArray[i]].nameInfo.label;
     var unitInfoLabel = resourcepool.sdkConnection.perfManager.perfCounter[metricIdArray[i]].unitInfo.label;
     System.log(nameInfoLabel + " = " + hashMaximums[metricIdArray[i]] + " " + unitInfoLabel);
}
/* Source Code Source
*/
function AverageArray(t_array) {
     var av = 0;
     var cnt = 0;
     var len = t_array.length;
     for (var i = 0; i < len; i++) {
          var e = +t_array[i];
          if(!e && t_array[i] !== 0 && t_array[i] !== '0') e--;
          if (t_array[i] == e) {av += e; cnt++;}
     }
     return av/cnt;
}

Reply
0 Kudos
1 Reply
tschoergez
Leadership
Leadership

Great! Thanks for sharing!

Can you explain exactly what versions you use (of vCenter, vCO and the vCenter Plugin)?

(I remember that there were some issues in certain combinations...)

Thanks!

Joerg

Reply
0 Kudos