-
Notifications
You must be signed in to change notification settings - Fork 3
/
checkRedis.sh
executable file
·178 lines (129 loc) · 5.94 KB
/
checkRedis.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
#!/bin/bash
PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
#set -x
#
#------------------------------
#
# Import settings
#
# Git branch
. ./settings.sh
# WriteLog() function
. ./timestampLogger.sh
#
#------------------------------
#
# Constants
#
BIN_HOME=$OBT_BIN_DIR
if [ ! -d ${BIN_HOME} ]
then
# Local dev/test/debug dir
BIN_HOME=~/Redis
fi
LONG_DATE=$(date "+%Y-%m-%d_%H-%M-%S")
REDIS_CHECK_LOG_FILE=${OBT_LOG_DIR}/checkRedis-${LONG_DATE}.log
PORT1=6379
PORT2=6380
PORT3=6381
APP_SHORT_NAME=redis
APPNAME=$APP_SHORT_NAME-server
pwd=$( pwd )
WriteLog "Start Redis Server config check..." "${REDIS_CHECK_LOG_FILE}"
cd ${BIN_HOME}
WriteLog "Check if we have ${BIN_HOME}/${APP_SHORT_NAME}.conf file. " "${REDIS_CHECK_LOG_FILE}"
if [ ! -f ${BIN_HOME}/${APP_SHORT_NAME}.conf ]
then
if [ -f /etc/${APP_SHORT_NAME}/${APP_SHORT_NAME}.conf ]
then
WriteLog "No, we have not. Copy one from /etc/${APP_SHORT_NAME}/${APP_SHORT_NAME}.conf." "${REDIS_CHECK_LOG_FILE}"
sudo cp /etc/${APP_SHORT_NAME}/${APP_SHORT_NAME}.conf .
else
WriteLog "No, we have not. Copy one from /etc/${APP_SHORT_NAME}.conf." "${REDIS_CHECK_LOG_FILE}"
sudo cp /etc/${APP_SHORT_NAME}.conf .
fi
sudo chmod 0755 ${BIN_HOME}/${APP_SHORT_NAME}.conf
sudo chown ${USER}:users ${BIN_HOME}/${APP_SHORT_NAME}.conf
else
WriteLog "Yes, we have it." "${REDIS_CHECK_LOG_FILE}"
fi
if [[ ( ! -f ${BIN_HOME}/${APP_SHORT_NAME}1.conf ) || ( $( stat --format="%s" ${BIN_HOME}/${APP_SHORT_NAME}1.conf ) -eq 0) ]]
then
WriteLog "There is not ${BIN_HOME}/${APP_SHORT_NAME}1.conf file! Derive it from original." "${REDIS_CHECK_LOG_FILE}"
sudo sed -e 's/^# requirepass/requirepass/g' -e 's/daemonize no/daemonize yes/' "${BIN_HOME}/${APP_SHORT_NAME}.conf" > ${BIN_HOME}/${APP_SHORT_NAME}1.conf
fi
if [[ ( ! -f ${BIN_HOME}/${APP_SHORT_NAME}2.conf ) || ( $( stat --format="%s" ${BIN_HOME}/${APP_SHORT_NAME}2.conf ) -eq 0) ]]
then
WriteLog "There is not ${BIN_HOME}/${APP_SHORT_NAME}2.conf file! Derive it from original." "${REDIS_CHECK_LOG_FILE}"
sed -e 's/^# requirepass foobared/requirepass youarefoobared/' -e 's/^port 6379/port '${PORT2}'/' -e 's/daemonize no/daemonize yes/' "${BIN_HOME}/${APP_SHORT_NAME}.conf" > ${BIN_HOME}/${APP_SHORT_NAME}2.conf
fi
if [[ ( ! -f ${BIN_HOME}/${APP_SHORT_NAME}3.conf ) || ( $( stat --format="%s" ${BIN_HOME}/${APP_SHORT_NAME}3.conf ) -eq 0) ]]
then
WriteLog "There is not ${BIN_HOME}/${APP_SHORT_NAME}3.conf file! Derive it from original." "${REDIS_CHECK_LOG_FILE}"
sed -e 's/^# requirepass foobared/requirepass foobared/' -e 's/^port 6379/port '${PORT3}'/' -e 's/daemonize no/daemonize yes/' "${BIN_HOME}/${APP_SHORT_NAME}.conf" > ${BIN_HOME}/${APP_SHORT_NAME}3.conf
fi
#
#--------------------------
#
# Check the state of Redis Servers It should be 3 instances
# on on port PORT1, PORT2 and on PORT3
#
WriteLog "Start Redis Server check..." "${REDIS_CHECK_LOG_FILE}"
tryCount=2
if [[ -f /usr/sbin/redis-server || -f /usr/bin/redis-server || -f /usr/local/bin/redis-server ]]
then
while [[ $tryCount -ne 0 ]]
do
WriteLog "Try count: ${tryCount}" "${REDIS_CHECK_LOG_FILE}"
numOfServers=$( sudo netstat -tulnap | egrep -c ":($PORT1|$PORT2|$PORT3).*LISTEN.*$APPNAME" )
WriteLog "numOfServers: ${numOfServers}" "${REDIS_CHECK_LOG_FILE}"
if [[ ${numOfServers} -ne 3 && ${numOfServers} -ne 6 ]]
then
WriteLog "We expected 3 servers on port ${PORT1}, ${PORT2} and ${PORT3} but there are none" "${REDIS_CHECK_LOG_FILE}"
# If there any Redis server, kill them
pgrep $APPNAME |
while read pid
do
WriteLog "Kill $APPNAME (pid:"$pid")" "${REDIS_CHECK_LOG_FILE}"
sudo kill -9 $pid
done
#start them
WriteLog "Start 3 servers on port ${PORT1} ${PORT2} and ${PORT3}." "${REDIS_CHECK_LOG_FILE}"
WriteLog "Start sudo $APPNAME ${BIN_HOME}/${APP_SHORT_NAME}1.conf on port ${PORT1}" "${REDIS_CHECK_LOG_FILE}"
$SUDO $APPNAME ${BIN_HOME}/${APP_SHORT_NAME}1.conf &
WriteLog "Start sudo $APPNAME ${BIN_HOME}/${APP_SHORT_NAME}2.conf on port ${PORT2}" "${REDIS_CHECK_LOG_FILE}"
$SUDO $APPNAME ${BIN_HOME}/${APP_SHORT_NAME}2.conf &
WriteLog "Start sudo $APPNAME ${BIN_HOME}/${APP_SHORT_NAME}3.conf on port ${PORT3}" "${REDIS_CHECK_LOG_FILE}"
$SUDO $APPNAME ${BIN_HOME}/${APP_SHORT_NAME}3.conf &
tryCount=$(( $tryCount-1 ))
numOfServers=$( sudo netstat -tulnap | egrep -c ":($PORT1|$PORT2|$PORT3).*LISTEN.*$APPNAME" )
if [[ ${numOfServers} -ge 3 || ${numOfServers} -eq 6 ]]
then
sleep 10
continue
else
WriteLog "It is OK! We have 3 servers on expected ports!" "${REDIS_CHECK_LOG_FILE}"
break
fi
else
WriteLog "It is OK! We have 3 servers on expected ports!" "${REDIS_CHECK_LOG_FILE}"
break
fi
done
if [[ $tryCount -eq 0 ]]
then
WriteLog "Redis won't start! Give up and send Email to Agyi!" "${REDIS_CHECK_LOG_FILE}"
# send email to Agyi
echo "Redis won't start!" | mailx -s "Problem with Redis" -u $USER ${ADMIN_EMAIL_ADDRESS}
fi
else
WriteLog "Redis not installed in this sysytem! Send Email to Agyi!" "${REDIS_CHECK_LOG_FILE}"
# send email to Agyi
echo "Redis not installed in this sysytem!" | mailx -s "Problem with Redis" -u $USER ${ADMIN_EMAIL_ADDRESS}
fi
WriteLog "Start monitor for every redis servers." "${REDIS_CHECK_LOG_FILE}"
redis-cli -a foobared monitor > ./redis1-${LONG_DATE}.out 2>&1 &
redis-cli -p 6380 -a youarefoobared monitor > ./redis2-${LONG_DATE}.out 2>&1 &
redis-cli -p 6381 -a foobared monitor > ./redis3-${LONG_DATE}.out 2>&1 &
cd ${pwd}
WriteLog "End of Redis Server check." "${REDIS_CHECK_LOG_FILE}"