-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpureelk.sh
executable file
·283 lines (236 loc) · 6.87 KB
/
pureelk.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
#!/bin/bash
PUREELK_PATH=/var/lib/pureelk
PUREELK_CONF=$PUREELK_PATH/conf
PUREELK_ESDATA=$PUREELK_PATH/esdata
PUREELK_LOG=/var/log/pureelk
PUREELK_ES=pureelk-elasticsearch
PUREELK_KI=pureelk-kibana
PUREELK=pureelk
LOGROTATE=blacklabelops-logrotate
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
PUREELK_SCRIPT_URL=https://raw.githubusercontent.com/pureelk/pureelk/master/pureelk.sh
PUREELK_SCRIPT_LOCALPATH=$PUREELK_PATH/pureelk.sh
print_help() {
echo "Usage: $0 {help|install [dns_ip]|start|stop|attach|delete}"
}
print_info() {
printf "${GREEN}$1${NC}\n"
}
print_warn() {
printf "${YELLOW}$1${NC}\n"
}
detect_distro()
{
# init process is pid 1
INIT=`ls -l /proc/1/exe`
if [[ $INIT == *"upstart"* ]]; then
SYSTEMINITDAEMON=upstart
elif [[ $INIT == *"systemd"* ]]; then
SYSTEMINITDAEMON=systemd
elif [[ $INIT == *"/sbin/init"* ]]; then
INIT=`/sbin/init --version`
if [[ $INIT == *"upstart"* ]]; then
SYSTEMINITDAEMON=upstart
elif [[ $INIT == *"systemd"* ]]; then
SYSTEMINITDAEMON=systemd
fi
fi
if [ -z "$SYSTEMINITDAEMON" ]; then
echo "WARNING: Unknown distribution, defaulting to systemd - this may fail." >&2
SYSTEMINITDAEMON=systemd
fi
}
config_service() {
if [ "$SYSTEMINITDAEMON" == "systemd" ]
then
config_systemd
else
config_upstart
fi
}
config_upstart() {
curl -o ${PUREELK_SCRIPT_LOCALPATH} ${PUREELK_SCRIPT_URL}
chmod u+x ${PUREELK_SCRIPT_LOCALPATH}
cat > /etc/init/pureelk.conf << END_OF_UPSTART
start on runlevel [2345]
stop on [!2345]
task
exec ${PUREELK_SCRIPT_LOCALPATH} start
END_OF_UPSTART
}
config_systemd() {
curl -o ${PUREELK_SCRIPT_LOCALPATH} ${PUREELK_SCRIPT_URL}
chmod u+x ${PUREELK_SCRIPT_LOCALPATH}
cat > /etc/systemd/system/docker-pureelk.service << END_OF_SYSTEMD
[Unit]
Description=pureelk container
Requires=docker.service
After=docker.service
[Service]
Type=oneshot
ExecStart=${PUREELK_SCRIPT_LOCALPATH} start
ExecStop=${PUREELK_SCRIPT_LOCALPATH} stop
RemainAfterExit=yes
[Install]
WantedBy=default.target
END_OF_SYSTEMD
systemctl enable docker-pureelk.service
}
install() {
if [ "$(uname)" == "Linux" ]; then
detect_distro
which docker
if [ $? -ne 0 ]
then
print_warn "Docker not yet installed, installing..."
curl -sSL https://get.docker.com/ | sh
# For CentOS, we need to start the docker service
if [ "$SYSTEMINITDAEMON" == "systemd" ]
then
systemctl start docker
systemctl enable docker
fi
else
print_info "Docker is already installed"
fi
fi
print_info "Pulling elasticsearch image..."
docker pull elasticsearch:2.4
print_info "Pulling kibana image..."
docker pull kibana:4
print_info "Pulling pureelk image..."
docker pull pureelk/pureelk
print_info "Pulling logrotate image..."
docker pull blacklabelops/logrotate
print_info "Creating local pureelk folders at $PUREELK_PATH"
if [ ! -d "$PUREELK_CONF" ]; then
sudo mkdir -p $PUREELK_CONF
fi
if [ ! -d "$PUREELK_ESDATA" ]; then
sudo mkdir -p $PUREELK_ESDATA
fi
if [ ! -d "$PUREELK_LOG" ]; then
sudo mkdir -p $PUREELK_LOG
fi
config_service
print_info "Installation complete."
}
start_containers() {
print_info "Starting PureElk elasticsearch container..."
if [ -n "$1" ];
then
DNS_ARG='--dns='$1
else
DNS_ARG=''
fi
RUNNING="$(docker inspect -f '{{.State.Running}}' $PUREELK_ES)"
if [ $? -eq 1 ];
then
print_warn "$PUREELK_ES doesn't exist, starting..."
docker run -d -P --name=$PUREELK_ES $DNS_ARG --log-opt max-size=100m -v "$PUREELK_ESDATA":/usr/share/elasticsearch/data elasticsearch:2.4 -Des.network.host=0.0.0.0
elif [ "$RUNNING" == "false" ];
then
docker start $PUREELK_ES
else
print_warn "$PUREELK_ES is already running."
fi
print_info "Start PureElk kibana container..."
RUNNING="$(docker inspect -f '{{.State.Running}}' $PUREELK_KI)"
if [ $? -eq 1 ];
then
print_warn "$PUREELK_KI doesn't, starting..."
docker run -d -p 5601:5601 --name=$PUREELK_KI $DNS_ARG --log-opt max-size=100m --link $PUREELK_ES:elasticsearch kibana:4
elif [ "$RUNNING" == "false" ];
then
docker start $PUREELK_KI
else
print_warn "$PUREELK_KI is already running."
fi
print_info "Start PureElk container..."
RUNNING="$(docker inspect -f '{{.State.Running}}' $PUREELK)"
if [ $? -eq 1 ];
then
print_warn "$PUREELK doesn't exist, starting..."
docker run -d -p 8080:8080 --name=$PUREELK $DNS_ARG --log-opt max-size=100m -v "$PUREELK_CONF":/pureelk/worker/conf -v "$PUREELK_LOG":/var/log/pureelk --link $PUREELK_ES:elasticsearch pureelk/pureelk
elif [ "$RUNNING" == "false" ];
then
docker start $PUREELK
else
print_warn "$PUREELK is already running."
fi
print_info "Start Logrotate container..."
RUNNING="$(docker inspect -f '{{.State.Running}}' $LOGROTATE)"
if [ $? -eq 1 ];
then
print_warn "$LOGROTATE doesn't exist, starting..."
docker run -d -v /var/lib/docker/containers:/var/lib/docker/containers -v /var/log/pureelk:/var/log/pureelk --name $LOGROTATE \
-e "LOGS_DIRECTORIES=/var/lib/docker/containers /var/log/pureelk" \
-e "LOGROTATE_SIZE=20M" \
-e "LOGROTATE_COPIES=10" \
-e "LOGROTATE_CRONSCHEDULE=* * * * * *" \
-e "LOGROTATE_LOGFILE=/logs/logrotatecron.log" \
blacklabelops/logrotate
elif [ "$RUNNING" == "false" ];
then
docker start $LOGROTATE
else
print_warn "$LOGROTATE is already running."
fi
print_info "PureELK management endpoint is at http://localhost:8080"
print_info "PureELK Kibana endpoint is at http://localhost:5601"
}
stop_containers() {
print_info "Stopping PureELK container..."
docker stop -t 2 $PUREELK
print_info "Stopping PureELK Kibana container..."
docker stop $PUREELK_KI
print_info "Stopping PureELK elasticsearch container..."
docker stop $PUREELK_ES
print_info "Stopping Logrotate elasticsearch container..."
docker stop $LOGROTATE
}
attach_pureelk() {
print_info "Attaching to PureELK container..."
docker exec -it $PUREELK bash
}
delete_containers() {
print_info "Removing PureELK container..."
docker rm -f $PUREELK
print_info "Removing PureELK Kibana container..."
docker rm -f $PUREELK_KI
print_info "Removing PureElk elastic search container..."
docker rm -f $PUREELK_ES
print_info "Removing Logrotate elastic search container..."
docker rm -f $LOGROTATE
}
if [ -n "$1" ];
then
case $1 in
help)
print_help
;;
install)
install
start_containers $2
;;
start)
start_containers
;;
stop)
stop_containers
;;
attach)
attach_pureelk
;;
delete)
delete_containers
;;
*)
print_help
exit 1
esac
else
print_help
fi