-
Notifications
You must be signed in to change notification settings - Fork 144
/
start-grafana.sh
executable file
·352 lines (333 loc) · 11.3 KB
/
start-grafana.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/env bash
CURRENT_VERSION="master"
if [ -f CURRENT_VERSION.sh ]; then
CURRENT_VERSION=`cat CURRENT_VERSION.sh`
fi
LOCAL=""
if [ -z "$GRAFANA_ADMIN_PASSWORD" ]; then
GRAFANA_ADMIN_PASSWORD="admin"
fi
if [ -z "$GRAFANA_AUTH" ]; then
GRAFANA_AUTH=false
fi
if [ -z "$GRAFANA_AUTH_ANONYMOUS" ]; then
GRAFANA_AUTH_ANONYMOUS=true
fi
EXTERNAL_VOLUME=""
BIND_ADDRESS=""
if [ -z "$ANONYMOUS_ROLE" ]; then
ANONYMOUS_ROLE="Admin"
fi
SPECIFIC_SOLUTION=""
LDAP_FILE=""
DATA_SOURCES=""
LIMITS=""
VOLUMES=""
PARAMS=""
DEFAULT_THEME="light"
. versions.sh
. UA.sh
if [ -f env.sh ]; then
. env.sh
fi
DOCKER_PARAM=""
BRANCH_VERSION=$CURRENT_VERSION
if [ -z ${DEFAULT_VERSION[$CURRENT_VERSION]} ]; then
BRANCH_VERSION=`echo $CURRENT_VERSION|cut -d'.' -f1,2`
fi
if [ "$1" = "-e" ]; then
DEFAULT_VERSION=${DEFAULT_ENTERPRISE_VERSION[$BRANCH_VERSION]}
fi
MANAGER_VERSION=${MANAGER_DEFAULT_VERSION[$BRANCH_VERSION]}
for arg; do
shift
if [ -z "$LIMIT" ]; then
case $arg in
(--limit)
LIMIT="1"
;;
(--alternator)
RUN_ALTERNATOR="1"
;;
(--volume)
LIMIT="1"
VOLUME="1"
;;
(--param)
LIMIT="1"
PARAM="1"
;;
(--scrap)
PARAM="scrap"
LIMIT="1"
;;
(--auth)
GRAFANA_AUTH=true
;;
(--disable-anonymous)
GRAFANA_AUTH_ANONYMOUS=false
;;
(*) set -- "$@" "$arg"
;;
esac
else
DOCR=`echo $arg|cut -d',' -f1`
VALUE=`echo $arg|cut -d',' -f2-|sed 's/#/ /g'`
NOSPACE=`echo $arg|sed 's/ /#/g'`
if [ "$PARAM" = "1" ]; then
if [ -z "${DOCKER_PARAMS[$DOCR]}" ]; then
DOCKER_PARAMS[$DOCR]=""
fi
DOCKER_PARAMS[$DOCR]="${DOCKER_PARAMS[$DOCR]} $VALUE"
PARAMS="$PARAMS --param $NOSPACE"
unset PARAM
elif [ "$PARAM" = "scrap" ]; then
SCRAP_INTERVAL="-S $NOSPACE"
unset PARAM
else
if [ -z "${DOCKER_LIMITS[$DOCR]}" ]; then
DOCKER_LIMITS[$DOCR]=""
fi
if [ "$VOLUME" = "1" ]; then
SRC=`echo $VALUE|cut -d':' -f1`
DST=`echo $VALUE|cut -d':' -f2-`
SRC=$(readlink -m $SRC)
DOCKER_LIMITS[$DOCR]="${DOCKER_LIMITS[$DOCR]} -v $SRC:$DST"
VOLUMES="$VOLUMES --volume $NOSPACE"
unset VOLUME
else
DOCKER_LIMITS[$DOCR]="${DOCKER_LIMITS[$DOCR]} $VALUE"
LIMITS="$LIMITS --limit $NOSPACE"
fi
fi
unset LIMIT
fi
done
usage="$(basename "$0") [-h] [-v comma separated versions ] [-g grafana port ] [-G path to external dir] [-n grafana container name ] [-p ip:port address of prometheus ] [-j additional dashboard to load to Grafana, multiple params are supported] [-c grafana environment variable, multiple params are supported] [-x http_proxy_host:port] [-m alert_manager address] [-a admin password] [ -M scylla-manager version ] [-D encapsulate docker param] [-Q Grafana anonymous role (Admin/Editor/Viewer)] [-S start with a system specific dashboard set] [-P ldap_config_file] -- loads the prometheus datasource and the Scylla dashboards into an existing grafana installation"
if [ "$DOCKER_PARAM" != "" ]; then
DOCKER_PARAM_FROM_FILE="1"
fi
while getopts ':hlEg:n:p:v:a:x:c:j:m:G:M:D:A:S:P:L:Q:s:' option; do
case "$option" in
h) echo "$usage"
exit
;;
v) VERSIONS=$OPTARG
;;
M) MANAGER_VERSION=$OPTARG
;;
g) GRAFANA_PORT=$OPTARG
;;
G) EXTERNAL_VOLUME="-v "`readlink -m $OPTARG`":/var/lib/grafana"
if [ ! -d $OPTARG ]; then
echo "Creating grafana external directory $OPTARG"
mkdir -p $OPTARG
fi
;;
n) GRAFANA_NAME=$OPTARG
;;
p) DATA_SOURCES="$DATA_SOURCES -p $OPTARG"
;;
m) DATA_SOURCES="$DATA_SOURCES -m $OPTARG"
;;
L) DATA_SOURCES="$DATA_SOURCES -L $OPTARG"
;;
l) if [[ "$DOCKER_PARAM" != *"--net=host"* ]]; then
DOCKER_PARAM="$DOCKER_PARAM --net=host"
fi
;;
P) LDAP_FILE="$OPTARG"
GRAFANA_ENV_ARRAY+=("GF_AUTH_LDAP_ENABLED=true" "GF_AUTH_LDAP_CONFIG_FILE=/etc/grafana/ldap.toml" "GF_AUTH_LDAP_ALLOW_SIGN_UP=true")
LDAP_FILE="-v "`readlink -m $OPTARG`":/etc/grafana/ldap.toml"
GRAFANA_AUTH=true
GRAFANA_AUTH_ANONYMOUS=false
;;
D) if [ "$DOCKER_PARAM_FROM_FILE" = "1" ]; then
DOCKER_PARAM=""
DOCKER_PARAM_FROM_FILE=""
fi
DOCKER_PARAM="$DOCKER_PARAM $OPTARG"
;;
Q) ANONYMOUS_ROLE=$OPTARG
;;
a) GRAFANA_ADMIN_PASSWORD=$OPTARG
;;
x) HTTP_PROXY="$OPTARG"
;;
c) GRAFANA_ENV_ARRAY+=("$OPTARG")
;;
j) GRAFANA_DASHBOARD_ARRAY+=("$OPTARG")
;;
A) BIND_ADDRESS="$OPTARG:"
;;
S) SPECIFIC_SOLUTION="-S $OPTARG"
;;
s) STACK="/stack/$OPTARG"
STACK_CMD="-s $OPTARG"
;;
E) RUN_RENDERER="-E"
;;
:) printf "missing argument for -%s\n" "$OPTARG" >&2
echo "$usage" >&2
exit 1
;;
\?) printf "illegal option: -%s\n" "$OPTARG" >&2
echo "$usage" >&2
exit 1
;;
esac
done
if [ -z $GRAFANA_PORT ]; then
GRAFANA_PORT=3000
if [ -z $GRAFANA_NAME ]; then
GRAFANA_NAME=agraf
fi
fi
if [ -z $ANALYTICS_REPORTING_ENABLED ]; then
ANALYTICS_REPORTING_ENABLED="false"
fi
if [ -z $ANALYTICS_CHECK_FOR_UPDATES ]; then
ANALYTICS_CHECK_FOR_UPDATES="false"
fi
if [ -z $ANALYTICS_CHECK_FOR_PLUGIN_UPDATES ]; then
ANALYTICS_CHECK_FOR_PLUGIN_UPDATES="false"
fi
if [ -z $SECURITY_ANGULAR_SUPPORT_ENABLED ]; then
SECURITY_ANGULAR_SUPPORT_ENABLED="false"
fi
if [ -z $SERVER_ENABLE_GZIP ]; then
SERVER_ENABLE_GZIP="true"
fi
VERSION=`echo $VERSIONS|cut -d',' -f1`
if [ "$VERSION" = "" ]; then
echo "Scylla-version was not not found, add the -v command-line with a specific version (i.e. -v 2021.1)"
exit 1
fi
if [ "$VERSION" = "latest" ]; then
if [ -z "$BRANCH_VERSION" ] || [ "$BRANCH_VERSION" = "master" ]; then
echo "Default versions (-v latest) is not supported on the master branch, use specific version instead"
exit 1
fi
VERSION=${DEFAULT_VERSION[$BRANCH_VERSION]}
echo "The use of -v latest is deprecated. Use a specific version instead."
fi
if [ -z $GRAFANA_NAME ]; then
GRAFANA_NAME=agraf-$GRAFANA_PORT
fi
docker container inspect $GRAFANA_NAME > /dev/null 2>&1
if [ $? -eq 0 ]; then
printf "\nSome of the monitoring docker instances ($GRAFANA_NAME) exist. Make sure all containers are killed and removed. You can use kill-all.sh for that\n"
exit 1
fi
group_args=()
is_podman="$(docker --help | grep -o podman)"
if [ ! -z "$is_podman" ]; then
group_args+=(--userns=keep-id)
fi
if [ "`id -u`" -ne 0 ]; then
GROUPID=`id -g`
USER_PERMISSIONS="-u $UID:$GROUPID"
fi
proxy_args=()
if [[ -n "$HTTP_PROXY" ]]; then
proxy_args=(-e http_proxy="$HTTP_PROXY")
fi
for val in "${GRAFANA_ENV_ARRAY[@]}"; do
GRAFANA_ENV_COMMAND="$GRAFANA_ENV_COMMAND -e $val"
if [[ $val == GF_USERS_DEFAULT_THEME=* ]]; then
DEFAULT_THEME=""
fi
done
if [[ $DEFAULT_THEME != "" ]]; then
GRAFANA_ENV_COMMAND="$GRAFANA_ENV_COMMAND -e GF_USERS_DEFAULT_THEME=$DEFAULT_THEME"
fi
for val in "${GRAFANA_DASHBOARD_ARRAY[@]}"; do
GRAFANA_DASHBOARD_COMMAND="$GRAFANA_DASHBOARD_COMMAND -j $val"
done
./generate-dashboards.sh -t $SPECIFIC_SOLUTION -v $VERSIONS -M $MANAGER_VERSION $STACK_CMD $GRAFANA_DASHBOARD_COMMAND
./grafana-datasource.sh $DATA_SOURCES $STACK_CMD $SCRAP_INTERVAL
if [[ ! $DOCKER_PARAM = *"--net=host"* ]]; then
PORT_MAPPING="-p $BIND_ADDRESS$GRAFANA_PORT:3000"
fi
if [[ "$HOME_DASHBOARD" = "" ]]; then
if [ "$RUN_ALTERNATOR" = "1" ]; then
HOME_DASHBOARD="/var/lib/grafana/dashboards/ver_$VERSION/alternator.$VERSION.json"
else
HOME_DASHBOARD="/var/lib/grafana/dashboards/ver_$VERSION/scylla-overview.$VERSION.json"
fi
fi
if [[ -z "${DOCKER_HOST}" ]]; then
if [ ! -z "$is_podman" ]; then
if [[ $(uname) == "Linux" ]]; then
DOCKER_HOST=$(hostname -I | awk '{print $1}')
elif [[ $(uname) == "Darwin" ]]; then
DOCKER_HOST=$(ifconfig bridge0 | awk '/inet / {print $2}')
fi
else
if [[ $(uname) == "Linux" ]]; then
DOCKER_HOST=$(ip -4 addr show docker0 | grep -Po 'inet \K[\d.]+')
elif [[ $(uname) == "Darwin" ]]; then
DOCKER_HOST=$(ifconfig bridge0 | awk '/inet / {print $2}')
fi
fi
fi
if [ ! -z $RUN_RENDERER ]; then
if [ ! -z "$is_podman" ]; then
HOST_ADDRESS=`hostname -I | awk '{print $1}'`
else
HOST_ADDRESS=$(ip -4 addr show docker0 | grep -Po 'inet \K[\d.]+')
fi
RENDERING_SERVER_URL=`./start-grafana-renderer.sh $LIMITS $VOLUMES $PARAMS -D "$DOCKER_PARAM"`
GRAFANA_ENV_COMMAND="$GRAFANA_ENV_COMMAND -e GF_RENDERING_SERVER_URL=http://$HOST_ADDRESS:8081/render -e GF_RENDERING_CALLBACK_URL=http://$HOST_ADDRESS:$GRAFANA_PORT/"
fi
if [ -z $STACK ]; then
STACK=""
fi
docker run -d $DOCKER_PARAM ${DOCKER_LIMITS["grafana"]} -i $USER_PERMISSIONS $PORT_MAPPING \
-e "GF_AUTH_BASIC_ENABLED=$GRAFANA_AUTH" \
-e "GF_AUTH_ANONYMOUS_ENABLED=$GRAFANA_AUTH_ANONYMOUS" \
-e "GF_AUTH_ANONYMOUS_ORG_ROLE=$ANONYMOUS_ROLE" \
-e "GF_PANELS_DISABLE_SANITIZE_HTML=true" \
$LDAP_FILE \
"${group_args[@]}" \
-v $PWD/grafana/build:/var/lib/grafana/dashboards:z \
-v $PWD/grafana/plugins:/var/lib/grafana/plugins:z \
-v $PWD/grafana$STACK/provisioning:/var/lib/grafana/provisioning:z $EXTERNAL_VOLUME \
-e "GF_PATHS_PROVISIONING=/var/lib/grafana/provisioning" \
-e "GF_SECURITY_ADMIN_PASSWORD=$GRAFANA_ADMIN_PASSWORD" \
-e "GF_ANALYTICS_GOOGLE_ANALYTICS_UA_ID=$UA_ANALTYICS" \
-e "GF_ANALYTICS_REPORTING_ENABLED=$ANALYTICS_REPORTING_ENABLED" \
-e "GF_ANALYTICS_CHECK_FOR_UPDATES=$ANALYTICS_CHECK_FOR_UPDATES" \
-e "GF_ANALYTICS_CHECK_FOR_PLUGIN_UPDATES=$ANALYTICS_CHECK_FOR_PLUGIN_UPDATES" \
-e "GF_SECURITY_ANGULAR_SUPPORT_ENABLED=$SECURITY_ANGULAR_SUPPORT_ENABLED" \
-e "GF_PLUGINS_ALLOW_LOADING_UNSIGNED_PLUGINS=scylladb-scylla-datasource" \
-e "GF_DASHBOARDS_DEFAULT_HOME_DASHBOARD_PATH=$HOME_DASHBOARD" \
-e "GF_SERVER_ENABLE_GZIP=$SERVER_ENABLE_GZIP" \
$GRAFANA_ENV_COMMAND \
"${proxy_args[@]}" \
--name $GRAFANA_NAME docker.io/grafana/grafana:$GRAFANA_VERSION ${DOCKER_PARAMS["grafana"]} >& /dev/null
if [ $? -ne 0 ]; then
echo "Error: Grafana container failed to start"
echo "For more information use: docker logs $GRAFANA_NAME"
exit 1
fi
# Wait till Grafana API is available
printf "Wait for Grafana container to start."
RETRIES=7
TRIES=0
until $(curl --output /dev/null -f --silent http://localhost:$GRAFANA_PORT/api/org) || [ $TRIES -eq $RETRIES ]; do
printf '.'
((TRIES=TRIES+1))
sleep 5
done
echo
if [ ! "$(docker ps -q -f name=$GRAFANA_NAME)" ]
then
echo "Error: Grafana container failed to start"
echo "For more information use: docker logs $GRAFANA_NAME"
exit 1
fi
if [ -z "$BIND_ADDRESS" ]; then
BIND_ADDRESS="localhost:"
fi
printf "Start completed successfully, check http://$BIND_ADDRESS$GRAFANA_PORT\n"