forked from mrsiano/openshift-grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-grafana.sh
executable file
·65 lines (50 loc) · 1.63 KB
/
setup-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
#!/bin/bash
datasource_name=$1
prometheus_namespace=$2
oauth=$3
yaml="grafana-ocp.yaml"
protocol="http://"
usage() {
echo "
USAGE
setup-grafana.sh pro-ocp openshift-metrics true
args:
datasource_name: grafana datasource name
prometheus_namespace: existing prometheus name e.g openshift-metrics
oauth: if set to true it will deploy grafana with oauth authorization
note:
the project must have view permissions for kube-system
"
exit 1
}
[[ -n ${datasource_name} ]] || usage
if [[ ${oauth} = true ]]; then
yaml="grafana-ocp-oauth.yaml"; protocol="https://"; echo "deploying with oauth";
fi
oc new-project grafana
oc process -f "${yaml}" |oc create -f -
oc rollout status deployment/grafana-ocp
oc adm policy add-role-to-user view -z grafana-ocp -n kube-system
payload="$( mktemp )"
cat <<EOF >"${payload}"
{
"name": "${datasource_name}",
"type": "prometheus",
"typeLogoUrl": "",
"access": "proxy",
"url": "https://$( oc get route prometheus -n "${prometheus_namespace}" -o jsonpath='{.spec.host}' )",
"basicAuth": false,
"withCredentials": false,
"jsonData": {
"tlsSkipVerify":true,
"token":"$( oc sa get-token grafana-ocp )"
}
}
EOF
grafana_host="${protocol}$( oc get route grafana-ocp -o jsonpath='{.spec.host}' )"
curl -H "Content-Type: application/json" -u admin:admin "${grafana_host}/api/datasources" -X POST -d "@${payload}"
dashboard_file="./openshift-cluster-monitoring.json"
sed -i.bak "s/\${DS_PR}/${datasource_name}/" "${dashboard_file}"
curl -H "Content-Type: application/json" -u admin:admin "${grafana_host}/api/dashboards/db" -X POST -d "@${dashboard_file}"
mv "${dashboard_file}.bak" "${dashboard_file}"
exit 0