新增虚拟机信息同步到调度器
parent
27a4404a05
commit
fd55f7cfae
@ -0,0 +1,52 @@
|
|||||||
|
package com.docus.server.common;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.docus.server.api.scheduling.management.SchVirtualLogApi;
|
||||||
|
import com.docus.server.common.utils.SystemInfoUtils;
|
||||||
|
import com.docus.server.dto.scheduling.management.schvirtuallog.AddSchVirtualLogDTO;
|
||||||
|
import com.xxl.job.core.util.IpUtil;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class SchVirtualLogTask {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private SchVirtualLogApi schVirtualLogApi;
|
||||||
|
|
||||||
|
//定时任务
|
||||||
|
// 5 * * * * ? 在每分钟的5秒执行
|
||||||
|
@Scheduled(cron = "${docus.vm-task-cron}")
|
||||||
|
public void runTask() {
|
||||||
|
try {
|
||||||
|
log.info("收集虚拟机信息定时任务: 开始执行");
|
||||||
|
|
||||||
|
JSONObject info = SystemInfoUtils.getInfo();
|
||||||
|
|
||||||
|
AddSchVirtualLogDTO addSchVirtualLogDTO = new AddSchVirtualLogDTO();
|
||||||
|
addSchVirtualLogDTO.setCpuUtilization((String) ((JSONObject) info.get("cpuInfo")).get("cSys"));
|
||||||
|
addSchVirtualLogDTO.setMemoryTotal((String) ((JSONObject) info.get("memInfo")).get("total"));
|
||||||
|
addSchVirtualLogDTO.setMemoryAllowance((String) ((JSONObject) info.get("memInfo")).get("free"));
|
||||||
|
addSchVirtualLogDTO.setUplinkRate((String) ((JSONObject) info.get("networkInfo")).get("txPercent"));
|
||||||
|
addSchVirtualLogDTO.setDescendingRate((String) ((JSONObject) info.get("networkInfo")).get("rxPercent"));
|
||||||
|
addSchVirtualLogDTO.setIp(IpUtil.getIp());
|
||||||
|
addSchVirtualLogDTO.setClientTime(new Date());
|
||||||
|
addSchVirtualLogDTO.setDiskJson(info.get("sysFileInfo").toString());
|
||||||
|
schVirtualLogApi.add(addSchVirtualLogDTO);
|
||||||
|
System.out.println(info);
|
||||||
|
|
||||||
|
log.info("收集虚拟机信息定时任务: 执行完毕");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("收集虚拟机信息定时任务执行出错", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,162 @@
|
|||||||
|
package com.docus.server;
|
||||||
|
|
||||||
|
import org.apache.tools.tar.TarInputStream;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.zip.GZIPInputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @program: JavaCode
|
||||||
|
* @ClassName FileUtils
|
||||||
|
* @description:
|
||||||
|
* @author: ltcz99
|
||||||
|
* @create: 2023-04-16
|
||||||
|
* @Version 1.0
|
||||||
|
**/
|
||||||
|
public class FileUtils {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解压tar.gz文件到指定目录
|
||||||
|
*
|
||||||
|
* @param sourceDir 源文件夹
|
||||||
|
* @param destDir 解压后的目标文件夹
|
||||||
|
*/
|
||||||
|
public static void unTarGz(String sourceDir, String destDir) throws Exception {
|
||||||
|
File outFile = new File(sourceDir);
|
||||||
|
File[] files = outFile.listFiles();
|
||||||
|
try {
|
||||||
|
//创建输出目录
|
||||||
|
createDirectory(destDir, null);
|
||||||
|
TarInputStream tarIn;
|
||||||
|
int index = 1;
|
||||||
|
for (File file : files) {
|
||||||
|
if (file.getName().contains("tar.gz")) {
|
||||||
|
tarIn = new TarInputStream(new GZIPInputStream(
|
||||||
|
new BufferedInputStream(new FileInputStream(file))),
|
||||||
|
1024 * 2);
|
||||||
|
|
||||||
|
String outFileName = destDir + "/" + file.getName();
|
||||||
|
OutputStream out = new FileOutputStream(new File(outFileName));
|
||||||
|
int length = 0;
|
||||||
|
byte[] b = new byte[2048];
|
||||||
|
while ((length = tarIn.read(b)) != -1) {
|
||||||
|
out.write(b, 0, length);
|
||||||
|
}
|
||||||
|
out.close();
|
||||||
|
tarIn.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解压gz到指定的文件夹下面
|
||||||
|
*
|
||||||
|
* @param sourceDir
|
||||||
|
* @param destDir
|
||||||
|
*/
|
||||||
|
public static void unGzipFile(String sourceDir, String destDir) {
|
||||||
|
//创建输出目录
|
||||||
|
createDirectory(destDir, null);
|
||||||
|
File sourceFile = new File(sourceDir);
|
||||||
|
File[] files = sourceFile.listFiles();
|
||||||
|
try {
|
||||||
|
int index = 1;
|
||||||
|
for (File file : files) {
|
||||||
|
if (file.getName().contains("gz")) {
|
||||||
|
FileInputStream fin = new FileInputStream(file);
|
||||||
|
//建立gzip解压工作流
|
||||||
|
GZIPInputStream gzin = new GZIPInputStream(fin);
|
||||||
|
//建立解压文件输出流
|
||||||
|
File tmpFile = new File(destDir + "/" + index++ + ".log");
|
||||||
|
FileOutputStream fout = new FileOutputStream(tmpFile);
|
||||||
|
int length;
|
||||||
|
byte[] buf = new byte[2048];
|
||||||
|
while ((length = gzin.read(buf, 0, buf.length)) != -1) {
|
||||||
|
fout.write(buf, 0, length);
|
||||||
|
}
|
||||||
|
gzin.close();
|
||||||
|
fout.close();
|
||||||
|
fin.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception ex) {
|
||||||
|
System.err.println(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取文件到指定的文件夹下面
|
||||||
|
*
|
||||||
|
* @param sourceLogPath
|
||||||
|
* @param destLogPath
|
||||||
|
*/
|
||||||
|
public static void readFileToDestLogPath(String sourceLogPath, String destLogPath) {
|
||||||
|
File sourceFile = new File(sourceLogPath);
|
||||||
|
File[] files = sourceFile.listFiles();
|
||||||
|
for (File file : files) {
|
||||||
|
String fileName = destLogPath + "/" + file.getName();
|
||||||
|
File destFile = new File(fileName);
|
||||||
|
if (file.getName().contains("log") && !fileName.contains("gz")) {
|
||||||
|
try {
|
||||||
|
if (destFile.exists()) {
|
||||||
|
destFile.delete();
|
||||||
|
}
|
||||||
|
String logFile = sourceFile + "/" + file.getName();
|
||||||
|
FileInputStream fis = new FileInputStream(logFile);
|
||||||
|
FileOutputStream fos = new FileOutputStream(destFile);
|
||||||
|
BufferedInputStream bis = new BufferedInputStream(fis);
|
||||||
|
BufferedOutputStream bos = new BufferedOutputStream(fos);
|
||||||
|
int len = 0;
|
||||||
|
while ((len = bis.read()) != -1) {
|
||||||
|
bos.write(len);
|
||||||
|
}
|
||||||
|
bos.flush();
|
||||||
|
// 关闭资源
|
||||||
|
fis.close();
|
||||||
|
bis.close();
|
||||||
|
fos.close();
|
||||||
|
bos.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建目录
|
||||||
|
*
|
||||||
|
* @param outputDir
|
||||||
|
* @param subDir
|
||||||
|
*/
|
||||||
|
public static void createDirectory(String outputDir, String subDir) {
|
||||||
|
File file = new File(outputDir);
|
||||||
|
//子目录不为空
|
||||||
|
if (!(subDir == null || subDir.trim().equals(""))) {
|
||||||
|
file = new File(outputDir + "/" + subDir);
|
||||||
|
}
|
||||||
|
if (!file.exists()) {
|
||||||
|
if (!file.getParentFile().exists()) {
|
||||||
|
file.getParentFile().mkdirs();
|
||||||
|
}
|
||||||
|
file.mkdirs();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
String sourceDir = "/Users/ltcz99/Downloads/templog";
|
||||||
|
String destDir = "/Users/ltcz99/Downloads/unzip/";
|
||||||
|
//解压.gz文件到指定的文件件下面
|
||||||
|
unGzipFile(sourceDir, destDir);
|
||||||
|
// 解压tar.gz文件到指定的文件夹下面
|
||||||
|
unTarGz(sourceDir, destDir);
|
||||||
|
//读取特定的文件到指定的文件夹下面
|
||||||
|
readFileToDestLogPath(sourceDir, destDir);
|
||||||
|
}
|
||||||
|
}
|
@ -1,51 +1,36 @@
|
|||||||
package com.docus.server.api.scheduling.management;
|
//package com.docus.server.api.scheduling.management;
|
||||||
|
//
|
||||||
import io.swagger.annotations.Api;
|
//import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiImplicitParam;
|
//import io.swagger.annotations.ApiImplicitParam;
|
||||||
import io.swagger.annotations.ApiImplicitParams;
|
//import io.swagger.annotations.ApiImplicitParams;
|
||||||
import io.swagger.annotations.ApiOperation;
|
//import io.swagger.annotations.ApiOperation;
|
||||||
import org.springframework.cloud.openfeign.FeignClient;
|
//import org.springframework.cloud.openfeign.FeignClient;
|
||||||
import org.springframework.core.io.ByteArrayResource;
|
//import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.http.ResponseEntity;
|
//import org.springframework.web.multipart.MultipartFile;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
//
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
//import javax.servlet.http.HttpServletResponse;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
//
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
//
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
///**
|
||||||
|
// * 文件上传下载 API
|
||||||
import javax.servlet.http.HttpServletResponse;
|
// *
|
||||||
|
// * @author AutoGenerator
|
||||||
|
// * @since 2023-07-15
|
||||||
/**
|
// */
|
||||||
* 文件上传下载 API
|
//@Api(value = "通用文件上传下载接口", tags = "通用文件上传下载接口")
|
||||||
*
|
//@FeignClient(value = "collector-scheduling-management", contextId = "collector-scheduling-management.FileApi")
|
||||||
* @author AutoGenerator
|
//@RequestMapping("/sch/file")
|
||||||
* @since 2023-07-15
|
//public interface FileApi {
|
||||||
*/
|
//
|
||||||
@Api(value = "通用文件上传下载接口", tags = "通用文件上传下载接口")
|
// @ApiOperation("文件下载")
|
||||||
@FeignClient(value = "collector-scheduling-management", contextId = "collector-scheduling-management.FileApi")
|
// @GetMapping("/download")
|
||||||
@RequestMapping("/sch/file")
|
// void downloadFile(@RequestParam(value = "filePath") String filePath, HttpServletResponse response) throws Exception;
|
||||||
public interface FileApi {
|
//
|
||||||
|
// @ApiOperation("文件上传")
|
||||||
@ApiOperation("文件下载")
|
// @PostMapping("/upload")
|
||||||
@GetMapping("/download0")
|
// @ApiImplicitParams({
|
||||||
void downLoadFromUrl(@RequestParam(value = "urlStr") String urlStr,
|
// @ApiImplicitParam(name = "files", value = "文件", required = true, dataTypeClass = MultipartFile.class)
|
||||||
@RequestParam(value = "fileName") String fileName,
|
// })
|
||||||
@RequestParam(value = "savePath") String savePath) throws Exception;
|
// void uploadFile(@RequestPart MultipartFile[] files, String pathKey) throws Exception;
|
||||||
|
//
|
||||||
@ApiOperation("文件下载")
|
//}
|
||||||
@GetMapping("/download1")
|
|
||||||
ResponseEntity<ByteArrayResource> downloadFile(@RequestParam(value = "filePath") String filePath) throws Exception;
|
|
||||||
|
|
||||||
@ApiOperation("文件下载")
|
|
||||||
@GetMapping("/download2")
|
|
||||||
void downloadFile(@RequestParam(value = "filePath") String filePath, HttpServletResponse response) throws Exception;
|
|
||||||
|
|
||||||
@ApiOperation("文件上传")
|
|
||||||
@PostMapping("/upload")
|
|
||||||
@ApiImplicitParams({
|
|
||||||
@ApiImplicitParam(name = "files", value = "文件", required = true, dataTypeClass = MultipartFile.class)
|
|
||||||
})
|
|
||||||
void uploadFile(MultipartFile[] multipartFiles, String pathKey) throws Exception;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue