Skip to content

Commit 5bc0ada

Browse files
authored
Update grafana-import.sh script and doco for latest dashboards (#765)
1 parent 2c644ea commit 5bc0ada

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

docs/metrics/030_importing.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ There are two ways to obtain the dashboards:
2222
2323
[source,bash]
2424
----
25-
curl https://oracle.github.io/coherence-operator/dashboards/latest/coherence-dashboards.tar.gz \
26-
-o coherence-dashboards.tar.gz
25+
curl https://github.com/oracle/coherence-operator/releases/download/v3.5.2/coherence-dashboards.tar.gz \
26+
-Lo coherence-dashboards.tar.gz
2727
tar -zxvf coherence-dashboards.tar.gz
2828
----
2929

hack/grafana/grafana-import.sh

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#set -x
1414
#set +x
15-
OPTSPEC=":hd:t:u:w:"
15+
OPTSPEC=":hd:t:u:w:I"
1616

1717
show_help() {
1818
cat << EOF
@@ -22,11 +22,14 @@ Script to import dashboards into Grafana
2222
-w Required. Grafana Password
2323
-d Required. Root path containing JSON dashboard files you want imported.
2424
-t Required. The full URL of the target host
25+
-I If set will default to http instead of https
2526
2627
-h Display this help and exit.
2728
EOF
2829
}
2930

31+
PROTOCOL="https"
32+
3033
###### Check script invocation options ######
3134
while getopts "$OPTSPEC" optchar; do
3235
case "$optchar" in
@@ -42,6 +45,8 @@ while getopts "$OPTSPEC" optchar; do
4245
GRAFANA_USER="$OPTARG";;
4346
w)
4447
GRAFANA_PASSWORD="$OPTARG";;
48+
I)
49+
PROTOCOL="http";;
4550
\?)
4651
echo "Invalid option: -$OPTARG" >&2
4752
exit 1
@@ -110,11 +115,13 @@ function log_title() {
110115
### API KEY GENERATION
111116

112117
KEYNAME=$(head /dev/urandom | LC_ALL=C tr -dc A-Za-z0-9 | head -c 13 | cut -c -7)
113-
KEYLENGTH=70
114-
GENERATE_POST_DATA="{\"name\": \"${KEYNAME}\", \"role\": \"Admin\", \"secondsToLive\": 3600 }"
118+
KEYLENGTH=30
119+
CREATE_SERVICE_ACCOUNT_DATA="{\"name\": \"${KEYNAME}\", \"role\": \"Admin\", \"isDisabled\": false }"
120+
CREATE_TOKEN_DATA="{\"name\": \"${KEYNAME}\", \"secondsToLive\": 3600 }"
115121

116122
if [ -n "$GRAFANA_USER" ] || [ -n "$GRAFANA_PASSWORD" ] || [ -n "$TARGET_HOST" ]; then
117-
KEY=$(curl -X POST -H "Content-Type: application/json" -d "${GENERATE_POST_DATA}" http://${GRAFANA_USER}:${GRAFANA_PASSWORD}@${TARGET_HOST}/api/auth/keys | jq -r '.key')
123+
ID=$(curl -X POST -H "Content-Type: application/json" -d "${CREATE_SERVICE_ACCOUNT_DATA}" -k $PROTOCOL://${GRAFANA_USER}:${GRAFANA_PASSWORD}@${TARGET_HOST}/api/serviceaccounts | jq -r '.id')
124+
KEY=$(curl -X POST -H "Content-Type: application/json" -d "${CREATE_TOKEN_DATA}" -k $PROTOCOL://${GRAFANA_USER}:${GRAFANA_PASSWORD}@${TARGET_HOST}/api/serviceaccounts/${ID}/tokens | jq -r '.key')
118125
if [ ${#KEY} -ge $KEYLENGTH ]; then
119126
log_title "---- API Key Generated successfully, correct character number generated in API Key, we're going into the next step -----"
120127
else
@@ -149,15 +156,20 @@ NUMSUCCESS=0
149156
NUMFAILURE=0
150157
COUNTER=0
151158

159+
# create Coherence folder
160+
CREATE_FOLDER_DATA="{\"title\": \"Coherence\"}"
161+
FOLDER=$(curl -X POST -H "Content-Type: application/json" -d "${CREATE_FOLDER_DATA}" -k $PROTOCOL://${GRAFANA_USER}:${GRAFANA_PASSWORD}@${TARGET_HOST}/api/folders | jq -r '.uid')
162+
echo "Created Coherence folder with UID ${FOLDER}"
163+
152164
for DASH_FILE in $DASH_LIST; do
153165
COUNTER=$((COUNTER + 1))
154166
echo "Import $COUNTER/$FILESTOTAL: $DASH_FILE..."
155-
echo '{ "overwrite": true, "dashboard":' > tmp.json
167+
echo "{\"folderUid\": \"$FOLDER\", \"overwrite\": true, \"dashboard\":" > tmp.json
156168
cat $DASH_FILE >> tmp.json
157169
echo '}' >> tmp.json
158-
RESULT=$(cat tmp.json | jq '.dashboard.id = null' | curl -s -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $KEY" "http://$TARGET_HOST/api/dashboards/import" -d @-)
170+
RESULT=$(cat tmp.json | jq '.dashboard.id = null' | curl -s -k -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $KEY" "$PROTOCOL://$TARGET_HOST/api/dashboards/db" -d @- | jq -r '.status')
159171
rm tmp.json
160-
if [[ "$RESULT" == *"\"imported\":true"* ]]; then
172+
if [[ "$RESULT" == "success" ]]; then
161173
log_success "$RESULT"
162174
NUMSUCCESS=$((NUMSUCCESS + 1))
163175
else
@@ -168,3 +180,4 @@ done
168180

169181
log_title "------------ Import complete. $NUMSUCCESS dashboards were successfully imported. $NUMFAILURE dashboard imports failed.------------";
170182
log_title "-------------------------------------------------------- FINISHED ------------------------------------------------------";
183+

0 commit comments

Comments
 (0)