use of com.cloud.legacymodel.vm.VmStatsEntry in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testGetVmStat.
@Test
public void testGetVmStat() throws LibvirtException {
final Connect connect = Mockito.mock(Connect.class);
final Domain domain = Mockito.mock(Domain.class);
final DomainInfo domainInfo = new DomainInfo();
Mockito.when(domain.getInfo()).thenReturn(domainInfo);
Mockito.when(connect.domainLookupByName(VMNAME)).thenReturn(domain);
final NodeInfo nodeInfo = new NodeInfo();
nodeInfo.cpus = 8;
nodeInfo.memory = 8 * 1024 * 1024;
nodeInfo.sockets = 2;
nodeInfo.threads = 2;
nodeInfo.model = "Foo processor";
Mockito.when(connect.nodeInfo()).thenReturn(nodeInfo);
// this is testing the interface stats, returns an increasing number of sent and received bytes
Mockito.when(domain.interfaceStats(Matchers.anyString())).thenAnswer(new org.mockito.stubbing.Answer<DomainInterfaceStats>() {
// increment with less than a KB, so this should be less than 1 KB
static final int increment = 1000;
int rxBytes = 1000;
int txBytes = 1000;
@Override
public DomainInterfaceStats answer(final InvocationOnMock invocation) throws Throwable {
final DomainInterfaceStats domainInterfaceStats = new DomainInterfaceStats();
domainInterfaceStats.rx_bytes = this.rxBytes += increment;
domainInterfaceStats.tx_bytes = this.txBytes += increment;
return domainInterfaceStats;
}
});
Mockito.when(domain.blockStats(Matchers.anyString())).thenAnswer(new org.mockito.stubbing.Answer<DomainBlockStats>() {
// a little less than a KB
static final int increment = 1000;
int rdBytes = 0;
int wrBytes = 1024;
@Override
public DomainBlockStats answer(final InvocationOnMock invocation) throws Throwable {
final DomainBlockStats domainBlockStats = new DomainBlockStats();
domainBlockStats.rd_bytes = this.rdBytes += increment;
domainBlockStats.wr_bytes = this.wrBytes += increment;
return domainBlockStats;
}
});
final LibvirtComputingResource libvirtComputingResource = new LibvirtComputingResource() {
@Override
public List<InterfaceDef> getInterfaces(final Connect conn, final String vmName) {
final InterfaceDef interfaceDef = new InterfaceDef();
return Arrays.asList(interfaceDef);
}
@Override
public List<LibvirtDiskDef> getDisks(final Connect conn, final String vmName) {
final LibvirtDiskDef diskDef = new LibvirtDiskDef();
return Arrays.asList(diskDef);
}
};
libvirtComputingResource.getVmStat(connect, VMNAME);
final VmStatsEntry vmStat = libvirtComputingResource.getVmStat(connect, VMNAME);
// network traffic as generated by the logic above, must be greater than zero
Assert.assertTrue(vmStat.getNetworkReadKBs() > 0);
Assert.assertTrue(vmStat.getNetworkWriteKBs() > 0);
// IO traffic as generated by the logic above, must be greater than zero
Assert.assertTrue(vmStat.getDiskReadKBs() > 0);
Assert.assertTrue(vmStat.getDiskWriteKBs() > 0);
}
use of com.cloud.legacymodel.vm.VmStatsEntry in project cosmic by MissionCriticalCloud.
the class UserVmManagerImpl method getVirtualMachineStatistics.
@Override
public HashMap<Long, VmStatsEntry> getVirtualMachineStatistics(final long hostId, final String hostName, final List<Long> vmIds) throws CloudRuntimeException {
final HashMap<Long, VmStatsEntry> vmStatsById = new HashMap<>();
if (vmIds.isEmpty()) {
return vmStatsById;
}
final List<String> vmNames = new ArrayList<>();
for (final Long vmId : vmIds) {
final UserVmVO vm = _vmDao.findById(vmId);
vmNames.add(vm.getInstanceName());
}
final Answer answer = _agentMgr.easySend(hostId, new GetVmStatsCommand(vmNames, _hostDao.findById(hostId).getGuid(), hostName));
if (answer == null || !answer.getResult()) {
s_logger.warn("Unable to obtain VM statistics.");
return null;
} else {
final HashMap<String, VmStatsEntry> vmStatsByName = ((GetVmStatsAnswer) answer).getVmStatsMap();
if (vmStatsByName == null) {
s_logger.warn("Unable to obtain VM statistics.");
return null;
}
for (final Map.Entry<String, VmStatsEntry> entry : vmStatsByName.entrySet()) {
vmStatsById.put(vmIds.get(vmNames.indexOf(entry.getKey())), entry.getValue());
}
}
return vmStatsById;
}
use of com.cloud.legacymodel.vm.VmStatsEntry in project cosmic by MissionCriticalCloud.
the class CitrixResourceBase method getVmStats.
public HashMap<String, VmStatsEntry> getVmStats(final Connection conn, final GetVmStatsCommand cmd, final List<String> vmUUIDs, final String hostGuid) {
final HashMap<String, VmStatsEntry> vmResponseMap = new HashMap<>();
for (final String vmUUID : vmUUIDs) {
vmResponseMap.put(vmUUID, new VmStatsEntry(0, 0, 0, 0, "vm"));
}
// call rrddata with 2 for
final Object[] rrdData = getRRDData(conn, 2);
if (rrdData == null) {
return null;
}
final Integer numRows = (Integer) rrdData[0];
final Integer numColumns = (Integer) rrdData[1];
final Node legend = (Node) rrdData[2];
final Node dataNode = (Node) rrdData[3];
final NodeList legendChildren = legend.getChildNodes();
for (int col = 0; col < numColumns; col++) {
if (legendChildren == null || legendChildren.item(col) == null) {
continue;
}
final String columnMetadata = getXMLNodeValue(legendChildren.item(col));
if (columnMetadata == null) {
continue;
}
final String[] columnMetadataList = columnMetadata.split(":");
if (columnMetadataList.length != 4) {
continue;
}
final String type = columnMetadataList[1];
final String uuid = columnMetadataList[2];
final String param = columnMetadataList[3];
if (type.equals("vm") && vmResponseMap.keySet().contains(uuid)) {
final VmStatsEntry vmStatsAnswer = vmResponseMap.get(uuid);
vmStatsAnswer.setEntityType("vm");
if (param.contains("cpu")) {
vmStatsAnswer.setNumCPUs(vmStatsAnswer.getNumCPUs() + 1);
vmStatsAnswer.setCPUUtilization(vmStatsAnswer.getCPUUtilization() + getDataAverage(dataNode, col, numRows));
} else if (param.matches("vif_\\d*_rx")) {
vmStatsAnswer.setNetworkReadKBs(vmStatsAnswer.getNetworkReadKBs() + getDataAverage(dataNode, col, numRows) / 1000);
} else if (param.matches("vif_\\d*_tx")) {
vmStatsAnswer.setNetworkWriteKBs(vmStatsAnswer.getNetworkWriteKBs() + getDataAverage(dataNode, col, numRows) / 1000);
} else if (param.matches("vbd_.*_read")) {
vmStatsAnswer.setDiskReadKBs(vmStatsAnswer.getDiskReadKBs() + getDataAverage(dataNode, col, numRows) / 1000);
} else if (param.matches("vbd_.*_write")) {
vmStatsAnswer.setDiskWriteKBs(vmStatsAnswer.getDiskWriteKBs() + getDataAverage(dataNode, col, numRows) / 1000);
}
}
}
for (final Map.Entry<String, VmStatsEntry> entry : vmResponseMap.entrySet()) {
final VmStatsEntry vmStatsAnswer = entry.getValue();
if (vmStatsAnswer.getNumCPUs() != 0) {
vmStatsAnswer.setCPUUtilization(vmStatsAnswer.getCPUUtilization() / vmStatsAnswer.getNumCPUs());
}
vmStatsAnswer.setCPUUtilization(vmStatsAnswer.getCPUUtilization() * 100);
if (s_logger.isDebugEnabled()) {
s_logger.debug("Vm cpu utilization " + vmStatsAnswer.getCPUUtilization());
}
}
return vmResponseMap;
}
use of com.cloud.legacymodel.vm.VmStatsEntry in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResource method getVmStat.
public VmStatsEntry getVmStat(final Connect conn, final String vmName) throws LibvirtException {
Domain dm = null;
try {
dm = getDomain(conn, vmName);
final DomainInfo info = dm.getInfo();
final VmStatsEntry stats = new VmStatsEntry();
stats.setNumCPUs(info.nrVirtCpu);
stats.setEntityType("vm");
/* get cpu utilization */
VmStats oldStats = null;
final Calendar now = Calendar.getInstance();
oldStats = this.vmStats.get(vmName);
long elapsedTime = 0;
if (oldStats != null) {
elapsedTime = now.getTimeInMillis() - oldStats.timestamp.getTimeInMillis();
double utilization = (info.cpuTime - oldStats.usedTime) / ((double) elapsedTime * 1000000);
final NodeInfo node = conn.nodeInfo();
utilization = utilization / node.cpus;
if (utilization > 0) {
stats.setCPUUtilization(utilization * 100);
}
}
/* get network stats */
final List<InterfaceDef> vifs = getInterfaces(conn, vmName);
long rx = 0;
long tx = 0;
for (final InterfaceDef vif : vifs) {
final DomainInterfaceStats ifStats = dm.interfaceStats(vif.getDevName());
rx += ifStats.rx_bytes;
tx += ifStats.tx_bytes;
}
if (oldStats != null) {
final double deltarx = rx - oldStats.rx;
if (deltarx > 0) {
stats.setNetworkReadKBs(deltarx / 1024);
}
final double deltatx = tx - oldStats.tx;
if (deltatx > 0) {
stats.setNetworkWriteKBs(deltatx / 1024);
}
}
/* get disk stats */
final List<LibvirtDiskDef> disks = getDisks(conn, vmName);
long ioRd = 0;
long ioWr = 0;
long bytesRd = 0;
long bytesWr = 0;
for (final LibvirtDiskDef disk : disks) {
if (disk.getDeviceType() == DeviceType.CDROM || disk.getDeviceType() == DeviceType.FLOPPY) {
continue;
}
final DomainBlockStats blockStats = dm.blockStats(disk.getDiskLabel());
ioRd += blockStats.rd_req;
ioWr += blockStats.wr_req;
bytesRd += blockStats.rd_bytes;
bytesWr += blockStats.wr_bytes;
}
if (oldStats != null) {
final long deltaiord = ioRd - oldStats.ioRead;
if (deltaiord > 0) {
stats.setDiskReadIOs(deltaiord);
}
final long deltaiowr = ioWr - oldStats.ioWrote;
if (deltaiowr > 0) {
stats.setDiskWriteIOs(deltaiowr);
}
final double deltabytesrd = bytesRd - oldStats.bytesRead;
if (deltabytesrd > 0) {
stats.setDiskReadKBs(deltabytesrd / 1024);
}
final double deltabyteswr = bytesWr - oldStats.bytesWrote;
if (deltabyteswr > 0) {
stats.setDiskWriteKBs(deltabyteswr / 1024);
}
}
/* save to Hashmap */
final VmStats newStat = new VmStats();
newStat.usedTime = info.cpuTime;
newStat.rx = rx;
newStat.tx = tx;
newStat.ioRead = ioRd;
newStat.ioWrote = ioWr;
newStat.bytesRead = bytesRd;
newStat.bytesWrote = bytesWr;
newStat.timestamp = now;
this.vmStats.put(vmName, newStat);
return stats;
} finally {
if (dm != null) {
dm.free();
}
}
}
use of com.cloud.legacymodel.vm.VmStatsEntry in project cosmic by MissionCriticalCloud.
the class LibvirtGetVmStatsCommandWrapper method execute.
@Override
public Answer execute(final GetVmStatsCommand command, final LibvirtComputingResource libvirtComputingResource) {
final List<String> vmNames = command.getVmNames();
try {
final HashMap<String, VmStatsEntry> vmStatsNameMap = new HashMap<>();
for (final String vmName : vmNames) {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);
try {
final VmStatsEntry statEntry = libvirtComputingResource.getVmStat(conn, vmName);
if (statEntry == null) {
continue;
}
vmStatsNameMap.put(vmName, statEntry);
} catch (final LibvirtException e) {
s_logger.warn("Can't get vm stats: " + e.toString() + ", continue");
}
}
return new GetVmStatsAnswer(command, vmStatsNameMap);
} catch (final LibvirtException e) {
s_logger.debug("Can't get vm stats: " + e.toString());
return new GetVmStatsAnswer(command, null);
}
}
Aggregations