use of com.vmware.vim25.VimPortType 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);
}
use of com.vmware.vim25.VimPortType in project cloudstack by apache.
the class VMwareUtil method getVMwareConnection.
public static VMwareConnection getVMwareConnection(LoginInfo loginInfo) throws Exception {
trustAllHttpsCertificates();
HostnameVerifier hv = new HostnameVerifier() {
@Override
public boolean verify(String urlHostName, SSLSession session) {
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(hv);
ManagedObjectReference serviceInstanceRef = new ManagedObjectReference();
final String serviceInstanceName = "ServiceInstance";
serviceInstanceRef.setType(serviceInstanceName);
serviceInstanceRef.setValue(serviceInstanceName);
VimService vimService = new VimService();
VimPortType vimPortType = vimService.getVimPort();
Map<String, Object> ctxt = ((BindingProvider) vimPortType).getRequestContext();
ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "https://" + loginInfo.getHost() + "/sdk");
ctxt.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true);
ServiceContent serviceContent = vimPortType.retrieveServiceContent(serviceInstanceRef);
vimPortType.login(serviceContent.getSessionManager(), loginInfo.getUsername(), loginInfo.getPassword(), null);
return new VMwareConnection(vimPortType, serviceContent);
}
use of com.vmware.vim25.VimPortType in project cloudstack by apache.
the class VMwareUtil method getVms.
public static Map<String, ManagedObjectReference> getVms(VMwareConnection connection) throws Exception {
Map<String, ManagedObjectReference> nameToVm = new HashMap<>();
ManagedObjectReference rootFolder = connection.getServiceContent().getRootFolder();
TraversalSpec tSpec = getVMTraversalSpec();
PropertySpec propertySpec = new PropertySpec();
propertySpec.setAll(Boolean.FALSE);
propertySpec.getPathSet().add("name");
propertySpec.setType("VirtualMachine");
ObjectSpec objectSpec = new ObjectSpec();
objectSpec.setObj(rootFolder);
objectSpec.setSkip(Boolean.TRUE);
objectSpec.getSelectSet().add(tSpec);
PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
propertyFilterSpec.getPropSet().add(propertySpec);
propertyFilterSpec.getObjectSet().add(objectSpec);
List<PropertyFilterSpec> lstPfs = new ArrayList<>(1);
lstPfs.add(propertyFilterSpec);
VimPortType vimPortType = connection.getVimPortType();
ManagedObjectReference propertyCollector = connection.getServiceContent().getPropertyCollector();
List<ObjectContent> lstObjectContent = retrievePropertiesAllObjects(lstPfs, vimPortType, propertyCollector);
if (lstObjectContent != null) {
for (ObjectContent oc : lstObjectContent) {
ManagedObjectReference mor = oc.getObj();
List<DynamicProperty> dps = oc.getPropSet();
String vmName = null;
if (dps != null) {
for (DynamicProperty dp : dps) {
vmName = (String) dp.getVal();
}
}
if (vmName != null) {
nameToVm.put(vmName, mor);
}
}
}
return nameToVm;
}
Aggregations