Commit 4ba5b2108e0e9e14b55992003c513bbc14bec2ae
1 parent
d7b1d619
添加基础单元监听模块;
添加包部署和简单监控功能;
Showing
13 changed files
with
602 additions
and
15 deletions
src/main/java/com/objecteye/VehicleApplication.java renamed to src/main/java/com/objecteye/LinuxListenerApplication.java
... | ... | @@ -14,10 +14,10 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; |
14 | 14 | @EnableTransactionManagement |
15 | 15 | @EnableScheduling |
16 | 16 | @EnableAsync |
17 | -public class VehicleApplication { | |
17 | +public class LinuxListenerApplication { | |
18 | 18 | |
19 | 19 | public static void main(String[] args) { |
20 | - ConfigurableApplicationContext run = SpringApplication.run(VehicleApplication.class, args); | |
20 | + ConfigurableApplicationContext run = SpringApplication.run(LinuxListenerApplication.class, args); | |
21 | 21 | } |
22 | 22 | |
23 | 23 | @Bean | ... | ... |
src/main/java/com/objecteye/common/GeneralContent.java
... | ... | @@ -9,6 +9,14 @@ public class GeneralContent { |
9 | 9 | */ |
10 | 10 | public final static String REDIS_LOG_KEY_PATH = "redisLogKeyPath"; |
11 | 11 | /** |
12 | + * key: 发布项目的Linux中进程号, value: 拥有者id | |
13 | + */ | |
14 | + public final static String REDIS_TASK_ID_OWNER = "redisTaskIdOwner"; | |
15 | + /** | |
16 | + * 需要保存到数据库中的监控信息key(shell文件对应的module. eg: arp_cache,bandwidth) | |
17 | + */ | |
18 | + public final static String REDIS_RECORD_MODULE = "redisRecordModule"; | |
19 | + /** | |
12 | 20 | * sh功能脚本map |
13 | 21 | */ |
14 | 22 | public final static Map<String, String> MODULE_MAP = new HashMap<>(); | ... | ... |
src/main/java/com/objecteye/controller/BackGroundController.java
0 → 100644
1 | +package com.objecteye.controller; | |
2 | + | |
3 | +import com.alibaba.fastjson.JSONObject; | |
4 | +import com.objecteye.common.CommonResult; | |
5 | +import com.objecteye.service.IBackGroundService; | |
6 | +import io.swagger.annotations.Api; | |
7 | +import io.swagger.annotations.ApiOperation; | |
8 | +import org.springframework.beans.factory.annotation.Autowired; | |
9 | +import org.springframework.web.bind.annotation.*; | |
10 | + | |
11 | +import java.util.Map; | |
12 | + | |
13 | +@RestController | |
14 | +@Api(tags = "background", description = "监控器调度") | |
15 | +@RequestMapping("/background") | |
16 | +@CrossOrigin | |
17 | +public class BackGroundController { | |
18 | + @Autowired | |
19 | + private IBackGroundService iBackGroundService; | |
20 | + | |
21 | + @ApiOperation("获取正在生效的监控功能") | |
22 | + @RequestMapping(value = "/getWorkingListener", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) | |
23 | + public CommonResult getWorkingListener() { | |
24 | + JSONObject resultObj = iBackGroundService.getWorkingListener(); | |
25 | + if (resultObj.containsKey("error")) { | |
26 | + return CommonResult.success(201, resultObj.getString("error"), null); | |
27 | + } | |
28 | + return CommonResult.success(resultObj); | |
29 | + } | |
30 | + | |
31 | + @ApiOperation("添加新的监控器") | |
32 | + @RequestMapping(value = "/addWorkingListener", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) | |
33 | + public CommonResult addWorkingListener(@RequestBody Map<String, Object> requestMap) { | |
34 | + JSONObject resultObj = iBackGroundService.addWorkingListener(requestMap); | |
35 | + if (resultObj.containsKey("error")) { | |
36 | + return CommonResult.success(201, resultObj.getString("error"), null); | |
37 | + } | |
38 | + return CommonResult.success(resultObj); | |
39 | + } | |
40 | + | |
41 | + @ApiOperation("删除存在的监控器") | |
42 | + @RequestMapping(value = "/removeWorkingListener", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) | |
43 | + public CommonResult removeWorkingListener(@RequestBody Map<String, Object> requestMap) { | |
44 | + JSONObject resultObj = iBackGroundService.removeWorkingListener(requestMap); | |
45 | + if (resultObj.containsKey("error")) { | |
46 | + return CommonResult.success(201, resultObj.getString("error"), null); | |
47 | + } | |
48 | + return CommonResult.success(resultObj); | |
49 | + } | |
50 | +} | ... | ... |
src/main/java/com/objecteye/controller/LogFileListenerController.java
1 | 1 | package com.objecteye.controller; |
2 | 2 | |
3 | +import com.alibaba.fastjson.JSONObject; | |
3 | 4 | import com.objecteye.common.CommonResult; |
4 | 5 | import com.objecteye.service.ILogFileListenerService; |
5 | 6 | import io.swagger.annotations.Api; |
... | ... | @@ -21,7 +22,11 @@ public class LogFileListenerController { |
21 | 22 | @ApiOperation("设置log文件所在位置信息") |
22 | 23 | @RequestMapping(value = "/setLogMsg", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) |
23 | 24 | public CommonResult setLogMsg(@RequestBody Map<String, Object> requestMap) { |
24 | - return CommonResult.success(iLogFileListenerService.setLogMsg(requestMap)); | |
25 | + JSONObject resultObj = iLogFileListenerService.setLogMsg(requestMap); | |
26 | + if (resultObj.containsKey("error")) { | |
27 | + return CommonResult.success(201, resultObj.getString("error"), null); | |
28 | + } | |
29 | + return CommonResult.success(resultObj); | |
25 | 30 | } |
26 | 31 | |
27 | 32 | @ApiOperation("开启日志监控输出") | ... | ... |
src/main/java/com/objecteye/controller/OccupationOfBasicResourcesController.java
1 | 1 | package com.objecteye.controller; |
2 | 2 | |
3 | 3 | import com.alibaba.fastjson.JSONArray; |
4 | +import com.alibaba.fastjson.JSONObject; | |
4 | 5 | import com.objecteye.common.CommonResult; |
5 | 6 | import com.objecteye.service.IOccupationOfBasicResourcesService; |
6 | 7 | import io.swagger.annotations.Api; |
... | ... | @@ -28,4 +29,24 @@ public class OccupationOfBasicResourcesController { |
28 | 29 | return CommonResult.success(201, "未获取输出信息", null); |
29 | 30 | } |
30 | 31 | } |
32 | + | |
33 | + @ApiOperation("jar包发布") | |
34 | + @RequestMapping(value = "/deployJarFile", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) | |
35 | + public CommonResult deployJarFile(@RequestBody Map<String, Object> requestMap) { | |
36 | + JSONObject resultObj = iOccupationOfBasicResourcesService.deployJarFile(requestMap); | |
37 | + if (resultObj.containsKey("error")) { | |
38 | + return CommonResult.success(201, resultObj.getString("error"), null); | |
39 | + } | |
40 | + return CommonResult.success(resultObj); | |
41 | + } | |
42 | + | |
43 | + @ApiOperation("获取当前进行中的java服务") | |
44 | + @RequestMapping(value = "/getJpsInfo", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) | |
45 | + public CommonResult getJpsInfo() { | |
46 | + JSONObject resultObj = iOccupationOfBasicResourcesService.getJpsInfo(); | |
47 | + if (resultObj.containsKey("error")) { | |
48 | + return CommonResult.success(201, resultObj.getString("error"), null); | |
49 | + } | |
50 | + return CommonResult.success(resultObj); | |
51 | + } | |
31 | 52 | } | ... | ... |
src/main/java/com/objecteye/entity/SyBasicResourceHistory.java
0 → 100644
1 | +package com.objecteye.entity; | |
2 | + | |
3 | +import java.io.Serializable; | |
4 | + | |
5 | +import lombok.AllArgsConstructor; | |
6 | +import lombok.Data; | |
7 | +import lombok.NoArgsConstructor; | |
8 | +import lombok.ToString; | |
9 | + | |
10 | +/** | |
11 | + * sy_basic_resource_history | |
12 | + * @author | |
13 | + */ | |
14 | +@Data | |
15 | +@AllArgsConstructor | |
16 | +@NoArgsConstructor | |
17 | +@ToString | |
18 | +public class SyBasicResourceHistory implements Serializable { | |
19 | + /** | |
20 | + * 主键 | |
21 | + */ | |
22 | + private String id; | |
23 | + | |
24 | + /** | |
25 | + * 类型 | |
26 | + */ | |
27 | + private String moduleId; | |
28 | + | |
29 | + /** | |
30 | + * 对应时间戳 | |
31 | + */ | |
32 | + private Long occurrenceTime; | |
33 | + | |
34 | + /** | |
35 | + * 占用率 | |
36 | + */ | |
37 | + private Double occupancy; | |
38 | + | |
39 | + private static final long serialVersionUID = 1L; | |
40 | +} | |
0 | 41 | \ No newline at end of file | ... | ... |
src/main/java/com/objecteye/mapper/SyBasicResourceHistoryMapper.java
0 → 100644
1 | +package com.objecteye.mapper; | |
2 | + | |
3 | +import com.objecteye.entity.SyBasicResourceHistory; | |
4 | + | |
5 | +public interface SyBasicResourceHistoryMapper { | |
6 | + int deleteByPrimaryKey(String id); | |
7 | + | |
8 | + int insert(SyBasicResourceHistory record); | |
9 | + | |
10 | + int insertSelective(SyBasicResourceHistory record); | |
11 | + | |
12 | + SyBasicResourceHistory selectByPrimaryKey(String id); | |
13 | + | |
14 | + int updateByPrimaryKeySelective(SyBasicResourceHistory record); | |
15 | + | |
16 | + int updateByPrimaryKey(SyBasicResourceHistory record); | |
17 | + | |
18 | + /** | |
19 | + * 删除时间点之前的数据 | |
20 | + * | |
21 | + * @param time 时间戳 | |
22 | + * @return 操作数量 | |
23 | + */ | |
24 | + int deleteLteTime(long time); | |
25 | +} | |
0 | 26 | \ No newline at end of file | ... | ... |
src/main/java/com/objecteye/service/IBackGroundService.java
0 → 100644
1 | +package com.objecteye.service; | |
2 | + | |
3 | +import com.alibaba.fastjson.JSONObject; | |
4 | + | |
5 | +import java.util.Map; | |
6 | + | |
7 | +public interface IBackGroundService { | |
8 | + | |
9 | + /** | |
10 | + * 定时写入的定制信息 | |
11 | + */ | |
12 | + void jobController(); | |
13 | + | |
14 | + /** | |
15 | + * 删除24小时之前的数据 | |
16 | + */ | |
17 | + void removeOverTimeData(); | |
18 | + | |
19 | + /** | |
20 | + * 获取正在生效的监控功能 | |
21 | + * | |
22 | + * @return 监控功能数组 | |
23 | + */ | |
24 | + JSONObject getWorkingListener(); | |
25 | + | |
26 | + /** | |
27 | + * 添加新的监控器 | |
28 | + * | |
29 | + * @param requestMap 请求参数 | |
30 | + * @return 返回信息 | |
31 | + */ | |
32 | + JSONObject addWorkingListener(Map<String, Object> requestMap); | |
33 | + | |
34 | + /** | |
35 | + * 删除存在的监控器 | |
36 | + * | |
37 | + * @param requestMap 请求参数 | |
38 | + * @return 返回信息 | |
39 | + */ | |
40 | + JSONObject removeWorkingListener(Map<String, Object> requestMap); | |
41 | +} | ... | ... |
src/main/java/com/objecteye/service/IOccupationOfBasicResourcesService.java
1 | 1 | package com.objecteye.service; |
2 | 2 | |
3 | 3 | import com.alibaba.fastjson.JSONArray; |
4 | +import com.alibaba.fastjson.JSONObject; | |
4 | 5 | |
5 | 6 | import java.util.Map; |
6 | 7 | |
... | ... | @@ -16,4 +17,27 @@ public interface IOccupationOfBasicResourcesService { |
16 | 17 | * @return 输出结果 |
17 | 18 | */ |
18 | 19 | JSONArray getInfoByModule(Map<String, Object> requestMap); |
20 | + | |
21 | + /** | |
22 | + * jar包发布 | |
23 | + * | |
24 | + * @param requestMap 请求参数 | |
25 | + * @return 发包后日志地址 | |
26 | + */ | |
27 | + JSONObject deployJarFile(Map<String, Object> requestMap); | |
28 | + | |
29 | + /** | |
30 | + * 停止jar包进程 | |
31 | + * | |
32 | + * @param requestMap 请求参数 | |
33 | + * @return 操作结果 | |
34 | + */ | |
35 | + JSONObject killJarTask(Map<String, Object> requestMap); | |
36 | + | |
37 | + /** | |
38 | + * 获取当前进行中的jar包 | |
39 | + * | |
40 | + * @return 进行中的jar包信息 | |
41 | + */ | |
42 | + JSONObject getJpsInfo(); | |
19 | 43 | } | ... | ... |
src/main/java/com/objecteye/service/impl/BackGroundServiceImpl.java
0 → 100644
1 | +package com.objecteye.service.impl; | |
2 | + | |
3 | +import com.alibaba.fastjson.JSONArray; | |
4 | +import com.alibaba.fastjson.JSONObject; | |
5 | +import com.objecteye.common.GeneralContent; | |
6 | +import com.objecteye.entity.SyBasicResourceHistory; | |
7 | +import com.objecteye.mapper.SyBasicResourceHistoryMapper; | |
8 | +import com.objecteye.service.IBackGroundService; | |
9 | +import com.objecteye.service.IOccupationOfBasicResourcesService; | |
10 | +import lombok.extern.slf4j.Slf4j; | |
11 | +import org.springframework.beans.factory.annotation.Autowired; | |
12 | +import org.springframework.data.redis.core.RedisTemplate; | |
13 | +import org.springframework.scheduling.annotation.Async; | |
14 | +import org.springframework.scheduling.annotation.Scheduled; | |
15 | +import org.springframework.stereotype.Component; | |
16 | + | |
17 | +import java.util.*; | |
18 | + | |
19 | +@Component | |
20 | +@Slf4j | |
21 | +public class BackGroundServiceImpl implements IBackGroundService { | |
22 | + @Autowired | |
23 | + private SyBasicResourceHistoryMapper syBasicResourceHistoryMapper; | |
24 | + @Autowired | |
25 | + private IOccupationOfBasicResourcesService iOccupationOfBasicResourcesService; | |
26 | + @Autowired | |
27 | + private RedisTemplate redisTemplate; | |
28 | + | |
29 | + /** | |
30 | + * 定时写入的定制信息 | |
31 | + */ | |
32 | + @Override | |
33 | + @Scheduled(fixedRate = 3 * 1000) | |
34 | + public void jobController() { | |
35 | + String moduleStr = (String) redisTemplate.opsForValue().get(GeneralContent.REDIS_RECORD_MODULE); | |
36 | + if (moduleStr == null || "".equals(moduleStr)) { | |
37 | + return; | |
38 | + } | |
39 | + String[] moduleList = moduleStr.split(","); | |
40 | + Map<String, Object> requestMap = new HashMap<>(); | |
41 | + for (String module : moduleList) { | |
42 | + requestMap.put("module", module); | |
43 | + switch (module) { | |
44 | + default: | |
45 | + break; | |
46 | + case "cpu_utilization": | |
47 | + doCpuUtilization(requestMap); | |
48 | + break; | |
49 | + case "memory_info": | |
50 | + doMemoryInfo(requestMap); | |
51 | + break; | |
52 | + } | |
53 | + } | |
54 | + } | |
55 | + | |
56 | + /** | |
57 | + * 删除24小时之前的数据 | |
58 | + */ | |
59 | + @Override | |
60 | + @Scheduled(fixedRate = 1000 * 60) | |
61 | + public void removeOverTimeData() { | |
62 | + Calendar calendar = Calendar.getInstance(); | |
63 | + calendar.setTimeInMillis(System.currentTimeMillis()); | |
64 | + calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 1); | |
65 | + long lastTime = calendar.getTimeInMillis(); | |
66 | + syBasicResourceHistoryMapper.deleteLteTime(lastTime); | |
67 | + } | |
68 | + | |
69 | + /** | |
70 | + * 获取正在生效的监控功能 | |
71 | + * | |
72 | + * @return 监控功能数组 | |
73 | + */ | |
74 | + @Override | |
75 | + public JSONObject getWorkingListener() { | |
76 | + String workingListenerStr = (String) redisTemplate.opsForValue().get(GeneralContent.REDIS_RECORD_MODULE); | |
77 | + JSONObject resultObj = new JSONObject(); | |
78 | + if (workingListenerStr == null || "".equals(workingListenerStr)) { | |
79 | + resultObj.put("error", "There is no listener working"); | |
80 | + } else { | |
81 | + resultObj.put("result", workingListenerStr.split(",")); | |
82 | + } | |
83 | + return resultObj; | |
84 | + } | |
85 | + | |
86 | + /** | |
87 | + * 添加新的监控器 | |
88 | + * | |
89 | + * @param requestMap 请求参数 | |
90 | + * @return 返回信息 | |
91 | + */ | |
92 | + @Override | |
93 | + public JSONObject addWorkingListener(Map<String, Object> requestMap) { | |
94 | + List<String> addModules = new ArrayList<>((List<String>) requestMap.get("modules")); | |
95 | + String workingListenerStr = (String) redisTemplate.opsForValue().get(GeneralContent.REDIS_RECORD_MODULE); | |
96 | + if (workingListenerStr == null || "".equals(workingListenerStr)) { | |
97 | + workingListenerStr = String.join(",", addModules); | |
98 | + } else { | |
99 | + List<String> workingListenerList = Arrays.asList(workingListenerStr.split(",")); | |
100 | + addModules.removeIf(workingListenerList::contains); | |
101 | + workingListenerStr += "," + String.join(",", addModules); | |
102 | + } | |
103 | + redisTemplate.opsForValue().set(GeneralContent.REDIS_RECORD_MODULE, workingListenerStr); | |
104 | + return new JSONObject(); | |
105 | + } | |
106 | + | |
107 | + /** | |
108 | + * 删除存在的监控器 | |
109 | + * | |
110 | + * @param requestMap 请求参数 | |
111 | + * @return 返回信息 | |
112 | + */ | |
113 | + @Override | |
114 | + public JSONObject removeWorkingListener(Map<String, Object> requestMap) { | |
115 | + List<String> removeModules = (List<String>) requestMap.get("modules"); | |
116 | + String workingListenerStr = (String) redisTemplate.opsForValue().get(GeneralContent.REDIS_RECORD_MODULE); | |
117 | + JSONObject resultObj = new JSONObject(); | |
118 | + if (workingListenerStr == null || "".equals(workingListenerStr)) { | |
119 | + resultObj.put("error", "There is no listener working"); | |
120 | + } else { | |
121 | + List<String> workingListenerList = new ArrayList<>(Arrays.asList(workingListenerStr.split(","))); | |
122 | + workingListenerList.removeIf(removeModules::contains); | |
123 | + redisTemplate.opsForValue().set(GeneralContent.REDIS_RECORD_MODULE, String.join(",", workingListenerList)); | |
124 | + } | |
125 | + return resultObj; | |
126 | + } | |
127 | + | |
128 | + /** | |
129 | + * cpu信息记录 | |
130 | + * | |
131 | + * @param requestMap 请求参数 | |
132 | + */ | |
133 | + @Async("taskExecutor") | |
134 | + public void doCpuUtilization(Map<String, Object> requestMap) { | |
135 | + JSONArray resultArr = iOccupationOfBasicResourcesService.getInfoByModule(requestMap); | |
136 | + if (resultArr.size() > 0) { | |
137 | + JSONObject resultObj = resultArr.getJSONObject(0); | |
138 | + SyBasicResourceHistory syBasicResourceHistory = new SyBasicResourceHistory(UUID.randomUUID().toString(), (String) requestMap.get("module"), | |
139 | + System.currentTimeMillis(), resultObj.getDoubleValue("result")); | |
140 | + syBasicResourceHistoryMapper.insertSelective(syBasicResourceHistory); | |
141 | + } | |
142 | + } | |
143 | + | |
144 | + /** | |
145 | + * 内存信息记录 | |
146 | + * | |
147 | + * @param requestMap 请求参数 | |
148 | + */ | |
149 | + @Async("taskExecutor") | |
150 | + public void doMemoryInfo(Map<String, Object> requestMap) { | |
151 | + JSONArray resultArr = iOccupationOfBasicResourcesService.getInfoByModule(requestMap); | |
152 | + if (resultArr.size() > 0) { | |
153 | + JSONObject resultObj = resultArr.getJSONObject(0); | |
154 | + double memTotal = Double.parseDouble(resultObj.getString("MemTotal").replaceAll("kB", "").trim()); | |
155 | + double memFree = Double.parseDouble(resultObj.getString("MemFree").replaceAll("kB", "").trim()); | |
156 | + double memUseQuality = Math.round((1 - memFree / memTotal) * 100); | |
157 | + SyBasicResourceHistory syBasicResourceHistory = new SyBasicResourceHistory(UUID.randomUUID().toString(), (String) requestMap.get("module"), | |
158 | + System.currentTimeMillis(), memUseQuality); | |
159 | + syBasicResourceHistoryMapper.insertSelective(syBasicResourceHistory); | |
160 | + } | |
161 | + } | |
162 | +} | ... | ... |
src/main/java/com/objecteye/service/impl/LogFileListenerServiceImpl.java
... | ... | @@ -31,15 +31,16 @@ public class LogFileListenerServiceImpl implements ILogFileListenerService { |
31 | 31 | public JSONObject setLogMsg(Map<String, Object> requestMap) { |
32 | 32 | String filePath = (String) requestMap.get("filePath"); |
33 | 33 | File file = new File(filePath); |
34 | + JSONObject resultObj = new JSONObject(); | |
34 | 35 | if (!file.exists()) { |
35 | - throw new RuntimeException("file is not exists! Please confirm your path"); | |
36 | + resultObj.put("error", "file is not exists! Please confirm your path"); | |
37 | + return resultObj; | |
36 | 38 | } else if (file.isDirectory()) { |
37 | - throw new RuntimeException("Can not analysis directory, please insert a file path"); | |
39 | + resultObj.put("error", "Can not analysis directory, please insert a file path"); | |
40 | + return resultObj; | |
38 | 41 | } |
39 | 42 | String key = file.getName() + "|" + System.currentTimeMillis(); |
40 | 43 | redisTemplate.opsForHash().put(GeneralContent.REDIS_LOG_KEY_PATH, key, filePath); |
41 | - | |
42 | - JSONObject resultObj = new JSONObject(); | |
43 | 44 | resultObj.put("logKey", key); |
44 | 45 | return resultObj; |
45 | 46 | } | ... | ... |
src/main/java/com/objecteye/service/impl/OccupationOfBasicResourcesServiceImpl.java
... | ... | @@ -6,6 +6,8 @@ import com.alibaba.fastjson.JSONObject; |
6 | 6 | import com.objecteye.common.GeneralContent; |
7 | 7 | import com.objecteye.service.IOccupationOfBasicResourcesService; |
8 | 8 | import lombok.extern.slf4j.Slf4j; |
9 | +import org.springframework.beans.factory.annotation.Autowired; | |
10 | +import org.springframework.data.redis.core.RedisTemplate; | |
9 | 11 | import org.springframework.stereotype.Component; |
10 | 12 | |
11 | 13 | import java.io.BufferedReader; |
... | ... | @@ -14,10 +16,13 @@ import java.io.InputStreamReader; |
14 | 16 | import java.util.ArrayList; |
15 | 17 | import java.util.List; |
16 | 18 | import java.util.Map; |
19 | +import java.util.Objects; | |
17 | 20 | |
18 | 21 | @Component |
19 | 22 | @Slf4j |
20 | 23 | public class OccupationOfBasicResourcesServiceImpl implements IOccupationOfBasicResourcesService { |
24 | + @Autowired | |
25 | + private RedisTemplate redisTemplate; | |
21 | 26 | |
22 | 27 | /** |
23 | 28 | * 获取指定module的输出 |
... | ... | @@ -34,11 +39,17 @@ public class OccupationOfBasicResourcesServiceImpl implements IOccupationOfBasic |
34 | 39 | List<String> outList = new ArrayList<>(); |
35 | 40 | executeLinuxCmd(cmd, outList); |
36 | 41 | if (outList.size() > 0) { |
37 | - Object object = JSON.parse(outList.get(0)); | |
42 | + String outStr = String.join("", outList); | |
43 | + log.debug("OccupationOfBasicResourcesServiceImpl|getInfoByModule|outStr: {}", outStr); | |
44 | + Object object = JSON.parse(outStr); | |
38 | 45 | if (object instanceof JSONObject) { |
39 | 46 | resultArr.add(object); |
40 | 47 | } else if (object instanceof JSONArray) { |
41 | 48 | resultArr = (JSONArray) object; |
49 | + } else { | |
50 | + JSONObject tempObj = new JSONObject(); | |
51 | + tempObj.put("result", outStr); | |
52 | + resultArr.add(tempObj); | |
42 | 53 | } |
43 | 54 | } |
44 | 55 | } |
... | ... | @@ -46,6 +57,115 @@ public class OccupationOfBasicResourcesServiceImpl implements IOccupationOfBasic |
46 | 57 | } |
47 | 58 | |
48 | 59 | /** |
60 | + * jar包发布 | |
61 | + * | |
62 | + * @param requestMap 请求参数 | |
63 | + * @return 发包后日志地址 | |
64 | + */ | |
65 | + @Override | |
66 | + public JSONObject deployJarFile(Map<String, Object> requestMap) { | |
67 | + String jarPath = (String) requestMap.get("filePath"); | |
68 | + String logPath = (String) requestMap.get("logPath"); | |
69 | + String owner = (String) requestMap.getOrDefault("user", "-1"); | |
70 | + if (logPath == null || "".equals(logPath)) { | |
71 | + logPath = jarPath.substring(0, jarPath.lastIndexOf("//")) + jarPath.substring(jarPath.lastIndexOf("//")).replaceAll("jar", "log"); | |
72 | + } | |
73 | + | |
74 | + JSONObject resultObj = new JSONObject(); | |
75 | + String jarName = jarPath.substring(jarPath.lastIndexOf("//") + 1); | |
76 | + if (!jarName.endsWith("\\.jar")) { | |
77 | + resultObj.put("error", "File type error. Please confirm your jar file is right."); | |
78 | + } | |
79 | + | |
80 | + List<String> outList = new ArrayList<>(); | |
81 | + String cmd = "nohup java -jar " + jarPath + " > " + logPath + " 2>&1 &"; | |
82 | + executeLinuxCmd(cmd, outList); | |
83 | + | |
84 | + String outStr; | |
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); | |
92 | + redisTemplate.opsForHash().put(GeneralContent.REDIS_TASK_ID_OWNER, taskId, owner); | |
93 | + } else { | |
94 | + resultObj.put("error", "linux error: " + outStr); | |
95 | + } | |
96 | + } else { | |
97 | + resultObj.put("error", "There is no response from Linux"); | |
98 | + } | |
99 | + | |
100 | + return resultObj; | |
101 | + } | |
102 | + | |
103 | + /** | |
104 | + * 停止jar包进程 | |
105 | + * | |
106 | + * @param requestMap 请求参数 | |
107 | + * @return 操作结果 | |
108 | + */ | |
109 | + @Override | |
110 | + public JSONObject killJarTask(Map<String, Object> requestMap) { | |
111 | + String user = (String) requestMap.get("user"); | |
112 | + String taskId = (String) requestMap.get("taskId"); | |
113 | + boolean superPower = ifSuperPower(user); | |
114 | + | |
115 | + JSONObject resultObj = new JSONObject(); | |
116 | + if (redisTemplate.opsForHash().hasKey(GeneralContent.REDIS_TASK_ID_OWNER, taskId)) { | |
117 | + String owner = (String) redisTemplate.opsForHash().get(GeneralContent.REDIS_TASK_ID_OWNER, taskId); | |
118 | + if (Objects.equals(owner, user) || superPower) { | |
119 | + String cmd = "kill -9 " + taskId; | |
120 | + List<String> outList = new ArrayList<>(); | |
121 | + executeLinuxCmd(cmd, outList); | |
122 | + if (outList.size() > 0) { | |
123 | + resultObj.put("error", outList); | |
124 | + } | |
125 | + } else { | |
126 | + resultObj.put("error", "No authority. The owner is " + owner); | |
127 | + } | |
128 | + } else { | |
129 | + resultObj.put("error", "illegal taskId"); | |
130 | + } | |
131 | + return resultObj; | |
132 | + } | |
133 | + | |
134 | + /** | |
135 | + * 是否拥有超级管理员权限 | |
136 | + * | |
137 | + * @param user 当前用户id | |
138 | + * @return 是否拥有超级管理员权限 | |
139 | + */ | |
140 | + private boolean ifSuperPower(String user) { | |
141 | + // 超级管理员 | |
142 | + boolean superPower = false; | |
143 | + if (Objects.equals(user, "-1")) { | |
144 | + superPower = true; | |
145 | + } | |
146 | + return superPower; | |
147 | + } | |
148 | + | |
149 | + /** | |
150 | + * 获取当前进行中的jar包 | |
151 | + * | |
152 | + * @return 进行中的jar包信息 | |
153 | + */ | |
154 | + @Override | |
155 | + public JSONObject getJpsInfo() { | |
156 | + String cmd = "jps"; | |
157 | + List<String> outList = new ArrayList<>(); | |
158 | + executeLinuxCmd(cmd, outList); | |
159 | + JSONObject resultObj = new JSONObject(); | |
160 | + if (outList.size() > 0) { | |
161 | + resultObj.put("process", outList); | |
162 | + } else { | |
163 | + resultObj.put("error", "Get no Message"); | |
164 | + } | |
165 | + return resultObj; | |
166 | + } | |
167 | + | |
168 | + /** | |
49 | 169 | * 执行Linux语句并获取返回值 |
50 | 170 | * |
51 | 171 | * @param cmd linux语句 |
... | ... | @@ -54,22 +174,27 @@ public class OccupationOfBasicResourcesServiceImpl implements IOccupationOfBasic |
54 | 174 | private void executeLinuxCmd(String cmd, List<String> outList) { |
55 | 175 | Runtime run = Runtime.getRuntime(); |
56 | 176 | try { |
57 | - ProcessBuilder builder = new ProcessBuilder("/bin/chmod", "755", cmd); | |
58 | - Process permission = builder.start(); | |
59 | - permission.waitFor(); | |
60 | - | |
61 | - Process process = run.exec("sh " + cmd); | |
62 | - log.info("OccupationOfBasicResourcesServiceImpl|executeLinuxCmd|cmd: {}", cmd); | |
177 | + Process process; | |
178 | + if (cmd.endsWith("\\.sh")) { | |
179 | + ProcessBuilder builder = new ProcessBuilder("/bin/chmod", "755", cmd); | |
180 | + Process permission = builder.start(); | |
181 | + permission.waitFor(); | |
182 | + process = run.exec("sh " + cmd); | |
183 | + } else { | |
184 | + process = run.exec(cmd); | |
185 | + } | |
186 | + log.debug("OccupationOfBasicResourcesServiceImpl|executeLinuxCmd|cmd: {}", cmd); | |
63 | 187 | String line; |
64 | 188 | BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(process.getInputStream())); |
65 | 189 | while ((line = stdoutReader.readLine()) != null) { |
66 | - log.info("OccupationOfBasicResourcesServiceImpl|executeLinuxCmd|line: {}", line); | |
190 | + log.debug("OccupationOfBasicResourcesServiceImpl|executeLinuxCmd|line: {}", line); | |
67 | 191 | outList.add(line); |
68 | 192 | } |
69 | 193 | process.waitFor(); |
70 | 194 | process.destroy(); |
71 | 195 | } catch (IOException | InterruptedException e) { |
72 | 196 | e.printStackTrace(); |
197 | + log.error(e.getMessage()); | |
73 | 198 | } |
74 | 199 | } |
75 | 200 | } | ... | ... |
src/main/resources/com.objecteye.mapper/SyBasicResourceHistoryMapper.xml
0 → 100644
1 | +<?xml version="1.0" encoding="UTF-8"?> | |
2 | +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | |
3 | +<mapper namespace="com.objecteye.mapper.SyBasicResourceHistoryMapper"> | |
4 | + <resultMap id="BaseResultMap" type="com.objecteye.entity.SyBasicResourceHistory"> | |
5 | + <id column="id" jdbcType="VARCHAR" property="id" /> | |
6 | + <result column="module_id" jdbcType="VARCHAR" property="moduleId" /> | |
7 | + <result column="occurrence_time" jdbcType="BIGINT" property="occurrenceTime" /> | |
8 | + <result column="occupancy" jdbcType="DOUBLE" property="occupancy" /> | |
9 | + </resultMap> | |
10 | + <sql id="Base_Column_List"> | |
11 | + id, module_id, occurrence_time, occupancy | |
12 | + </sql> | |
13 | + <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap"> | |
14 | + select | |
15 | + <include refid="Base_Column_List" /> | |
16 | + from sy_basic_resource_history | |
17 | + where id = #{id,jdbcType=VARCHAR} | |
18 | + </select> | |
19 | + <delete id="deleteByPrimaryKey" parameterType="java.lang.String"> | |
20 | + delete from sy_basic_resource_history | |
21 | + where id = #{id,jdbcType=VARCHAR} | |
22 | + </delete> | |
23 | + <delete id="deleteLteTime"><![CDATA[ | |
24 | + delete from sy_basic_resource_history where occurrence_time <= #{time,jdbcType=BIGINT} | |
25 | + ]]></delete> | |
26 | + <insert id="insert" parameterType="com.objecteye.entity.SyBasicResourceHistory"> | |
27 | + insert into sy_basic_resource_history (id, module_id, occurrence_time, | |
28 | + occupancy) | |
29 | + values (#{id,jdbcType=VARCHAR}, #{moduleId,jdbcType=VARCHAR}, #{occurrenceTime,jdbcType=BIGINT}, | |
30 | + #{occupancy,jdbcType=DOUBLE}) | |
31 | + </insert> | |
32 | + <insert id="insertSelective" parameterType="com.objecteye.entity.SyBasicResourceHistory"> | |
33 | + insert into sy_basic_resource_history | |
34 | + <trim prefix="(" suffix=")" suffixOverrides=","> | |
35 | + <if test="id != null"> | |
36 | + id, | |
37 | + </if> | |
38 | + <if test="moduleId != null"> | |
39 | + module_id, | |
40 | + </if> | |
41 | + <if test="occurrenceTime != null"> | |
42 | + occurrence_time, | |
43 | + </if> | |
44 | + <if test="occupancy != null"> | |
45 | + occupancy, | |
46 | + </if> | |
47 | + </trim> | |
48 | + <trim prefix="values (" suffix=")" suffixOverrides=","> | |
49 | + <if test="id != null"> | |
50 | + #{id,jdbcType=VARCHAR}, | |
51 | + </if> | |
52 | + <if test="moduleId != null"> | |
53 | + #{moduleId,jdbcType=VARCHAR}, | |
54 | + </if> | |
55 | + <if test="occurrenceTime != null"> | |
56 | + #{occurrenceTime,jdbcType=BIGINT}, | |
57 | + </if> | |
58 | + <if test="occupancy != null"> | |
59 | + #{occupancy,jdbcType=DOUBLE}, | |
60 | + </if> | |
61 | + </trim> | |
62 | + </insert> | |
63 | + <update id="updateByPrimaryKeySelective" parameterType="com.objecteye.entity.SyBasicResourceHistory"> | |
64 | + update sy_basic_resource_history | |
65 | + <set> | |
66 | + <if test="moduleId != null"> | |
67 | + module_id = #{moduleId,jdbcType=VARCHAR}, | |
68 | + </if> | |
69 | + <if test="occurrenceTime != null"> | |
70 | + occurrence_time = #{occurrenceTime,jdbcType=BIGINT}, | |
71 | + </if> | |
72 | + <if test="occupancy != null"> | |
73 | + occupancy = #{occupancy,jdbcType=DOUBLE}, | |
74 | + </if> | |
75 | + </set> | |
76 | + where id = #{id,jdbcType=VARCHAR} | |
77 | + </update> | |
78 | + <update id="updateByPrimaryKey" parameterType="com.objecteye.entity.SyBasicResourceHistory"> | |
79 | + update sy_basic_resource_history | |
80 | + set module_id = #{moduleId,jdbcType=VARCHAR}, | |
81 | + occurrence_time = #{occurrenceTime,jdbcType=BIGINT}, | |
82 | + occupancy = #{occupancy,jdbcType=DOUBLE} | |
83 | + where id = #{id,jdbcType=VARCHAR} | |
84 | + </update> | |
85 | +</mapper> | |
0 | 86 | \ No newline at end of file | ... | ... |