You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
8.0 KiB
Java

package cfs.trsmrcv.web;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import cfs.WASInfo;
import cfs.common.util.ResultSmartPlatform;
import cfs.schd.dao.SchdDao;
import cfs.trsmrcv.dao.TrsmrcvDao;
@Controller
public class TestController {
protected Logger logger = LoggerFactory.getLogger(this.getClass());
@Resource(name = "trsmrcvDao")
private TrsmrcvDao trsmrcvDao;
@Resource(name = "schdDao")
private SchdDao schdDao;
@RequestMapping(value="/cfs/trsmrcv/sleep.do", method=RequestMethod.GET)
public @ResponseBody Map<String, Object> sleep(HttpServletRequest request) throws Exception {
TrsmrcvUtil.printRequestHeader(request);
logger.info("sleep 요청");
Thread.sleep(100000);
logger.info("sleep 끝");
ResultSmartPlatform result = new ResultSmartPlatform();
result.setMsg(ResultSmartPlatform.STATUS_SUCESS, "정상적으로 처리되었습니다.");
return result.getResult();
}
@RequestMapping(value="/cfs/trsmrcv/insertSusinLog.do", method=RequestMethod.GET)
public @ResponseBody Map<String, Object> insertSusinLog(HttpServletRequest request) throws Exception {
TrsmrcvUtil.printRequestHeader(request);
Map<String,String> susinInfo = TrsmrcvUtil.getInfoMapForConnSusinLog(request);
trsmrcvDao.insertConnSusinLog(susinInfo);
Map result = new HashMap();
result.put("res", "ok");
return result;
}
@RequestMapping(value="/cfs/trsmrcv/updateRunWas.do", method=RequestMethod.GET)
public @ResponseBody Map<String, Object> updateRunWas(HttpServletRequest request) throws Exception {
String schdulName = request.getParameter("schdulName");
Map<String,String> map = new HashMap<String,String>();
map.put("runWas", WASInfo.getWAS());
map.put("schdulName", schdulName);
schdDao.updateRunWas(map);
Map result = new HashMap();
result.put("res", "ok");
return result;
}
@RequestMapping(value="/rest/test/nothing.do", method=RequestMethod.POST)
public @ResponseBody Map<String, Object> nothing(@RequestBody Map<String,Object> reqMap) throws Exception {
Map result = new HashMap();
String commandSeq = trsmrcvDao.getCommandSeq("");
Map<String, String> commandInMap = new HashMap<String,String>();
commandInMap.put("commandSeq", commandSeq);
commandInMap.put("sysName", "monitoring");
commandInMap.put("command", "nothing");
trsmrcvDao.insertCommand(commandInMap);
this.touchAndWait("monitoring", commandSeq);
result.put("res", "OK");
return result;
}
@RequestMapping(value="/rest/test/bringJuso.do", method=RequestMethod.POST)
public @ResponseBody Map<String, Object> bringJuso(@RequestBody Map<String,Object> reqMap) throws Exception {
Map result = new HashMap();
String commandSeq = trsmrcvDao.getCommandSeq("");
Map<String, String> commandInMap = new HashMap<String,String>();
commandInMap.put("commandSeq", commandSeq);
commandInMap.put("sysName", "monitoring");
commandInMap.put("command", "bringJuso");
trsmrcvDao.insertCommand(commandInMap);
this.touchAndWait("monitoring", commandSeq);
result.put("res", "OK");
return result;
}
@RequestMapping(value="/rest/test/sendMail.do", method=RequestMethod.POST)
public @ResponseBody Map<String, Object> sendMail(@RequestBody Map<String,Object> reqMap) throws Exception {
Map result = new HashMap();
String SEND_MAIL_MANAGER = "monitoring";
String commandSeq = trsmrcvDao.getCommandSeq("");
Map<String, String> commandInMap = new HashMap<String,String>();
commandInMap.put("commandSeq", commandSeq);
commandInMap.put("sysName", SEND_MAIL_MANAGER);
commandInMap.put("command", "sendMail");
trsmrcvDao.insertCommand(commandInMap);
List<String> hpup = (List<String>) reqMap.get("hpup");
List<String> mailProps = (List<String>) reqMap.get("mailProps");
String from = reqMap.get("from").toString();
List<String> to = (List<String>) reqMap.get("to");
String subject = reqMap.get("subject").toString();
String text = reqMap.get("text").toString();
Map<String,String> mailInMap = new HashMap<String,String>();
mailInMap.put("commandSeq", commandSeq);
mailInMap.put("hpup", String.join(",", hpup));
mailInMap.put("mailProps", String.join(",", mailProps));
mailInMap.put("from", from);
mailInMap.put("to", String.join(",", to));
mailInMap.put("subject", subject);
mailInMap.put("text", text);
trsmrcvDao.insertSendMail(mailInMap);
this.touchAndWait(SEND_MAIL_MANAGER, commandSeq);
result.put("res", "OK");
return result;
}
private Map<String,Object> touchAndWait(String touchSys, String commandSeq) throws Exception {
Map<String,Object> commandResult = new HashMap<String,Object>();
this.shellCmd("/gpta/source-app/cfs/relay_jar/ssh_sh.sh " + touchSys);
int FIRST_SLEEP = 2000;
Thread.sleep(FIRST_SLEEP);
int WAITING_CNT = 20;
int WAITING_SLEEP = 1000;
boolean commandComplate = false;
for(int i=0; i < WAITING_CNT; i++){
Map<String, String> commandOutMap = trsmrcvDao.selectCommandById(commandSeq);
if(commandOutMap.get("STAT_CD").equals("2")){
commandComplate = true;
//TODO if need result
//commandResult.put
}
if(commandComplate){
break;
}
Thread.sleep(WAITING_SLEEP);
}
return commandResult;
}
private boolean shellCmd(String cmd) {
Process process = null;
Runtime runtime = Runtime.getRuntime();
StringBuffer successOutput = new StringBuffer();
StringBuffer errorOutput = new StringBuffer();
BufferedReader successBufferReader = null;
BufferedReader errorBufferReader = null;
String msg = null;
boolean result = false;
logger.info("shellCmd - 1");
List<String> cmdList = new ArrayList<String>();
cmdList.add("/bin/sh");
cmdList.add("-c");
cmdList.add(cmd);
String[] array = cmdList.toArray(new String[cmdList.size()]);
try {
process = runtime.exec(array);
successBufferReader = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
while ((msg = successBufferReader.readLine()) != null) {
successOutput.append(msg + System.getProperty("line.separator"));
}
errorBufferReader = new BufferedReader(new InputStreamReader(process.getErrorStream(), "UTF-8"));
while ((msg = errorBufferReader.readLine()) != null) {
errorOutput.append(msg + System.getProperty("line.separator"));
}
process.waitFor();
if (process.exitValue() == 0) {
result = true;
} else {
logger.info("abnormal finish error : " + successOutput.toString());
}
if (errorOutput.toString().length() > 0) {
logger.info("error : " + errorOutput.toString());
}
} catch (IOException e) {
logger.info(e.getMessage());
} catch (InterruptedException e) {
logger.info(e.getMessage());
} finally {
try {
process.destroy();
if (successBufferReader != null) successBufferReader.close();
if (errorBufferReader != null) errorBufferReader.close();
} catch (IOException e1) {
logger.info(e1.getMessage());
}
}
return result;
}
}