2021-08-18

This commit is contained in:
2021-08-18 12:46:22 +09:00
parent b0d3615378
commit 84bdef283c
13 changed files with 649 additions and 1 deletions

View File

@@ -14,5 +14,5 @@ include(
"thread", "hibernate", "jdbc-sqlite", "thread", "hibernate", "jdbc-sqlite",
"xml", "jackson", "jsoup", "markdown", "network", "httpd", "xml", "jackson", "jsoup", "markdown", "network", "httpd",
"swing", "java-fx", "properties", "swing", "java-fx", "properties",
"mustache", "thymeleaf", "locale", "quartz" "mustache", "thymeleaf", "locale", "quartz", "sysinfo"
) )

15
sysinfo/build.gradle.kts Normal file
View File

@@ -0,0 +1,15 @@
/*
* Examples for Java
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
plugins {
id("elex-java")
}
dependencies {
}

20
sysinfo/logback.xml Normal file
View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Examples for Java
~
~ Copyright (c) 2021. Elex. All Rights Reserved.
~ https://www.elex-project.com/
-->
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="TRACE">
<appender-ref ref="CONSOLE" />
</root>
</configuration>

View File

@@ -0,0 +1,35 @@
/*
* Examples for Java
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import lombok.extern.slf4j.Slf4j;
import java.lang.management.ClassLoadingMXBean;
import java.lang.management.ManagementFactory;
@Slf4j
public class ClassLoadingInfo {
private final ClassLoadingMXBean bean;
private ClassLoadingInfo() {
bean = ManagementFactory.getClassLoadingMXBean();
}
public static ClassLoadingInfo get() {
return new ClassLoadingInfo();
}
public long getTotalLoadedClassCount() {
return bean.getTotalLoadedClassCount();
}
public int getLoadedClassCount() {
return bean.getLoadedClassCount();
}
}

View File

@@ -0,0 +1,74 @@
/*
* Examples for Java
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.lang.management.ManagementFactory;
@Slf4j
public class CpuInfo {
private static final int availableProcessors = OSInfo.get().getAvailableProcessors();
private static long lastSystemTime = 0;
private static long lastProcessCpuTime = 0;
private double systemCpuLoad, processCpuLoad;
private long processCpuTime;
private CpuInfo() {
com.sun.management.OperatingSystemMXBean mxBean =
(com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
this.systemCpuLoad = mxBean.getSystemCpuLoad();
this.processCpuLoad = mxBean.getProcessCpuLoad();
this.processCpuTime = mxBean.getProcessCpuTime();
}
@NotNull
@Contract(" -> new")
public static CpuInfo get() {
return new CpuInfo();
}
public static synchronized double getCpuUsage() {
if (lastSystemTime == 0) {
baselineCounters();
return 0;
}
long systemTime = System.nanoTime();
long processCpuTime = 0;
processCpuTime = new CpuInfo().getProcessCpuTime();
double cpuUsage = (double) (processCpuTime - lastProcessCpuTime) / (systemTime - lastSystemTime);
lastSystemTime = systemTime;
lastProcessCpuTime = processCpuTime;
return cpuUsage / availableProcessors;
}
private static void baselineCounters() {
lastSystemTime = System.nanoTime();
lastProcessCpuTime = new CpuInfo().getProcessCpuTime();
}
public double getSystemCpuLoad() {
return systemCpuLoad;
}
public double getProcessCpuLoad() {
return processCpuLoad;
}
public long getProcessCpuTime() {
return processCpuTime;
}
}

View File

@@ -0,0 +1,53 @@
/*
* Examples for Java
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.io.File;
@Slf4j
public class DiskInfo {
private final long totalSpace, freeSpace, usableSpace;
private DiskInfo(@NotNull File root) {
this.totalSpace = root.getTotalSpace();
this.freeSpace = root.getFreeSpace();
this.usableSpace = root.getUsableSpace();
}
@NotNull
@Contract("_ -> new")
public static DiskInfo get(@NotNull File root) {
return new DiskInfo(root);
}
/**
* @return bytes
*/
public long getTotalSpace() {
return totalSpace;
}
/**
* @return bytes
*/
public long getFreeSpace() {
return freeSpace;
}
/**
* @return bytes
*/
public long getUsableSpace() {
return usableSpace;
}
}

View File

@@ -0,0 +1,79 @@
/*
* Examples for Java
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
@Slf4j
public class MemoryInfo {
private final long heapMax, heapUsed;
private final long nonHeapMax, nonHeapUsed;
private final long totalPhysicalMemory, freePhysicalMemory;
private final long totalSwapMemory, freeSwapMemory;
private MemoryInfo() {
MemoryMXBean memory = ManagementFactory.getMemoryMXBean();
MemoryUsage usage = memory.getHeapMemoryUsage();
this.heapMax = usage.getMax();
this.heapUsed = usage.getUsed();
usage = memory.getNonHeapMemoryUsage();
this.nonHeapMax = usage.getMax();
this.nonHeapUsed = usage.getUsed();
com.sun.management.OperatingSystemMXBean osBean =
(com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
this.totalPhysicalMemory = osBean.getTotalPhysicalMemorySize();
this.freePhysicalMemory = osBean.getFreePhysicalMemorySize();
this.totalSwapMemory = osBean.getTotalSwapSpaceSize();
this.freeSwapMemory = osBean.getFreeSwapSpaceSize();
}
@NotNull
@Contract(" -> new")
public static MemoryInfo get() {
return new MemoryInfo();
}
public long getHeapMax() {
return heapMax;
}
public long getHeapUsed() {
return heapUsed;
}
public long getNonHeapMax() {
return nonHeapMax;
}
public long getNonHeapUsed() {
return nonHeapUsed;
}
public long getTotalPhysicalMemory() {
return totalPhysicalMemory;
}
public long getFreePhysicalMemory() {
return freePhysicalMemory;
}
public long getTotalSwapMemory() {
return totalSwapMemory;
}
public long getFreeSwapMemory() {
return freeSwapMemory;
}
}

View File

@@ -0,0 +1,96 @@
/*
* Examples for Java
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.lang.management.ManagementFactory;
@Slf4j
public class OSInfo {
private String name, arch, version;
private int availableProcessors;
private double systemLoadAverage;
private long totalPhysicalMemorySize, freePhysicalMemorySize;
private long totalSwapSpaceSize, freeSwapSpaceSize;
private long vmMemorySize;
private double processCpuLoad;
private long processCpuTime;
private OSInfo() {
com.sun.management.OperatingSystemMXBean bean =
(com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
this.name = bean.getName();
this.arch = bean.getArch();
this.version = bean.getVersion();
this.availableProcessors = bean.getAvailableProcessors();
this.systemLoadAverage = bean.getSystemLoadAverage();
this.processCpuLoad = bean.getProcessCpuLoad();
this.processCpuTime = bean.getProcessCpuTime();
this.totalPhysicalMemorySize = bean.getTotalPhysicalMemorySize();
this.freePhysicalMemorySize = bean.getFreePhysicalMemorySize();
this.totalSwapSpaceSize = bean.getTotalSwapSpaceSize();
this.freeSwapSpaceSize = bean.getFreeSwapSpaceSize();
this.vmMemorySize = bean.getCommittedVirtualMemorySize();
}
@NotNull
@Contract(" -> new")
public static OSInfo get() {
return new OSInfo();
}
public String getName() {
return name;
}
public String getArch() {
return arch;
}
public String getVersion() {
return version;
}
public int getAvailableProcessors() {
return availableProcessors;
}
public double getSystemLoadAverage() {
return systemLoadAverage;
}
public long getTotalPhysicalMemorySize() {
return totalPhysicalMemorySize;
}
public long getFreePhysicalMemorySize() {
return freePhysicalMemorySize;
}
public long getTotalSwapSpaceSize() {
return totalSwapSpaceSize;
}
public long getFreeSwapSpaceSize() {
return freeSwapSpaceSize;
}
public long getVmMemorySize() {
return vmMemorySize;
}
public double getProcessCpuLoad() {
return processCpuLoad;
}
public long getProcessCpuTime() {
return processCpuTime;
}
}

View File

@@ -0,0 +1,86 @@
/*
* Examples for Java
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
@Slf4j
public class RuntimeInfo {
private String name, specName, specVendor, specVersion;
private String vmName, vmVendor, vmVersion;
private long startTime, upTime;
private RuntimeInfo() {
RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
this.name = runtime.getName();
this.specName = runtime.getSpecName();
this.specVendor = runtime.getSpecVendor();
this.specVersion = runtime.getSpecVersion();
this.vmName = runtime.getVmName();
this.vmVendor = runtime.getVmVendor();
this.vmVersion = runtime.getVmVersion();
this.startTime = runtime.getStartTime();
this.upTime = runtime.getUptime();
try {
/* Requires 10+ */
//info.pid = runtime.getPid();
} catch (Throwable e) {
//info.pid = 0L;
}
}
//private long pid; /* Requires 10+ */
@NotNull
@Contract(" -> new")
public static RuntimeInfo get() {
return new RuntimeInfo();
}
public String getName() {
return name;
}
public String getSpecName() {
return specName;
}
public String getSpecVendor() {
return specVendor;
}
public String getSpecVersion() {
return specVersion;
}
public String getVmName() {
return vmName;
}
public String getVmVendor() {
return vmVendor;
}
public String getVmVersion() {
return vmVersion;
}
public long getStartTime() {
return startTime;
}
public long getUpTime() {
return upTime;
}
}

View File

@@ -0,0 +1,28 @@
/*
* Examples for Java
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import lombok.extern.slf4j.Slf4j;
import java.lang.management.CompilationMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryManagerMXBean;
import java.lang.management.ThreadMXBean;
@Slf4j
class SysInfo {
private SysInfo() {
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
//threadMXBean.
MemoryManagerMXBean memoryManagerMXBean = (MemoryManagerMXBean) ManagementFactory.getMemoryManagerMXBeans();
//memoryManagerMXBean.
CompilationMXBean compilationMXBean = ManagementFactory.getCompilationMXBean();
//compilationMXBean.
}
}

View File

@@ -0,0 +1,41 @@
/*
* Examples for Java
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import com.elex_project.abraxas.Exez;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
public class SysInfoUtils {
private SysInfoUtils() {
}
@NotNull
public static Map<String, String> getOSInfo() {
HashMap<String, String> map = new HashMap<>();
try {
List<String> result = Exez.exec("cat /etc/*-release");
for (String line : result) {
String[] parted = line.split("=");
map.put(parted[0], parted[1]);
}
} catch (IOException | InterruptedException e) {
log.error("Oops!", e);
}
return map;
}
}

View File

@@ -0,0 +1,49 @@
/*
* Examples for Java
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import com.sun.management.ThreadMXBean;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.lang.management.ManagementFactory;
@Slf4j
public class ThreadInfo {
private final ThreadMXBean bean;
private ThreadInfo() {
bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
}
@Contract(" -> new")
public static @NotNull ThreadInfo get() {
return new ThreadInfo();
}
public long[] getThreadIds() {
return bean.getAllThreadIds();
}
public long getThreadCpuTime(long id) {
return bean.getThreadCpuTime(id);
}
public long getThreadUserTime(long id) {
return bean.getThreadUserTime(id);
}
public int getThreadCount() {
return bean.getThreadCount();
}
public long getAllocatedBytes(long id) {
return bean.getThreadAllocatedBytes(id);
}
}

View File

@@ -0,0 +1,72 @@
/*
* Examples for Java
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
@Slf4j
public class SysInfoTest {
/*@Test
public void testProcessorInfo(){
ProcessorInfo info = HardwareInfo.getProcessorInfo();
L.i("getModelName", info.getModelName());
L.i("getStepping", info.getStepping());
L.i("getCacheSize", info.getCacheSize());
L.i("getFamily", info.getFamily());
L.i("getVendorId", info.getVendorId());
L.i("getNumCores", info.getNumCores());
L.i("getMhz", info.getMhz());
L.i("getTemperature", info.getTemperature());
}
@Test
public void testBiosInfo(){
BiosInfo info = HardwareInfo.getBiosInfo();
L.i("getManufacturer", info.getManufacturer());
L.i("getVersion", info.getVersion());
L.i("getDate", info.getDate());
}
@Test
public void testMotherboardInfo(){
MotherboardInfo info = HardwareInfo.getMotherboardInfo();
L.i("getManufacturer", info.getManufacturer());
L.i("getVersion", info.getVersion());
L.i("Name", info.getName());
}
@Test
public void testOSInfo(){
OSInfo info = HardwareInfo.getOSInfo();
L.i("getName", info.getName());
L.i("getManufacturer", info.getManufacturer());
L.i("getVersion", info.getVersion());
L.i("getLastBootTime", info.getLastBootTime());
}
@Test
public void testMemoryInfo(){
MemoryInfo info = HardwareInfo.getMemoryInfo();
L.i("getTotalMemory", info.getTotalMemory());
L.i("getAvailableMemory", info.getAvailableMemory());
L.i("getFreeMemory", info.getFreeMemory());
}
@Test
public void testNetworkInfo(){
NetworkInfo info = HardwareInfo.getNetworkInfo();
NetworkInterfaceInfo networkInterface = info.getNetworkInterfaces().get(0);
L.i("getName", networkInterface.getName());
L.i("getType", networkInterface.getType());
L.i("getIpv4", networkInterface.getIpv4());
L.i("getIpv6", networkInterface.getIpv6());
L.i("getReceivedBytes", networkInterface.getReceivedBytes());
L.i("getTransmittedBytes", networkInterface.getTransmittedBytes());
}*/
}