-
Notifications
You must be signed in to change notification settings - Fork 0
/
haproxy_monitor.sh
executable file
·165 lines (129 loc) · 5.12 KB
/
haproxy_monitor.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
#!/bin/bash
# This script will monitor Haproxy nodes and take over a Virtual IP (VIP)
source `pwd`/haproxy_monitor.conf
# ---
FAILOVER_SCRIPT="reassign_vip.sh" # Script executed to reassign virtual ip.
SSH_TIMEOUT="-o ConnectTimeout=1"
SYSTEM_STABLE="yes"
IP_NODE_1=`ssh $SSH_TIMEOUT $SSH_CONFIG_1 /sbin/ifconfig eth0 | grep 'inet ' | awk '{print $2}' | sed 's/addr://'`
IP_NODE_2=`ssh $SSH_TIMEOUT $SSH_CONFIG_2 /sbin/ifconfig eth0 | grep 'inet ' | awk '{print $2}' | sed 's/addr://'`
echo `date` "-- Starting Haproxy monitor"
echo `date` "IP node 1 = $IP_NODE_1"
echo `date` "IP node 2 = $IP_NODE_2"
while [ . ]; do
HAPROXY_PID_NODE_1=`ssh $SSH_TIMEOUT $SSH_CONFIG_1 "ps cax | grep haproxy$" | awk '{print $1;}'`
HAPROXY_PID_NODE_2=`ssh $SSH_TIMEOUT $SSH_CONFIG_2 "ps cax | grep haproxy$" | awk '{print $1;}'`
# ==============================
# --- Come back to stability ---
# ==============================
if [ "$SYSTEM_STABLE" == "no" ] && [ "$HAPROXY_PID_NODE_1" != "" ] && [ "$HAPROXY_PID_NODE_2" != "" ]
then
SYSTEM_STABLE="yes"
echo `date` "[NOTICE] Come back to stability. All is done."
fi
# ========================================
# --- All Haproxy instances are down ? ---
# ========================================
if [ "$HAPROXY_PID_NODE_1" == "" ] && [ "$HAPROXY_PID_NODE_2" == "" ]
then
SYSTEM_STABLE="no"
echo `date` " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! "
echo `date` "### CRITICAL ### !!! ALL HAPROXY INSTANCES ARE DOWN !!! ### CRITICAL ###"
echo `date` " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! "
# ======================================
# --- One Haproxy instance is down ? ---
# ======================================
elif [ "$HAPROXY_PID_NODE_1" == "" ] || [ "$HAPROXY_PID_NODE_2" == "" ]
then
SYSTEM_STABLE="no"
if [ "$HAPROXY_PID_NODE_1" == "" ]
then
echo `date` "[WARNING] Haproxy node 1 ($IP_NODE_1) is down."
fi
if [ "$HAPROXY_PID_NODE_2" == "" ]
then
echo `date` "[WARNING] Haproxy node 2 ($IP_NODE_2) is down."
fi
# ==================================
# --- Who is the master server ? ---
# ==================================
OLDIFS=$IFS
IFS=$'\n'
for line in `ec2-describe-addresses --aws-access-key $AWSAccessKeyId --aws-secret-key $AWSSecretKey`
do
ENI=`echo $line | awk '{print $7;}'`
IP=`echo $line | awk '{print $8;}'`
case $IP in
$VIP)
ENI_NODE_0=$ENI
;;
$IP_NODE_1)
ENI_NODE_1=$ENI
;;
$IP_NODE_2)
ENI_NODE_2=$ENI
;;
*)
esac
done
IFS=$OLDIFS
echo `date` "ENI eth0 vip = $ENI_NODE_0"
echo `date` "ENI eth0 node 1 = $ENI_NODE_1"
echo `date` "ENI eth0 node 2 = $ENI_NODE_2"
if [ "$ENI_NODE_0" == "$ENI_NODE_1" ]
then
MASTER=$IP_NODE_1
HAPROXY_PID_MASTER=$HAPROXY_PID_NODE_1
HAPROXY_PID_SLAVE=$HAPROXY_PID_NODE_2
elif [ "$ENI_NODE_0" == "$ENI_NODE_2" ]
then
MASTER=$IP_NODE_2
HAPROXY_PID_MASTER=$HAPROXY_PID_NODE_2
HAPROXY_PID_SLAVE=$HAPROXY_PID_NODE_1
else
MASTER=""
HAPROXY_PID_MASTER=""
HAPROXY_PID_SLAVE=""
fi
# =============================
# --- I'm slave or master ? ---
# =============================
IP_LOCALHOST=`/sbin/ifconfig eth0 | grep 'inet ' | awk '{print $2}' | sed 's/addr://'`
echo `date` "Master $MASTER / Localhost $IP_LOCALHOST"
if [ "$MASTER" == "$IP_LOCALHOST" ]
then
MY_STATUS="master"
else
MY_STATUS="slave"
fi
# ==========================
# --- Execute failover ? ---
# ==========================
# Failover if :
# 1) Master Haproxy is down
# 2) Slave Haproxy is up
# 3) This server is the slave
if [ "$MY_STATUS" == "master" ]
then
echo `date` "I'm master. Nothing to do."
elif [ "$MY_STATUS" == "slave" ] && [ "$HAPROXY_PID_MASTER" != "" ]
then
echo `date` "I'm slave & master is up. Nothing to do."
elif [ "$MY_STATUS" == "slave" ] && [ "$HAPROXY_PID_MASTER" == "" ] && [ "$HAPROXY_PID_SLAVE" != "" ]
then
echo `date` "[WARNING] Haproxy on master server $MASTER is down !"
echo `date` "[-------] Haproxy on slave server is up."
echo `date`
echo `date` "########################"
echo `date` "### EXECUTE FAILOVER ###"
echo `date` "########################"
echo `date`
source `pwd`/$FAILOVER_SCRIPT
echo `date` "Failover done."
echo `date`
else
echo `date` "### CRITICAL ###"
fi
fi
sleep $CHECK_EVERY
done