Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/src/main/java/com/cloud/host/HostStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public interface HostStats {

public HostStats getHostStats();

// public double getAverageLoad();
public double getLoadAverage();
// public double getXapiMemoryUsageKBs();
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public class HostResponse extends BaseResponse {

@SerializedName("averageload")
@Param(description = "the cpu average load on the host")
private Long averageLoad;
private Double averageLoad;

@SerializedName("networkkbsread")
@Param(description = "the incoming network traffic on the host")
Expand Down Expand Up @@ -337,7 +337,7 @@ public void setCpuUsed(String cpuUsed) {
this.cpuUsed = cpuUsed;
}

public void setAverageLoad(Long averageLoad) {
public void setCpuAverageLoad(Double averageLoad) {
this.averageLoad = averageLoad;
}

Expand Down Expand Up @@ -570,7 +570,7 @@ public String getCpuUsed() {
return cpuUsed;
}

public Long getAverageLoad() {
public Double getAverageLoad() {
return averageLoad;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public double getCpuUtilization() {
return hostStats.getCpuUtilization();
}

@Override
public double getLoadAverage() {
return hostStats.getLoadAverage();
}

@Override
public double getNetworkReadKBs() {
return hostStats.getNetworkReadKBs();
Expand Down
11 changes: 11 additions & 0 deletions core/src/main/java/com/cloud/agent/api/HostStatsEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class HostStatsEntry implements HostStats {
HostVO hostVo;
String entityType;
double cpuUtilization;
double averageLoad;
double networkReadKBs;
double networkWriteKBs;
double totalMemoryKBs;
Expand All @@ -45,6 +46,7 @@ public HostStatsEntry(long hostId, double cpuUtilization, double networkReadKBs,
this.networkWriteKBs = networkWriteKBs;
this.totalMemoryKBs = totalMemoryKBs;
this.freeMemoryKBs = freeMemoryKBs;
this.averageLoad = averageLoad;
}

@Override
Expand Down Expand Up @@ -101,6 +103,15 @@ public void setCpuUtilization(double cpuUtilization) {
this.cpuUtilization = cpuUtilization;
}

@Override
public double getLoadAverage() {
return this.averageLoad;
}

public void setAverageLoad(double cpuAvgLoad) {
this.averageLoad = cpuAvgLoad;
}

@Override
public double getUsedMemory() {
return (totalMemoryKBs - freeMemoryKBs) * 1024;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ public Answer execute(final GetHostStatsCommand command, final LibvirtComputingR
MemStat memStat = libvirtComputingResource.getMemStat();

final double cpuUtil = cpuStat.getCpuUsedPercent();
final double loadAvg = cpuStat.getCpuLoadAverage();

final Pair<Double, Double> nicStats = libvirtComputingResource.getNicStats(libvirtComputingResource.getPublicBridgeName());

final HostStatsEntry hostStats = new HostStatsEntry(command.getHostId(), cpuUtil, nicStats.first() / 1024, nicStats.second() / 1024, "host", memStat.getTotal() / 1024, memStat.getAvailable() / 1024, 0, 0);
final HostStatsEntry hostStats = new HostStatsEntry(command.getHostId(), cpuUtil, nicStats.first() / 1024, nicStats.second() / 1024, "host", memStat.getTotal() / 1024, memStat.getAvailable() / 1024, 0, loadAvg);
return new GetHostStatsAnswer(command, hostStats);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class CPUStat {
private UptimeStats _lastStats;
private final String _sysfsCpuDir = "/sys/devices/system/cpu";
private final String _uptimeFile = "/proc/uptime";
private final String _loadavgFile = "/proc/loadavg";

class UptimeStats {
public Double upTime = 0d;
Expand Down Expand Up @@ -80,6 +81,17 @@ public Integer getCores() {
return _cores;
}

public Double getCpuLoadAverage() {
File f = new File(_loadavgFile);
String[] load = {"0.0"};
try (Scanner scanner = new Scanner(f,"UTF-8");) {
load = scanner.useDelimiter("\\Z").next().split("\\s+");
} catch (FileNotFoundException ex) {
s_logger.warn("File " + _uptimeFile + " not found:" + ex.toString());
}
return Double.parseDouble(load[0]);
}

public Double getCpuUsedPercent() {
Double cpuUsed = 0d;
if (_cores == null || _cores == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ public List<HostMetricsResponse> listHostMetrics(List<HostResponse> hostResponse
metricsResponse.setCpuTotal(hostResponse.getCpuNumber(), hostResponse.getCpuSpeed(), cpuOvercommitRatio);
metricsResponse.setCpuUsed(hostResponse.getCpuUsed(), hostResponse.getCpuNumber(), hostResponse.getCpuSpeed());
metricsResponse.setCpuAllocated(hostResponse.getCpuAllocated(), hostResponse.getCpuNumber(), hostResponse.getCpuSpeed());
metricsResponse.setLoadAverage(hostResponse.getAverageLoad());
metricsResponse.setMemTotal(hostResponse.getMemoryTotal(), memoryOvercommitRatio);
metricsResponse.setMemAllocated(hostResponse.getMemoryAllocated());
metricsResponse.setMemUsed(hostResponse.getMemoryUsed());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public class HostMetricsResponse extends HostResponse {
@Param(description = "the total cpu allocated in Ghz")
private String cpuAllocated;

@SerializedName("loadAverage")
@Param(description = "the average cpu load the last minute")
private String loadAverage;

@SerializedName("memorytotalgb")
@Param(description = "the total cpu capacity in GiB")
private String memTotal;
Expand Down Expand Up @@ -117,6 +121,12 @@ public void setCpuUsed(final String cpuUsed, final Integer cpuNumber, final Long
}
}

public void setLoadAverage(final Double loadAverage) {
if (loadAverage != null) {
this.loadAverage = String.format("%.2f", loadAverage);
}
}

public void setCpuAllocated(final String cpuAllocated, final Integer cpuNumber, final Long cpuSpeed) {
if (cpuAllocated != null && cpuNumber != null && cpuSpeed != null) {
this.cpuAllocated = String.format("%.2f Ghz", Double.valueOf(cpuAllocated.replace("%", "")) * cpuNumber * cpuSpeed / (100.0 * 1000.0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public HostResponse newHostResponse(HostJoinVO host, EnumSet<HostDetails> detail
float cpuUtil = (float)hostStats.getCpuUtilization();
cpuUsed = decimalFormat.format(cpuUtil) + "%";
hostResponse.setCpuUsed(cpuUsed);
hostResponse.setCpuAverageLoad(hostStats.getLoadAverage());
hostResponse.setMemoryUsed((new Double(hostStats.getUsedMemory())).longValue());
hostResponse.setNetworkKbsRead((new Double(hostStats.getNetworkReadKBs())).longValue());
hostResponse.setNetworkKbsWrite((new Double(hostStats.getNetworkWriteKBs())).longValue());
Expand Down