Authored by qinchao

优化代码

... ... @@ -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;
/**
* Created by xh on 2017/6/13.
... ... @@ -38,4 +40,36 @@ public class CommodUtil {
}
return sb.toString();
}
/**
* 执行远程命令
* @param commond
* @return
*/
public static List<String> exeReturnList(String commond) {
Process process;
List<String> list = new ArrayList<>();
BufferedReader input = null;
try {
logger.info(" - exe 1 parms - command=" + commond);
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;
}
}
\ No newline at end of file
... ...
... ... @@ -128,67 +128,4 @@ public class SwitchCmdUtil {
return mapList;
}
/**
* 执行远程命令
* @param commond
* @return
*/
public static List<String> exe(String commond) {
Process process;
List<String> list = new ArrayList<>();
BufferedReader input = null;
try {
logger.info(" - exe 1 parms - command=" + commond);
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;
}
/*
* dns切换查看
* @param commond
* @param param
* @return
*/
/*public static String exeDns(String commond, String param) {
Process process = null;
StringBuilder sb = new StringBuilder();
BufferedReader input = null;
try {
String command = BASE_DIR_DNS + commond + " " + param;
logger.info(" - exeDns parms - command=" + command);
process = Runtime.getRuntime().exec(command);
input = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = input.readLine()) != null) {
sb.append(line + "\r\n");
}
} catch (Exception e) {
logger.error(" process exeDns failed!", e);
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
logger.error("process exeDns failed!", e);
}
}
}
return sb.toString();
}*/
}
\ No newline at end of file
... ...
package com.monitor.switchs.centerSwitch.service;
import com.contants.AlarmGroupContants;
import com.monitor.cloudtools.util.CommodUtil;
import com.monitor.common.service.AlarmMsgService;
import com.monitor.model.domain.CenterSwitchModel;
import com.monitor.switchs.SwitchCmdUtil;
... ... @@ -46,7 +47,7 @@ public class SwitchService {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
logger.info(" SwitchService - doCommond - "+df.format(new Date())+" - command=" + commond.toString());
List<String> resultList = SwitchCmdUtil.exe(commond.toString());
List<String> resultList = CommodUtil.exeReturnList(commond.toString());
StringBuilder result = new StringBuilder();
for (String str : resultList) {
... ...
package com.monitor.switchs.projectRestart.common;
import com.monitor.cloudtools.util.CommodUtil;
import com.monitor.common.ProjectConstant;
import com.monitor.model.domain.RestartProcess;
import com.monitor.switchs.SwitchCmdUtil;
... ... @@ -53,7 +54,7 @@ public class ExeIpTask implements Runnable {
if ("restart".equals(exe)){
IpStatus.add(project,ip);
}
SwitchCmdUtil.exe(commond);
CommodUtil.exeReturnList(commond);
if ("restart".equals(exe)){
IpStatus.remove(project,ip);
}
... ...
package com.monitor.switchs.projectRestart.common;
import com.monitor.cloudtools.util.CommodUtil;
import com.monitor.common.ProjectConstant;
import com.monitor.model.domain.RestartModel;
import com.monitor.model.domain.RestartProcess;
... ... @@ -36,7 +37,7 @@ public class ExeProjectTask implements Runnable {
int firstLength;
if (!"single".equals(restartModel.getCloud())) {
String commond = "ssh master@"+ ProjectConstant.DEPLOY_IP+" sh /home/master/.java_pm/java_restart.sh getIPs " + restartModel.getCloud() + " " + project;
List<String> ips = SwitchCmdUtil.exe(commond);
List<String> ips = CommodUtil.exeReturnList(commond);
if (ips.isEmpty()) {
log.error("ips is null,cloud:{},project:{}", restartModel.getCloud(), project);
restartProcess.setStatus(2);//结束
... ...
package com.monitor.switchs.projectRestart.service.impl;
import com.monitor.cloudtools.util.CommodUtil;
import com.monitor.common.ProjectConstant;
import com.monitor.model.domain.JavaProjectStatus;
import com.monitor.switchs.SwitchCmdUtil;
... ... @@ -67,7 +68,7 @@ public class JavaProjectStatusLoaderTask {
private List<JavaProjectStatus> getStatusInfos(String projectName) {
List<JavaProjectStatus> javaProjectStatusList = new ArrayList<>();
String commond = "ssh master@"+ ProjectConstant.DEPLOY_IP+" sh /home/master/.java_pm/java_restart.sh checkstatus mix " + projectName;
List<String> list = SwitchCmdUtil.exe(commond);
List<String> list = CommodUtil.exeReturnList(commond);
for (String string : list) {
String[] array = string.split("@@");
if (array.length < 3) {
... ...
package com.monitor.switchs.projectRestart.service.impl;
import com.model.HostInfo;
import com.monitor.cloudtools.util.CommodUtil;
import com.monitor.cmdb.service.IHostInfoService;
import com.monitor.common.ProjectConstant;
import com.monitor.common.contants.ProjectOnline;
... ... @@ -64,7 +65,7 @@ public class RestartServiceImpl implements RestartService {
String[] projectTag_array=currentProject.getTags().split(",");
String commond = "ssh master@"+ ProjectConstant.DEPLOY_IP+" sh /home/master/.java_pm/java_restart.sh getIPs mix " + projectName;
List<String> ips = SwitchCmdUtil.exe(commond);
List<String> ips = CommodUtil.exeReturnList(commond);
log.info("RestartServiceImpl updateHostTags get host ips is {} ",ips);
if (ips!=null&&ips.size()>0) {
for (String string : ips) {
... ...