forked from spujadas/elk-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.sh
192 lines (152 loc) · 6.42 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
#!/bin/bash
#
# /usr/local/bin/start.sh
# Start Elasticsearch, Logstash and Kibana services
#
# spujadas 2015-10-09; added initial pidfile removal and graceful termination
# 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"
service elasticsearch stop
service logstash stop
service kibana stop
exit 0
}
trap _term SIGTERM
## 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
# override ES_HEAP_SIZE variable if set
if [ ! -z "$ES_HEAP_SIZE" ]; then
awk -v LINE="-Xmx$ES_HEAP_SIZE" '{ sub(/^.Xmx.*/, LINE); print; }' /opt/elasticsearch/config/jvm.options \
> /opt/elasticsearch/config/jvm.options.new && mv /opt/elasticsearch/config/jvm.options.new /opt/elasticsearch/config/jvm.options
awk -v LINE="-Xms$ES_HEAP_SIZE" '{ sub(/^.Xms.*/, LINE); print; }' /opt/elasticsearch/config/jvm.options \
> /opt/elasticsearch/config/jvm.options.new && mv /opt/elasticsearch/config/jvm.options.new /opt/elasticsearch/config/jvm.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: 30, override using ES_CONNECT_RETRY env var)
re_is_numeric='^[0-9]+$'
if ! [[ $ES_CONNECT_RETRY =~ $re_is_numeric ]] ; then
ES_CONNECT_RETRY=30
fi
counter=0
while [ ! "$(curl localhost:9200 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 localhost:9200 2> /dev/null)" ]; then
echo "Couln'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 30 ]; do
sleep 1
((counter++))
CLUSTER_NAME=$(curl localhost:9200/_cat/health?h=cluster 2> /dev/null | tr -d '[:space:]')
echo "Waiting for Elasticsearch cluster to respond ($counter/30)"
done
if [ -z "$CLUSTER_NAME" ]; then
echo "Couln't get name of cluster. Exiting."
echo "Elasticsearch log follows below."
cat /var/log/elasticsearch/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; }' /opt/logstash/config/jvm.options \
> /opt/logstash/config/jvm.options.new && mv /opt/logstash/config/jvm.options.new /opt/logstash/config/jvm.options
awk -v LINE="-Xms$LS_HEAP_SIZE" '{ sub(/^.Xms.*/, LINE); print; }' /opt/logstash/config/jvm.options \
> /opt/logstash/config/jvm.options.new && mv /opt/logstash/config/jvm.options.new /opt/logstash/config/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
touch $OUTPUT_LOGFILES
tail -f $OUTPUT_LOGFILES &
wait