Authored by zhengyouwei

nginx switch

... ... @@ -74,7 +74,11 @@ public class AlarmMsgServiceImpl implements AlarmMsgService {
sendStatus = false;
}
try {
monitorAlarmMapper.insertAlarmMsg(InfluxDBContants.AWS, type, content, "", sendStatus ? "successed" : "failed");
}catch (Exception e){
logger.error("insert sms into influxdb failed",e);
}
return sendStatus;
}
... ...
package com.monitor.common.util;
import org.apache.commons.lang.StringUtils;
import java.util.Random;
import java.util.UUID;
public class RandomUtil {
private static Random r = new Random();
public static String getToken(){
return UUID.randomUUID().toString().replaceAll("-", "");
}
public static String getTicket(){
return StringUtils.leftPad(r.nextInt(9999)+"", 4, "0");
}
/**
* main函数.
* @param args
* 启动参数
* @throws Exception
* Exception
*/
public static void main(String... args) throws Exception {
System.out.println(getTicket());
System.out.println(getToken());
}
}
... ...
package com.monitor.model.domain;
public class SmsTicket {
private String ticket;
private String cloudName;
private String target;
public SmsTicket(String ticket, String cloudName, String target) {
this.ticket = ticket;
this.cloudName = cloudName;
this.target = target;
}
public String getTicket() {
return ticket;
}
public void setTicket(String ticket) {
this.ticket = ticket;
}
public String getCloudName() {
return cloudName;
}
public void setCloudName(String cloudName) {
this.cloudName = cloudName;
}
public String getTarget() {
return target;
}
}
... ...
package com.monitor.switchs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 执行脚本
* Created by zhengyouwei on 2016/5/26.
*/
public class CommodUtil {
private static Logger logger = LoggerFactory.getLogger(CommodUtil.class);
public static final String BASE_DIR = System.getenv("HOME") + "/ops_sh/";
/**
* NGINX 查询切换脚本
*/
public static final String NGINX_SWITCH_SH = "switch_nginx.sh";
/**
* dns 切换前获取
*/
public static final String DNS_SWITCH_TOOL = "dnskeytools";
/**
* 登录校验
*/
public static final String LOGIN_TOOL = "authtools";
public static String exe(String commond, String param) {
logger.info("start to exe commond:{},param:{}",commond,param);
Process process = null;
StringBuilder sb = new StringBuilder();
BufferedReader input = null;
try {
process = Runtime.getRuntime().exec(BASE_DIR + commond + " " + param);
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 exe failed!", e);
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
logger.error("process exe failed!", e);
}
}
}
return sb.toString();
}
}
... ...
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());
}
}
... ...
package com.monitor.switchs;
/**
* Created by zhengyouwei on 2016/6/13.
*/
public class Test {
}
... ... @@ -16,7 +16,7 @@
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">
<context:property-placeholder location="classpath:test/*.properties" />
<context:property-placeholder location="classpath:*.properties" />
<context:component-scan base-package="com.monitor" />
... ...
... ... @@ -5,6 +5,8 @@ sendsms.pwd=NCftHmJ9
sendsms.notice.productid=8
base_mobile=15905144483,18751986615,18502542319
swaith_mobile=15905144483
... ...
... ... @@ -5,6 +5,8 @@ sendsms.pwd=E
sendsms.notice.productid=8
base_mobile=15905144483
swaith_mobile=15905144483
... ...
user www www;
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
pid /usr/local/nginx/logs/nginx.pid;
# [ debug | info | notice | warn | error | crit ]
#error_log /Data/logs/nginx/nginx_error.log info;
error_log /Data/logs/nginx/nginx_error.log error;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
use epoll;
#maxclient = worker_processes * worker_connections / cpu_number
worker_connections 51200;
}
http
{
include /usr/local/nginx/conf/mime.types;
default_type application/octet-stream;
#charset gb2312,utf-8;
charset utf-8;
log_format fenxi '$remote_addr|$http_x_forwarded_for|[$time_local]|$http_host|$request|$request_body|'
'$status|$body_bytes_sent|$request_time|$upstream_response_time|$upstream_cache_status|$http_referer|'
'$http_user_agent|$upstream_addr';
#log_format main '$remote_addr - $remote_user [$time_local] $request $request_body '
# '"$status" $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for" "$request_time"';
#error_page 502 =200 /.busy.jpg;
#error_page 403 = http://www.yohoboys.com;
#error_page 404 = http://www.yohoboys.com;
#General Options
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_body_buffer_size 8m; #256k
#
#server_tokens off;
ignore_invalid_headers on;
recursive_error_pages on;
server_name_in_redirect off;
sendfile on;
#timeouts
keepalive_timeout 60;
#test
#client_body_timeout 3m;
#client_header_timeout 3m;
#send_timeout 3m;
#TCP Options
tcp_nopush on;
tcp_nodelay on;
#fastcgi options
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
#set_real_ip_from 10.159.191.0/24;
#real_ip_header X-Forwarded-For;
#hiden php version
fastcgi_hide_header X-Powered-By;
#size limits
client_max_body_size 50m;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
#gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml application/json;
gzip_vary on;
#proxy_temp_path /dev/shm/proxy_temp;
#proxy_temp_path /us/local/nginx-1.8.0/ngx_temp_dir;
#proxy_cache_path /Data/local/nginx-1.8.0/ngx_cache levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=10g;
#proxy_connect_timeout 5;
#proxy_read_timeout 60;
#proxy_send_timeout 5;
#proxy_buffer_size 16k;
#proxy_buffers 4 64k;
#proxy_busy_buffers_size 128k;
#proxy_temp_file_write_size 128k;
fastcgi_temp_path /dev/shm/fastcgi_temp;
client_body_temp_path /dev/shm/client_body_temp;
#upstream web
upstream oldservice {
# ip_hash;
server service.api.yohobuy.com ;
}
upstream oldapi {
# ip_hash;
server api.open.yohobuy.com ;
}
upstream apigateway {
# ip_hash;
# server 10.66.0.4:8080 max_fails=5 fail_timeout=3s;
# server 10.66.0.5:8080 max_fails=5 fail_timeout=3s;
server 172.31.28.166:8080 max_fails=5 fail_timeout=3s;
server 172.31.17.195:8080 max_fails=5 fail_timeout=3s;
keepalive 32;
}
upstream grayapigateway{
# ip_hash;
# server 10.66.0.4:8080 max_fails=5 fail_timeout=3s;
# server 10.66.0.5:8080 max_fails=5 fail_timeout=3s;
server 10.66.101.7:8080 max_fails=5 fail_timeout=3s;
# server 172.31.28.166:8080 max_fails=5 fail_timeout=3s;
# server 172.31.17.195:8080 max_fails=5 fail_timeout=3s;
keepalive 32;
}
upstream yoho-dashboard {
# ip_hash;
server 172.31.25.5:8089;
}
upstream pay.yohobuy.com {
server 172.31.55.207;
#server 119.254.81.217;
}
upstream www.yohobuy.com {
server www.yohobuy.com;
}
upstream union {
server 192.168.90.9:8088;
}
upstream activityApi{
server 172.31.16.98:8090;
#server 192.168.90.9:8090;
#server 192.168.74.14:8090;
}
#upstream
fastcgi_next_upstream error timeout invalid_header http_500;
#limit_zone limit $binary_remote_addr 1m;
#fastcgi cache
#fastcgi_cache_path /nginxcache levels=1:2 keys_zone=two:10m inactive=1d max_size=3000m;
#for example just for study! have fun!
# include vhosts/yohobuy_java.conf;
include vhosts/yohobuyaws_java.conf;
# include vhosts/yohobuyaws_java_aws.conf;
}
... ...
user www www;
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
pid /usr/local/nginx/logs/nginx.pid;
# [ debug | info | notice | warn | error | crit ]
#error_log /Data/logs/nginx/nginx_error.log info;
error_log /Data/logs/nginx/nginx_error.log error;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
use epoll;
#maxclient = worker_processes * worker_connections / cpu_number
worker_connections 51200;
}
http
{
include /usr/local/nginx/conf/mime.types;
default_type application/octet-stream;
#charset gb2312,utf-8;
charset utf-8;
log_format fenxi '$remote_addr|$http_x_forwarded_for|[$time_local]|$http_host|$request|$request_body|'
'$status|$body_bytes_sent|$request_time|$upstream_response_time|$upstream_cache_status|$http_referer|'
'$http_user_agent|$upstream_addr';
#log_format main '$remote_addr - $remote_user [$time_local] $request $request_body '
# '"$status" $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for" "$request_time"';
#error_page 502 =200 /.busy.jpg;
#error_page 403 = http://www.yohoboys.com;
#error_page 404 = http://www.yohoboys.com;
#General Options
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_body_buffer_size 8m; #256k
#
#server_tokens off;
ignore_invalid_headers on;
recursive_error_pages on;
server_name_in_redirect off;
sendfile on;
#timeouts
keepalive_timeout 60;
#test
#client_body_timeout 3m;
#client_header_timeout 3m;
#send_timeout 3m;
#TCP Options
tcp_nopush on;
tcp_nodelay on;
#fastcgi options
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
#set_real_ip_from 10.159.191.0/24;
#real_ip_header X-Forwarded-For;
#hiden php version
fastcgi_hide_header X-Powered-By;
#size limits
client_max_body_size 50m;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
#gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml application/json;
gzip_vary on;
#proxy_temp_path /dev/shm/proxy_temp;
#proxy_temp_path /us/local/nginx-1.8.0/ngx_temp_dir;
#proxy_cache_path /Data/local/nginx-1.8.0/ngx_cache levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=10g;
#proxy_connect_timeout 5;
#proxy_read_timeout 60;
#proxy_send_timeout 5;
#proxy_buffer_size 16k;
#proxy_buffers 4 64k;
#proxy_busy_buffers_size 128k;
#proxy_temp_file_write_size 128k;
fastcgi_temp_path /dev/shm/fastcgi_temp;
client_body_temp_path /dev/shm/client_body_temp;
#upstream web
upstream oldservice {
# ip_hash;
server service.api.yohobuy.com ;
}
upstream oldapi {
# ip_hash;
server api.open.yohobuy.com ;
}
upstream apigateway {
# ip_hash;
server 10.66.0.4:8080 max_fails=5 fail_timeout=3s;
server 10.66.0.5:8080 max_fails=5 fail_timeout=3s;
# server 172.31.28.166:8080 max_fails=5 fail_timeout=3s;
# server 172.31.17.195:8080 max_fails=5 fail_timeout=3s;
keepalive 32;
}
upstream grayapigateway{
# ip_hash;
#server 10.66.0.4:8080 max_fails=5 fail_timeout=3s;
#server 10.66.0.5:8080 max_fails=5 fail_timeout=3s;
server 10.66.101.7:8080 max_fails=5 fail_timeout=3s;
#server 172.31.28.166:8080 max_fails=5 fail_timeout=3s;
#server 172.31.17.195:8080 max_fails=5 fail_timeout=3s;
keepalive 32;
}
upstream yoho-dashboard {
# ip_hash;
server 172.31.25.5:8089;
}
upstream pay.yohobuy.com {
server 172.31.55.207;
#server 119.254.81.217;
}
upstream www.yohobuy.com {
server www.yohobuy.com;
}
upstream union {
server 192.168.90.9:8088;
}
upstream activityApi{
server 10.66.0.16:8090;
#server 192.168.90.9:8090;
#server 192.168.74.14:8090;
}
#upstream
fastcgi_next_upstream error timeout invalid_header http_500;
#limit_zone limit $binary_remote_addr 1m;
#fastcgi cache
#fastcgi_cache_path /nginxcache levels=1:2 keys_zone=two:10m inactive=1d max_size=3000m;
#for example just for study! have fun!
# include vhosts/yohobuy_java.conf;
include vhosts/yohobuyaws_java.conf;
}
... ...