Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
0005538: reuse getCpuUsage
  • Loading branch information
erilong committed Oct 18, 2022
1 parent 753b1d7 commit 9ddd027
Showing 1 changed file with 11 additions and 7 deletions.
Expand Up @@ -48,6 +48,13 @@ public String getName() {
@Override
public MonitorEvent check(Monitor monitor) {
MonitorEvent event = new MonitorEvent();
int cpuUsage = getCpuUsage();
event.setValue(cpuUsage);
event.setDetails(getNotificationMessage(cpuUsage, 0l, 0l));
return event;
}

public int getCpuUsage() {
int availableProcessors = osBean.getAvailableProcessors();
long prevUpTime = runtimeBean.getUptime();
long prevProcessCpuTime = getProcessCpuTime();
Expand All @@ -59,14 +66,11 @@ public MonitorEvent check(Monitor monitor) {
long processCpuTime = getProcessCpuTime();
long elapsedCpu = processCpuTime - prevProcessCpuTime;
long elapsedTime = upTime - prevUpTime;
long value = (long) (elapsedCpu / (elapsedTime * 1000f * availableProcessors));
if (value >= 100) {
event.setValue(100);
} else {
event.setValue(value);
int cpuUsage = (int) (elapsedCpu / (elapsedTime * 1000f * availableProcessors));
if (cpuUsage > 100) {
cpuUsage = 100;
}
event.setDetails(getNotificationMessage(value, 0l, 0l));
return event;
return cpuUsage;
}

protected long getProcessCpuTime() {
Expand Down

0 comments on commit 9ddd027

Please sign in to comment.