Commit f1d1834fcf753cee1458b973556b3129992b9270

Authored by Liu Haoyu
1 parent 7d61f114

代码重构;

sy_basic_resource_history表主键设置自增;
添加查看所有正在被监听的日志文件的接口;
log.debug()添加判断当前是否是debug模式判断;
加强安全性处理;
Linux服务器监听运维功能.md
... ... @@ -41,17 +41,21 @@
41 41 | POST | http://ip:port/occupationOfBasicResources/deployJarFile |
42 42 | Content-Type | application/json;charset=UTF-8 |
43 43  
44   -| 请求参数 | | | | |
45   -| -------- | ------------- | ------ | ---- | -------------------------------------------- |
46   -| 参数项 | 名称 | 类型 | 必选 | 描述 |
47   -| filePath | jar包保存位置 | string | 是 | jar包保存位置 |
48   -| logPath | 日志输出位置 | string | 否 | 默认jar包相同位置名称和jar包文件名相同 |
49   -| user | 用户信息 | string | 否 | 用户信息(权限用, 默认-1, 表示超级管理员权限) |
  44 +| 请求参数 | | | | |
  45 +| ----------- | ------------- | ------ | ---- | -------------------------------------------- |
  46 +| 参数项 | 名称 | 类型 | 必选 | 描述 |
  47 +| filePath | jar包保存位置 | string | 是 | jar包保存位置 |
  48 +| logPath | 日志输出位置 | string | 否 | 默认jar包相同位置名称和jar包文件名相同 |
  49 +| user | 用户信息 | string | 否 | 用户信息(权限用, 默认-1, 表示超级管理员权限) |
  50 +| startListen | 是否开启监听 | int | 否 | 传1 表示直接开始监听 |
50 51  
51 52 ```
52 53 请求示例
53 54 {
54   - "filePath": "/home/liuhaoyu/linuxTest/linuxTest-1.0-SNAPSHOT.jar"
  55 + "filePath": "/home/liuhaoyu/linuxTest/linuxTest-1.0-SNAPSHOT.jar",
  56 + "logPath": "/home/liuhaoyu/linuxTest/linuxTest.log",
  57 + "user": "1",
  58 + "startListen": "1"
55 59 }
56 60 ```
57 61  
... ... @@ -292,6 +296,36 @@
292 296 }
293 297 ```
294 298  
  299 +## 2.5 获取所有正在监听中的日志
  300 +
  301 +| 调用方式 | 接口地址 |
  302 +| ------------ | :-------------------------------------------- |
  303 +| POST | http://ip:port/logListener/getAllListeningLog |
  304 +| Content-Type | application/json;charset=UTF-8 |
  305 +
  306 +| 返回结果 | | | |
  307 +| -------- | ------------- | ------ | --------------------------------- |
  308 +| 参数项 | 名称 | 类型 | 描述 |
  309 +| code | 响应码 | int | 200为操作成功,其他code表示失败 |
  310 +| message | 提示信息 | string | 200为操作成功, 其他为对应错误信息 |
  311 +| data | 返回信息 | 数组 | |
  312 +| +logKey | 日志存在的key | string | 日志存在的key |
  313 +| +logPath | 日志位置 | string | 日志位置 |
  314 +
  315 +```
  316 +响应示例
  317 +{
  318 + "code": 200,
  319 + "message": "操作成功",
  320 + "data": [
  321 + {
  322 + "logKey": "linuxTest-1.0-SNAPSHOT-1575604486338.log|1575604486582",
  323 + "logPath": "/home/.../linuxTest/linuxTest-1.0-SNAPSHOT-1575604486338.log"
  324 + }
  325 + ]
  326 +}
  327 +```
  328 +
295 329  
296 330 # 3. 监控器调度
297 331  
... ...
src/main/java/com/objecteye/controller/LogFileListenerController.java
1 1 package com.objecteye.controller;
2 2  
  3 +import com.alibaba.fastjson.JSONArray;
3 4 import com.alibaba.fastjson.JSONObject;
4 5 import com.objecteye.common.CommonResult;
5 6 import com.objecteye.service.ILogFileListenerService;
... ... @@ -38,14 +39,24 @@ public class LogFileListenerController {
38 39  
39 40 @ApiOperation("停止日志监控输出")
40 41 @RequestMapping(value = "/stopLogListener", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
41   - public CommonResult stopLogListener(@RequestBody Map<String, Object> requestMap) throws IOException, InterruptedException {
  42 + public CommonResult stopLogListener(@RequestBody Map<String, Object> requestMap) {
42 43 iLogFileListenerService.stopLogListener(requestMap);
43 44 return CommonResult.success(null);
44 45 }
45 46  
46 47 @ApiOperation("获取日志的输出内容")
47 48 @RequestMapping(value = "/getLogOutput", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
48   - public CommonResult getLogOutput(@RequestBody Map<String, Object> requestMap) throws IOException, InterruptedException {
  49 + public CommonResult getLogOutput(@RequestBody Map<String, Object> requestMap) {
49 50 return CommonResult.success(iLogFileListenerService.getLogOutput(requestMap));
50 51 }
  52 +
  53 + @ApiOperation("获取所有正在监听中的日志")
  54 + @RequestMapping(value = "/getAllListeningLog", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
  55 + public CommonResult getAllListeningLog() {
  56 + JSONArray logKeyPathArr = iLogFileListenerService.getAllListeningLog();
  57 + if (logKeyPathArr.size() == 0) {
  58 + return CommonResult.success(201, "No logs are listening now", null);
  59 + }
  60 + return CommonResult.success(logKeyPathArr);
  61 + }
51 62 }
... ...
src/main/java/com/objecteye/service/ILogFileListenerService.java
1 1 package com.objecteye.service;
2 2  
  3 +import com.alibaba.fastjson.JSONArray;
3 4 import com.alibaba.fastjson.JSONObject;
4 5  
5 6 import java.io.IOException;
... ... @@ -37,5 +38,10 @@ public interface ILogFileListenerService {
37 38 */
38 39 String getLogOutput(Map<String, Object> requestMap);
39 40  
40   -
  41 + /**
  42 + * 获取所有正在监听中的日志
  43 + *
  44 + * @return 输出内容
  45 + */
  46 + JSONArray getAllListeningLog();
41 47 }
... ...
src/main/java/com/objecteye/service/IOccupationOfBasicResourcesService.java
... ... @@ -3,7 +3,6 @@ package com.objecteye.service;
3 3 import com.alibaba.fastjson.JSONArray;
4 4 import com.alibaba.fastjson.JSONObject;
5 5  
6   -import java.util.List;
7 6 import java.util.Map;
8 7  
9 8 /**
... ... @@ -41,12 +40,4 @@ public interface IOccupationOfBasicResourcesService {
41 40 * @return 进行中的jar包信息
42 41 */
43 42 JSONObject getJpsInfo();
44   -
45   - /**
46   - * 执行Linux语句并获取返回值
47   - *
48   - * @param cmd linux语句
49   - * @param outList 输出参数
50   - */
51   - void executeLinuxCmd(String cmd, List<String> outList);
52 43 }
... ...
src/main/java/com/objecteye/service/impl/BackGroundServiceImpl.java
... ... @@ -7,6 +7,7 @@ import com.objecteye.entity.SyBasicResourceHistory;
7 7 import com.objecteye.mapper.SyBasicResourceHistoryMapper;
8 8 import com.objecteye.service.IBackGroundService;
9 9 import com.objecteye.service.IOccupationOfBasicResourcesService;
  10 +import com.objecteye.utils.LinuxUtils;
10 11 import lombok.extern.slf4j.Slf4j;
11 12 import org.springframework.beans.factory.annotation.Autowired;
12 13 import org.springframework.data.redis.core.RedisTemplate;
... ... @@ -181,7 +182,7 @@ public class BackGroundServiceImpl implements IBackGroundService {
181 182 public void doGpuUtilization() {
182 183 String cmd = "nvidia-smi -q -d UTILIZATION";
183 184 List<String> outList = new ArrayList<>();
184   - iOccupationOfBasicResourcesService.executeLinuxCmd(cmd, outList);
  185 + LinuxUtils.executeLinuxCmd(cmd, outList);
185 186 long occurrenceTime = System.currentTimeMillis();
186 187 if (outList.size() > 0) {
187 188 boolean nextLevelRecord = false;
... ...
src/main/java/com/objecteye/service/impl/LogFileListenerServiceImpl.java
1 1 package com.objecteye.service.impl;
2 2  
  3 +import com.alibaba.fastjson.JSONArray;
3 4 import com.alibaba.fastjson.JSONObject;
4 5 import com.objecteye.common.GeneralContent;
5 6 import com.objecteye.service.ILogFileListenerService;
... ... @@ -110,4 +111,22 @@ public class LogFileListenerServiceImpl implements ILogFileListenerService {
110 111 }
111 112 return null;
112 113 }
  114 +
  115 + /**
  116 + * 获取所有正在监听中的日志
  117 + *
  118 + * @return 输出内容
  119 + */
  120 + @Override
  121 + public JSONArray getAllListeningLog() {
  122 + Map<String, String> logKeyPathMap = redisTemplate.opsForHash().entries(GeneralContent.REDIS_LOG_KEY_PATH);
  123 + JSONArray resultArr = new JSONArray();
  124 + for (Map.Entry<String, String> entry : logKeyPathMap.entrySet()) {
  125 + JSONObject tempObj = new JSONObject();
  126 + tempObj.put("logKey", entry.getKey());
  127 + tempObj.put("logPath", entry.getValue());
  128 + resultArr.add(tempObj);
  129 + }
  130 + return resultArr;
  131 + }
113 132 }
... ...
src/main/java/com/objecteye/service/impl/OccupationOfBasicResourcesServiceImpl.java
... ... @@ -4,25 +4,24 @@ import com.alibaba.fastjson.JSON;
4 4 import com.alibaba.fastjson.JSONArray;
5 5 import com.alibaba.fastjson.JSONObject;
6 6 import com.objecteye.common.GeneralContent;
  7 +import com.objecteye.service.ILogFileListenerService;
7 8 import com.objecteye.service.IOccupationOfBasicResourcesService;
  9 +import com.objecteye.utils.LinuxUtils;
8 10 import lombok.extern.slf4j.Slf4j;
9 11 import org.springframework.beans.factory.annotation.Autowired;
10 12 import org.springframework.data.redis.core.RedisTemplate;
11 13 import org.springframework.stereotype.Component;
12 14  
13   -import java.io.BufferedReader;
14 15 import java.io.IOException;
15   -import java.io.InputStreamReader;
16   -import java.util.ArrayList;
17   -import java.util.List;
18   -import java.util.Map;
19   -import java.util.Objects;
  16 +import java.util.*;
20 17  
21 18 @Component
22 19 @Slf4j
23 20 public class OccupationOfBasicResourcesServiceImpl implements IOccupationOfBasicResourcesService {
24 21 @Autowired
25 22 private RedisTemplate redisTemplate;
  23 + @Autowired
  24 + private ILogFileListenerService iLogFileListenerService;
26 25  
27 26 /**
28 27 * 获取指定module的输出
... ... @@ -37,10 +36,12 @@ public class OccupationOfBasicResourcesServiceImpl implements IOccupationOfBasic
37 36 if (GeneralContent.MODULE_MAP.containsKey(module)) {
38 37 String cmd = System.getProperty("user.dir") + GeneralContent.MODULE_MAP.get(module);
39 38 List<String> outList = new ArrayList<>();
40   - executeLinuxCmd(cmd, outList);
  39 + LinuxUtils.executeLinuxCmd(cmd, outList);
41 40 if (outList.size() > 0) {
42 41 String outStr = String.join("", outList);
43   - log.debug("OccupationOfBasicResourcesServiceImpl|getInfoByModule|outStr: {}", outStr);
  42 + if (log.isDebugEnabled()) {
  43 + log.debug("OccupationOfBasicResourcesServiceImpl|getInfoByModule|outStr: {}", outStr);
  44 + }
44 45 Object object = JSON.parse(outStr);
45 46 if (object instanceof JSONObject) {
46 47 resultArr.add(object);
... ... @@ -66,10 +67,15 @@ public class OccupationOfBasicResourcesServiceImpl implements IOccupationOfBasic
66 67 public JSONObject deployJarFile(Map<String, Object> requestMap) {
67 68 String jarPath = (String) requestMap.get("filePath");
68 69 String logPath = (String) requestMap.get("logPath");
  70 + // 是否直接开启监听
  71 + Integer ifSetLogAndStart = (Integer) requestMap.get("startListen");
69 72 String owner = (String) requestMap.getOrDefault("user", "-1");
70 73 if (logPath == null || "".equals(logPath)) {
71   - logPath = jarPath.substring(0, jarPath.lastIndexOf("/")) + jarPath.substring(jarPath.lastIndexOf("/")).replaceAll("jar", "log");
72   - log.debug("OccupationOfBasicResourcesServiceImpl|deployJarFile|logPath: {}", logPath);
  74 + logPath = jarPath.substring(0, jarPath.lastIndexOf("/"))
  75 + + jarPath.substring(jarPath.lastIndexOf("/")).replaceAll(".jar", "-") + System.currentTimeMillis() + ".log";
  76 + if (log.isDebugEnabled()) {
  77 + log.debug("OccupationOfBasicResourcesServiceImpl|deployJarFile|logPath: {}", logPath);
  78 + }
73 79 }
74 80  
75 81 JSONObject resultObj = new JSONObject();
... ... @@ -80,7 +86,7 @@ public class OccupationOfBasicResourcesServiceImpl implements IOccupationOfBasic
80 86  
81 87 List<String> outList = new ArrayList<>();
82 88 String cmd = "nohup java -jar " + jarPath + " > " + logPath + " 2>&1 &";
83   - executeLinuxCmd(cmd, outList);
  89 + LinuxUtils.executeLinuxCmd(cmd, outList);
84 90  
85 91 if (outList.size() > 0) {
86 92 resultObj.put("error", outList.get(0));
... ... @@ -89,13 +95,33 @@ public class OccupationOfBasicResourcesServiceImpl implements IOccupationOfBasic
89 95  
90 96 outList = new ArrayList<>();
91 97 String jpsCommand = "jps -l";
92   - executeLinuxCmd(jpsCommand, outList);
  98 + LinuxUtils.executeLinuxCmd(jpsCommand, outList);
93 99 for (String jpsDetail : outList) {
94 100 String taskId = jpsDetail.split(" ")[0].trim();
95 101 String command = jpsDetail.split(" ")[1].trim();
96 102 if (command.equals(jarPath)) {
97 103 redisTemplate.opsForHash().put(GeneralContent.REDIS_TASK_ID_OWNER, taskId, owner);
98 104 resultObj.put("taskId", taskId);
  105 + resultObj.put("logPath", logPath);
  106 + // 配置监听日志服务 子功能异常输出内部异常信息
  107 + if (ifSetLogAndStart != null && 1 == ifSetLogAndStart) {
  108 + requestMap.put("filePath", logPath);
  109 + JSONObject logKeyObj = iLogFileListenerService.setLogMsg(requestMap);
  110 + if (logKeyObj.containsKey("error")) {
  111 + resultObj.put("innerError", logKeyObj.getString("error"));
  112 + log.error(logKeyObj.getString("error"));
  113 + } else {
  114 + String logKey = logKeyObj.getString("logKey");
  115 + resultObj.put("logKey", logKey);
  116 + requestMap.put("logKey", logKey);
  117 + try {
  118 + iLogFileListenerService.startLogListener(requestMap);
  119 + } catch (IOException | InterruptedException e) {
  120 + resultObj.put("innerError", e.getMessage());
  121 + log.error(logKeyObj.getString("error"));
  122 + }
  123 + }
  124 + }
99 125 }
100 126 }
101 127  
... ... @@ -120,7 +146,7 @@ public class OccupationOfBasicResourcesServiceImpl implements IOccupationOfBasic
120 146 if (Objects.equals(user, owner) || superPower) {
121 147 String cmd = "kill -9 " + taskId;
122 148 List<String> outList = new ArrayList<>();
123   - executeLinuxCmd(cmd, outList);
  149 + LinuxUtils.executeLinuxCmd(cmd, outList);
124 150 if (outList.size() > 0) {
125 151 resultObj.put("error", outList);
126 152 }
... ... @@ -155,10 +181,18 @@ public class OccupationOfBasicResourcesServiceImpl implements IOccupationOfBasic
155 181 */
156 182 @Override
157 183 public JSONObject getJpsInfo() {
158   - String cmd = "jps";
  184 + String cmd = "jps -l";
159 185 List<String> outList = new ArrayList<>();
160   - executeLinuxCmd(cmd, outList);
  186 + LinuxUtils.executeLinuxCmd(cmd, outList);
161 187 JSONObject resultObj = new JSONObject();
  188 + Iterator<String> iterator = outList.iterator();
  189 + while (iterator.hasNext()) {
  190 + String jpsDetail = iterator.next();
  191 + String[] jpsMsg = jpsDetail.split(" ");
  192 + if (!redisTemplate.opsForHash().hasKey(GeneralContent.REDIS_TASK_ID_OWNER, jpsMsg[0])) {
  193 + iterator.remove();
  194 + }
  195 + }
162 196 if (outList.size() > 0) {
163 197 resultObj.put("process", outList);
164 198 } else {
... ... @@ -166,43 +200,4 @@ public class OccupationOfBasicResourcesServiceImpl implements IOccupationOfBasic
166 200 }
167 201 return resultObj;
168 202 }
169   -
170   - /**
171   - * 执行Linux语句并获取返回值
172   - *
173   - * @param cmd linux语句
174   - * @param outList 输出参数
175   - */
176   - @Override
177   - public void executeLinuxCmd(String cmd, List<String> outList) {
178   - Runtime run = Runtime.getRuntime();
179   - try {
180   - Process process;
181   - if (cmd.endsWith(".sh")) {
182   - ProcessBuilder builder = new ProcessBuilder("/bin/chmod", "755", cmd);
183   - Process permission = builder.start();
184   - permission.waitFor();
185   - process = run.exec("sh " + cmd);
186   - } else {
187   - String[] cmdList = {"/bin/sh", "-c", cmd};
188   - process = run.exec(cmdList);
189   - }
190   - log.debug("OccupationOfBasicResourcesServiceImpl|executeLinuxCmd|cmd: {}", cmd);
191   - String line;
192   - BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
193   - while ((line = stdoutReader.readLine()) != null) {
194   - log.debug("OccupationOfBasicResourcesServiceImpl|executeLinuxCmd|line: {}", line);
195   - if (line.contains("command not found") && line.contains("-bash")) {
196   - log.error(line);
197   - return;
198   - }
199   - outList.add(line);
200   - }
201   - process.waitFor();
202   - process.destroy();
203   - } catch (IOException | InterruptedException e) {
204   - e.printStackTrace();
205   - log.error(e.getMessage());
206   - }
207   - }
208 203 }
... ...
src/main/java/com/objecteye/utils/LinuxUtils.java 0 → 100644
  1 +package com.objecteye.utils;
  2 +
  3 +import lombok.extern.slf4j.Slf4j;
  4 +
  5 +import java.io.BufferedReader;
  6 +import java.io.IOException;
  7 +import java.io.InputStreamReader;
  8 +import java.util.List;
  9 +
  10 +@Slf4j
  11 +public class LinuxUtils {
  12 +
  13 + /**
  14 + * 执行Linux语句并获取返回值
  15 + *
  16 + * @param cmd linux语句
  17 + * @param outList 输出参数
  18 + */
  19 + public static void executeLinuxCmd(String cmd, List<String> outList) {
  20 + Runtime run = Runtime.getRuntime();
  21 + try {
  22 + Process process;
  23 + if (cmd.endsWith(".sh")) {
  24 + ProcessBuilder builder = new ProcessBuilder("/bin/chmod", "755", cmd);
  25 + Process permission = builder.start();
  26 + permission.waitFor();
  27 + process = run.exec("sh " + cmd);
  28 + } else {
  29 + String[] cmdList = {"/bin/sh", "-c", cmd};
  30 + process = run.exec(cmdList);
  31 + }
  32 + if (log.isDebugEnabled()) {
  33 + log.debug("OccupationOfBasicResourcesServiceImpl|executeLinuxCmd|cmd: {}", cmd);
  34 + }
  35 + String line;
  36 + BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
  37 + while ((line = stdoutReader.readLine()) != null) {
  38 + if (log.isDebugEnabled()) {
  39 + log.debug("OccupationOfBasicResourcesServiceImpl|executeLinuxCmd|line: {}", line);
  40 + }
  41 +
  42 + if (line.contains("command not found") && line.contains("-bash")) {
  43 + log.error(line);
  44 + return;
  45 + }
  46 + outList.add(line);
  47 + }
  48 + process.waitFor();
  49 + process.destroy();
  50 + } catch (IOException | InterruptedException e) {
  51 + e.printStackTrace();
  52 + log.error(e.getMessage());
  53 + }
  54 + }
  55 +}
... ...
src/main/resources/com.objecteye.mapper/SyBasicResourceHistoryMapper.xml
... ... @@ -42,18 +42,17 @@
42 42 from sy_basic_resource_history
43 43 where occurrence_time <= #{time,jdbcType=BIGINT}
44 44 ]]></delete>
45   - <insert id="insert" parameterType="com.objecteye.entity.SyBasicResourceHistory">
46   - insert into sy_basic_resource_history (id, module_id, occurrence_time,
  45 + <insert id="insert" parameterType="com.objecteye.entity.SyBasicResourceHistory" useGeneratedKeys="true"
  46 + keyProperty="id">
  47 + insert into sy_basic_resource_history (module_id, occurrence_time,
47 48 occupancy)
48   - values (#{id,jdbcType=VARCHAR}, #{moduleId,jdbcType=VARCHAR}, #{occurrenceTime,jdbcType=BIGINT},
  49 + values (#{moduleId,jdbcType=VARCHAR}, #{occurrenceTime,jdbcType=BIGINT},
49 50 #{occupancy,jdbcType=DOUBLE})
50 51 </insert>
51   - <insert id="insertSelective" parameterType="com.objecteye.entity.SyBasicResourceHistory">
  52 + <insert id="insertSelective" parameterType="com.objecteye.entity.SyBasicResourceHistory" useGeneratedKeys="true"
  53 + keyProperty="id">
52 54 insert into sy_basic_resource_history
53 55 <trim prefix="(" suffix=")" suffixOverrides=",">
54   - <if test="id != null">
55   - id,
56   - </if>
57 56 <if test="moduleId != null">
58 57 module_id,
59 58 </if>
... ... @@ -65,9 +64,6 @@
65 64 </if>
66 65 </trim>
67 66 <trim prefix="values (" suffix=")" suffixOverrides=",">
68   - <if test="id != null">
69   - #{id,jdbcType=VARCHAR},
70   - </if>
71 67 <if test="moduleId != null">
72 68 #{moduleId,jdbcType=VARCHAR},
73 69 </if>
... ...