}
@Override
public GetHostStatsAnswer getHostStatistic(GetHostStatsCommand cmd) {
String hostGuid = cmd.getHostGuid();
MockHost host = null;
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
try {
txn.start();
host = _mockHostDao.findByGuid(hostGuid);
txn.commit();
if (host == null) {
return null;
}
} catch (Exception ex) {
txn.rollback();
throw new CloudRuntimeException("Unable to get host " + hostGuid + " due to " + ex.getMessage(), ex);
} finally {
txn.close();
txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
txn.close();
}
TransactionLegacy vmtxn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
try {
vmtxn.start();
List<MockVMVO> vms = _mockVmDao.findByHostId(host.getId());
vmtxn.commit();
double usedMem = 0.0;
double usedCpu = 0.0;
for (MockVMVO vm : vms) {
usedMem += vm.getMemory();
usedCpu += vm.getCpu();
}
HostStatsEntry hostStats = new HostStatsEntry();
hostStats.setTotalMemoryKBs(host.getMemorySize());
hostStats.setFreeMemoryKBs(host.getMemorySize() - usedMem);
hostStats.setNetworkReadKBs(32768);
hostStats.setNetworkWriteKBs(16384);
hostStats.setCpuUtilization(usedCpu / (host.getCpuCount() * host.getCpuSpeed()));
hostStats.setEntityType("simulator-host");
hostStats.setHostId(cmd.getHostId());
return new GetHostStatsAnswer(cmd, hostStats);
} catch (Exception ex) {
vmtxn.rollback();
throw new CloudRuntimeException("Unable to get Vms on host " + host.getGuid() + " due to " + ex.getMessage(), ex);
} finally {
vmtxn.close();
vmtxn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
vmtxn.close();
}