forked from sitespeedio/grafana-bootstrap-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·68 lines (54 loc) · 1.59 KB
/
entrypoint.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
#!/bin/sh
GF_API=${GF_API:-http://grafana:3000/api}
GF_USER=${GF_USER:-admin}
GF_PASSWORD=${GF_PASSWORD:-admin}
BACKEND=${BACKEND:-graphite}
print_header() {
echo " "
echo "------------------"
echo $1
echo "------------------"
}
wait_for_api() {
echo -n "Waiting for Grafana API "
curl -s -f -u $GF_USER:$GF_PASSWORD ${GF_API}/datasources &> /dev/null
while [ $? -ne 0 ]; do
echo -n "."
sleep 2
curl -s -f -u $GF_USER:$GF_PASSWORD ${GF_API}/datasources &> /dev/null
done
echo " "
}
# $1 = file-path, $2 = json, $3 = api-path
import_data() {
set -e
echo " "
echo $1
echo "$2" | curl -s -S -H 'Content-Type:application/json' -u $GF_USER:$GF_PASSWORD --data @- ${GF_API}$3
echo " "
set +e
}
# $1 = filename
wrap_dashboard_json() {
cat $1 | jq '.id = null | { dashboard:., inputs:[.__inputs[] | .value = .label | del(.label)], overwrite: true }'
}
# -----------
wait_for_api
print_header "Adding datasources"
for datasource in `ls -1 /datasources/$BACKEND/*.json`; do
datasource_json=$( cat $datasource )
ds_name=$( echo $datasource_json | jq -r '.name' )
api_path="${GF_API}/datasources/id/${ds_name}"
curl -f -s -u $GF_USER:$GF_PASSWORD "$api_path" &> /dev/null
if [ $? -eq 0 ]; then
echo "Datasource already exists: ${datasource}"
else
import_data "$datasource" "$datasource_json" "/datasources"
fi
done
print_header "Adding Graphite dashboards"
for dashboard in `ls -1 /dashboards/$BACKEND/*.json`; do
dashboard_json=$( wrap_dashboard_json $dashboard )
import_data "$dashboard" "$dashboard_json" "/dashboards/import"
done
print_header "Done!"