-
Notifications
You must be signed in to change notification settings - Fork 0
/
elk_stack.sh
executable file
·131 lines (109 loc) · 3.57 KB
/
elk_stack.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
#!/usr/bin/env bash
#!/usr/bin/env bash
# Borrowed bash subcommand template from: https://gist.github.com/waylan/4080362
prog_name=$(basename $0)
sub_help() {
echo "Usage: $prog_name <subcommand> [options]\n"
echo "Subcommands:"
echo " init Start the ELK Stack & add sample Logshipper dashboard"
echo ""
echo " start Start the ELK Stack"
echo " stop Stop the ELK Stack"
echo " status Status of ELK Stack"
echo " restart Restart the ELK Stack"
echo ""
echo " purge Stop & delete all containers associated with Logshipper"
echo ""
echo "Note: This script assumes you have 'docker-compose' installed."
echo ""
}
access_info() {
echo "#################################################"
echo "## ##"
echo "## Access Kibana via: http://localhost:5601/ ##"
echo "## ##"
echo "## Username: elastic ##"
echo "## Password: changeme ##"
echo "## ##"
echo "#################################################"
}
sub_init() {
pushd ./elk >/dev/null || exit
# Bring up the containers
docker-compose up -d --force-recreate --renew-anon-volumes
# Test for Kibana being ready
function test_kibana_status() {
docker exec -it elk-kibana-1 /bin/bash -c "curl localhost:5601/api/status -u 'elastic:changeme'" 2>&1 | jq .status.overall.summary 2>&1
}
until test_kibana_status | grep -q -m 1 "All services are available"; do
printf "[$(date +"%Y-%m-%dT%H:%M:%S%z")] Waiting for Kibana to be available...\r"
sleep 1
done
echo 'Kibana ready!'
popd >/dev/null || exit
# Add a sample dashboard into Kibana
sub_create_dashboard
sub_status
}
sub_create_dashboard() {
pushd ./elk >/dev/null || exit
# Add a sample dashboard into Kibana
echo "Adding dashboard for 'Logshipper Telemetry PoC'..."
DASHBOARD_FILE=./kibana/Logshipper\ Telemetry\ PoC\ Dashboard.ndjson
cat "${DASHBOARD_FILE}" | jq -c >"${DASHBOARD_FILE}.TMP" && mv "${DASHBOARD_FILE}.TMP" "${DASHBOARD_FILE}"
curl --silent -X POST localhost:5601/api/saved_objects/_import?createNewCopies=true \
-H 'kbn-xsrf: true' \
--form file=@"${DASHBOARD_FILE}" \
-u 'elastic:changeme' > /tmp/logshipper_dashboard_import.json
json_output=$(cat /tmp/logshipper_dashboard_import.json)
echo "Imported $(echo "$json_output" | jq '.successCount') Items:"
echo "=================="
echo "$json_output" | jq -r '.successResults[] | @sh "printf \"[%-13s] %s\\n\" \(.type) \(.meta.title)"' | bash
rm /tmp/logshipper_dashboard_import.json
git restore "${DASHBOARD_FILE}"
echo 'Dashboard added!'
popd >/dev/null || exit
}
sub_start() {
pushd ./elk >/dev/null || exit
docker-compose up -d --force-recreate --renew-anon-volumes
popd >/dev/null || exit
sub_status
}
sub_stop() {
pushd ./elk >/dev/null || exit
docker-compose stop
popd >/dev/null || exit
}
sub_status() {
pushd ./elk >/dev/null || exit
docker-compose ps
popd >/dev/null || exit
access_info
}
sub_restart() {
sub_stop
sub_start
}
sub_purge() {
pushd ./elk >/dev/null || exit
docker-compose rm -s -v -f
docker-compose down -v
docker volume prune -f
popd >/dev/null || exit
}
subcommand=$1
case $subcommand in
"" | "-h" | "--help")
sub_help
;;
*)
shift
sub_${subcommand} $@
if [[ $? = 127 ]]; then
echo "Error: '$subcommand' is not a known subcommand." >&2
echo " Run '$prog_name --help' for a list of known subcommands." >&2
exit 1
fi
;;
esac