package com.vmware.util;
import com.vmware.vim25.*;
import com.vmware.vim25.mo.InventoryNavigator;
import com.vmware.vim25.mo.ManagedEntity;
import com.vmware.vim25.mo.PerformanceManager;
import com.vmware.vim25.mo.ServiceInstance;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* VirtualMachine CPU利用率和Memory利用率
* Created by vixuan-008 on 2015/4/20.
*/
public class VM {
/**
* Created by vixuan-008 on 2015/4/20.
*/
private ServiceInstance instance = null;
private InventoryNavigator inventoryNavigator = null;
public List getVMCPUPerByTimeRange(String vcomName, ServiceInstance serviceInstance,int timeRange,String instanceName) throws Exception {
List result = new ArrayList();
instance = serviceInstance;
inventoryNavigator = new InventoryNavigator(instance.getRootFolder());
ManagedEntity obj = inventoryNavigator.searchManagedEntity("VirtualMachine", vcomName);
if (null == obj)
throw new Exception();
com.vmware.vim25.mo.VirtualMachine vm = (com.vmware.vim25.mo.VirtualMachine) obj;
PerformanceManager pfMgr = instance.getPerformanceManager();
PerfProviderSummary summary = pfMgr.queryPerfProviderSummary(vm);
PerfInterval[] rfRates = pfMgr.getHistoricalInterval();
PerfMetricId[] pfMtIds = pfMgr.queryAvailablePerfMetric(vm, null, null, timeRange);
List
package com.vmware.client;
import com.vmware.util.Session;
import com.vmware.util.VM;
import com.vmware.vim25.*;
import com.vmware.vim25.mo.*;
import java.util.List;
import java.util.Map;
/**
* 测试代码
* Created by vixuan-008 on 2015/4/20.
*/
public class VirtualMachineCPUpercent {
public static void main(String[] args){
try{
VM vm = new VM();
ServiceInstance serviceInstance= Session.getInstance("172.16.1.20", "root", "vmware");
//获取 PerformanceManger
PerformanceManager performanceManager=serviceInstance.getPerformanceManager();
List cpuPerList = null;
cpuPerList = vm.getVMCPUPerByTimeRange("Mssql", serviceInstance, 20, "");
List memPerList = vm.getVMMemPerByTimeRange("Mssql", serviceInstance, 20,"");
for (int s = 0; s < cpuPerList.size(); s++) {
Map m = (Map) cpuPerList.get(s);
String point = (String) m.get("point");
PerfMetricSeriesCSV[] csvs = (PerfMetricSeriesCSV[]) m.get("csvs");
if (point.equals("usage")) {
PerfMetricSeriesCSV _value = csvs[0];
Float f = Float.valueOf(_value.getValue())/100f;
System.out.println("usage:"+f+"\n");
}
else if (point.equals("usagemhz")) {
PerfMetricSeriesCSV _value = csvs[0];
System.out.println("usagemhz:"+_value+"\n");
}
}
if( null != memPerList&&memPerList.size() > 0)
{
for (int s = 0; s < memPerList.size(); s++) {
Map m = (Map) memPerList.get(s);
String point = (String) m.get("point");
PerfMetricSeriesCSV[] csvs = (PerfMetricSeriesCSV[]) m.get("csvs");
if (point.equals("usage")) {
PerfMetricSeriesCSV _value = csvs[0];
Float f = Float.valueOf(_value.getValue())/100f;
System.out.println("usage"+f+"\n");
}
else if (point.equals("consumed")) {
PerfMetricSeriesCSV _value = csvs[0];
System.out.println("consumed:"+_value+"\n");
}
}
}
}catch (Exception e){
e.printStackTrace();
}
}
}
相关展示: