-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
haproxy.sh
289 lines (270 loc) · 8.04 KB
/
haproxy.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/env bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
#=================================================
# System Required: CentOS/Debian/Ubuntu
# Description: HaProxy
# Version: 1.0.1
# Author: Toyo
# Blog: https://doub.io/wlzy-19/
#=================================================
HaProxy_file="/etc/haproxy"
HaProxy_cfg_file="/etc/haproxy/haproxy.cfg"
#检查是否安装HaProxy
check_HaProxy(){
HaProxy_exist=`haproxy -v`
if [[ ${HaProxy_exist} = "" ]]; then
echo -e "\033[41;37m [错误] \033[0m 没有安装HaProxy,请检查 !"
exit 1
fi
}
#检查系统
check_sys(){
if [[ -f /etc/redhat-release ]]; then
release="centos"
elif cat /etc/issue | grep -q -E -i "debian"; then
release="debian"
elif cat /etc/issue | grep -q -E -i "ubuntu"; then
release="ubuntu"
elif cat /etc/issue | grep -q -E -i "centos|red hat|redhat"; then
release="centos"
elif cat /proc/version | grep -q -E -i "debian"; then
release="debian"
elif cat /proc/version | grep -q -E -i "ubuntu"; then
release="ubuntu"
elif cat /proc/version | grep -q -E -i "centos|red hat|redhat"; then
release="centos"
fi
#bit=`uname -m`
}
# 安装HaProxy
installHaProxy(){
# 判断是否安装HaProxy
HaProxy_exist=`haproxy -v`
if [[ ${HaProxy_exist} != "" ]]; then
echo -e "\033[41;37m [错误] \033[0m 已经安装HaProxy,请检查 !"
exit 1
fi
check_sys
# 系统判断
if [[ ${release} == "centos" ]]; then
yum update
yum install -y vim curl haproxy
else
apt-get update
apt-get install -y vim curl haproxy
fi
chmod +x /etc/rc.local
#判断HaProxy是否安装成功
HaProxy_exist=`haproxy -v`
if [[ ${HaProxy_exist} = "" ]]; then
echo -e "\033[41;37m [错误] \033[0m 安装HaProxy失败,请检查 !"
exit 1
else
setHaProxy
fi
}
setHaProxy(){
# 判断是否安装HaProxy
check_HaProxy
# 设置本地监听端口
read -p "请输入 HaProxy 的 本地监听端口(转发端口) [1-65535](支持端口段: 2333-6666): " HaProxyport
[[ -z "${HaProxyport}" ]] && echo "取消..." && exit 1
# 设置欲转发 IP
read -p "请输入 HaProxy 欲转发的 IP:" HaProxyip
[[ -z "${HaProxyip}" ]] && echo "取消..." && exit 1
#最后确认
echo
echo "——————————————————————————————"
echo " 请检查 HaProxy 配置是否有误 !"
echo
echo -e " 本地监听端口 : \033[41;37m ${HaProxyport} \033[0m"
echo -e " 欲转发 IP : \033[41;37m ${HaProxyip} \033[0m"
echo "——————————————————————————————"
echo
read -p "请按任意键继续,如有配置错误请使用 Ctrl+C 退出。" var
HaProxy_port_1=`cat ${HaProxy_cfg_file} | sed -n "13p" | cut -c 12-23 | grep "-"`
HaProxy_port=`cat ${HaProxy_cfg_file} | sed -n "13p" | cut -c 12-23`
if [[ ${HaProxy_port_1} = "" ]]; then
iptables -D INPUT -p tcp --dport ${HaProxy_port} -j ACCEPT
else
HaProxy_port_1=`echo ${HaProxy_port_1} | sed 's/-/:/g'`
iptables -D INPUT -p tcp --dport ${HaProxy_port_1} -j ACCEPT
fi
cat > ${HaProxy_cfg_file}<<-EOF
global
ulimit -n 51200
defaults
log global
mode tcp
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
frontend ss-in1
bind *:${HaProxyport}
default_backend ss-out1
backend ss-out1
server server1 ${HaProxyip} maxconn 20480
EOF
PID=`ps -ef | grep "haproxy" | grep -v grep | grep -v "haproxy.sh" | awk '{print $2}'`
if [[ ! -z $PID ]]; then
stopHaProxy
fi
startHaProxy
}
# 查看HaProxy列表
viewHaProxy(){
# 检查是否安装
check_HaProxy
HaProxy_port=`cat ${HaProxy_cfg_file} | sed -n "13p" | cut -c 12-23`
HaProxy_ip=`cat ${HaProxy_cfg_file} | sed -n "17p" | awk '{print $3}'`
# 获取IP
ip=`curl -m 10 -s http://members.3322.org/dyndns/getip`
if [[ -z $ip ]]; then
ip="ip"
fi
echo
echo "——————————————————————————————"
echo " HaProxy 配置信息: "
echo
echo -e " 本地 IP : \033[41;37m ${ip} \033[0m"
echo -e " 本地监听端口 : \033[41;37m ${HaProxy_port} \033[0m"
echo
echo -e " 欲转发 IP : \033[41;37m ${HaProxy_ip} \033[0m"
echo -e " 欲转发端口 : \033[41;37m ${HaProxy_port} \033[0m"
echo "——————————————————————————————"
echo
}
# 启动aProxy
startHaProxy(){
# 检查是否安装
check_HaProxy
# 判断进程是否存在
PID=`ps -ef | grep "haproxy" | grep -v grep | grep -v "haproxy.sh" | awk '{print $2}'`
if [[ ! -z $PID ]]; then
echo -e "\033[41;37m [错误] \033[0m 发现 HaProxy 正在运行,请检查 !"
exit 1
fi
/etc/init.d/haproxy start
sleep 2s
PID=`ps -ef | grep "haproxy" | grep -v grep | grep -v "haproxy.sh" | awk '{print $2}'`
if [[ -z $PID ]]; then
echo -e "\033[41;37m [错误] \033[0m HaProxy 启动失败 !"
exit 1
fi
HaProxy_port_1=`cat ${HaProxy_cfg_file} | sed -n "13p" | cut -c 12-23 | grep "-"`
HaProxy_port=`cat ${HaProxy_cfg_file} | sed -n "13p" | cut -c 12-23`
if [[ ${HaProxy_port_1} = "" ]]; then
iptables -I INPUT -p tcp --dport ${HaProxy_port} -j ACCEPT
else
HaProxy_port_1=`echo ${HaProxy_port_1} | sed 's/-/:/g'`
iptables -I INPUT -p tcp --dport ${HaProxy_port_1} -j ACCEPT
fi
# 系统判断
check_sys
if [[ ${release} == "debian" ]]; then
sed -i '$d' /etc/rc.local
echo -e "/etc/init.d/haproxy start" >> /etc/rc.local
echo -e "exit 0" >> /etc/rc.local
else
echo -e "/etc/init.d/haproxy start" >> /etc/rc.local
fi
clear
echo
echo "——————————————————————————————"
echo
echo " HaProxy 已启动 !"
viewHaProxy
}
# 停止aProxy
stopHaProxy(){
# 检查是否安装
check_HaProxy
# 判断进程是否存在
PID=`ps -ef | grep "haproxy" | grep -v grep | grep -v "haproxy.sh" | awk '{print $2}'`
if [[ -z $PID ]]; then
echo -e "\033[41;37m [错误] \033[0m 发现 HaProxy 没有运行,请检查 !"
exit 1
fi
sed -i '/\/etc\/init.d\/haproxy start/d' /etc/rc.local
HaProxy_port_1=`cat ${HaProxy_cfg_file} | sed -n "13p" | cut -c 12-23 | grep "-"`
HaProxy_port=`cat ${HaProxy_cfg_file} | sed -n "13p" | cut -c 12-23`
if [[ ${HaProxy_port_1} = "" ]]; then
iptables -D INPUT -p tcp --dport ${HaProxy_port} -j ACCEPT
else
HaProxy_port_1=`echo ${HaProxy_port_1} | sed 's/-/:/g'`
iptables -D INPUT -p tcp --dport ${HaProxy_port_1} -j ACCEPT
fi
/etc/init.d/haproxy stop
sleep 2s
PID=`ps -ef | grep "haproxy" | grep -v grep | grep -v "haproxy.sh" | awk '{print $2}'`
if [[ ! -z $PID ]]; then
echo -e "\033[41;37m [错误] \033[0m HaProxy 停止失败 !"
exit 1
else
echo " HaProxy 已停止 !"
fi
}
restartHaProxy(){
# 检查是否安装
check_HaProxy
PID=`ps -ef | grep "haproxy" | grep -v grep | grep -v "haproxy.sh" | awk '{print $2}'`
if [[ ! -z $PID ]]; then
stopHaProxy
fi
startHaProxy
}
statusHaProxy(){
# 检查是否安装
check_HaProxy
# 判断进程是否存在
PID=`ps -ef | grep "haproxy" | grep -v grep | grep -v "haproxy.sh" | awk '{print $2}'`
if [[ ! -z $PID ]]; then
echo -e "\033[42;37m [信息] \033[0m HaProxy 正在运行,PID: ${PID} !"
else
echo -e "\033[42;37m [信息] \033[0m HaProxy 没有运行 !"
fi
}
uninstallHaProxy(){
# 检查是否安装
check_HaProxy
printf "确定要卸载 HaProxy ? (y/N)"
printf "\n"
read -p "(默认: n):" unyn
[[ -z ${unyn} ]] && unyn="n"
if [[ ${unyn} == [Yy] ]]; then
check_sys
# 系统判断
if [[ ${release} == "centos" ]]; then
yum remove haproxy -y
else
sudo apt-get remove haproxy -y
sudo apt-get autoremove
fi
rm -rf ${HaProxy_file}
HaProxy_exist=`haproxy -v`
if [[ ${HaProxy_exist} != "" ]]; then
echo -e "\033[41;37m [错误] \033[0m HaProxy卸载失败,请检查 !"
exit 1
fi
echo
echo " HaProxy 已卸载 !"
echo
else
echo
echo "卸载已取消..."
echo
fi
}
action=$1
[[ -z $1 ]] && action=install
case "$action" in
install|set|view|start|stop|restart|status|uninstall)
${action}HaProxy
;;
*)
echo "输入错误 !"
echo "用法: { install | view | set | start | stop | restart | status | uninstall }"
;;
esac