|
|
package com.monitor.switchs;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.monitor.common.config.SnsMobileConfig;
|
|
|
import com.monitor.common.service.AlarmMsgService;
|
|
|
import com.monitor.common.util.RandomUtil;
|
|
|
import com.monitor.model.domain.SmsTicket;
|
|
|
import com.monitor.model.response.BaseResponse;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
|
|
import java.io.*;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
/**
|
|
|
* @author zhengyouwei 2016年5月12日 下午1:49:55
|
|
|
*/
|
|
|
@Controller
|
|
|
@RequestMapping("nginxswitch")
|
|
|
public class NginxSwitchCtrl {
|
|
|
|
|
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
|
|
|
private static ConcurrentHashMap<String, SmsTicket> smsTicketMap = new ConcurrentHashMap<String, SmsTicket>();
|
|
|
|
|
|
@Autowired
|
|
|
private AlarmMsgService alarmMsgService;
|
|
|
|
|
|
@Value("${system.envi}")
|
|
|
private String env;
|
|
|
|
|
|
@Value("${swaith_mobile}")
|
|
|
private String mobile;
|
|
|
|
|
|
/**
|
|
|
* 查看nginx配置
|
|
|
*
|
|
|
* @return 2016年5月12日下午1:49:48
|
|
|
*/
|
|
|
@RequestMapping(value = "viewNginxConf")
|
|
|
@ResponseBody
|
|
|
public BaseResponse viewNginxConf() {
|
|
|
Map<String,List<Map<String, Object>>> map = new HashMap<>();
|
|
|
if ("test".equals(env)) {
|
|
|
List<Map<String, Object>> awsList = localTest("aws");
|
|
|
List<Map<String, Object>> qcloudList = localTest("qq");
|
|
|
map.put("awsList", awsList);
|
|
|
map.put("qcloudList", qcloudList);
|
|
|
return new BaseResponse(map);
|
|
|
}
|
|
|
|
|
|
List<Map<String, Object>> awsList = analyseConfigFile(exe("aws", "view", null));
|
|
|
List<Map<String, Object>> qcloudList = analyseConfigFile(exe("qcloud", "view", null));
|
|
|
map.put("awsList", awsList);
|
|
|
map.put("qcloudList", qcloudList);
|
|
|
return new BaseResponse(map);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送验证码
|
|
|
*
|
|
|
* @return 2016年5月23日下午13:39:48
|
|
|
*/
|
|
|
@RequestMapping(value = "switchbefore")
|
|
|
@ResponseBody
|
|
|
public BaseResponse switchbefore(String cloudName, String target) {
|
|
|
String token = RandomUtil.getToken();
|
|
|
String ticket = RandomUtil.getTicket();
|
|
|
sendticket(ticket, cloudName, target);
|
|
|
smsTicketMap.put(token, new SmsTicket(ticket, cloudName, target));
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
|
@Override
|
|
|
public void run() {
|
|
|
try {
|
|
|
Thread.sleep(2 * 60 * 1000);
|
|
|
} catch (InterruptedException e) {
|
|
|
// TODO Auto-generated catch block
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
smsTicketMap.remove(token);
|
|
|
}
|
|
|
}).start();
|
|
|
BaseResponse<String> baseResponse = new BaseResponse<>();
|
|
|
baseResponse.setCode(200);
|
|
|
baseResponse.setData(token);
|
|
|
return baseResponse;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 修改配置
|
|
|
*
|
|
|
* @return 2016年5月12日下午1:49:48
|
|
|
*/
|
|
|
@RequestMapping(value = "switchNginxConf")
|
|
|
@ResponseBody
|
|
|
public BaseResponse switchNginxConf(String token, String ticket) {
|
|
|
SmsTicket smsTicket = null;
|
|
|
if (smsTicketMap.containsKey(token)) {
|
|
|
smsTicket = smsTicketMap.get(token);
|
|
|
String ticket_ = smsTicket.getTicket();
|
|
|
if (!ticket_.equals(ticket)) {
|
|
|
return new BaseResponse("验证码无效");
|
|
|
}
|
|
|
}
|
|
|
smsTicketMap.remove(token);
|
|
|
|
|
|
String result = exe(smsTicket.getCloudName(), "switch", smsTicket.getTarget());
|
|
|
return new BaseResponse(result);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行命令
|
|
|
*
|
|
|
* @param cloudName
|
|
|
* @param commod
|
|
|
* @return 2016年5月17日下午3:26:29
|
|
|
*/
|
|
|
public String exe(String cloudName, String commod, String target) {
|
|
|
StringBuilder commond = new StringBuilder();
|
|
|
commond.append(" -s " + cloudName);
|
|
|
commond.append(" -e " + commod);
|
|
|
commond.append(" -d " + target);
|
|
|
return CommodUtil.exe(CommodUtil.NGINX_SWITCH_SH, commond.toString());
|
|
|
}
|
|
|
|
|
|
private List<Map<String, Object>> analyseConfigFile(String rlt) {
|
|
|
List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
|
|
|
String[] str = rlt.split("\r\n");
|
|
|
Pattern pattern = Pattern.compile("upstream.*", Pattern.CASE_INSENSITIVE);
|
|
|
for (int i = 0; i < str.length; i++) {
|
|
|
Matcher matcher = pattern.matcher(str[i].trim());
|
|
|
if (matcher.matches()) {
|
|
|
List<String> serverList = new ArrayList<String>();
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
|
map.put("name", str[i].substring(str[i].indexOf("upstream") + 8, str[i].indexOf("{")).trim());
|
|
|
while (true) {
|
|
|
i++;
|
|
|
System.out.println(str[i]);
|
|
|
if (str[i].trim().startsWith("#"))
|
|
|
continue;
|
|
|
if (str[i].trim().startsWith("}")) {
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
if (str[i].trim().startsWith("server")) {
|
|
|
serverList.add(str[i].replace("server", "").trim().replace(";", ""));
|
|
|
} else {
|
|
|
String key = str[i].trim().split(" ")[0];
|
|
|
map.put(key, str[i].replace(key, "").trim().replace(";", ""));
|
|
|
}
|
|
|
|
|
|
}
|
|
|
map.put("server", serverList);
|
|
|
mapList.add(map);
|
|
|
}
|
|
|
}
|
|
|
return mapList;
|
|
|
}
|
|
|
|
|
|
public void sendticket(String ticket, String cloud, String target) {
|
|
|
alarmMsgService.sendSms("nginx_switch",cloud + "流量切至:" + target + ",验证码:" + ticket + ",两分钟内有效",mobile);
|
|
|
}
|
|
|
|
|
|
|
|
|
private List<Map<String, Object>> localTest(String cloud) {
|
|
|
|
|
|
String path = this.getClass().getResource("").getPath().substring(6);
|
|
|
int index = path.indexOf("WEB-INF");
|
|
|
path = path.substring(0, index) + "bin/";
|
|
|
if ("aws".equals(cloud)) {
|
|
|
path = path + "aws.nginx.conf";
|
|
|
} else {
|
|
|
path = path + "qq.nginx.conf";
|
|
|
}
|
|
|
BufferedReader bufferedReader = null;
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
try {
|
|
|
bufferedReader = new BufferedReader(new FileReader(new File(path)));
|
|
|
String tmp;
|
|
|
while ((tmp = bufferedReader.readLine()) != null) {
|
|
|
sb.append(tmp + "\r\n");
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
try {
|
|
|
bufferedReader.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
return analyseConfigFile(sb.toString());
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|