Added warning message if OperatingSystemMXBean not available

Also corrected spacing after code review comments.

Fixes #11786
This commit is contained in:
Rob Bavey 2020-04-24 10:08:46 -04:00 committed by Robert Bavey
parent caa40036bc
commit 51431af405
2 changed files with 7 additions and 2 deletions

View file

@ -97,7 +97,7 @@ public final class LogstashJavaCompat {
* @return True if running on Java whose major version is greater than or equal to the * @return True if running on Java whose major version is greater than or equal to the
* specified version. * specified version.
*/ */
public static boolean isJavaAtLeast(int version){ public static boolean isJavaAtLeast(int version) {
final String value = System.getProperty("java.specification.version"); final String value = System.getProperty("java.specification.version");
final int actualVersion; final int actualVersion;
// Java specification version prior to Java 9 were of the format `1.X`, and after the format `X` // Java specification version prior to Java 9 were of the format `1.X`, and after the format `X`

View file

@ -28,12 +28,16 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.logstash.Logstash;
import org.logstash.LogstashJavaCompat; import org.logstash.LogstashJavaCompat;
public class ProcessMonitor { public class ProcessMonitor {
private static final OperatingSystemMXBean osMxBean = ManagementFactory.getOperatingSystemMXBean(); private static final OperatingSystemMXBean osMxBean = ManagementFactory.getOperatingSystemMXBean();
private static final Method CPU_LOAD_METHOD = getCpuLoadMethod(); private static final Method CPU_LOAD_METHOD = getCpuLoadMethod();
private static final Logger LOGGER = LogManager.getLogger(ProcessMonitor.class);
public static class Report { public static class Report {
private long memTotalVirtualInBytes = -1; private long memTotalVirtualInBytes = -1;
@ -97,7 +101,7 @@ public class ProcessMonitor {
// The method `getSystemCpuLoad` is deprecated in favour of `getCpuLoad` since JDK14 // The method `getSystemCpuLoad` is deprecated in favour of `getCpuLoad` since JDK14
// This method uses reflection to use the correct method depending on the version of // This method uses reflection to use the correct method depending on the version of
// the JDK being used. // the JDK being used.
private short getSystemCpuLoad(){ private short getSystemCpuLoad() {
if (CPU_LOAD_METHOD == null){ if (CPU_LOAD_METHOD == null){
return -1; return -1;
} }
@ -118,6 +122,7 @@ public class ProcessMonitor {
String methodName = (LogstashJavaCompat.isJavaAtLeast(14)) ? "getCpuLoad" : "getSystemCpuLoad"; String methodName = (LogstashJavaCompat.isJavaAtLeast(14)) ? "getCpuLoad" : "getSystemCpuLoad";
return Class.forName("com.sun.management.OperatingSystemMXBean").getMethod(methodName); return Class.forName("com.sun.management.OperatingSystemMXBean").getMethod(methodName);
} catch (ReflectiveOperationException e){ } catch (ReflectiveOperationException e){
LOGGER.warn("OperatingSystemMXBean CPU load method not available, CPU load will not be measured", e);
return null; return null;
} }
} }