Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
preface
This section describes the code for the ServerController part of the server monitoring module in ruoyi-Vue’s Ruoyi-admin module. This interface is mainly used to show the server
code
public AjaxResult getInfo(a) throws Exception {
Server server = new Server();
server.copyTo();
return AjaxResult.success(server);
}
Copy the code
All the code is in the copyTo() method, which calls Oshi-core
<dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
<version>${oshi.version}</version>
</dependency>
Copy the code
Oshi-core is a third-party JAR package for obtaining hardware and system information. Based on JNA
Get CPU information
SystemInfo si = new SystemInfo();
HardwareAbstractionLayer hal = si.getHardware();
setCpuInfo(hal.getProcessor());
private void setCpuInfo(CentralProcessor processor)
{
/ / CPU information
long[] prevTicks = processor.getSystemCpuLoadTicks();
Util.sleep(OSHI_WAIT_SECOND);
long[] ticks = processor.getSystemCpuLoadTicks();
long nice = ticks[TickType.NICE.getIndex()] - prevTicks[TickType.NICE.getIndex()];
long irq = ticks[TickType.IRQ.getIndex()] - prevTicks[TickType.IRQ.getIndex()];
long softirq = ticks[TickType.SOFTIRQ.getIndex()] - prevTicks[TickType.SOFTIRQ.getIndex()];
long steal = ticks[TickType.STEAL.getIndex()] - prevTicks[TickType.STEAL.getIndex()];
long cSys = ticks[TickType.SYSTEM.getIndex()] - prevTicks[TickType.SYSTEM.getIndex()];
long user = ticks[TickType.USER.getIndex()] - prevTicks[TickType.USER.getIndex()];
long iowait = ticks[TickType.IOWAIT.getIndex()] - prevTicks[TickType.IOWAIT.getIndex()];
long idle = ticks[TickType.IDLE.getIndex()] - prevTicks[TickType.IDLE.getIndex()];
long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal;
cpu.setCpuNum(processor.getLogicalProcessorCount());
cpu.setTotal(totalCpu);
cpu.setSys(cSys);
cpu.setUsed(user);
cpu.setWait(iowait);
cpu.setFree(idle);
}
Copy the code
IDLE
The time when the CPU is idle and the system has no outstanding disk I/O requests
IOWAIT
The amount of time the CPU is idle and the system has an outstanding disk I/O request, unavailable on Windows and MacOS is 0.
IRQ (Hardware Interrupts)
The CPU time used to service hardware interrupts, which is not available in MacOS, is always 0
NICE
CPU utilization that occurs when executing at the user level with nice priority
Nice is a static priority value. It is unique to Unix-like systems and will not be changed by the kernel until it is reset.
SOFTIRQ
The CPU time used to service software outages
STEAL
Time allocated by the operating system to other users in the system
SYSTEM
System-level CPU utilization
USER
User-level run-time CPU utilization
When using the Linux Top command we can see the above data, the site directly displayed on the page to facilitate the monitoring of server data.
Setting Server Information
private void setSysInfo(a) {
Properties props = System.getProperties();
sys.setComputerName(IpUtils.getHostName());
sys.setComputerIp(IpUtils.getHostIp());
sys.setOsName(props.getProperty("os.name"));
sys.setOsArch(props.getProperty("os.arch"));
sys.setUserDir(props.getProperty("user.dir"));
}
Copy the code
Obtain Java VM information
/** * Set the Java virtual machine */
private void setJvmInfo(a) throws UnknownHostException
{
Properties props = System.getProperties();
jvm.setTotal(Runtime.getRuntime().totalMemory());
jvm.setMax(Runtime.getRuntime().maxMemory());
jvm.setFree(Runtime.getRuntime().freeMemory());
jvm.setVersion(props.getProperty("java.version"));
jvm.setHome(props.getProperty("java.home"));
}
Copy the code