forked from scylladb/scylla-monitoring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start-grafana.sh
executable file
·268 lines (247 loc) · 8.59 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
#!/usr/bin/env bash
CURRENT_VERSION="master"
if [ -f CURRENT_VERSION.sh ]; then
CURRENT_VERSION=`cat CURRENT_VERSION.sh`
fi
. versions.sh
. UA.sh
if [ -f env.sh ]; then
. env.sh
fi
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]}
LOCAL=""
GRAFANA_ADMIN_PASSWORD="admin"
GRAFANA_AUTH=false
GRAFANA_AUTH_ANONYMOUS=true
DOCKER_PARAM=""
EXTERNAL_VOLUME=""
BIND_ADDRESS=""
ANONYMOUS_ROLE="Admin"
SPECIFIC_SOLUTION=""
LDAP_FILE=""
DATA_SOURCES=""
LIMITS=""
VOLUMES=""
PARAMS=""
for arg; do
shift
if [ -z "$LIMIT" ]; then
case $arg in
(--limit)
LIMIT="1"
;;
(--volume)
LIMIT="1"
VOLUME="1"
;;
(--param)
LIMIT="1"
PARAM="1"
;;
(*) 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
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 enviroment 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"
while getopts ':hlEg:n:p:v:a:x:c:j:m:G:M:D:A:S:P:L:Q:' 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) DOCKER_PARAM="$DOCKER_PARAM --net=host"
;;
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) DOCKER_PARAM="$DOCKER_PARAM $OPTARG"
;;
Q) ANONYMOUS_ROLE=$OPTARG
;;
a) GRAFANA_ADMIN_PASSWORD=$OPTARG
GRAFANA_AUTH=true
GRAFANA_AUTH_ANONYMOUS=false
;;
x) HTTP_PROXY="$OPTARG"
;;
c) GRAFANA_ENV_ARRAY+=("$OPTARG")
;;
j) GRAFANA_DASHBOARD_ARRAY+=("$OPTARG")
;;
A) BIND_ADDRESS="$OPTARG:"
;;
S) SPECIFIC_SOLUTION="-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
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"
done
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 $GRAFANA_DASHBOARD_COMMAND
./grafana-datasource.sh $DATA_SOURCES
if [[ ! $DOCKER_PARAM = *"--net=host"* ]]; then
PORT_MAPPING="-p $BIND_ADDRESS$GRAFANA_PORT:3000"
fi
if [ ! -z $RUN_RENDERER ]; then
if [ ! -z "$is_podman" ]; then
DOCKER_HOST=`hostname -I | awk '{print $1}'`
else
DOCKER_HOST=$(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://$DOCKER_HOST:8081/render -e GF_RENDERING_CALLBACK_URL=http://$DOCKER_HOST:$GRAFANA_PORT/"
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/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_PLUGINS_ALLOW_LOADING_UNSIGNED_PLUGINS=scylladb-scylla-datasource,camptocamp-prometheus-alertmanager-datasource" \
-e "GF_DASHBOARDS_DEFAULT_HOME_DASHBOARD_PATH=/var/lib/grafana/dashboards/ver_$VERSION/scylla-overview.$VERSION.json" \
$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"