protected HashMap<String, VmStatsEntry> getVmStats(Connection conn, GetVmStatsCommand cmd, List<String> vmUUIDs, String hostGuid) {
HashMap<String, VmStatsEntry> vmResponseMap = new HashMap<String, VmStatsEntry>();
for (String vmUUID : vmUUIDs) {
vmResponseMap.put(vmUUID, new VmStatsEntry(0, 0, 0, 0, "vm"));
}
Object[] rrdData = getRRDData(conn, 2); // call rrddata with 2 for vm
if (rrdData == null) {
return null;
}
Integer numRows = (Integer) rrdData[0];
Integer numColumns = (Integer) rrdData[1];
Node legend = (Node) rrdData[2];
Node dataNode = (Node) rrdData[3];
NodeList legendChildren = legend.getChildNodes();
for (int col = 0; col < numColumns; col++) {
if (legendChildren == null || legendChildren.item(col) == null) {
continue;
}
String columnMetadata = getXMLNodeValue(legendChildren.item(col));
if (columnMetadata == null) {
continue;
}
String[] columnMetadataList = columnMetadata.split(":");
if (columnMetadataList.length != 4) {
continue;
}
String type = columnMetadataList[1];
String uuid = columnMetadataList[2];
String param = columnMetadataList[3];
if (type.equals("vm") && vmResponseMap.keySet().contains(uuid)) {
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)/(8*2)));
} else if (param.matches("vif_\\d_tx")) {
vmStatsAnswer.setNetworkWriteKBs(vmStatsAnswer.getNetworkWriteKBs() + (getDataAverage(dataNode, col, numRows)/(8*2)));
}
}
}
for (String vmUUID : vmResponseMap.keySet()) {
VmStatsEntry vmStatsAnswer = vmResponseMap.get(vmUUID);
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());
}
}
try {
for (String vmUUID : vmUUIDs) {
VM vm = VM.getByUuid(conn, vmUUID);
VmStatsEntry stats = vmResponseMap.get(vmUUID);
double diskReadKBs = 0;
double diskWriteKBs = 0;
for (VBD vbd : vm.getVBDs(conn)) {
VBDMetrics record = vbd.getMetrics(conn);
diskReadKBs += record.getIoReadKbs(conn);
diskWriteKBs += record.getIoWriteKbs(conn);
}
if (stats == null) {
stats = new VmStatsEntry();
}
stats.setDiskReadKBs(diskReadKBs);
stats.setDiskWriteKBs(diskWriteKBs);
vmResponseMap.put(vmUUID, stats);
}
} catch (Exception e) {
s_logger.warn("Error while collecting disk stats from : ", e);
return null;