修复一处脚本调用仍然使用原expect方式的问题;

增加执行linux命令的日志
This commit is contained in:
chenjinsong
2018-10-15 14:20:43 +08:00
parent 2c6be7c10c
commit c8439b9403
4 changed files with 50 additions and 10 deletions

View File

@@ -61,6 +61,7 @@ public class ConfigController extends Controller {
result = safeProcessUtil.execShellSafe("device_config", sb.toString()); result = safeProcessUtil.execShellSafe("device_config", sb.toString());
} else { } else {
String remoteCommand = PropKit.get("device_config_scriptCmd") + " '" + sb.toString() + "'"; String remoteCommand = PropKit.get("device_config_scriptCmd") + " '" + sb.toString() + "'";
logger.info(ip + "执行:" + remoteCommand);
try { try {
List remoteExec = LinuxUtil.remoteExec(ip, user, pwd, remoteCommand); List remoteExec = LinuxUtil.remoteExec(ip, user, pwd, remoteCommand);
String execResult = remoteExec.get(0).toString(); String execResult = remoteExec.get(0).toString();
@@ -197,6 +198,7 @@ public class ConfigController extends Controller {
result = safeProcessUtil.execShellSafe("device_config", sb.toString()); result = safeProcessUtil.execShellSafe("device_config", sb.toString());
} else { } else {
String remoteCommand = PropKit.get("device_config_scriptCmd") + " '" + sb.toString() + "'"; String remoteCommand = PropKit.get("device_config_scriptCmd") + " '" + sb.toString() + "'";
logger.info(ip + "执行:" + remoteCommand);
try { try {
List remoteExec = LinuxUtil.remoteExec(temp, user, pwd, remoteCommand); List remoteExec = LinuxUtil.remoteExec(temp, user, pwd, remoteCommand);
String execResult = remoteExec.get(0).toString(); String execResult = remoteExec.get(0).toString();
@@ -210,6 +212,7 @@ public class ConfigController extends Controller {
// 修改数据库的通信服务器地址信息 // 修改数据库的通信服务器地址信息
try { try {
String execLinuxCmd = LinuxUtil.execLinuxCmd("ping -c 3 " + ip); String execLinuxCmd = LinuxUtil.execLinuxCmd("ping -c 3 " + ip);
logger.info(ip + "执行:" + execLinuxCmd);
if (execLinuxCmd.contains("ttl") && execLinuxCmd.contains("time")) { if (execLinuxCmd.contains("ttl") && execLinuxCmd.contains("time")) {
NodeTable modifyData = new NodeTable(); NodeTable modifyData = new NodeTable();
modifyData.set("ip", ip); modifyData.set("ip", ip);
@@ -267,6 +270,7 @@ public class ConfigController extends Controller {
for (NodeTable node : serverDtos) { for (NodeTable node : serverDtos) {
if (node.getInt("isMaster") == 0) { if (node.getInt("isMaster") == 0) {
try { try {
logger.info(node.getStr("ip") + "执行:" + "reboot");
LinuxUtil.remoteExec(node.getStr("ip"), node.getStr("user"), node.getStr("pwd"), "reboot"); LinuxUtil.remoteExec(node.getStr("ip"), node.getStr("user"), node.getStr("pwd"), "reboot");
} catch (JSchException e) { } catch (JSchException e) {
logger.error("reboot error:" + node.getStr("ip"), e); logger.error("reboot error:" + node.getStr("ip"), e);
@@ -290,6 +294,7 @@ public class ConfigController extends Controller {
for (NodeTable node : serverDtos) { for (NodeTable node : serverDtos) {
if (node.getInt("isMaster") == 0) { if (node.getInt("isMaster") == 0) {
try { try {
logger.info(node.getStr("ip") + "执行:" + "shutdown -h now");
LinuxUtil.remoteExec(node.getStr("ip"), node.getStr("user"), node.getStr("pwd"), "shutdown -h now"); LinuxUtil.remoteExec(node.getStr("ip"), node.getStr("user"), node.getStr("pwd"), "shutdown -h now");
} catch (JSchException e) { } catch (JSchException e) {
logger.error("shutdown error:" + node.getStr("ip"), e); logger.error("shutdown error:" + node.getStr("ip"), e);
@@ -297,6 +302,7 @@ public class ConfigController extends Controller {
} }
} }
try { try {
logger.info("本机执行:" + "shutdown -h now");
LinuxUtil.execLinuxCmd("shutdown -h now"); LinuxUtil.execLinuxCmd("shutdown -h now");
} catch (Exception e) { } catch (Exception e) {
logger.error("shutdown error", e); logger.error("shutdown error", e);

View File

@@ -164,6 +164,7 @@ public class ProcessController extends Controller {
//String updateResult = LinuxUtil.execLinuxCmd("/home/wwr/update.sh "+updateFile+" "+uploadPath); //String updateResult = LinuxUtil.execLinuxCmd("/home/wwr/update.sh "+updateFile+" "+uploadPath);
File uploadF = new File(updateFile); File uploadF = new File(updateFile);
updateFile = uploadPath + File.separator + uploadF.getName(); updateFile = uploadPath + File.separator + uploadF.getName();
logger.info(ip + "执行:" + updateFile + " -C " + uploadPath);
List remoteExec = LinuxUtil.remoteExec(ip, user, pwd, "tar -xf " + updateFile + " -C " + uploadPath); List remoteExec = LinuxUtil.remoteExec(ip, user, pwd, "tar -xf " + updateFile + " -C " + uploadPath);
if (remoteExec.get(1).toString().equals("0")) { if (remoteExec.get(1).toString().equals("0")) {
process.set("update_result", 1); process.set("update_result", 1);
@@ -174,7 +175,9 @@ public class ProcessController extends Controller {
process.set("update_date", DateUtil.formatTime(new Date())); process.set("update_date", DateUtil.formatTime(new Date()));
logger.error("更新进程失败!"); logger.error("更新进程失败!");
} }
logger.info(ip + "执行:" + scriptPath);
LinuxUtil.remoteExec(ip, user, pwd, scriptPath); LinuxUtil.remoteExec(ip, user, pwd, scriptPath);
logger.info(ip + "执行:" + "rm -f " + updateFile);
LinuxUtil.remoteExec(ip, user, pwd, "rm -f " + updateFile); LinuxUtil.remoteExec(ip, user, pwd, "rm -f " + updateFile);
/*if(StrKit.notBlank(updateResult)&&updateResult.equals("success")){ /*if(StrKit.notBlank(updateResult)&&updateResult.equals("success")){
/*}else{ /*}else{

View File

@@ -59,9 +59,11 @@ public class RouteController extends Controller {
logger.info("query route request para --" + sb.toString()); logger.info("query route request para --" + sb.toString());
Map<String, Object> result = new HashMap<String, Object>(); Map<String, Object> result = new HashMap<String, Object>();
if ((Integer) serverDto.get("isMaster") == 1) { if ((Integer) serverDto.get("isMaster") == 1) {
logger.info("本机执行:" + sb.toString());
result = LinuxUtil.execShell("device_config", sb.toString()); result = LinuxUtil.execShell("device_config", sb.toString());
} else { } else {
String remoteCommand = PropKit.get("device_config_scriptCmd") + " '" + sb.toString() + "'"; String remoteCommand = PropKit.get("device_config_scriptCmd") + " '" + sb.toString() + "'";
logger.info(ip + "执行:" + remoteCommand);
try { try {
List remoteExec = LinuxUtil.remoteExec(ip, user, pwd, remoteCommand); List remoteExec = LinuxUtil.remoteExec(ip, user, pwd, remoteCommand);
String execResult = remoteExec.get(0).toString(); String execResult = remoteExec.get(0).toString();
@@ -119,10 +121,12 @@ public class RouteController extends Controller {
logger.info("modify route request para --" + sb.toString()); logger.info("modify route request para --" + sb.toString());
Map<String, Object> result = new HashMap<String, Object>(); Map<String, Object> result = new HashMap<String, Object>();
if ((Integer) nodeTable.get("isMaster") == 1) { if ((Integer) nodeTable.get("isMaster") == 1) {
logger.info("本机执行:" + "device_config " + sb.toString());
result = LinuxUtil.execShell("device_config", sb.toString()); result = LinuxUtil.execShell("device_config", sb.toString());
} else { } else {
String remoteCommand = PropKit.get("device_config_scriptCmd") + " '" + sb.toString() + "'"; String remoteCommand = PropKit.get("device_config_scriptCmd") + " '" + sb.toString() + "'";
try { try {
logger.info(ip + "执行:" + remoteCommand);
List remoteExec = LinuxUtil.remoteExec(ip, user, pwd, remoteCommand); List remoteExec = LinuxUtil.remoteExec(ip, user, pwd, remoteCommand);
String execResult = remoteExec.get(0).toString(); String execResult = remoteExec.get(0).toString();
result.put("in", execResult); result.put("in", execResult);
@@ -168,10 +172,12 @@ public class RouteController extends Controller {
logger.info("remove route request para --" + sb.toString()); logger.info("remove route request para --" + sb.toString());
Map<String, Object> result = new HashMap<String, Object>(); Map<String, Object> result = new HashMap<String, Object>();
if ((Integer) nodeTable.get("isMaster") == 1) { if ((Integer) nodeTable.get("isMaster") == 1) {
logger.info("本机执行:" + "device_config " + sb.toString());
result = LinuxUtil.execShell("device_config", sb.toString()); result = LinuxUtil.execShell("device_config", sb.toString());
} else { } else {
String remoteCommand = PropKit.get("device_config_scriptCmd") + " '" + sb.toString() + "'"; String remoteCommand = PropKit.get("device_config_scriptCmd") + " '" + sb.toString() + "'";
try { try {
logger.info(ip + "执行:" + remoteCommand);
List remoteExec = LinuxUtil.remoteExec(ip, user, pwd, remoteCommand); List remoteExec = LinuxUtil.remoteExec(ip, user, pwd, remoteCommand);
String execResult = remoteExec.get(0).toString(); String execResult = remoteExec.get(0).toString();
result.put("in", execResult); result.put("in", execResult);
@@ -215,10 +221,12 @@ public class RouteController extends Controller {
logger.info("create route request para --" + sb.toString()); logger.info("create route request para --" + sb.toString());
Map<String, Object> result = new HashMap<String, Object>(); Map<String, Object> result = new HashMap<String, Object>();
if ((Integer) nodeTable.get("isMaster") == 1) { if ((Integer) nodeTable.get("isMaster") == 1) {
logger.info("本机执行:" + "device_config " + sb.toString());
result = LinuxUtil.execShell("device_config", sb.toString()); result = LinuxUtil.execShell("device_config", sb.toString());
} else { } else {
String remoteCommand = PropKit.get("device_config_scriptCmd") + " '" + sb.toString() + "'"; String remoteCommand = PropKit.get("device_config_scriptCmd") + " '" + sb.toString() + "'";
try { try {
logger.info(ip + "执行:" + remoteCommand);
List remoteExec = LinuxUtil.remoteExec(ip, user, pwd, remoteCommand); List remoteExec = LinuxUtil.remoteExec(ip, user, pwd, remoteCommand);
String execResult = remoteExec.get(0).toString(); String execResult = remoteExec.get(0).toString();
result.put("in", execResult); result.put("in", execResult);
@@ -255,10 +263,12 @@ public class RouteController extends Controller {
logger.info("queryRouteExist request para --" + sb.toString()); logger.info("queryRouteExist request para --" + sb.toString());
Map<String, Object> result = new HashMap<String, Object>(); Map<String, Object> result = new HashMap<String, Object>();
if ((Integer) nodeTable.get("isMaster") == 1) { if ((Integer) nodeTable.get("isMaster") == 1) {
logger.info("本机执行:" + "device_config " + sb.toString());
result = LinuxUtil.execShell("device_config", sb.toString()); result = LinuxUtil.execShell("device_config", sb.toString());
} else { } else {
String remoteCommand = PropKit.get("device_config_scriptCmd") + " '" + sb.toString() + "'"; String remoteCommand = PropKit.get("device_config_scriptCmd") + " '" + sb.toString() + "'";
try { try {
logger.info(ip + "执行:" + remoteCommand);
List remoteExec = LinuxUtil.remoteExec(ip, user, pwd, remoteCommand); List remoteExec = LinuxUtil.remoteExec(ip, user, pwd, remoteCommand);
String execResult = remoteExec.get(0).toString(); String execResult = remoteExec.get(0).toString();
result.put("in", execResult); result.put("in", execResult);

View File

@@ -55,8 +55,7 @@ public class ProcessInfoService {
//获取脚本路径 //获取脚本路径
String scriptPath = PropKit.get("process_info_scriptPath"); String scriptPath = PropKit.get("process_info_scriptPath");
logger.debug("scriptPath:"+scriptPath); if(procList!=null && procList.size() > 0){
if(procList!=null && procList.size()>0 && !StringUtils.isEmpty(scriptPath)){
for (ProcessInfo processInfo : procList) { for (ProcessInfo processInfo : procList) {
Map<String,Object> proc=new HashMap<>(); Map<String,Object> proc=new HashMap<>();
@@ -65,17 +64,18 @@ public class ProcessInfoService {
String result=null; String result=null;
if(!StringUtils.isEmpty(pidFile)){ if(!StringUtils.isEmpty(pidFile)){
try { try {
String cmd=scriptPath+" "+ip+" "+user+" "+pwd+" "+0+" "+pidFile; //String cmd=scriptPath+" "+ip+" "+user+" "+pwd+" "+0+" "+pidFile;
//result = LinuxUtil.execLinuxExpect(cmd); //result = LinuxUtil.execLinuxExpect(cmd);
result = getResult(ip, user, pwd, pidFile); result = getByPid(ip, user, pwd, pidFile);
} catch (Exception e) { } catch (Exception e) {
logger.error("执行脚本失败!"+e); logger.error("执行脚本失败!"+e);
} }
}else{ }else{
String keyword=processInfo.getStr("keyword"); String keyword=processInfo.getStr("keyword");
try { try {
String cmd=scriptPath+" "+ip+" "+user+" "+pwd+" "+1+" "+keyword; //String cmd=scriptPath+" "+ip+" "+user+" "+pwd+" "+1+" "+keyword;
result = LinuxUtil.execLinuxExpect(cmd); //result = LinuxUtil.execLinuxExpect(cmd);
result = getByKeyword(ip, user, pwd, keyword);
} catch (Exception e) { } catch (Exception e) {
logger.error("执行脚本失败!"+e); logger.error("执行脚本失败!"+e);
} }
@@ -189,7 +189,7 @@ public class ProcessInfoService {
return processInfo; return processInfo;
} }
public static String getResult(String ip, String user, String pwd, String pidFile) { public static String getByPid(String ip, String user, String pwd, String pidFile) {
String command = ""; String command = "";
String result = ""; String result = "";
try { try {
@@ -205,10 +205,29 @@ public class ProcessInfoService {
}*/ }*/
command = "pid=`cat " + pidFile + "`;" command = "pid=`cat " + pidFile + "`;"
+ "status=`ps -ef |grep $pid |grep -v -e grep -e tail -e expect |wc -l`;" + "status=`ps -ef |grep $pid |grep -v -e grep -e tail -e expect |wc -l`;"
+ "if [[ $status -eq '1' ]];then " + "if [ $status -ge 1 ];then "
+ "echo ${status},`ps -ef |grep $pid |grep -v -e grep -e tail -e expect | awk '{print $5}'`,$pid,`ps aux |grep $pid |grep -v -e grep -e tail -e expect | awk '{print $3}'`,`ps aux |grep $pid |grep -v -e grep -e tail -e expect | awk '{print $4}'`;" + "echo ${status},`ps -ef |grep $pid |grep -v -e grep -e tail -e expect | awk '{print $5}'`,$pid,`ps aux |grep $pid |grep -v -e grep -e tail -e expect | awk '{print $3}'`,`ps aux |grep $pid |grep -v -e grep -e tail -e expect | awk '{print $4}'`;"
+ "fi"; + "fi";
logger.info(ip + "执行:" + command);
result = LinuxUtil.remoteExec(ip, user, pwd, command).get(0).toString(); result = LinuxUtil.remoteExec(ip, user, pwd, command).get(0).toString();
logger.info(ip + "执行结果:" + result);
} catch (JSchException e) {
logger.error(e);
}
return result;
}
public static String getByKeyword(String ip, String user, String pwd, String keyword) {
String command = "";
String result = "";
try {
command = "status=`ps -ef |grep " + keyword + " |grep -v -e grep -e tail -e expect |wc -l`;"
+ "if [ $status -ge 1 ];then "
+ "echo ${status},`ps -ef |grep $pid |grep -v -e grep -e tail -e expect | awk '{print $5}'`,$pid,`ps aux |grep $pid |grep -v -e grep -e tail -e expect | awk '{print $3}'`,`ps aux |grep $pid |grep -v -e grep -e tail -e expect | awk '{print $4}'`;"
+ "fi";
logger.info(ip + "执行:" + command);
result = LinuxUtil.remoteExec(ip, user, pwd, command).get(0).toString();
logger.info(ip + "执行结果:" + result);
} catch (JSchException e) { } catch (JSchException e) {
logger.error(e); logger.error(e);
} }
@@ -220,17 +239,19 @@ public class ProcessInfoService {
String user = node.getStr("user"); String user = node.getStr("user");
String pwd = node.getStr("pwd"); String pwd = node.getStr("pwd");
String uploadPath = proc.getStr("upload_path"); String uploadPath = proc.getStr("upload_path");
String scriptPath = PropKit.get("process_info_scriptPath");
String mirrorPath = proc.getStr("mirror_path"); String mirrorPath = proc.getStr("mirror_path");
String restartSh = proc.getStr("restart_sh"); String restartSh = proc.getStr("restart_sh");
try { try {
List reset = LinuxUtil.remoteExec(ip, user, pwd, "tar -xf " + mirrorPath + " -C " + uploadPath); String cmd = "tar -xf " + mirrorPath + " -C " + uploadPath;
logger.info(ip + "执行:" + cmd);
List reset = LinuxUtil.remoteExec(ip, user, pwd, cmd);
int resetState = Integer.parseInt(reset.get(1).toString()); int resetState = Integer.parseInt(reset.get(1).toString());
//String execResult = ProcessUtil.execLinuxExpect(scriptPath+" "+ip+" "+user+" "+pwd+" "+3+" "+mirrorPath); //String execResult = ProcessUtil.execLinuxExpect(scriptPath+" "+ip+" "+user+" "+pwd+" "+3+" "+mirrorPath);
//if(StrKit.notBlank(execResult)&&execResult.contains("%#%")){ //if(StrKit.notBlank(execResult)&&execResult.contains("%#%")){
// execResult=execResult.substring(execResult.indexOf("%#%")+3,execResult.lastIndexOf("%#%")); // execResult=execResult.substring(execResult.indexOf("%#%")+3,execResult.lastIndexOf("%#%"));
if (resetState == 0) { if (resetState == 0) {
logger.info(ip + "执行:" + restartSh);
List restart = LinuxUtil.remoteExec(ip, user, pwd, restartSh); List restart = LinuxUtil.remoteExec(ip, user, pwd, restartSh);
logger.info(restart.get(0)); logger.info(restart.get(0));
int restartState = Integer.parseInt(restart.get(1).toString()); int restartState = Integer.parseInt(restart.get(1).toString());