如何使用vSphere和Java查找esx主机的iops?

时间:2014-04-08 09:56:56

标签: java vsphere

我想找出esx主机的iops。 我可以使用以下代码连接到esx主机。

ServiceInstance si = new ServiceInstance(new URL(url), user, pass,
                true);
ManagedEntity[] managedEntities = new InventoryNavigator(
    si.getRootFolder()).searchManagedEntities("VirtualMachine");
ManagedEntity[] hostmanagedEntities = new InventoryNavigator(
    si.getRootFolder()).searchManagedEntities("HostSystem");

通过使用上面的代码,我可以连接到主机并列出在其上运行的所有vms。

如何查找连接主机的iops?

1 个答案:

答案 0 :(得分:0)

我做了什么来找出计数器及其矩阵如下: 假设我使用上面给出的代码(在此代码的开头)连接到esx主机。

     //set params to find out counter
 groupinfo = "datastore";
         nameinfo="datastoreIops";
         rollup = "average";
      //set interval    
Calendar startTime = (Calendar) curTime.clone(); 
startTime.add(Calendar.MINUTE, -5); 
PerformanceManager perfMgr = si.getPerformanceManager();
PerfProviderSummary summary = perfMgr.queryPerfProviderSummary(myhost);
int perfInterval = summary.getRefreshRate();
//get performance counters
PerfCounterInfo[] perfCounters = perfMgr.getPerfCounter();
int counter_id = 0;
//find id of right counter by iterating
for (int i = 0; i < perfCounters.length; i++) {
PerfCounterInfo perfCounterInfo = perfCounters[i];
String perfCounterString = perfCounterInfo.getNameInfo().getLabel() + " (" + perfCounterInfo.getGroupInfo().getKey() + ") in "
        + perfCounterInfo.getUnitInfo().getLabel() + " (" + perfCounterInfo.getStatsType().toString() + ")";
if (perfCounterInfo.getGroupInfo().getKey().equalsIgnoreCase(groupinfo) 
&& perfCounterInfo.getNameInfo().getKey().equalsIgnoreCase(nameinfo) 
&&  perfCounterInfo.getRollupType().name().equalsIgnoreCase(rollup) )
{
counter_id =    perfCounterInfo.getKey();
break;  
}
}
int i3;
PerfMetricId[] queryAvailablePerfMetric = perfMgr.queryAvailablePerfMetric(myhost, null, null, perfInterval);
ArrayList<PerfMetricId> list = new ArrayList<PerfMetricId>();
for (int i2 = 0; i2 < queryAvailablePerfMetric.length; i2++) {
PerfMetricId perfMetricId = queryAvailablePerfMetric[i2];
if (counter_id == perfMetricId.getCounterId()) {
    list.add(perfMetricId);
}
}
PerfMetricId[] pmis = list.toArray(new PerfMetricId[list.size()]);
PerfQuerySpec qSpec = new PerfQuerySpec();
qSpec.setEntity(myhost.getMOR());
qSpec.setMetricId(pmis);
qSpec.setStartTime(startTime);
qSpec.setEndTime(curTime);
qSpec.intervalId = perfInterval;

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

PerfEntityMetricBase val = pembs[0];
PerfEntityMetric pem = (PerfEntityMetric) val;
PerfMetricSeries[] vals = pem.getValue();

JSONObject perfout = new JSONObject();

for (int instanceid =0; vals != null && instanceid <vals.length;instanceid++ ) {

PerfMetricIntSeries val1 = (PerfMetricIntSeries) vals[instanceid];
String InstanceName = val1.getId().instance;
if (InstanceName.equals("")){
    InstanceName="Total_"+instanceid;
}

long[] longs = val1.getValue();
long last_longs_value = longs[longs.length-1];
try {
perfout.put(InstanceName.replace(".", "-"), last_longs_value);
} catch (JSONException e) {
e.printStackTrace();
}
}
System.out.println(perfout);

可能对你有帮助..