forked from spujadas/elk-docker
-
Notifications
You must be signed in to change notification settings - Fork 3
/
start.sh
278 lines (223 loc) · 9.23 KB
/
start.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
#!/bin/bash
#
# /usr/local/bin/start.sh
# Start Elasticsearch, Logstash and Kibana services
# WARNING - This script assumes that the ELK services are not running, and is
# only expected to be run once, when the container is started.
# Do not attempt to run this script if the ELK services are running (or be
# prepared to reap zombie processes).
## handle termination gracefully
_term() {
echo "Terminating ELK"
# shut down services
timeout 60 service elasticsearch stop &
timeout 60 service logstash stop &
timeout 60 service kibana stop &
trap - SIGTERM SIGINT
# wait for all services to stop
wait
# kill script PGID so all the child processes are killed, to avoid zombies
kill -TERM -- -$$ 2>/dev/null
exit 0
}
trap _term SIGTERM SIGINT
## remove pidfiles in case previous graceful termination failed
# NOTE - This is the reason for the WARNING at the top - it's a bit hackish,
# but if it's good enough for Fedora (https://goo.gl/88eyXJ), it's good
# enough for me :)
rm -f /var/run/elasticsearch/elasticsearch.pid /var/run/logstash.pid \
/var/run/kibana5.pid
## initialise list of log files to stream in console (initially empty)
OUTPUT_LOGFILES=""
## override default time zone (Etc/UTC) if TZ variable is set
if [ ! -z "$TZ" ]; then
ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
fi
## run pre-hooks
if [ -x /usr/local/bin/elk-pre-hooks.sh ]; then
. /usr/local/bin/elk-pre-hooks.sh
fi
## start services as needed
### crond
service cron start
### Elasticsearch
if [ -z "$ELASTICSEARCH_START" ]; then
ELASTICSEARCH_START=1
fi
if [ "$ELASTICSEARCH_START" -ne "1" ]; then
echo "ELASTICSEARCH_START is set to something different from 1, not starting..."
else
# update permissions of ES data directory
chown -R elasticsearch:elasticsearch /var/lib/elasticsearch
# override JVM heap size using custom JVM options file if ES_HEAP_SIZE variable is set
if [ ! -z "$ES_HEAP_SIZE" ]; then
cat << EOF > ${ES_PATH_CONF}/jvm.options.d/heap_size.options
-Xmx$ES_HEAP_SIZE
-Xms$ES_HEAP_SIZE
EOF
fi
# disable JVM HeapDumpOnOutOfMemoryError if ES_HEAP_DISABLE variable is set
if [ ! -z "$ES_HEAP_DISABLE" ]; then
echo "-XX:-HeapDumpOnOutOfMemoryError" > ${ES_PATH_CONF}/jvm.options.d/heap_dump.options
fi
# override ES_JAVA_OPTS variable if set
if [ ! -z "$ES_JAVA_OPTS" ]; then
awk -v LINE="ES_JAVA_OPTS=\"$ES_JAVA_OPTS\"" '{ sub(/^#?ES_JAVA_OPTS=.*/, LINE); print; }' /etc/default/elasticsearch \
> /etc/default/elasticsearch.new && mv /etc/default/elasticsearch.new /etc/default/elasticsearch
fi
# override MAX_OPEN_FILES variable if set
if [ ! -z "$MAX_OPEN_FILES" ]; then
awk -v LINE="MAX_OPEN_FILES=$MAX_OPEN_FILES" '{ sub(/^#?MAX_OPEN_FILES=.*/, LINE); print; }' /etc/init.d/elasticsearch \
> /etc/init.d/elasticsearch.new && mv /etc/init.d/elasticsearch.new /etc/init.d/elasticsearch \
&& chmod +x /etc/init.d/elasticsearch
fi
# override MAX_MAP_COUNT variable if set
if [ ! -z "$MAX_MAP_COUNT" ]; then
awk -v LINE="MAX_MAP_COUNT=$MAX_MAP_COUNT" '{ sub(/^#?MAX_MAP_COUNT=.*/, LINE); print; }' /etc/init.d/elasticsearch \
> /etc/init.d/elasticsearch.new && mv /etc/init.d/elasticsearch.new /etc/init.d/elasticsearch \
&& chmod +x /etc/init.d/elasticsearch
fi
service elasticsearch start
# wait for Elasticsearch to start up before either starting Kibana (if enabled)
# or attempting to stream its log file
# - https://github.com/elasticsearch/kibana/issues/3077
# set number of retries (default: 60, override using ES_CONNECT_RETRY env var)
re_is_numeric='^[0-9]+$'
if ! [[ $ES_CONNECT_RETRY =~ $re_is_numeric ]] ; then
ES_CONNECT_RETRY=60
fi
if [ -z "$ELASTICSEARCH_URL" ]; then
ELASTICSEARCH_URL=${ES_PROTOCOL:-http}://localhost:9200
fi
counter=0
while [ ! "$(curl -k ${ELASTICSEARCH_URL} 2> /dev/null)" -a $counter -lt $ES_CONNECT_RETRY ]; do
sleep 1
((counter++))
echo "waiting for Elasticsearch to be up ($counter/$ES_CONNECT_RETRY)"
done
if [ ! "$(curl -k ${ELASTICSEARCH_URL} 2> /dev/null)" ]; then
echo "Couldn't start Elasticsearch. Exiting."
echo "Elasticsearch log follows below."
cat /var/log/elasticsearch/elasticsearch.log
exit 1
fi
# wait for cluster to respond before getting its name
counter=0
while [ -z "$CLUSTER_NAME" -a $counter -lt 60 ]; do
sleep 1
((counter++))
CLUSTER_NAME=$(curl -k ${ELASTICSEARCH_URL}/_cat/health?h=cluster 2> /dev/null | tr -d '[:space:]')
echo "Waiting for Elasticsearch cluster to respond ($counter/60)"
done
if [ -z "$CLUSTER_NAME" ]; then
echo "Couldn't get name of cluster. Exiting."
echo "Elasticsearch log follows."
cat /var/log/elasticsearch/elasticsearch.log
exit 1
elif [[ "$CLUSTER_NAME" =~ "master_not_discovered_exception" ]]; then
# If we got a JSON error back, don't treat it like the literal name of the cluster.
# Example of what this error looks like:
# [{"error":{"root_cause":[{"type":"master_not_discovered_exception","reason":null}]
# We don't know the cluster name, so we'll just glob it.
echo "Failed to contact a healthy master in cluster."
echo "Elasticsearch logs follow."
cat /var/log/elasticsearch/*.log
exit 1
fi
OUTPUT_LOGFILES+="/var/log/elasticsearch/${CLUSTER_NAME}.log "
fi
### Logstash
if [ -z "$LOGSTASH_START" ]; then
LOGSTASH_START=1
fi
if [ "$LOGSTASH_START" -ne "1" ]; then
echo "LOGSTASH_START is set to something different from 1, not starting..."
else
# override LS_HEAP_SIZE variable if set
if [ ! -z "$LS_HEAP_SIZE" ]; then
awk -v LINE="-Xmx$LS_HEAP_SIZE" '{ sub(/^.Xmx.*/, LINE); print; }' ${LOGSTASH_PATH_SETTINGS}/jvm.options \
> ${LOGSTASH_PATH_SETTINGS}/jvm.options.new && mv ${LOGSTASH_PATH_SETTINGS}/jvm.options.new ${LOGSTASH_PATH_SETTINGS}/jvm.options
awk -v LINE="-Xms$LS_HEAP_SIZE" '{ sub(/^.Xms.*/, LINE); print; }' ${LOGSTASH_PATH_SETTINGS}/jvm.options \
> ${LOGSTASH_PATH_SETTINGS}/jvm.options.new && mv ${LOGSTASH_PATH_SETTINGS}/jvm.options.new ${LOGSTASH_PATH_SETTINGS}/jvm.options
fi
if [ ! -z "$LS_HEAP_DISABLE" ]; then
awk -v LINE="#-XX:+HeapDumpOnOutOfMemoryError" '{ sub(/^-XX:\+HeapDumpOnOutOfMemoryError.*/, LINE); print; }' ${LOGSTASH_PATH_SETTINGS}/jvm.options \
> ${LOGSTASH_PATH_SETTINGS}/jvm.options.new && mv ${LOGSTASH_PATH_SETTINGS}/jvm.options.new ${LOGSTASH_PATH_SETTINGS}/jvm.options
fi
# override LS_OPTS variable if set
if [ ! -z "$LS_OPTS" ]; then
awk -v LINE="LS_OPTS=\"$LS_OPTS\"" '{ sub(/^LS_OPTS=.*/, LINE); print; }' /etc/init.d/logstash \
> /etc/init.d/logstash.new && mv /etc/init.d/logstash.new /etc/init.d/logstash && chmod +x /etc/init.d/logstash
fi
service logstash start
OUTPUT_LOGFILES+="/var/log/logstash/logstash-plain.log "
fi
### Kibana
if [ -z "$KIBANA_START" ]; then
KIBANA_START=1
fi
if [ "$KIBANA_START" -ne "1" ]; then
echo "KIBANA_START is set to something different from 1, not starting..."
else
# override NODE_OPTIONS variable if set
if [ ! -z "$NODE_OPTIONS" ]; then
awk -v LINE="NODE_OPTIONS=\"$NODE_OPTIONS\"" '{ sub(/^NODE_OPTIONS=.*/, LINE); print; }' /etc/init.d/kibana \
> /etc/init.d/kibana.new && mv /etc/init.d/kibana.new /etc/init.d/kibana && chmod +x /etc/init.d/kibana
fi
service kibana start
OUTPUT_LOGFILES+="/var/log/kibana/kibana5.log "
fi
# Exit if nothing has been started
if [ "$ELASTICSEARCH_START" -ne "1" ] && [ "$LOGSTASH_START" -ne "1" ] \
&& [ "$KIBANA_START" -ne "1" ]; then
>&2 echo "No services started. Exiting."
exit 1
fi
## run post-hooks
if [ -x /usr/local/bin/elk-post-hooks.sh ] || [ -x /opt/fidc/fidc-init.sh ]; then
### if Kibana was started...
if [ "$KIBANA_START" -eq "1" ]; then
### ... then wait for Kibana to be up first to ensure that .kibana index is
### created before the post-hooks are executed
# set number of retries (default: 60, override using KIBANA_CONNECT_RETRY env var)
if ! [[ $KIBANA_CONNECT_RETRY =~ $re_is_numeric ]] ; then
KIBANA_CONNECT_RETRY=60
fi
if [ -z "$KIBANA_URL" ]; then
KIBANA_URL=http://localhost:5601
fi
counter=0
while [ ! "$(curl ${KIBANA_URL} 2> /dev/null)" -a $counter -lt $KIBANA_CONNECT_RETRY ]; do
sleep 1
((counter++))
echo "waiting for Kibana to be up ($counter/$KIBANA_CONNECT_RETRY)"
done
if [ ! "$(curl ${KIBANA_URL} 2> /dev/null)" ]; then
echo "Couldn't start Kibana. Exiting."
echo "Kibana log follows below."
cat /var/log/kibana/kibana5.log
exit 1
fi
# wait for Kibana to not only be up but to return 200 OK
counter=0
while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' ${KIBANA_URL}/api/status)" != "200" && $counter -lt 60 ]]; do
sleep 1
((counter++))
echo "waiting for Kibana to respond ($counter/60)"
done
if [[ "$(curl -s -o /dev/null -w ''%{http_code}'' ${KIBANA_URL}/api/status)" != "200" ]]; then
echo "Timed out waiting for Kibana to respond. Exiting."
echo "Kibana log follows below."
cat /var/log/kibana/kibana5.log
exit 1
fi
fi
if [ -x /usr/local/bin/elk-post-hooks.sh ]; then
. /usr/local/bin/elk-post-hooks.sh
fi
# ForgeRock IDC - Kibana started, create FIDC config
/opt/fidc/fidc-init.sh
fi
touch $OUTPUT_LOGFILES
tail -f $OUTPUT_LOGFILES &
wait