Authored by zhengyouwei

java restart

package com.monitor.other.projectRestart.common;
package com.monitor.model.domain;
/**
* Created by zhengyouwei on 2016/6/29.
... ...
package com.monitor.model.domain;
import lombok.Data;
/**
* Created by zhengyouwei on 2016/10/19.
*/
@Data
public class RestartModel {
//restart,stop
private String exe;
//aws qcloud mix,single
private String cloud;
private String project;
private String ip;
private Integer id;
}
... ...
package com.monitor.model.domain;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
/**
* Created by zhengyouwei on 2016/10/19.
*/
@Data
public class RestartProcess {
private RestartModel restartModel;
private List<String> toDoList = new ArrayList<>();
private List<String> doneList = new ArrayList<>();
private List<String> allList = new ArrayList<>();
private String donePersent = "0%";
//1 正在进行 //2 已经结束
private int status = 1;
}
... ...
package com.monitor.model.domain;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
/**
* Created by zhengyouwei on 2016/10/19.
*/
@Data
public class RestartProcessResult {
private String toDoList;
private String doneList;
private String donePersent = "0%";
}
... ...
package com.monitor.other.projectRestart.task;
import com.monitor.influxdb.mapper.JavaProjectMapper;
import com.monitor.model.domain.JavaProjectStatus;
import com.monitor.other.projectRestart.common.Project;
import com.monitor.other.projectRestart.common.ProjectOnline;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
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;
/**
* Created by zhengyouwei on 2016/10/17.
*/
@Component
public class ProjectStatusTask {
private static Logger logger = LoggerFactory.getLogger(ProjectStatusTask.class);
@Autowired
private JavaProjectMapper javaProjectMapper;
@Scheduled(cron = "${cron_task_project_status}")
public void check(){
for (Project project :ProjectOnline.getJavaList()){
try {
String name = project.getName();
String commond = "ssh master@10.66.4.25 sh /home/master/.java_pm/java_restart.sh checkstatus mix " + name;
List<String> list = exe(commond);
for (String string : list){
String[] array = string.split("@@");
if (array.length < 3){
continue;
}
JavaProjectStatus javaProjectStatus = new JavaProjectStatus();
javaProjectStatus.setName(name);
javaProjectStatus.setCloud(array[0]);
javaProjectStatus.setIp(array[1]);
javaProjectStatus.setStatus(array[2]);
javaProjectStatus.setLastStart(array[3]);
javaProjectMapper.insert(javaProjectStatus);
}
}catch (Exception e){
logger.error("check erreo!",e);
}
}
}
public List<String> exe(String commond) {
Process process;
List<String> list = new ArrayList<>();
BufferedReader input = null;
try {
process = Runtime.getRuntime().exec(commond);
input = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
list.add(line);
}
} catch (Exception e) {
logger.error("process exe failed!", e);
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
logger.error("process exe failed!", e);
}
}
}
return list;
}
}
... ... @@ -6,6 +6,8 @@ import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* 执行脚本
... ... @@ -63,4 +65,48 @@ public class CommodUtil {
return sb.toString();
}
/**
* 执行远程命令
* @param commond
* @return
*/
public static List<String> exe(String commond) {
// try {
// Thread.sleep(5000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// List<String> list = new ArrayList<>();
//
// if (commond.indexOf("getIPs") != -1){
// list.add("$SERVER@@master@172.31.28.161@@1@@");
// list.add("$SERVER@@master@172.31.28.162@@1@@");
// }
// return list;
Process process;
List<String> list = new ArrayList<>();
BufferedReader input = null;
try {
process = Runtime.getRuntime().exec(commond);
input = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
list.add(line);
}
} catch (Exception e) {
logger.error("process exe failed!", e);
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
logger.error("process exe failed!", e);
}
}
}
return list;
}
}
... ...
package com.monitor.other.projectRestart.common;
package com.monitor.switchs.projectRestart.common;
import com.monitor.model.domain.Project;
import java.util.ArrayList;
import java.util.List;
... ...
package com.monitor.switchs.projectRestart.common;
import com.monitor.model.domain.RestartProcess;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Created by zhengyouwei on 2016/10/19.
*/
public class RestartProcessStore {
private static ConcurrentHashMap<Integer, RestartProcess> processMap = new ConcurrentHashMap<Integer, RestartProcess>();
private static AtomicInteger atomicInteger = new AtomicInteger(1);
public static Integer put(RestartProcess process) {
Integer id = atomicInteger.getAndIncrement();
processMap.put(id, process);
return id;
}
public static RestartProcess get(Integer id) {
return processMap.get(id);
}
public static void remove(Integer id) {
processMap.remove(id);
}
public static void clear(){
boolean clear = true;
for(Integer key:processMap.keySet()){
if(!clear){
break;
}
RestartProcess process = processMap.get(key);
if(process.getStatus() == 1 ){
clear = false;
}
}
if (clear){
processMap.clear();
}
}
}
... ...
package com.monitor.other.projectRestart.ctrl;
package com.monitor.switchs.projectRestart.ctrl;
import com.model.JavaApiInfo;
import com.monitor.influxdb.mapper.JavaProjectMapper;
import com.monitor.model.domain.JavaProjectStatus;
import com.monitor.model.domain.PageBean;
import com.monitor.model.domain.*;
import com.monitor.model.page.PageRequest;
import com.monitor.model.request.JavaApiInfoReq;
import com.monitor.model.response.BaseResponse;
import com.monitor.model.response.PageResponse;
import com.monitor.other.projectRestart.common.Project;
import com.monitor.other.projectRestart.common.ProjectOnline;
import com.monitor.switchs.projectRestart.common.ProjectOnline;
import com.monitor.switchs.projectRestart.common.RestartProcessStore;
import com.monitor.switchs.projectRestart.service.RestartService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
/**
... ... @@ -30,12 +28,12 @@ public class JavaRestartCtrl {
Logger log = LoggerFactory.getLogger(JavaRestartCtrl.class);
@Autowired
private JavaProjectMapper javaProjectMapper;
private RestartService restartService;
@RequestMapping("/query")
@ResponseBody
public BaseResponse<PageResponse<Project>> getProjectInfos(@RequestBody PageRequest req) throws Exception {
PageResponse<Project> response = new PageResponse<Project>();
PageResponse<Project> response = new PageResponse<>();
try {
// 组装分页对象
PageBean page = PageBean.initPageInfo(req.getCurrentPage(),
... ... @@ -61,7 +59,7 @@ public class JavaRestartCtrl {
@ResponseBody
public BaseResponse<List<JavaProjectStatus>> getStatusInfos(String project) {
try {
List<JavaProjectStatus> list = javaProjectMapper.getByProject(project);
List<JavaProjectStatus> list = restartService.getStatusInfos(project);
return new BaseResponse<>(list);
} catch (Exception e) {
log.error("getStatusInfos ~", e);
... ... @@ -70,4 +68,60 @@ public class JavaRestartCtrl {
}
@RequestMapping("/stopOrRestart")
public BaseResponse<List<RestartModel>> stopOrRestart(@RequestBody RestartModel restartModel) {
try {
List<RestartModel> integerList = new ArrayList<>();
if ("all".equals(restartModel.getProject())) {
RestartModel model;
for (Project project : ProjectOnline.getJavaList()) {
model = new RestartModel();
model.setProject(project.getName());
model.setIp(restartModel.getIp());
model.setCloud(restartModel.getCloud());
model.setExe(restartModel.getExe());
RestartProcess restartProcess = new RestartProcess();
restartProcess.setRestartModel(restartModel);
Integer id = RestartProcessStore.put(restartProcess);
model.setId(id);
integerList.add(model);
restartService.stopOrRestart(id);
}
} else {
RestartProcess restartProcess = new RestartProcess();
restartProcess.setRestartModel(restartModel);
Integer id = RestartProcessStore.put(restartProcess);
restartModel.setId(id);
integerList.add(restartModel);
restartService.stopOrRestart(id);
}
return new BaseResponse<>(integerList);
} catch (Exception e) {
log.error("stopOrRestart ~", e);
return null;
}
}
@RequestMapping("/getMessage")
@ResponseBody
public BaseResponse<RestartProcessResult> getMessage(Integer id) {
RestartProcess restartProcess = RestartProcessStore.get(id);
if (restartProcess == null){
BaseResponse baseResponse = new BaseResponse<>();
baseResponse.setCode(300);
return baseResponse;
}
RestartProcessResult restartProcessResult = new RestartProcessResult();
restartProcessResult.setToDoList(restartProcess.getToDoList().toString());
restartProcessResult.setDoneList(restartProcess.getDoneList().toString());
restartProcessResult.setDonePersent(restartProcess.getDonePersent());
if (restartProcess.getStatus() == 2) {
RestartProcessStore.remove(id);
}
return new BaseResponse<>(restartProcessResult);
}
}
... ...
package com.monitor.switchs.projectRestart.service;
import com.monitor.model.domain.JavaProjectStatus;
import java.util.List;
/**
* Created by zhengyouwei on 2016/10/19.
*/
public interface RestartService {
void cehckStatus(String cloud,String project);
List<JavaProjectStatus> getStatusInfos(String project);
void stopOrRestart(Integer id);
}
... ...
package com.monitor.switchs.projectRestart.service.impl;
import com.monitor.influxdb.mapper.JavaProjectMapper;
import com.monitor.model.domain.JavaProjectStatus;
import com.monitor.model.domain.RestartModel;
import com.monitor.model.domain.RestartProcess;
import com.monitor.switchs.CommodUtil;
import com.monitor.switchs.projectRestart.common.RestartProcessStore;
import com.monitor.switchs.projectRestart.service.RestartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
/**
* Created by zhengyouwei on 2016/10/19.
*/
@Service
public class RestartServiceImpl implements RestartService {
@Autowired
private JavaProjectMapper javaProjectMapper;
@Override
public void cehckStatus(String cloud, String project) {
String commond = "ssh master@10.66.4.25 sh /home/master/.java_pm/java_restart.sh checkstatus " + cloud + " " + project;
List<String> list = CommodUtil.exe(commond);
for (String string : list) {
String[] array = string.split("@@");
if (array.length < 3) {
continue;
}
JavaProjectStatus javaProjectStatus = new JavaProjectStatus();
javaProjectStatus.setName(project);
javaProjectStatus.setCloud(array[0]);
javaProjectStatus.setIp(array[1]);
javaProjectStatus.setStatus(array[2]);
javaProjectStatus.setLastStart(array[3]);
javaProjectMapper.insert(javaProjectStatus);
}
}
@Override
public List<JavaProjectStatus> getStatusInfos(String project) {
//return javaProjectMapper.getByProject(project);
List<JavaProjectStatus> javaProjectStatusList = new ArrayList<>();
String commond = "ssh master@10.66.4.25 sh /home/master/.java_pm/java_restart.sh checkstatus mix " + project;
List<String> list = CommodUtil.exe(commond);
for (String string : list) {
String[] array = string.split("@@");
if (array.length < 3) {
continue;
}
JavaProjectStatus javaProjectStatus = new JavaProjectStatus();
javaProjectStatus.setName(project);
javaProjectStatus.setCloud(array[0]);
javaProjectStatus.setIp(array[1]);
javaProjectStatus.setStatus(array[2]);
javaProjectStatus.setLastStart(array[3]);
javaProjectStatusList.add(javaProjectStatus);
}
return javaProjectStatusList;
}
@Async
@Override
public void stopOrRestart(Integer id) {
RestartProcess restartProcess = RestartProcessStore.get(id);
RestartModel restartModel = restartProcess.getRestartModel();
String project = restartModel.getProject();
if (!"single".equals(restartModel.getCloud())) {
String commond = "ssh master@10.66.4.25 sh /home/master/.java_pm/java_restart.sh getIPs " + restartModel.getCloud() + " " + project;
List<String> ips = CommodUtil.exe(commond);
if (ips.isEmpty()) {
restartProcess.setStatus(2);//结束
return;
}
for (String string : ips) {
if (string.indexOf("@@") == -1) {
continue;
}
String[] array = string.split("@@");
for (String str : array) {
if (str.indexOf("@") != -1) {
restartProcess.getAllList().add(str.split("@")[1]);
restartProcess.getToDoList().add(str.split("@")[1]);
}
}
}
} else {
restartProcess.getAllList().add(restartModel.getIp());
restartProcess.getToDoList().add(restartModel.getIp());
}
if (restartProcess.getAllList().isEmpty()) {
restartProcess.setStatus(2);//结束
return;
}
DecimalFormat formater = new DecimalFormat("######0.00");
formater.setRoundingMode(RoundingMode.UP);
for (int i = 0; i < restartProcess.getAllList().size(); i++) {
String commond = "ssh master@10.66.4.25 sh /home/master/.java_pm/java_restart.sh " + restartModel.getExe() + " single " + project + " " + restartProcess.getAllList().get(i);
CommodUtil.exe(commond);
restartProcess.getToDoList().remove(0);
restartProcess.getDoneList().add(restartProcess.getAllList().get(i));
restartProcess.setDonePersent(formater.format((double) restartProcess.getDoneList().size() / restartProcess.getAllList().size() * 100) + "%");
}
restartProcess.setStatus(2);
cehckStatus(restartModel.getCloud(),restartModel.getProject());
}
}
... ...
package com.monitor.switchs.projectRestart.task;
import com.monitor.model.domain.Project;
import com.monitor.switchs.projectRestart.common.ProjectOnline;
import com.monitor.switchs.projectRestart.service.RestartService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* Created by zhengyouwei on 2016/10/17.
*/
@Component
public class ProjectStatusTask {
private static Logger logger = LoggerFactory.getLogger(ProjectStatusTask.class);
@Autowired
private RestartService restartService;
@Scheduled(cron = "${cron_task_project_status}")
public void check(){
for (Project project : ProjectOnline.getJavaList()){
try {
String name = project.getName();
restartService.cehckStatus("mix",name);
}catch (Exception e){
logger.error("check erreo!",e);
}
}
}
}
... ...