Commit 20fe6d5213f95f72bfa4bee5a0544e492ed99b34
1 parent
4ba5b210
jar包运维功能测试;
添加GPU监控功能(暂时只支持NVIDIA显卡);
Showing
8 changed files
with
149 additions
and
26 deletions
src/main/java/com/objecteye/controller/BackGroundController.java
@@ -2,12 +2,14 @@ package com.objecteye.controller; | @@ -2,12 +2,14 @@ package com.objecteye.controller; | ||
2 | 2 | ||
3 | import com.alibaba.fastjson.JSONObject; | 3 | import com.alibaba.fastjson.JSONObject; |
4 | import com.objecteye.common.CommonResult; | 4 | import com.objecteye.common.CommonResult; |
5 | +import com.objecteye.entity.SyBasicResourceHistory; | ||
5 | import com.objecteye.service.IBackGroundService; | 6 | import com.objecteye.service.IBackGroundService; |
6 | import io.swagger.annotations.Api; | 7 | import io.swagger.annotations.Api; |
7 | import io.swagger.annotations.ApiOperation; | 8 | import io.swagger.annotations.ApiOperation; |
8 | import org.springframework.beans.factory.annotation.Autowired; | 9 | import org.springframework.beans.factory.annotation.Autowired; |
9 | import org.springframework.web.bind.annotation.*; | 10 | import org.springframework.web.bind.annotation.*; |
10 | 11 | ||
12 | +import java.util.List; | ||
11 | import java.util.Map; | 13 | import java.util.Map; |
12 | 14 | ||
13 | @RestController | 15 | @RestController |
@@ -47,4 +49,14 @@ public class BackGroundController { | @@ -47,4 +49,14 @@ public class BackGroundController { | ||
47 | } | 49 | } |
48 | return CommonResult.success(resultObj); | 50 | return CommonResult.success(resultObj); |
49 | } | 51 | } |
52 | + | ||
53 | + @ApiOperation("获取module对应的内容") | ||
54 | + @RequestMapping(value = "/getModuleInfo", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) | ||
55 | + public CommonResult getModuleInfo(@RequestBody Map<String, Object> requestMap) { | ||
56 | + List<SyBasicResourceHistory> syBasicResourceHistories = iBackGroundService.getModuleInfo(requestMap); | ||
57 | + if (syBasicResourceHistories == null || syBasicResourceHistories.size() == 0) { | ||
58 | + return CommonResult.success(201, "没有找到符合要求的数据", null); | ||
59 | + } | ||
60 | + return CommonResult.success(syBasicResourceHistories); | ||
61 | + } | ||
50 | } | 62 | } |
src/main/java/com/objecteye/controller/OccupationOfBasicResourcesController.java
@@ -49,4 +49,14 @@ public class OccupationOfBasicResourcesController { | @@ -49,4 +49,14 @@ public class OccupationOfBasicResourcesController { | ||
49 | } | 49 | } |
50 | return CommonResult.success(resultObj); | 50 | return CommonResult.success(resultObj); |
51 | } | 51 | } |
52 | + | ||
53 | + @ApiOperation("停止jar包进程") | ||
54 | + @RequestMapping(value = "/killJarTask", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) | ||
55 | + public CommonResult killJarTask(@RequestBody Map<String, Object> requestMap) { | ||
56 | + JSONObject resultObj = iOccupationOfBasicResourcesService.killJarTask(requestMap); | ||
57 | + if (resultObj.containsKey("error")) { | ||
58 | + return CommonResult.success(201, resultObj.getString("error"), null); | ||
59 | + } | ||
60 | + return CommonResult.success(resultObj); | ||
61 | + } | ||
52 | } | 62 | } |
src/main/java/com/objecteye/mapper/SyBasicResourceHistoryMapper.java
@@ -2,6 +2,9 @@ package com.objecteye.mapper; | @@ -2,6 +2,9 @@ package com.objecteye.mapper; | ||
2 | 2 | ||
3 | import com.objecteye.entity.SyBasicResourceHistory; | 3 | import com.objecteye.entity.SyBasicResourceHistory; |
4 | 4 | ||
5 | +import java.util.List; | ||
6 | +import java.util.Map; | ||
7 | + | ||
5 | public interface SyBasicResourceHistoryMapper { | 8 | public interface SyBasicResourceHistoryMapper { |
6 | int deleteByPrimaryKey(String id); | 9 | int deleteByPrimaryKey(String id); |
7 | 10 | ||
@@ -22,4 +25,12 @@ public interface SyBasicResourceHistoryMapper { | @@ -22,4 +25,12 @@ public interface SyBasicResourceHistoryMapper { | ||
22 | * @return 操作数量 | 25 | * @return 操作数量 |
23 | */ | 26 | */ |
24 | int deleteLteTime(long time); | 27 | int deleteLteTime(long time); |
28 | + | ||
29 | + /** | ||
30 | + * 获取过滤后的指定module的信息 | ||
31 | + * | ||
32 | + * @param requestMap 请求参数 | ||
33 | + * @return 结果集 | ||
34 | + */ | ||
35 | + List<SyBasicResourceHistory> getModuleInfoQueryByCondition(Map<String, Object> requestMap); | ||
25 | } | 36 | } |
26 | \ No newline at end of file | 37 | \ No newline at end of file |
src/main/java/com/objecteye/service/IBackGroundService.java
1 | package com.objecteye.service; | 1 | package com.objecteye.service; |
2 | 2 | ||
3 | import com.alibaba.fastjson.JSONObject; | 3 | import com.alibaba.fastjson.JSONObject; |
4 | +import com.objecteye.entity.SyBasicResourceHistory; | ||
4 | 5 | ||
6 | +import java.util.List; | ||
5 | import java.util.Map; | 7 | import java.util.Map; |
6 | 8 | ||
7 | public interface IBackGroundService { | 9 | public interface IBackGroundService { |
@@ -38,4 +40,12 @@ public interface IBackGroundService { | @@ -38,4 +40,12 @@ public interface IBackGroundService { | ||
38 | * @return 返回信息 | 40 | * @return 返回信息 |
39 | */ | 41 | */ |
40 | JSONObject removeWorkingListener(Map<String, Object> requestMap); | 42 | JSONObject removeWorkingListener(Map<String, Object> requestMap); |
43 | + | ||
44 | + /** | ||
45 | + * 获取module对应的内容 | ||
46 | + * | ||
47 | + * @param requestMap 请求参数 | ||
48 | + * @return 结果集 | ||
49 | + */ | ||
50 | + List<SyBasicResourceHistory> getModuleInfo(Map<String, Object> requestMap); | ||
41 | } | 51 | } |
src/main/java/com/objecteye/service/IOccupationOfBasicResourcesService.java
@@ -3,6 +3,7 @@ package com.objecteye.service; | @@ -3,6 +3,7 @@ package com.objecteye.service; | ||
3 | import com.alibaba.fastjson.JSONArray; | 3 | import com.alibaba.fastjson.JSONArray; |
4 | import com.alibaba.fastjson.JSONObject; | 4 | import com.alibaba.fastjson.JSONObject; |
5 | 5 | ||
6 | +import java.util.List; | ||
6 | import java.util.Map; | 7 | import java.util.Map; |
7 | 8 | ||
8 | /** | 9 | /** |
@@ -40,4 +41,12 @@ public interface IOccupationOfBasicResourcesService { | @@ -40,4 +41,12 @@ public interface IOccupationOfBasicResourcesService { | ||
40 | * @return 进行中的jar包信息 | 41 | * @return 进行中的jar包信息 |
41 | */ | 42 | */ |
42 | JSONObject getJpsInfo(); | 43 | JSONObject getJpsInfo(); |
44 | + | ||
45 | + /** | ||
46 | + * 执行Linux语句并获取返回值 | ||
47 | + * | ||
48 | + * @param cmd linux语句 | ||
49 | + * @param outList 输出参数 | ||
50 | + */ | ||
51 | + void executeLinuxCmd(String cmd, List<String> outList); | ||
43 | } | 52 | } |
src/main/java/com/objecteye/service/impl/BackGroundServiceImpl.java
@@ -49,6 +49,9 @@ public class BackGroundServiceImpl implements IBackGroundService { | @@ -49,6 +49,9 @@ public class BackGroundServiceImpl implements IBackGroundService { | ||
49 | case "memory_info": | 49 | case "memory_info": |
50 | doMemoryInfo(requestMap); | 50 | doMemoryInfo(requestMap); |
51 | break; | 51 | break; |
52 | + case "GPU": | ||
53 | + doGpuUtilization(); | ||
54 | + break; | ||
52 | } | 55 | } |
53 | } | 56 | } |
54 | } | 57 | } |
@@ -126,6 +129,17 @@ public class BackGroundServiceImpl implements IBackGroundService { | @@ -126,6 +129,17 @@ public class BackGroundServiceImpl implements IBackGroundService { | ||
126 | } | 129 | } |
127 | 130 | ||
128 | /** | 131 | /** |
132 | + * 获取module对应的内容 | ||
133 | + * | ||
134 | + * @param requestMap 请求参数 | ||
135 | + * @return 结果集 | ||
136 | + */ | ||
137 | + @Override | ||
138 | + public List<SyBasicResourceHistory> getModuleInfo(Map<String, Object> requestMap) { | ||
139 | + return syBasicResourceHistoryMapper.getModuleInfoQueryByCondition(requestMap); | ||
140 | + } | ||
141 | + | ||
142 | + /** | ||
129 | * cpu信息记录 | 143 | * cpu信息记录 |
130 | * | 144 | * |
131 | * @param requestMap 请求参数 | 145 | * @param requestMap 请求参数 |
@@ -159,4 +173,35 @@ public class BackGroundServiceImpl implements IBackGroundService { | @@ -159,4 +173,35 @@ public class BackGroundServiceImpl implements IBackGroundService { | ||
159 | syBasicResourceHistoryMapper.insertSelective(syBasicResourceHistory); | 173 | syBasicResourceHistoryMapper.insertSelective(syBasicResourceHistory); |
160 | } | 174 | } |
161 | } | 175 | } |
176 | + | ||
177 | + /** | ||
178 | + * NVIDIA GPU信息记录(多卡就会有多条记录) | ||
179 | + */ | ||
180 | + @Async("taskExecutor") | ||
181 | + public void doGpuUtilization() { | ||
182 | + String cmd = "nvidia-smi -q -d UTILIZATION"; | ||
183 | + List<String> outList = new ArrayList<>(); | ||
184 | + iOccupationOfBasicResourcesService.executeLinuxCmd(cmd, outList); | ||
185 | + long occurrenceTime = System.currentTimeMillis(); | ||
186 | + if (outList.size() > 0) { | ||
187 | + boolean nextLevelRecord = false; | ||
188 | + String lastLine = ""; | ||
189 | + for (String s : outList) { | ||
190 | + String nowLine = s.trim(); | ||
191 | + if (nextLevelRecord) { | ||
192 | + String moduleId = "GPU" + lastLine.split(" ")[1].trim(); | ||
193 | + String occupancy = nowLine.split(":")[1].replaceAll(" ", "").replaceAll("%", ""); | ||
194 | + SyBasicResourceHistory syBasicResourceHistory = new SyBasicResourceHistory(UUID.randomUUID().toString(), moduleId, | ||
195 | + occurrenceTime, Double.parseDouble(occupancy)); | ||
196 | + syBasicResourceHistoryMapper.insertSelective(syBasicResourceHistory); | ||
197 | + } | ||
198 | + nextLevelRecord = "Utilization".equals(nowLine); | ||
199 | + if (!nextLevelRecord) { | ||
200 | + lastLine = nowLine; | ||
201 | + } | ||
202 | + } | ||
203 | + } | ||
204 | + } | ||
205 | + | ||
206 | + | ||
162 | } | 207 | } |
src/main/java/com/objecteye/service/impl/OccupationOfBasicResourcesServiceImpl.java
@@ -68,12 +68,13 @@ public class OccupationOfBasicResourcesServiceImpl implements IOccupationOfBasic | @@ -68,12 +68,13 @@ public class OccupationOfBasicResourcesServiceImpl implements IOccupationOfBasic | ||
68 | String logPath = (String) requestMap.get("logPath"); | 68 | String logPath = (String) requestMap.get("logPath"); |
69 | String owner = (String) requestMap.getOrDefault("user", "-1"); | 69 | String owner = (String) requestMap.getOrDefault("user", "-1"); |
70 | if (logPath == null || "".equals(logPath)) { | 70 | if (logPath == null || "".equals(logPath)) { |
71 | - logPath = jarPath.substring(0, jarPath.lastIndexOf("//")) + jarPath.substring(jarPath.lastIndexOf("//")).replaceAll("jar", "log"); | 71 | + logPath = jarPath.substring(0, jarPath.lastIndexOf("/")) + jarPath.substring(jarPath.lastIndexOf("/")).replaceAll("jar", "log"); |
72 | + log.info("OccupationOfBasicResourcesServiceImpl|deployJarFile|logPath: {}", logPath); | ||
72 | } | 73 | } |
73 | 74 | ||
74 | JSONObject resultObj = new JSONObject(); | 75 | JSONObject resultObj = new JSONObject(); |
75 | - String jarName = jarPath.substring(jarPath.lastIndexOf("//") + 1); | ||
76 | - if (!jarName.endsWith("\\.jar")) { | 76 | + String jarName = jarPath.substring(jarPath.lastIndexOf("/") + 1); |
77 | + if (!jarName.endsWith(".jar")) { | ||
77 | resultObj.put("error", "File type error. Please confirm your jar file is right."); | 78 | resultObj.put("error", "File type error. Please confirm your jar file is right."); |
78 | } | 79 | } |
79 | 80 | ||
@@ -81,20 +82,21 @@ public class OccupationOfBasicResourcesServiceImpl implements IOccupationOfBasic | @@ -81,20 +82,21 @@ public class OccupationOfBasicResourcesServiceImpl implements IOccupationOfBasic | ||
81 | String cmd = "nohup java -jar " + jarPath + " > " + logPath + " 2>&1 &"; | 82 | String cmd = "nohup java -jar " + jarPath + " > " + logPath + " 2>&1 &"; |
82 | executeLinuxCmd(cmd, outList); | 83 | executeLinuxCmd(cmd, outList); |
83 | 84 | ||
84 | - String outStr; | ||
85 | if (outList.size() > 0) { | 85 | if (outList.size() > 0) { |
86 | - outStr = outList.get(0); | ||
87 | - String regex = "^[[0-9]*] [0-9]*$"; | ||
88 | - if (outStr.matches(regex)) { | ||
89 | - resultObj.put("logPath", logPath); | ||
90 | - String taskId = outStr.split(" ")[1]; | ||
91 | - resultObj.put("taskId", taskId); | 86 | + resultObj.put("error", outList.get(0)); |
87 | + return resultObj; | ||
88 | + } | ||
89 | + | ||
90 | + outList = new ArrayList<>(); | ||
91 | + String jpsCommand = "jps -l"; | ||
92 | + executeLinuxCmd(jpsCommand, outList); | ||
93 | + for (String jpsDetail : outList) { | ||
94 | + String taskId = jpsDetail.split(" ")[0].trim(); | ||
95 | + String command = jpsDetail.split(" ")[1].trim(); | ||
96 | + if (command.equals(jarPath)) { | ||
92 | redisTemplate.opsForHash().put(GeneralContent.REDIS_TASK_ID_OWNER, taskId, owner); | 97 | redisTemplate.opsForHash().put(GeneralContent.REDIS_TASK_ID_OWNER, taskId, owner); |
93 | - } else { | ||
94 | - resultObj.put("error", "linux error: " + outStr); | 98 | + resultObj.put("taskId", taskId); |
95 | } | 99 | } |
96 | - } else { | ||
97 | - resultObj.put("error", "There is no response from Linux"); | ||
98 | } | 100 | } |
99 | 101 | ||
100 | return resultObj; | 102 | return resultObj; |
@@ -171,23 +173,29 @@ public class OccupationOfBasicResourcesServiceImpl implements IOccupationOfBasic | @@ -171,23 +173,29 @@ public class OccupationOfBasicResourcesServiceImpl implements IOccupationOfBasic | ||
171 | * @param cmd linux语句 | 173 | * @param cmd linux语句 |
172 | * @param outList 输出参数 | 174 | * @param outList 输出参数 |
173 | */ | 175 | */ |
174 | - private void executeLinuxCmd(String cmd, List<String> outList) { | 176 | + @Override |
177 | + public void executeLinuxCmd(String cmd, List<String> outList) { | ||
175 | Runtime run = Runtime.getRuntime(); | 178 | Runtime run = Runtime.getRuntime(); |
176 | try { | 179 | try { |
177 | Process process; | 180 | Process process; |
178 | - if (cmd.endsWith("\\.sh")) { | 181 | + if (cmd.endsWith(".sh")) { |
179 | ProcessBuilder builder = new ProcessBuilder("/bin/chmod", "755", cmd); | 182 | ProcessBuilder builder = new ProcessBuilder("/bin/chmod", "755", cmd); |
180 | Process permission = builder.start(); | 183 | Process permission = builder.start(); |
181 | permission.waitFor(); | 184 | permission.waitFor(); |
182 | process = run.exec("sh " + cmd); | 185 | process = run.exec("sh " + cmd); |
183 | } else { | 186 | } else { |
184 | - process = run.exec(cmd); | 187 | + String[] cmdList = {"/bin/sh", "-c", cmd}; |
188 | + process = run.exec(cmdList); | ||
185 | } | 189 | } |
186 | - log.debug("OccupationOfBasicResourcesServiceImpl|executeLinuxCmd|cmd: {}", cmd); | 190 | + log.info("OccupationOfBasicResourcesServiceImpl|executeLinuxCmd|cmd: {}", cmd); |
187 | String line; | 191 | String line; |
188 | BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(process.getInputStream())); | 192 | BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(process.getInputStream())); |
189 | while ((line = stdoutReader.readLine()) != null) { | 193 | while ((line = stdoutReader.readLine()) != null) { |
190 | - log.debug("OccupationOfBasicResourcesServiceImpl|executeLinuxCmd|line: {}", line); | 194 | + log.info("OccupationOfBasicResourcesServiceImpl|executeLinuxCmd|line: {}", line); |
195 | + if (line.contains("command not found") && line.contains("-bash")) { | ||
196 | + log.error(line); | ||
197 | + return; | ||
198 | + } | ||
191 | outList.add(line); | 199 | outList.add(line); |
192 | } | 200 | } |
193 | process.waitFor(); | 201 | process.waitFor(); |
src/main/resources/com.objecteye.mapper/SyBasicResourceHistoryMapper.xml
@@ -11,21 +11,39 @@ | @@ -11,21 +11,39 @@ | ||
11 | id, module_id, occurrence_time, occupancy | 11 | id, module_id, occurrence_time, occupancy |
12 | </sql> | 12 | </sql> |
13 | <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap"> | 13 | <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap"> |
14 | - select | ||
15 | - <include refid="Base_Column_List" /> | 14 | + select |
15 | + <include refid="Base_Column_List"/> | ||
16 | from sy_basic_resource_history | 16 | from sy_basic_resource_history |
17 | where id = #{id,jdbcType=VARCHAR} | 17 | where id = #{id,jdbcType=VARCHAR} |
18 | </select> | 18 | </select> |
19 | + <select id="getModuleInfoQueryByCondition" resultType="com.objecteye.entity.SyBasicResourceHistory"> | ||
20 | + select * from sy_basic_resource_history a | ||
21 | + <where> | ||
22 | + <if test="module != null and '' != module"> | ||
23 | + and a.module_id like '%' || #{module} || '%' | ||
24 | + </if> | ||
25 | + <if test="startTime != null"><![CDATA[ | ||
26 | + and a.occurrence_time >= #{startTime,jdbcType=BIGINT} | ||
27 | + ]]></if> | ||
28 | + <if test="endTime != null"><![CDATA[ | ||
29 | + and a.occurrence_time <= #{endTime,jdbcType=BIGINT} | ||
30 | + ]]></if> | ||
31 | + </where> | ||
32 | + order by a.occurrence_time asc | ||
33 | + </select> | ||
19 | <delete id="deleteByPrimaryKey" parameterType="java.lang.String"> | 34 | <delete id="deleteByPrimaryKey" parameterType="java.lang.String"> |
20 | - delete from sy_basic_resource_history | 35 | + delete |
36 | + from sy_basic_resource_history | ||
21 | where id = #{id,jdbcType=VARCHAR} | 37 | where id = #{id,jdbcType=VARCHAR} |
22 | </delete> | 38 | </delete> |
23 | <delete id="deleteLteTime"><![CDATA[ | 39 | <delete id="deleteLteTime"><![CDATA[ |
24 | - delete from sy_basic_resource_history where occurrence_time <= #{time,jdbcType=BIGINT} | ||
25 | - ]]></delete> | 40 | + delete |
41 | + from sy_basic_resource_history | ||
42 | + where occurrence_time <= #{time,jdbcType=BIGINT} | ||
43 | + ]]></delete> | ||
26 | <insert id="insert" parameterType="com.objecteye.entity.SyBasicResourceHistory"> | 44 | <insert id="insert" parameterType="com.objecteye.entity.SyBasicResourceHistory"> |
27 | - insert into sy_basic_resource_history (id, module_id, occurrence_time, | ||
28 | - occupancy) | 45 | + insert into sy_basic_resource_history (id, module_id, occurrence_time, |
46 | + occupancy) | ||
29 | values (#{id,jdbcType=VARCHAR}, #{moduleId,jdbcType=VARCHAR}, #{occurrenceTime,jdbcType=BIGINT}, | 47 | values (#{id,jdbcType=VARCHAR}, #{moduleId,jdbcType=VARCHAR}, #{occurrenceTime,jdbcType=BIGINT}, |
30 | #{occupancy,jdbcType=DOUBLE}) | 48 | #{occupancy,jdbcType=DOUBLE}) |
31 | </insert> | 49 | </insert> |