Search in sources :

Example 1 with HealthCheckResult

use of fish.payara.nucleus.healthcheck.HealthCheckResult in project Payara by payara.

the class HeapMemoryUsageHealthCheck method doCheck.

@Override
public HealthCheckResult doCheck() {
    HealthCheckResult result = new HealthCheckResult();
    MemoryMXBean memBean = ManagementFactory.getMemoryMXBean();
    MemoryUsage heap = memBean.getHeapMemoryUsage();
    String heapValueText = String.format("heap: init: %s, used: %s, committed: %s, max.: %s", prettyPrintBytes(heap.getInit()), prettyPrintBytes(heap.getUsed()), prettyPrintBytes(heap.getCommitted()), prettyPrintBytes(heap.getMax()));
    Double percentage = calculatePercentage(heap);
    result.add(new HealthCheckResultEntry(decideOnStatusWithRatio(percentage), heapValueText + "heap%: " + percentage + "%"));
    return result;
}
Also used : MemoryMXBean(java.lang.management.MemoryMXBean) HealthCheckResult(fish.payara.nucleus.healthcheck.HealthCheckResult) MemoryUsage(java.lang.management.MemoryUsage) HealthCheckResultEntry(fish.payara.notification.healthcheck.HealthCheckResultEntry)

Example 2 with HealthCheckResult

use of fish.payara.nucleus.healthcheck.HealthCheckResult in project Payara by payara.

the class HoggingThreadsHealthCheck method doCheck.

@Override
public HealthCheckResult doCheck() {
    if (!getOptions().isEnabled()) {
        return null;
    }
    HealthCheckResult result = new HealthCheckResult();
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    if (!threadBean.isCurrentThreadCpuTimeSupported()) {
        result.add(new HealthCheckResultEntry(HealthCheckResultStatus.CHECK_ERROR, "JVM implementation or OS does" + " not support getting CPU times"));
        return result;
    }
    final long[] ids = threadBean.getAllThreadIds();
    for (long id : ids) {
        if (id == Thread.currentThread().getId())
            continue;
        final long c = threadBean.getThreadCpuTime(id);
        final long u = threadBean.getThreadUserTime(id);
        ThreadInfo threadInfo = threadBean.getThreadInfo(id);
        if (c == -1 || u == -1)
            continue;
        ThreadTimes times = threadTimes.get(id);
        if (times == null) {
            times = new ThreadTimes();
            times.setId(id);
            times.setName(threadInfo.getThreadName());
            times.setStartCpuTime(c);
            times.setEndCpuTime(c);
            times.setStartUserTime(u);
            times.setEndUserTime(u);
            threadTimes.put(id, times);
        } else {
            times.setStartCpuTime(times.getEndCpuTime());
            times.setStartUserTime(times.getEndUserTime());
            times.setEndCpuTime(c);
            times.setEndUserTime(u);
            long checkTime = getOptions().getUnit().toMillis(getOptions().getTime());
            long duration = times.getEndCpuTime() - times.getStartCpuTime();
            double percentage = ((double) (TimeUnit.NANOSECONDS.toMillis(duration)) / (double) (checkTime)) * 100;
            if (percentage > options.getThresholdPercentage()) {
                if (times.getRetryCount() == 0) {
                    times.setInitialStartCpuTime(System.nanoTime());
                    times.setInitialStartUserTime(System.nanoTime());
                }
                if (times.getRetryCount() >= options.getRetryCount()) {
                    result.add(new HealthCheckResultEntry(HealthCheckResultStatus.CRITICAL, "Thread with <id-name>: " + id + "-" + times.getName() + " is a hogging thread for the last " + prettyPrintDuration(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - times.getInitialStartCpuTime())) + "\n" + prettyPrintStackTrace(threadInfo.getStackTrace())));
                }
                times.setRetryCount(times.getRetryCount() + 1);
            }
        }
    }
    return result;
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo) HealthCheckResult(fish.payara.nucleus.healthcheck.HealthCheckResult) HealthCheckResultEntry(fish.payara.notification.healthcheck.HealthCheckResultEntry) ThreadTimes(fish.payara.nucleus.healthcheck.entity.ThreadTimes)

Example 3 with HealthCheckResult

use of fish.payara.nucleus.healthcheck.HealthCheckResult in project Payara by payara.

the class MachineMemoryUsageHealthCheck method doCheck.

@Override
public HealthCheckResult doCheck() {
    HealthCheckResult result = new HealthCheckResult();
    long memAvailable = 0;
    long memFree = 0;
    long memTotal = 0;
    long memActiveFile = 0;
    long memInactiveFile = 0;
    long memReclaimable = 0;
    boolean memAvailableFound = false;
    if (isLinux()) {
        try {
            List<String> lines = Files.readAllLines(Paths.get("/proc/meminfo"), StandardCharsets.UTF_8);
            if (lines.isEmpty()) {
                return result;
            }
            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 = ((double) 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));
        }
    } else {
        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;
}
Also used : DecimalFormat(java.text.DecimalFormat) HealthCheckResult(fish.payara.nucleus.healthcheck.HealthCheckResult) IOException(java.io.IOException) IOException(java.io.IOException) HealthCheckResultEntry(fish.payara.notification.healthcheck.HealthCheckResultEntry) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Example 4 with HealthCheckResult

use of fish.payara.nucleus.healthcheck.HealthCheckResult in project Payara by payara.

the class ConnectionPoolHealthCheck method doCheck.

@Override
public HealthCheckResult doCheck() {
    HealthCheckResult result = new HealthCheckResult();
    Collection<JdbcResource> allJdbcResources = getAllJdbcResources();
    for (JdbcResource resource : allJdbcResources) {
        ResourceInfo resourceInfo = ResourceUtil.getResourceInfo(resource);
        JdbcConnectionPool pool = JdbcResourcesUtil.createInstance().getJdbcConnectionPoolOfResource(resourceInfo);
        PoolInfo poolInfo = ResourceUtil.getPoolInfo(pool);
        if (getOptions().getPoolName() != null) {
            if (getOptions().getPoolName().equals(poolInfo.getName())) {
                evaluatePoolUsage(result, poolInfo);
            }
        } else {
            evaluatePoolUsage(result, poolInfo);
        }
    }
    return result;
}
Also used : ResourceInfo(org.glassfish.resourcebase.resources.api.ResourceInfo) JdbcResource(org.glassfish.jdbc.config.JdbcResource) JdbcConnectionPool(org.glassfish.jdbc.config.JdbcConnectionPool) HealthCheckResult(fish.payara.nucleus.healthcheck.HealthCheckResult) PoolInfo(org.glassfish.resourcebase.resources.api.PoolInfo)

Example 5 with HealthCheckResult

use of fish.payara.nucleus.healthcheck.HealthCheckResult in project Payara by payara.

the class CpuUsageHealthCheck method doCheck.

@Override
public HealthCheckResult doCheck() {
    if (!getOptions().isEnabled()) {
        return null;
    }
    HealthCheckResult result = new HealthCheckResult();
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    if (!threadBean.isCurrentThreadCpuTimeSupported()) {
        result.add(new HealthCheckResultEntry(HealthCheckResultStatus.CHECK_ERROR, "JVM implementation or OS does not support getting CPU times"));
        return result;
    }
    final long[] ids = threadBean.getAllThreadIds();
    for (long id : ids) {
        if (id == java.lang.Thread.currentThread().getId())
            continue;
        final long c = threadBean.getThreadCpuTime(id);
        final long u = threadBean.getThreadUserTime(id);
        if (c == -1 || u == -1)
            continue;
        ThreadTimes times = threadTimes.get(id);
        if (times == null) {
            times = new ThreadTimes();
            times.setId(id);
            times.setStartCpuTime(c);
            times.setEndCpuTime(c);
            times.setStartUserTime(u);
            times.setEndUserTime(u);
            threadTimes.put(id, times);
        } else {
            times.setEndCpuTime(c);
            times.setEndUserTime(u);
        }
    }
    long totalCpuTime = getTotalCpuTime();
    long time = System.nanoTime();
    double percentage = ((double) (totalCpuTime - totalTimeBefore) / (double) (time - timeBefore)) * 100;
    result.add(new HealthCheckResultEntry(decideOnStatusWithRatio(percentage), "CPU%: " + new DecimalFormat("#.00").format(percentage) + ", Time CPU used: " + prettyPrintDuration(TimeUnit.NANOSECONDS.toMillis(getTotalCpuTime() - totalTimeBefore))));
    totalTimeBefore = totalCpuTime;
    timeBefore = time;
    return result;
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) DecimalFormat(java.text.DecimalFormat) HealthCheckResult(fish.payara.nucleus.healthcheck.HealthCheckResult) HealthCheckResultEntry(fish.payara.notification.healthcheck.HealthCheckResultEntry) ThreadTimes(fish.payara.nucleus.healthcheck.entity.ThreadTimes)

Aggregations

HealthCheckResult (fish.payara.nucleus.healthcheck.HealthCheckResult)6 HealthCheckResultEntry (fish.payara.notification.healthcheck.HealthCheckResultEntry)5 ThreadMXBean (java.lang.management.ThreadMXBean)3 ThreadTimes (fish.payara.nucleus.healthcheck.entity.ThreadTimes)2 ThreadInfo (java.lang.management.ThreadInfo)2 DecimalFormat (java.text.DecimalFormat)2 IOException (java.io.IOException)1 MemoryMXBean (java.lang.management.MemoryMXBean)1 MemoryUsage (java.lang.management.MemoryUsage)1 OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)1 JdbcConnectionPool (org.glassfish.jdbc.config.JdbcConnectionPool)1 JdbcResource (org.glassfish.jdbc.config.JdbcResource)1 PoolInfo (org.glassfish.resourcebase.resources.api.PoolInfo)1 ResourceInfo (org.glassfish.resourcebase.resources.api.ResourceInfo)1