完成客户端系统基本信息读取

This commit is contained in:
柏码の讲师 2023-11-03 23:52:16 +08:00
parent a5e4d42aaa
commit fcaad91939
8 changed files with 215 additions and 0 deletions

33
itbaima-monitor-client/.gitignore vendored Normal file
View File

@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/

View File

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>itbaima-monitor-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>my-project-backend</name>
<description>my-project-backend</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
<version>6.4.0</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,11 @@
package com.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MonitorClientApplication {
public static void main(String[] args) {
SpringApplication.run(MonitorClientApplication.class, args);
}
}

View File

@ -0,0 +1,36 @@
package com.test.config;
import com.test.task.MonitorJobBean;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import oshi.SystemInfo;
@Slf4j
@Configuration
public class QuartzConfiguration {
@Bean
public SystemInfo info(){
return new SystemInfo();
}
@Bean
public JobDetail jobDetailFactoryBean() {
return JobBuilder.newJob(MonitorJobBean.class)
.withIdentity("monitor-task")
.storeDurably()
.build();
}
@Bean
public Trigger cronTriggerFactoryBean(JobDetail detail) {
CronScheduleBuilder cron= CronScheduleBuilder.cronSchedule("*/1 * * * * ?");
return TriggerBuilder.newTrigger()
.forJob(detail)
.withIdentity("monitor-timer")
.withSchedule(cron)
.build();
}
}

View File

@ -0,0 +1,17 @@
package com.test.entity;
import lombok.Data;
import lombok.experimental.Accessors;
@Data
@Accessors(chain = true)
public class SystemData {
String osArch;
String osName;
String osVersion;
int osBit;
int cpuCore;
double memory;
double disk;
String ip;
}

View File

@ -0,0 +1,15 @@
package com.test.task;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;
@Component
public class MonitorJobBean extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
}
}

View File

@ -0,0 +1,5 @@
package com.test.util;
public interface DataGetter<T> {
T getData();
}

View File

@ -0,0 +1,57 @@
package com.test.util;
import com.test.entity.SystemData;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import oshi.SystemInfo;
import oshi.hardware.HWDiskStore;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.hardware.NetworkIF;
import oshi.software.os.OperatingSystem;
import java.io.IOException;
import java.net.NetworkInterface;
import java.util.Properties;
@Slf4j
@Service
public class SystemDataGetter implements DataGetter<SystemData>{
@Resource
SystemInfo info;
private final Properties properties = System.getProperties();
public SystemData getData() {
OperatingSystem os = info.getOperatingSystem();
HardwareAbstractionLayer hardware = info.getHardware();
double memory = hardware.getMemory().getTotal() / 1024.0 / 1024 / 1024;
double diskSize = hardware.getDiskStores().stream().mapToLong(HWDiskStore::getSize).sum() / 1024.0 / 1024 / 1024;
try {
String ip = null;
for (NetworkIF network : hardware.getNetworkIFs()) {
String[] iPv4addr = network.getIPv4addr();
NetworkInterface ni = network.queryNetworkInterface();
if(!ni.isLoopback() && !ni.isPointToPoint() && ni.isUp() && !ni.isVirtual()
&& (ni.getName().startsWith("eth") || ni.getName().startsWith("en"))
&& iPv4addr.length > 0) {
ip = network.getIPv4addr()[0];
break;
}
}
return new SystemData()
.setOsBit(os.getBitness())
.setOsArch(properties.getProperty("os.arch"))
.setOsVersion(os.getVersionInfo().getVersion())
.setOsName(os.getFamily())
.setCpuCore(hardware.getProcessor().getLogicalProcessorCount())
.setMemory(memory)
.setDisk(diskSize)
.setIp(ip);
} catch (IOException e) {
log.error("读取系统网络配置时出错", e);
return null;
}
}
}