新增获取磁盘主机的信息
This commit is contained in:
@@ -3,11 +3,12 @@ package com.mini.capi.job;
|
||||
|
||||
import com.mini.capi.biz.domain.*;
|
||||
import com.mini.capi.biz.service.*;
|
||||
import com.mini.capi.model.ApiResult;
|
||||
import com.mini.capi.utils.HostInfo;
|
||||
import com.mini.capi.utils.vDate;
|
||||
import com.mini.capi.utils.vId;
|
||||
import com.mini.capi.utils.vToken;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
@@ -37,66 +38,68 @@ public class taskEnable {
|
||||
private SysHostService sysHostService;
|
||||
|
||||
|
||||
@Scheduled(cron = "0 0/10 * * * ?")
|
||||
@GetMapping("/getTaskDockerDiskInfo")
|
||||
public void jobHostDisk() {
|
||||
List<DockerHost> dockerHosts = dockerHostService.list();
|
||||
for (DockerHost host : dockerHosts) {
|
||||
SshUser sshUser = sshUserService.getById(host.getUserId());
|
||||
SshInfo sshInfo = sshInfoService.getById(host.getHostId());
|
||||
try {
|
||||
/* 1. 采集实时数据 */
|
||||
HostInfo.Result r = HostInfo.collect(
|
||||
sshInfo.getHostIp(),
|
||||
Integer.parseInt(sshInfo.getHostPort()),
|
||||
sshUser.getCUsername(),
|
||||
sshUser.getCPassword());
|
||||
public ApiResult<?> jobHostDisk(String token) {
|
||||
if (vToken.isValidToken(token)) {
|
||||
List<DockerHost> dockerHosts = dockerHostService.list();
|
||||
for (DockerHost host : dockerHosts) {
|
||||
SshUser sshUser = sshUserService.getById(host.getUserId());
|
||||
SshInfo sshInfo = sshInfoService.getById(host.getHostId());
|
||||
try {
|
||||
/* 1. 采集实时数据 */
|
||||
HostInfo.Result r = HostInfo.collect(
|
||||
sshInfo.getHostIp(),
|
||||
Integer.parseInt(sshInfo.getHostPort()),
|
||||
sshUser.getCUsername(),
|
||||
sshUser.getCPassword());
|
||||
/* 2. 主机维度 saveOrUpdate */
|
||||
SysHost sysHost = r.host;
|
||||
sysHost.setSysHostId(host.getHostId());
|
||||
sysHost.setUpdateTime(vDate.getNow());
|
||||
sysHost.setDokerHostId(host.getDokerHostId());
|
||||
sysHostService.saveOrUpdate(sysHost);
|
||||
|
||||
/* 2. 主机维度 saveOrUpdate */
|
||||
SysHost sysHost = r.host;
|
||||
sysHost.setSysHostId(host.getHostId());
|
||||
sysHost.setUpdateTime(vDate.getNow());
|
||||
sysHost.setDokerHostId(host.getDokerHostId());
|
||||
sysHostService.saveOrUpdate(sysHost);
|
||||
/* 3. 处理磁盘:先查库做索引,再比对 */
|
||||
List<DiskMount> dbDisks = diskMountService.lambdaQuery()
|
||||
.eq(DiskMount::getSysHostId, host.getHostId())
|
||||
.list();
|
||||
Map<String, DiskMount> dbDiskMap = dbDisks.stream()
|
||||
.collect(Collectors.toMap(DiskMount::getMountPoint, Function.identity()));
|
||||
|
||||
/* 3. 处理磁盘:先查库做索引,再比对 */
|
||||
List<DiskMount> dbDisks = diskMountService.lambdaQuery()
|
||||
.eq(DiskMount::getSysHostId, host.getHostId())
|
||||
.list();
|
||||
Map<String, DiskMount> dbDiskMap = dbDisks.stream()
|
||||
.collect(Collectors.toMap(DiskMount::getMountPoint, Function.identity()));
|
||||
List<DiskMount> toSaveOrUpdate = new ArrayList<>();
|
||||
Set<String> liveMountPoint = new HashSet<>();
|
||||
|
||||
List<DiskMount> toSaveOrUpdate = new ArrayList<>();
|
||||
Set<String> liveMountPoint = new HashSet<>();
|
||||
|
||||
for (DiskMount d : r.disks) {
|
||||
liveMountPoint.add(d.getMountPoint());
|
||||
DiskMount exist = dbDiskMap.get(d.getMountPoint());
|
||||
if (exist != null) {
|
||||
// 存在 -> 更新
|
||||
d.setDiskMountId(exist.getDiskMountId());
|
||||
} else {
|
||||
// 不存在 -> 新增
|
||||
d.setDiskMountId(vId.getUid());
|
||||
for (DiskMount d : r.disks) {
|
||||
liveMountPoint.add(d.getMountPoint());
|
||||
DiskMount exist = dbDiskMap.get(d.getMountPoint());
|
||||
if (exist != null) {
|
||||
// 存在 -> 更新
|
||||
d.setDiskMountId(exist.getDiskMountId());
|
||||
} else {
|
||||
// 不存在 -> 新增
|
||||
d.setDiskMountId(vId.getUid());
|
||||
}
|
||||
d.setSysHostId(host.getHostId());
|
||||
d.setUpdateTime(vDate.getNow());
|
||||
toSaveOrUpdate.add(d);
|
||||
}
|
||||
d.setSysHostId(host.getHostId());
|
||||
d.setUpdateTime(vDate.getNow());
|
||||
toSaveOrUpdate.add(d);
|
||||
/* 4. 批量保存/更新 */
|
||||
diskMountService.saveOrUpdateBatch(toSaveOrUpdate);
|
||||
/* 5. 删除实时已消失的盘 */
|
||||
List<String> delIds = dbDisks.stream()
|
||||
.filter(d -> !liveMountPoint.contains(d.getMountPoint()))
|
||||
.map(DiskMount::getDiskMountId)
|
||||
.collect(Collectors.toList());
|
||||
if (!delIds.isEmpty()) {
|
||||
diskMountService.removeByIds(delIds);
|
||||
}
|
||||
return ApiResult.success();
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
/* 4. 批量保存/更新 */
|
||||
diskMountService.saveOrUpdateBatch(toSaveOrUpdate);
|
||||
/* 5. 删除实时已消失的盘 */
|
||||
List<String> delIds = dbDisks.stream()
|
||||
.filter(d -> !liveMountPoint.contains(d.getMountPoint()))
|
||||
.map(DiskMount::getDiskMountId)
|
||||
.collect(Collectors.toList());
|
||||
if (!delIds.isEmpty()) {
|
||||
diskMountService.removeByIds(delIds);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
return ApiResult.error();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user