use of com.vmware.vim25.PerfEntityMetric in project cloudstack by apache.
the class VmwareResource method getVmStats.
private HashMap<String, VmStatsEntry> getVmStats(List<String> vmNames) throws Exception {
VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
HashMap<String, VmStatsEntry> vmResponseMap = new HashMap<String, VmStatsEntry>();
ManagedObjectReference perfMgr = getServiceContext().getServiceContent().getPerfManager();
VimPortType service = getServiceContext().getService();
PerfCounterInfo rxPerfCounterInfo = null;
PerfCounterInfo txPerfCounterInfo = null;
PerfCounterInfo diskReadIOPerfCounterInfo = null;
PerfCounterInfo diskWriteIOPerfCounterInfo = null;
PerfCounterInfo diskReadKbsPerfCounterInfo = null;
PerfCounterInfo diskWriteKbsPerfCounterInfo = null;
Integer windowInterval = getVmwareWindowTimeInterval();
final XMLGregorianCalendar startTime = VmwareHelper.getXMLGregorianCalendar(new Date(), windowInterval);
final XMLGregorianCalendar endTime = VmwareHelper.getXMLGregorianCalendar(new Date(), 0);
List<PerfCounterInfo> cInfo = getServiceContext().getVimClient().getDynamicProperty(perfMgr, "perfCounter");
for (PerfCounterInfo info : cInfo) {
if ("net".equalsIgnoreCase(info.getGroupInfo().getKey()) && "average".equalsIgnoreCase(info.getRollupType().value())) {
if ("transmitted".equalsIgnoreCase(info.getNameInfo().getKey())) {
txPerfCounterInfo = info;
}
if ("received".equalsIgnoreCase(info.getNameInfo().getKey())) {
rxPerfCounterInfo = info;
}
}
if ("virtualdisk".equalsIgnoreCase(info.getGroupInfo().getKey())) {
if ("numberReadAveraged".equalsIgnoreCase(info.getNameInfo().getKey())) {
diskReadIOPerfCounterInfo = info;
}
if ("numberWriteAveraged".equalsIgnoreCase(info.getNameInfo().getKey())) {
diskWriteIOPerfCounterInfo = info;
}
if ("read".equalsIgnoreCase(info.getNameInfo().getKey())) {
diskReadKbsPerfCounterInfo = info;
}
if ("write".equalsIgnoreCase(info.getNameInfo().getKey())) {
diskWriteKbsPerfCounterInfo = info;
}
}
}
int key = ((HostMO) hyperHost).getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_VM_INTERNAL_NAME);
if (key == 0) {
s_logger.warn("Custom field " + CustomFieldConstants.CLOUD_VM_INTERNAL_NAME + " is not registered ?!");
}
String instanceNameCustomField = "value[" + key + "]";
final String numCpuStr = "summary.config.numCpu";
final String cpuUseStr = "summary.quickStats.overallCpuUsage";
final String guestMemUseStr = "summary.quickStats.guestMemoryUsage";
final String memLimitStr = "resourceConfig.memoryAllocation.limit";
final String memMbStr = "config.hardware.memoryMB";
final String allocatedCpuStr = "summary.runtime.maxCpuUsage";
ObjectContent[] ocs = hyperHost.getVmPropertiesOnHyperHost(new String[] { "name", numCpuStr, cpuUseStr, guestMemUseStr, memLimitStr, memMbStr, allocatedCpuStr, instanceNameCustomField });
if (ocs != null && ocs.length > 0) {
for (ObjectContent oc : ocs) {
List<DynamicProperty> objProps = oc.getPropSet();
if (objProps != null) {
String name = null;
String numberCPUs = null;
double maxCpuUsage = 0;
String memlimit = null;
String memkb = null;
String guestMemusage = null;
String vmNameOnVcenter = null;
String vmInternalCSName = null;
double allocatedCpu = 0;
for (DynamicProperty objProp : objProps) {
if (objProp.getName().equals("name")) {
vmNameOnVcenter = objProp.getVal().toString();
} else if (objProp.getName().contains(instanceNameCustomField)) {
if (objProp.getVal() != null)
vmInternalCSName = ((CustomFieldStringValue) objProp.getVal()).getValue();
} else if (objProp.getName().equals(guestMemUseStr)) {
guestMemusage = objProp.getVal().toString();
} else if (objProp.getName().equals(numCpuStr)) {
numberCPUs = objProp.getVal().toString();
} else if (objProp.getName().equals(cpuUseStr)) {
maxCpuUsage = NumberUtils.toDouble(objProp.getVal().toString());
} else if (objProp.getName().equals(memLimitStr)) {
memlimit = objProp.getVal().toString();
} else if (objProp.getName().equals(memMbStr)) {
memkb = objProp.getVal().toString();
} else if (objProp.getName().equals(allocatedCpuStr)) {
allocatedCpu = NumberUtils.toDouble(objProp.getVal().toString());
}
}
maxCpuUsage = (maxCpuUsage / allocatedCpu) * 100;
if (vmInternalCSName != null) {
name = vmInternalCSName;
} else {
name = vmNameOnVcenter;
}
if (!vmNames.contains(name)) {
continue;
}
ManagedObjectReference vmMor = hyperHost.findVmOnHyperHost(name).getMor();
assert (vmMor != null);
double networkReadKBs = 0;
double networkWriteKBs = 0;
double diskReadIops = 0;
double diskWriteIops = 0;
double diskReadKbs = 0;
double diskWriteKbs = 0;
final ArrayList<PerfMetricId> perfMetricsIds = new ArrayList<PerfMetricId>();
if (rxPerfCounterInfo != null) {
perfMetricsIds.add(VmwareHelper.createPerfMetricId(rxPerfCounterInfo, ""));
}
if (txPerfCounterInfo != null) {
perfMetricsIds.add(VmwareHelper.createPerfMetricId(txPerfCounterInfo, ""));
}
if (diskReadIOPerfCounterInfo != null) {
perfMetricsIds.add(VmwareHelper.createPerfMetricId(diskReadIOPerfCounterInfo, "*"));
}
if (diskWriteIOPerfCounterInfo != null) {
perfMetricsIds.add(VmwareHelper.createPerfMetricId(diskWriteIOPerfCounterInfo, "*"));
}
if (diskReadKbsPerfCounterInfo != null) {
perfMetricsIds.add(VmwareHelper.createPerfMetricId(diskReadKbsPerfCounterInfo, ""));
}
if (diskWriteKbsPerfCounterInfo != null) {
perfMetricsIds.add(VmwareHelper.createPerfMetricId(diskWriteKbsPerfCounterInfo, ""));
}
if (perfMetricsIds.size() > 0) {
try {
final PerfQuerySpec qSpec = new PerfQuerySpec();
qSpec.setEntity(vmMor);
qSpec.setFormat("normal");
qSpec.setIntervalId(windowInterval);
qSpec.setStartTime(startTime);
qSpec.setEndTime(endTime);
qSpec.getMetricId().addAll(perfMetricsIds);
final List<PerfEntityMetricBase> perfValues = service.queryPerf(perfMgr, Collections.singletonList(qSpec));
for (final PerfEntityMetricBase perfValue : perfValues) {
if (!(perfValue instanceof PerfEntityMetric)) {
continue;
}
final List<PerfMetricSeries> seriesList = ((PerfEntityMetric) perfValue).getValue();
for (final PerfMetricSeries series : seriesList) {
if (!(series instanceof PerfMetricIntSeries)) {
continue;
}
final List<Long> values = ((PerfMetricIntSeries) series).getValue();
double sum = 0;
for (final Long value : values) {
sum += value;
}
double avg = sum / (values.size() * 1f);
if (series.getId().getCounterId() == rxPerfCounterInfo.getKey()) {
networkReadKBs = avg;
}
if (series.getId().getCounterId() == txPerfCounterInfo.getKey()) {
networkWriteKBs = avg;
}
if (series.getId().getCounterId() == diskReadIOPerfCounterInfo.getKey()) {
diskReadIops += avg;
}
if (series.getId().getCounterId() == diskWriteIOPerfCounterInfo.getKey()) {
diskWriteIops += avg;
}
if (series.getId().getCounterId() == diskReadKbsPerfCounterInfo.getKey()) {
diskReadKbs = avg;
}
if (series.getId().getCounterId() == diskWriteKbsPerfCounterInfo.getKey()) {
diskWriteKbs = avg;
}
}
}
} catch (Exception e) {
s_logger.error(String.format("Unable to execute PerfQuerySpec due to: [%s]. The window interval is enabled in vCenter?", VmwareHelper.getExceptionMessage(e)), e);
}
}
final VmStatsEntry vmStats = new VmStatsEntry(NumberUtils.toDouble(memkb) * 1024, NumberUtils.toDouble(guestMemusage) * 1024, NumberUtils.toDouble(memlimit) * 1024, maxCpuUsage, networkReadKBs, networkWriteKBs, NumberUtils.toInt(numberCPUs), "vm");
vmStats.setDiskReadIOs(diskReadIops);
vmStats.setDiskWriteIOs(diskWriteIops);
vmStats.setDiskReadKBs(diskReadKbs);
vmStats.setDiskWriteKBs(diskWriteKbs);
vmResponseMap.put(name, vmStats);
}
}
}
return vmResponseMap;
}
use of com.vmware.vim25.PerfEntityMetric in project cloudstack by apache.
the class VmwareResource method execute.
protected Answer execute(GetVmDiskStatsCommand cmd) {
try {
final VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
final ManagedObjectReference perfMgr = getServiceContext().getServiceContent().getPerfManager();
VimPortType service = getServiceContext().getService();
Integer windowInterval = getVmwareWindowTimeInterval();
final XMLGregorianCalendar startTime = VmwareHelper.getXMLGregorianCalendar(new Date(), windowInterval);
final XMLGregorianCalendar endTime = VmwareHelper.getXMLGregorianCalendar(new Date(), 0);
PerfCounterInfo diskReadIOPerfCounterInfo = null;
PerfCounterInfo diskWriteIOPerfCounterInfo = null;
PerfCounterInfo diskReadKbsPerfCounterInfo = null;
PerfCounterInfo diskWriteKbsPerfCounterInfo = null;
// https://pubs.vmware.com/vsphere-5-5/topic/com.vmware.wssdk.apiref.doc/virtual_disk_counters.html
List<PerfCounterInfo> cInfo = getServiceContext().getVimClient().getDynamicProperty(perfMgr, "perfCounter");
for (PerfCounterInfo info : cInfo) {
if ("virtualdisk".equalsIgnoreCase(info.getGroupInfo().getKey()) && "average".equalsIgnoreCase(info.getRollupType().value())) {
if ("numberReadAveraged".equalsIgnoreCase(info.getNameInfo().getKey())) {
diskReadIOPerfCounterInfo = info;
}
if ("numberWriteAveraged".equalsIgnoreCase(info.getNameInfo().getKey())) {
diskWriteIOPerfCounterInfo = info;
}
if ("read".equalsIgnoreCase(info.getNameInfo().getKey())) {
diskReadKbsPerfCounterInfo = info;
}
if ("write".equalsIgnoreCase(info.getNameInfo().getKey())) {
diskWriteKbsPerfCounterInfo = info;
}
}
}
final ManagedObjectReference dcMor = hyperHost.getHyperHostDatacenter();
final DatacenterMO dcMo = new DatacenterMO(getServiceContext(), dcMor);
final HashMap<String, List<VmDiskStatsEntry>> vmStatsMap = new HashMap<>();
for (final String vmName : cmd.getVmNames()) {
final VirtualMachineMO vmMo = dcMo.findVm(vmName);
final List<VmDiskStatsEntry> diskStats = new ArrayList<>();
for (final VirtualDisk disk : vmMo.getAllDiskDevice()) {
final String diskBusName = vmMo.getDeviceBusName(vmMo.getAllDeviceList(), disk);
long readReq = 0;
long readBytes = 0;
long writeReq = 0;
long writeBytes = 0;
final ArrayList<PerfMetricId> perfMetricsIds = new ArrayList<PerfMetricId>();
if (diskReadIOPerfCounterInfo != null) {
perfMetricsIds.add(VmwareHelper.createPerfMetricId(diskReadIOPerfCounterInfo, diskBusName));
}
if (diskWriteIOPerfCounterInfo != null) {
perfMetricsIds.add(VmwareHelper.createPerfMetricId(diskWriteIOPerfCounterInfo, diskBusName));
}
if (diskReadKbsPerfCounterInfo != null) {
perfMetricsIds.add(VmwareHelper.createPerfMetricId(diskReadKbsPerfCounterInfo, diskBusName));
}
if (diskWriteKbsPerfCounterInfo != null) {
perfMetricsIds.add(VmwareHelper.createPerfMetricId(diskWriteKbsPerfCounterInfo, diskBusName));
}
if (perfMetricsIds.size() > 0) {
try {
final PerfQuerySpec qSpec = new PerfQuerySpec();
qSpec.setEntity(vmMo.getMor());
qSpec.setFormat("normal");
qSpec.setIntervalId(windowInterval);
qSpec.setStartTime(startTime);
qSpec.setEndTime(endTime);
qSpec.getMetricId().addAll(perfMetricsIds);
for (final PerfEntityMetricBase perfValue : service.queryPerf(perfMgr, Collections.singletonList(qSpec))) {
if (!(perfValue instanceof PerfEntityMetric)) {
continue;
}
final List<PerfMetricSeries> values = ((PerfEntityMetric) perfValue).getValue();
if (values == null || values.isEmpty()) {
continue;
}
for (final PerfMetricSeries value : values) {
if (!(value instanceof PerfMetricIntSeries) || !value.getId().getInstance().equals(diskBusName)) {
continue;
}
final List<Long> perfStats = ((PerfMetricIntSeries) value).getValue();
if (perfStats.size() > 0) {
long sum = 0;
for (long val : perfStats) {
sum += val;
}
long avg = sum / perfStats.size();
if (value.getId().getCounterId() == diskReadIOPerfCounterInfo.getKey()) {
readReq = avg;
} else if (value.getId().getCounterId() == diskWriteIOPerfCounterInfo.getKey()) {
writeReq = avg;
} else if (value.getId().getCounterId() == diskReadKbsPerfCounterInfo.getKey()) {
readBytes = avg * 1024;
} else if (value.getId().getCounterId() == diskWriteKbsPerfCounterInfo.getKey()) {
writeBytes = avg * 1024;
}
}
}
}
} catch (Exception e) {
s_logger.error(String.format("Unable to execute PerfQuerySpec due to: [%s]. The window interval is enabled in vCenter?", VmwareHelper.getExceptionMessage(e)), e);
}
}
diskStats.add(new VmDiskStatsEntry(vmName, VmwareHelper.getDiskDeviceFileName(disk), writeReq, readReq, writeBytes, readBytes));
}
if (CollectionUtils.isNotEmpty(diskStats)) {
vmStatsMap.put(vmName, diskStats);
}
}
s_logger.debug(String.format("VM Disks Maps is: [%s].", _gson.toJson(vmStatsMap)));
if (MapUtils.isNotEmpty(vmStatsMap)) {
return new GetVmDiskStatsAnswer(cmd, "", cmd.getHostName(), vmStatsMap);
}
} catch (Exception e) {
s_logger.error(String.format("Unable to execute GetVmDiskStatsCommand due to [%s].", VmwareHelper.getExceptionMessage(e)), e);
}
return new GetVmDiskStatsAnswer(cmd, null, null, null);
}
Aggregations