diff --git a/Linux服务器监听运维功能.md b/Linux服务器监听运维功能.md index 2e2fd1f..31d70cc 100644 --- a/Linux服务器监听运维功能.md +++ b/Linux服务器监听运维功能.md @@ -41,17 +41,21 @@ | POST | http://ip:port/occupationOfBasicResources/deployJarFile | | Content-Type | application/json;charset=UTF-8 | -| 请求参数 | | | | | -| -------- | ------------- | ------ | ---- | -------------------------------------------- | -| 参数项 | 名称 | 类型 | 必选 | 描述 | -| filePath | jar包保存位置 | string | 是 | jar包保存位置 | -| logPath | 日志输出位置 | string | 否 | 默认jar包相同位置名称和jar包文件名相同 | -| user | 用户信息 | string | 否 | 用户信息(权限用, 默认-1, 表示超级管理员权限) | +| 请求参数 | | | | | +| ----------- | ------------- | ------ | ---- | -------------------------------------------- | +| 参数项 | 名称 | 类型 | 必选 | 描述 | +| filePath | jar包保存位置 | string | 是 | jar包保存位置 | +| logPath | 日志输出位置 | string | 否 | 默认jar包相同位置名称和jar包文件名相同 | +| user | 用户信息 | string | 否 | 用户信息(权限用, 默认-1, 表示超级管理员权限) | +| startListen | 是否开启监听 | int | 否 | 传1 表示直接开始监听 | ``` 请求示例 { - "filePath": "/home/liuhaoyu/linuxTest/linuxTest-1.0-SNAPSHOT.jar" + "filePath": "/home/liuhaoyu/linuxTest/linuxTest-1.0-SNAPSHOT.jar", + "logPath": "/home/liuhaoyu/linuxTest/linuxTest.log", + "user": "1", + "startListen": "1" } ``` @@ -292,6 +296,36 @@ } ``` +## 2.5 获取所有正在监听中的日志 + +| 调用方式 | 接口地址 | +| ------------ | :-------------------------------------------- | +| POST | http://ip:port/logListener/getAllListeningLog | +| Content-Type | application/json;charset=UTF-8 | + +| 返回结果 | | | | +| -------- | ------------- | ------ | --------------------------------- | +| 参数项 | 名称 | 类型 | 描述 | +| code | 响应码 | int | 200为操作成功,其他code表示失败 | +| message | 提示信息 | string | 200为操作成功, 其他为对应错误信息 | +| data | 返回信息 | 数组 | | +| +logKey | 日志存在的key | string | 日志存在的key | +| +logPath | 日志位置 | string | 日志位置 | + +``` +响应示例 +{ + "code": 200, + "message": "操作成功", + "data": [ + { + "logKey": "linuxTest-1.0-SNAPSHOT-1575604486338.log|1575604486582", + "logPath": "/home/.../linuxTest/linuxTest-1.0-SNAPSHOT-1575604486338.log" + } + ] +} +``` + # 3. 监控器调度 diff --git a/src/main/java/com/objecteye/controller/LogFileListenerController.java b/src/main/java/com/objecteye/controller/LogFileListenerController.java index 2b68a03..7000254 100644 --- a/src/main/java/com/objecteye/controller/LogFileListenerController.java +++ b/src/main/java/com/objecteye/controller/LogFileListenerController.java @@ -1,5 +1,6 @@ package com.objecteye.controller; +import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.objecteye.common.CommonResult; import com.objecteye.service.ILogFileListenerService; @@ -38,14 +39,24 @@ public class LogFileListenerController { @ApiOperation("停止日志监控输出") @RequestMapping(value = "/stopLogListener", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) - public CommonResult stopLogListener(@RequestBody Map requestMap) throws IOException, InterruptedException { + public CommonResult stopLogListener(@RequestBody Map requestMap) { iLogFileListenerService.stopLogListener(requestMap); return CommonResult.success(null); } @ApiOperation("获取日志的输出内容") @RequestMapping(value = "/getLogOutput", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) - public CommonResult getLogOutput(@RequestBody Map requestMap) throws IOException, InterruptedException { + public CommonResult getLogOutput(@RequestBody Map requestMap) { return CommonResult.success(iLogFileListenerService.getLogOutput(requestMap)); } + + @ApiOperation("获取所有正在监听中的日志") + @RequestMapping(value = "/getAllListeningLog", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) + public CommonResult getAllListeningLog() { + JSONArray logKeyPathArr = iLogFileListenerService.getAllListeningLog(); + if (logKeyPathArr.size() == 0) { + return CommonResult.success(201, "No logs are listening now", null); + } + return CommonResult.success(logKeyPathArr); + } } diff --git a/src/main/java/com/objecteye/service/ILogFileListenerService.java b/src/main/java/com/objecteye/service/ILogFileListenerService.java index d7468bb..920b84c 100644 --- a/src/main/java/com/objecteye/service/ILogFileListenerService.java +++ b/src/main/java/com/objecteye/service/ILogFileListenerService.java @@ -1,5 +1,6 @@ package com.objecteye.service; +import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import java.io.IOException; @@ -37,5 +38,10 @@ public interface ILogFileListenerService { */ String getLogOutput(Map requestMap); - + /** + * 获取所有正在监听中的日志 + * + * @return 输出内容 + */ + JSONArray getAllListeningLog(); } diff --git a/src/main/java/com/objecteye/service/IOccupationOfBasicResourcesService.java b/src/main/java/com/objecteye/service/IOccupationOfBasicResourcesService.java index b8271ef..ec2ad28 100644 --- a/src/main/java/com/objecteye/service/IOccupationOfBasicResourcesService.java +++ b/src/main/java/com/objecteye/service/IOccupationOfBasicResourcesService.java @@ -3,7 +3,6 @@ package com.objecteye.service; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; -import java.util.List; import java.util.Map; /** @@ -41,12 +40,4 @@ public interface IOccupationOfBasicResourcesService { * @return 进行中的jar包信息 */ JSONObject getJpsInfo(); - - /** - * 执行Linux语句并获取返回值 - * - * @param cmd linux语句 - * @param outList 输出参数 - */ - void executeLinuxCmd(String cmd, List outList); } diff --git a/src/main/java/com/objecteye/service/impl/BackGroundServiceImpl.java b/src/main/java/com/objecteye/service/impl/BackGroundServiceImpl.java index 15159b9..c6d6cd0 100644 --- a/src/main/java/com/objecteye/service/impl/BackGroundServiceImpl.java +++ b/src/main/java/com/objecteye/service/impl/BackGroundServiceImpl.java @@ -7,6 +7,7 @@ import com.objecteye.entity.SyBasicResourceHistory; import com.objecteye.mapper.SyBasicResourceHistoryMapper; import com.objecteye.service.IBackGroundService; import com.objecteye.service.IOccupationOfBasicResourcesService; +import com.objecteye.utils.LinuxUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; @@ -181,7 +182,7 @@ public class BackGroundServiceImpl implements IBackGroundService { public void doGpuUtilization() { String cmd = "nvidia-smi -q -d UTILIZATION"; List outList = new ArrayList<>(); - iOccupationOfBasicResourcesService.executeLinuxCmd(cmd, outList); + LinuxUtils.executeLinuxCmd(cmd, outList); long occurrenceTime = System.currentTimeMillis(); if (outList.size() > 0) { boolean nextLevelRecord = false; diff --git a/src/main/java/com/objecteye/service/impl/LogFileListenerServiceImpl.java b/src/main/java/com/objecteye/service/impl/LogFileListenerServiceImpl.java index 4c7a7a2..a4181c6 100644 --- a/src/main/java/com/objecteye/service/impl/LogFileListenerServiceImpl.java +++ b/src/main/java/com/objecteye/service/impl/LogFileListenerServiceImpl.java @@ -1,5 +1,6 @@ package com.objecteye.service.impl; +import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.objecteye.common.GeneralContent; import com.objecteye.service.ILogFileListenerService; @@ -110,4 +111,22 @@ public class LogFileListenerServiceImpl implements ILogFileListenerService { } return null; } + + /** + * 获取所有正在监听中的日志 + * + * @return 输出内容 + */ + @Override + public JSONArray getAllListeningLog() { + Map logKeyPathMap = redisTemplate.opsForHash().entries(GeneralContent.REDIS_LOG_KEY_PATH); + JSONArray resultArr = new JSONArray(); + for (Map.Entry entry : logKeyPathMap.entrySet()) { + JSONObject tempObj = new JSONObject(); + tempObj.put("logKey", entry.getKey()); + tempObj.put("logPath", entry.getValue()); + resultArr.add(tempObj); + } + return resultArr; + } } diff --git a/src/main/java/com/objecteye/service/impl/OccupationOfBasicResourcesServiceImpl.java b/src/main/java/com/objecteye/service/impl/OccupationOfBasicResourcesServiceImpl.java index 82e24d9..022ad89 100644 --- a/src/main/java/com/objecteye/service/impl/OccupationOfBasicResourcesServiceImpl.java +++ b/src/main/java/com/objecteye/service/impl/OccupationOfBasicResourcesServiceImpl.java @@ -4,25 +4,24 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.objecteye.common.GeneralContent; +import com.objecteye.service.ILogFileListenerService; import com.objecteye.service.IOccupationOfBasicResourcesService; +import com.objecteye.utils.LinuxUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Objects; +import java.util.*; @Component @Slf4j public class OccupationOfBasicResourcesServiceImpl implements IOccupationOfBasicResourcesService { @Autowired private RedisTemplate redisTemplate; + @Autowired + private ILogFileListenerService iLogFileListenerService; /** * 获取指定module的输出 @@ -37,10 +36,12 @@ public class OccupationOfBasicResourcesServiceImpl implements IOccupationOfBasic if (GeneralContent.MODULE_MAP.containsKey(module)) { String cmd = System.getProperty("user.dir") + GeneralContent.MODULE_MAP.get(module); List outList = new ArrayList<>(); - executeLinuxCmd(cmd, outList); + LinuxUtils.executeLinuxCmd(cmd, outList); if (outList.size() > 0) { String outStr = String.join("", outList); - log.debug("OccupationOfBasicResourcesServiceImpl|getInfoByModule|outStr: {}", outStr); + if (log.isDebugEnabled()) { + log.debug("OccupationOfBasicResourcesServiceImpl|getInfoByModule|outStr: {}", outStr); + } Object object = JSON.parse(outStr); if (object instanceof JSONObject) { resultArr.add(object); @@ -66,10 +67,15 @@ public class OccupationOfBasicResourcesServiceImpl implements IOccupationOfBasic public JSONObject deployJarFile(Map requestMap) { String jarPath = (String) requestMap.get("filePath"); String logPath = (String) requestMap.get("logPath"); + // 是否直接开启监听 + Integer ifSetLogAndStart = (Integer) requestMap.get("startListen"); String owner = (String) requestMap.getOrDefault("user", "-1"); if (logPath == null || "".equals(logPath)) { - logPath = jarPath.substring(0, jarPath.lastIndexOf("/")) + jarPath.substring(jarPath.lastIndexOf("/")).replaceAll("jar", "log"); - log.debug("OccupationOfBasicResourcesServiceImpl|deployJarFile|logPath: {}", logPath); + logPath = jarPath.substring(0, jarPath.lastIndexOf("/")) + + jarPath.substring(jarPath.lastIndexOf("/")).replaceAll(".jar", "-") + System.currentTimeMillis() + ".log"; + if (log.isDebugEnabled()) { + log.debug("OccupationOfBasicResourcesServiceImpl|deployJarFile|logPath: {}", logPath); + } } JSONObject resultObj = new JSONObject(); @@ -80,7 +86,7 @@ public class OccupationOfBasicResourcesServiceImpl implements IOccupationOfBasic List outList = new ArrayList<>(); String cmd = "nohup java -jar " + jarPath + " > " + logPath + " 2>&1 &"; - executeLinuxCmd(cmd, outList); + LinuxUtils.executeLinuxCmd(cmd, outList); if (outList.size() > 0) { resultObj.put("error", outList.get(0)); @@ -89,13 +95,33 @@ public class OccupationOfBasicResourcesServiceImpl implements IOccupationOfBasic outList = new ArrayList<>(); String jpsCommand = "jps -l"; - executeLinuxCmd(jpsCommand, outList); + LinuxUtils.executeLinuxCmd(jpsCommand, outList); for (String jpsDetail : outList) { String taskId = jpsDetail.split(" ")[0].trim(); String command = jpsDetail.split(" ")[1].trim(); if (command.equals(jarPath)) { redisTemplate.opsForHash().put(GeneralContent.REDIS_TASK_ID_OWNER, taskId, owner); resultObj.put("taskId", taskId); + resultObj.put("logPath", logPath); + // 配置监听日志服务 子功能异常输出内部异常信息 + if (ifSetLogAndStart != null && 1 == ifSetLogAndStart) { + requestMap.put("filePath", logPath); + JSONObject logKeyObj = iLogFileListenerService.setLogMsg(requestMap); + if (logKeyObj.containsKey("error")) { + resultObj.put("innerError", logKeyObj.getString("error")); + log.error(logKeyObj.getString("error")); + } else { + String logKey = logKeyObj.getString("logKey"); + resultObj.put("logKey", logKey); + requestMap.put("logKey", logKey); + try { + iLogFileListenerService.startLogListener(requestMap); + } catch (IOException | InterruptedException e) { + resultObj.put("innerError", e.getMessage()); + log.error(logKeyObj.getString("error")); + } + } + } } } @@ -120,7 +146,7 @@ public class OccupationOfBasicResourcesServiceImpl implements IOccupationOfBasic if (Objects.equals(user, owner) || superPower) { String cmd = "kill -9 " + taskId; List outList = new ArrayList<>(); - executeLinuxCmd(cmd, outList); + LinuxUtils.executeLinuxCmd(cmd, outList); if (outList.size() > 0) { resultObj.put("error", outList); } @@ -155,10 +181,18 @@ public class OccupationOfBasicResourcesServiceImpl implements IOccupationOfBasic */ @Override public JSONObject getJpsInfo() { - String cmd = "jps"; + String cmd = "jps -l"; List outList = new ArrayList<>(); - executeLinuxCmd(cmd, outList); + LinuxUtils.executeLinuxCmd(cmd, outList); JSONObject resultObj = new JSONObject(); + Iterator iterator = outList.iterator(); + while (iterator.hasNext()) { + String jpsDetail = iterator.next(); + String[] jpsMsg = jpsDetail.split(" "); + if (!redisTemplate.opsForHash().hasKey(GeneralContent.REDIS_TASK_ID_OWNER, jpsMsg[0])) { + iterator.remove(); + } + } if (outList.size() > 0) { resultObj.put("process", outList); } else { @@ -166,43 +200,4 @@ public class OccupationOfBasicResourcesServiceImpl implements IOccupationOfBasic } return resultObj; } - - /** - * 执行Linux语句并获取返回值 - * - * @param cmd linux语句 - * @param outList 输出参数 - */ - @Override - public void executeLinuxCmd(String cmd, List outList) { - Runtime run = Runtime.getRuntime(); - try { - Process process; - if (cmd.endsWith(".sh")) { - ProcessBuilder builder = new ProcessBuilder("/bin/chmod", "755", cmd); - Process permission = builder.start(); - permission.waitFor(); - process = run.exec("sh " + cmd); - } else { - String[] cmdList = {"/bin/sh", "-c", cmd}; - process = run.exec(cmdList); - } - log.debug("OccupationOfBasicResourcesServiceImpl|executeLinuxCmd|cmd: {}", cmd); - String line; - BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(process.getInputStream())); - while ((line = stdoutReader.readLine()) != null) { - log.debug("OccupationOfBasicResourcesServiceImpl|executeLinuxCmd|line: {}", line); - if (line.contains("command not found") && line.contains("-bash")) { - log.error(line); - return; - } - outList.add(line); - } - process.waitFor(); - process.destroy(); - } catch (IOException | InterruptedException e) { - e.printStackTrace(); - log.error(e.getMessage()); - } - } } diff --git a/src/main/java/com/objecteye/utils/LinuxUtils.java b/src/main/java/com/objecteye/utils/LinuxUtils.java new file mode 100644 index 0000000..67aa543 --- /dev/null +++ b/src/main/java/com/objecteye/utils/LinuxUtils.java @@ -0,0 +1,55 @@ +package com.objecteye.utils; + +import lombok.extern.slf4j.Slf4j; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.List; + +@Slf4j +public class LinuxUtils { + + /** + * 执行Linux语句并获取返回值 + * + * @param cmd linux语句 + * @param outList 输出参数 + */ + public static void executeLinuxCmd(String cmd, List outList) { + Runtime run = Runtime.getRuntime(); + try { + Process process; + if (cmd.endsWith(".sh")) { + ProcessBuilder builder = new ProcessBuilder("/bin/chmod", "755", cmd); + Process permission = builder.start(); + permission.waitFor(); + process = run.exec("sh " + cmd); + } else { + String[] cmdList = {"/bin/sh", "-c", cmd}; + process = run.exec(cmdList); + } + if (log.isDebugEnabled()) { + log.debug("OccupationOfBasicResourcesServiceImpl|executeLinuxCmd|cmd: {}", cmd); + } + String line; + BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(process.getInputStream())); + while ((line = stdoutReader.readLine()) != null) { + if (log.isDebugEnabled()) { + log.debug("OccupationOfBasicResourcesServiceImpl|executeLinuxCmd|line: {}", line); + } + + if (line.contains("command not found") && line.contains("-bash")) { + log.error(line); + return; + } + outList.add(line); + } + process.waitFor(); + process.destroy(); + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + log.error(e.getMessage()); + } + } +} diff --git a/src/main/resources/com.objecteye.mapper/SyBasicResourceHistoryMapper.xml b/src/main/resources/com.objecteye.mapper/SyBasicResourceHistoryMapper.xml index 9089bbb..f43fbbb 100644 --- a/src/main/resources/com.objecteye.mapper/SyBasicResourceHistoryMapper.xml +++ b/src/main/resources/com.objecteye.mapper/SyBasicResourceHistoryMapper.xml @@ -42,18 +42,17 @@ from sy_basic_resource_history where occurrence_time <= #{time,jdbcType=BIGINT} ]]> - - insert into sy_basic_resource_history (id, module_id, occurrence_time, + + insert into sy_basic_resource_history (module_id, occurrence_time, occupancy) - values (#{id,jdbcType=VARCHAR}, #{moduleId,jdbcType=VARCHAR}, #{occurrenceTime,jdbcType=BIGINT}, + values (#{moduleId,jdbcType=VARCHAR}, #{occurrenceTime,jdbcType=BIGINT}, #{occupancy,jdbcType=DOUBLE}) - + insert into sy_basic_resource_history - - id, - module_id, @@ -65,9 +64,6 @@ - - #{id,jdbcType=VARCHAR}, - #{moduleId,jdbcType=VARCHAR},