Skip to content

Commit

Permalink
prevent NPE when cpu values could not fetched or parsed correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
mcalmer committed Aug 11, 2023
1 parent 7eff71e commit cde1081
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,14 @@ private void updateServerCpu(Server server, HostJson host) {

cpu.setArch(ServerFactory.lookupCPUArchByName(host.getCpuArch()));
cpu.setMHz(Long.toString(Math.round(host.getCpuMhz())));
cpu.setNrCPU(host.getTotalCpuThreads().longValue());
cpu.setNrsocket(host.getTotalCpuSockets().longValue());
cpu.setNrCore(host.getTotalCpuCores().longValue() / host.getTotalCpuSockets().longValue());
cpu.setNrThread(host.getTotalCpuThreads().longValue() / host.getTotalCpuCores().longValue());
if (host.getTotalCpuSockets().longValue() > 0L) {
cpu.setNrsocket(host.getTotalCpuSockets().longValue());
cpu.setNrCore(host.getTotalCpuCores().longValue() / host.getTotalCpuSockets().longValue());
cpu.setNrThread(host.getTotalCpuThreads().longValue() / host.getTotalCpuCores().longValue());
cpu.setNrCPU(host.getTotalCpuThreads().longValue());
}
// else insufficient data for subscription matching. We keep totalSockets == null as matcher react on it
// set no CPU value in that case to prevent division by zero
cpu.setVendor(host.getCpuVendor());
cpu.setModel(host.getCpuDescription());

Expand Down
8 changes: 4 additions & 4 deletions java/code/src/com/suse/manager/gatherer/HostJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,26 @@ public String getHostIdentifier() {

/**
* Gets the total CPU socket count.
* @return the number of sockets
* @return the number of sockets - can be 0 if insufficient data were send
*/
public Integer getTotalCpuSockets() {
return totalCpuSockets;
return totalCpuSockets == null ? 0 : totalCpuSockets;
}

/**
* Gets the total CPU core count.
* @return the cpu cores
*/
public Integer getTotalCpuCores() {
return totalCpuCores;
return totalCpuCores == null ? 1 : totalCpuCores;
}

/**
* Gets the total CPU thread count.
* @return the CPU thread count
*/
public Integer getTotalCpuThreads() {
return totalCpuThreads;
return totalCpuThreads == null ? 1 : totalCpuThreads;
}

/**
Expand Down

0 comments on commit cde1081

Please sign in to comment.