Skip to content

Update grafana-import.sh script and doco for latest dashboards #765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/metrics/030_importing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ There are two ways to obtain the dashboards:

[source,bash]
----
curl https://oracle.github.io/coherence-operator/dashboards/latest/coherence-dashboards.tar.gz \
-o coherence-dashboards.tar.gz
curl https://github.com/oracle/coherence-operator/releases/download/v3.5.2/coherence-dashboards.tar.gz \
-Lo coherence-dashboards.tar.gz
tar -zxvf coherence-dashboards.tar.gz
----

Expand Down
27 changes: 20 additions & 7 deletions hack/grafana/grafana-import.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#set -x
#set +x
OPTSPEC=":hd:t:u:w:"
OPTSPEC=":hd:t:u:w:I"

show_help() {
cat << EOF
Expand All @@ -22,11 +22,14 @@ Script to import dashboards into Grafana
-w Required. Grafana Password
-d Required. Root path containing JSON dashboard files you want imported.
-t Required. The full URL of the target host
-I If set will default to http instead of https

-h Display this help and exit.
EOF
}

PROTOCOL="https"

###### Check script invocation options ######
while getopts "$OPTSPEC" optchar; do
case "$optchar" in
Expand All @@ -42,6 +45,8 @@ while getopts "$OPTSPEC" optchar; do
GRAFANA_USER="$OPTARG";;
w)
GRAFANA_PASSWORD="$OPTARG";;
I)
PROTOCOL="http";;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
Expand Down Expand Up @@ -110,11 +115,13 @@ function log_title() {
### API KEY GENERATION

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

if [ -n "$GRAFANA_USER" ] || [ -n "$GRAFANA_PASSWORD" ] || [ -n "$TARGET_HOST" ]; then
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')
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')
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')
if [ ${#KEY} -ge $KEYLENGTH ]; then
log_title "---- API Key Generated successfully, correct character number generated in API Key, we're going into the next step -----"
else
Expand Down Expand Up @@ -149,15 +156,20 @@ NUMSUCCESS=0
NUMFAILURE=0
COUNTER=0

# create Coherence folder
CREATE_FOLDER_DATA="{\"title\": \"Coherence\"}"
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')
echo "Created Coherence folder with UID ${FOLDER}"

for DASH_FILE in $DASH_LIST; do
COUNTER=$((COUNTER + 1))
echo "Import $COUNTER/$FILESTOTAL: $DASH_FILE..."
echo '{ "overwrite": true, "dashboard":' > tmp.json
echo "{\"folderUid\": \"$FOLDER\", \"overwrite\": true, \"dashboard\":" > tmp.json
cat $DASH_FILE >> tmp.json
echo '}' >> tmp.json
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 @-)
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')
rm tmp.json
if [[ "$RESULT" == *"\"imported\":true"* ]]; then
if [[ "$RESULT" == "success" ]]; then
log_success "$RESULT"
NUMSUCCESS=$((NUMSUCCESS + 1))
else
Expand All @@ -168,3 +180,4 @@ done

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