Skip to content

Commit

Permalink
extended copyright year to 2019 - addressed commonets on formatting a…
Browse files Browse the repository at this point in the history
…nd unnecessary lines
  • Loading branch information
jbee committed Jan 21, 2019
1 parent 0063416 commit cbc03de
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 94 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2016-2018 Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) 2016-2019 Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
Expand Down Expand Up @@ -83,7 +83,7 @@ protected HealthCheckResultStatus decideOnStatusWithRatio(Double percentage) {
if (percentage >= options.getThresholdGood()) {
return HealthCheckResultStatus.GOOD;
}
return HealthCheckResultStatus.FINE;
return HealthCheckResultStatus.FINE;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2016-2018 Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) 2016-2019 Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2016-2018 Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) 2016-2019 Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
Expand Down Expand Up @@ -92,106 +92,105 @@ public String getDescription() {
@Override
public HealthCheckResult doCheck() {
if (isLinux()) {
return doCheckLinux();
return doCheckLinux();
}
return doCheckNonLinux();
return doCheckNonLinux();
}

private HealthCheckResult doCheckLinux() {
HealthCheckResult result = new HealthCheckResult();
long memTotal = 0;
try {
List<String> lines = Files.readAllLines(Paths.get("/proc/meminfo"), StandardCharsets.UTF_8);
if (lines.isEmpty()) {
return result;
}
long memAvailable = 0;
long memFree = 0;
long memActiveFile = 0;
long memInactiveFile = 0;
long memReclaimable= 0;
boolean memAvailableFound = false;

for (String line : lines) {
String[] parts = line.split("\\s+");

if (parts.length > 1) {
String part = parts[0];
if (MEMAVAILABLE.equals(part)) {
memAvailable = parseMemInfo(parts);
memAvailableFound = true;
}
if (MEMFREE.equals(part)) {
memFree = parseMemInfo(parts);
}
if (MEMTOTAL.equals(part)) {
memTotal = parseMemInfo(parts);
}
if (ACTIVEFILE.equals(part)) {
memActiveFile = parseMemInfo(parts);
}
if (INACTIVEFILE.equals(part)) {
memInactiveFile = parseMemInfo(parts);
}
if (RECLAIMABLE.equals(part)) {
memReclaimable = parseMemInfo(parts);
}
}
}

if (!memAvailableFound) {
memAvailable = memFree + memActiveFile + memInactiveFile + memReclaimable;
}

double usedPercentage = memTotal == 0L ? 0d :((double) (memTotal - memAvailable) / memTotal) * 100;

result.add(new HealthCheckResultEntry(decideOnStatusWithRatio(usedPercentage),
"Physical Memory Used: " + prettyPrintBytes(memTotal - memAvailable) + " - " +
"Total Physical Memory: " + prettyPrintBytes(memTotal) + " - " +
"Memory Used%: " + new DecimalFormat("#.00").format(usedPercentage) + "%"));

} catch (IOException exception) {
result.add(new HealthCheckResultEntry(HealthCheckResultStatus.CHECK_ERROR,
"Memory information cannot be read for retrieving physical memory usage values",
exception));
} catch (ArithmeticException exception) {
result.add(new HealthCheckResultEntry(HealthCheckResultStatus.CHECK_ERROR,
"Error occurred while calculating memory usage values. Total memory is " + memTotal,
exception));
}
return result;
}

private HealthCheckResult doCheckNonLinux() {
HealthCheckResult result = new HealthCheckResult();
try {
OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();
Long totalPhysicalMemSize = invokeMethodFor(osBean, "getTotalPhysicalMemorySize");
Long freePhysicalMemSize = invokeMethodFor(osBean, "getFreePhysicalMemorySize");

double usedPercentage = ((double) (totalPhysicalMemSize - freePhysicalMemSize) / totalPhysicalMemSize) *
100;

result.add(new HealthCheckResultEntry(decideOnStatusWithRatio(usedPercentage),
"Physical Memory Used: " + prettyPrintBytes((totalPhysicalMemSize - freePhysicalMemSize)) + " - " +
"Total Physical Memory: " + prettyPrintBytes(totalPhysicalMemSize) + " - " +
"Memory Used%: " + new DecimalFormat("#.00").format(usedPercentage) + "%"));

} catch (Exception exception) {
result.add(new HealthCheckResultEntry(HealthCheckResultStatus.CHECK_ERROR,
"Operating system methods cannot be invoked for retrieving physical memory usage values",
exception));
}
return result;
}
private HealthCheckResult doCheckLinux() {
HealthCheckResult result = new HealthCheckResult();
long memTotal = 0;
try {
List<String> lines = Files.readAllLines(Paths.get("/proc/meminfo"), StandardCharsets.UTF_8);
if (lines.isEmpty()) {
return result;
}
long memAvailable = 0;
long memFree = 0;
long memActiveFile = 0;
long memInactiveFile = 0;
long memReclaimable= 0;
boolean memAvailableFound = false;

for (String line : lines) {
String[] parts = line.split("\\s+");

if (parts.length > 1) {
String part = parts[0];
if (MEMAVAILABLE.equals(part)) {
memAvailable = parseMemInfo(parts);
memAvailableFound = true;
}
if (MEMFREE.equals(part)) {
memFree = parseMemInfo(parts);
}
if (MEMTOTAL.equals(part)) {
memTotal = parseMemInfo(parts);
}
if (ACTIVEFILE.equals(part)) {
memActiveFile = parseMemInfo(parts);
}
if (INACTIVEFILE.equals(part)) {
memInactiveFile = parseMemInfo(parts);
}
if (RECLAIMABLE.equals(part)) {
memReclaimable = parseMemInfo(parts);
}
}
}

if (!memAvailableFound) {
memAvailable = memFree + memActiveFile + memInactiveFile + memReclaimable;
}

double usedPercentage = memTotal == 0L ? 0d :((double) (memTotal - memAvailable) / memTotal) * 100;

result.add(new HealthCheckResultEntry(decideOnStatusWithRatio(usedPercentage),
"Physical Memory Used: " + prettyPrintBytes(memTotal - memAvailable) + " - " +
"Total Physical Memory: " + prettyPrintBytes(memTotal) + " - " +
"Memory Used%: " + new DecimalFormat("#.00").format(usedPercentage) + "%"));

} catch (IOException exception) {
result.add(new HealthCheckResultEntry(HealthCheckResultStatus.CHECK_ERROR,
"Memory information cannot be read for retrieving physical memory usage values",
exception));
} catch (ArithmeticException exception) {
result.add(new HealthCheckResultEntry(HealthCheckResultStatus.CHECK_ERROR,
"Error occurred while calculating memory usage values. Total memory is " + memTotal,
exception));
}
return result;
}

private HealthCheckResult doCheckNonLinux() {
HealthCheckResult result = new HealthCheckResult();
try {
OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();
Long totalPhysicalMemSize = invokeMethodFor(osBean, "getTotalPhysicalMemorySize");
Long freePhysicalMemSize = invokeMethodFor(osBean, "getFreePhysicalMemorySize");

double usedPercentage = ((double) (totalPhysicalMemSize - freePhysicalMemSize) / totalPhysicalMemSize) *
100;

result.add(new HealthCheckResultEntry(decideOnStatusWithRatio(usedPercentage),
"Physical Memory Used: " + prettyPrintBytes((totalPhysicalMemSize - freePhysicalMemSize)) + " - " +
"Total Physical Memory: " + prettyPrintBytes(totalPhysicalMemSize) + " - " +
"Memory Used%: " + new DecimalFormat("#.00").format(usedPercentage) + "%"));

} catch (Exception exception) {
result.add(new HealthCheckResultEntry(HealthCheckResultStatus.CHECK_ERROR,
"Operating system methods cannot be invoked for retrieving physical memory usage values",
exception));
}
return result;
}

private static boolean isLinux() {
String osName = System.getProperty("os.name");
return osName.startsWith("Linux") ||
osName.startsWith("FreeBSD") ||
osName.startsWith("OpenBSD") ||
osName.startsWith("gnu") ||
osName.startsWith("gnu/kfreebsd") ||
osName.startsWith("netbsd");
}

Expand Down

0 comments on commit cbc03de

Please sign in to comment.