use of com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost in project cloudstack by apache.
the class VmwareResource method execute.
protected Answer execute(CheckVirtualMachineCommand cmd) {
final String vmName = cmd.getVmName();
PowerState powerState = PowerState.PowerUnknown;
Integer vncPort = null;
VmwareContext context = getServiceContext();
VmwareHypervisorHost hyperHost = getHyperHost(context);
try {
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName);
if (vmMo != null) {
powerState = getVmPowerState(vmMo);
return new CheckVirtualMachineAnswer(cmd, powerState, vncPort);
} else {
s_logger.warn("Can not find vm " + vmName + " to execute CheckVirtualMachineCommand");
return new CheckVirtualMachineAnswer(cmd, powerState, vncPort);
}
} catch (Throwable e) {
createLogMessageException(e, cmd);
return new CheckVirtualMachineAnswer(cmd, powerState, vncPort);
}
}
use of com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost in project cloudstack by apache.
the class VmwareResource method execute.
protected Answer execute(PingTestCommand cmd) {
String controlIp = cmd.getRouterIp();
if (controlIp != null) {
String args = " -c 1 -n -q " + cmd.getPrivateIp();
try {
Pair<Boolean, String> result = SshHelper.sshExecute(controlIp, DefaultDomRSshPort, "root", getSystemVmKeyFile(), null, "/bin/ping" + args);
if (result.first())
return new Answer(cmd);
} catch (Exception e) {
s_logger.error("Unable to execute ping command on DomR (" + controlIp + "), domR may not be ready yet. failure due to " + VmwareHelper.getExceptionMessage(e), e);
}
return new Answer(cmd, false, "PingTestCommand failed");
} else {
VmwareContext context = getServiceContext();
VmwareHypervisorHost hyperHost = getHyperHost(context);
try {
HostMO hostMo = (HostMO) hyperHost;
ClusterMO clusterMo = new ClusterMO(context, hostMo.getHyperHostCluster());
VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
List<Pair<ManagedObjectReference, String>> hosts = clusterMo.getClusterHosts();
for (Pair<ManagedObjectReference, String> entry : hosts) {
HostMO hostInCluster = new HostMO(context, entry.first());
String hostIp = hostInCluster.getHostManagementIp(mgr.getManagementPortGroupName());
if (hostIp != null && hostIp.equals(cmd.getComputingHostIp())) {
if (hostInCluster.isHyperHostConnected())
return new Answer(cmd);
else
return new Answer(cmd, false, "PingTestCommand failed");
}
}
} catch (Exception e) {
s_logger.error("Unable to execute ping command on host (" + cmd.getComputingHostIp() + "). failure due to " + VmwareHelper.getExceptionMessage(e), e);
}
return new Answer(cmd, false, "PingTestCommand failed");
}
}
use of com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost in project cloudstack by apache.
the class VmwareResource method execute.
private Answer execute(SetupPersistentNetworkCommand cmd) {
VmwareHypervisorHost host = getHyperHost(getServiceContext());
String hostname = null;
VmwareContext context = getServiceContext();
HostMO hostMO = new HostMO(context, host.getMor());
try {
prepareNetworkFromNicInfo(hostMO, cmd.getNic(), false, null);
hostname = host.getHyperHostName();
} catch (Exception e) {
return new SetupPersistentNetworkAnswer(cmd, false, "failed to setup port-group due to: " + e.getLocalizedMessage());
}
return new SetupPersistentNetworkAnswer(cmd, true, hostname);
}
use of com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost 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.cloud.hypervisor.vmware.mo.VmwareHypervisorHost in project cloudstack by apache.
the class VmwareResource method execute.
protected ScaleVmAnswer execute(ScaleVmCommand cmd) {
VmwareContext context = getServiceContext();
VirtualMachineTO vmSpec = cmd.getVirtualMachine();
try {
VmwareHypervisorHost hyperHost = getHyperHost(context);
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(cmd.getVmName());
VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
int ramMb = getReservedMemoryMb(vmSpec);
long hotaddIncrementSizeInMb;
long hotaddMemoryLimitInMb;
long requestedMaxMemoryInMb = vmSpec.getMaxRam() / (1024 * 1024);
// Check if VM is really running on hypervisor host
if (getVmPowerState(vmMo) != PowerState.PowerOn) {
throw new CloudRuntimeException("Found that the VM " + vmMo.getVmName() + " is not running. Unable to scale-up this VM");
}
// Check max hot add limit
hotaddIncrementSizeInMb = vmMo.getHotAddMemoryIncrementSizeInMb();
hotaddMemoryLimitInMb = vmMo.getHotAddMemoryLimitInMb();
if (requestedMaxMemoryInMb > hotaddMemoryLimitInMb) {
throw new CloudRuntimeException("Memory of VM " + vmMo.getVmName() + " cannot be scaled to " + requestedMaxMemoryInMb + "MB." + " Requested memory limit is beyond the hotadd memory limit for this VM at the moment is " + hotaddMemoryLimitInMb + "MB.");
}
// Check increment is multiple of increment size
long reminder = hotaddIncrementSizeInMb > 0 ? requestedMaxMemoryInMb % hotaddIncrementSizeInMb : 0;
if (reminder != 0) {
requestedMaxMemoryInMb = requestedMaxMemoryInMb + hotaddIncrementSizeInMb - reminder;
}
// Check if license supports the feature
VmwareHelper.isFeatureLicensed(hyperHost, FeatureKeyConstants.HOTPLUG);
VmwareHelper.setVmScaleUpConfig(vmConfigSpec, vmSpec.getCpus(), vmSpec.getMaxSpeed(), getReservedCpuMHZ(vmSpec), (int) requestedMaxMemoryInMb, ramMb, vmSpec.getLimitCpuUse());
if (!vmMo.configureVm(vmConfigSpec)) {
throw new Exception("Unable to execute ScaleVmCommand");
}
} catch (Exception e) {
s_logger.error(String.format("ScaleVmCommand failed due to: [%s].", VmwareHelper.getExceptionMessage(e)), e);
return new ScaleVmAnswer(cmd, false, String.format("Unable to execute ScaleVmCommand due to: [%s].", e.toString()));
}
return new ScaleVmAnswer(cmd, true, null);
}
Aggregations