Skip to content
Open
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
169 changes: 169 additions & 0 deletions utilities/transfer/dashboard_export.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
#!/bin/bash

OPTSPEC=":hu:p:t:f:"

show_help() {
cat << EOF
Usage: $0 [-u USER] [-p PASSWORD] [-f FROM_FOLDER] [-t TARGET_HOST]
Script to export grafana dashboards
-u Required. cClear user to login
-p Required. cClear user password to login
-t Required. The IP of the source cClear host i.e 10.51.10.32
-f Optional. The name of the folder to export from, double quotes with spaces. Export all folders if not
specified.
-h Display this help and exit.
EOF
}

###### Check script invocation options ######
while getopts "$OPTSPEC" optchar; do
case "$optchar" in
h)
show_help
exit
;;
u)
USER="$OPTARG";;
p)
PASSWORD="$OPTARG";;
t)
HOST="$OPTARG";;
f)
FROM="$OPTARG";;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done

if [ -z "$USER" ] || [ -z "$PASSWORD" ] || [ -z "$HOST" ]; then
show_help
exit 1
fi

# set some colors for status OK, FAIL and titles
SETCOLOR_SUCCESS="echo -en \\033[0;32m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_NORMAL="echo -en \\033[0;39m"
SETCOLOR_TITLE_PURPLE="echo -en \\033[0;35m" # purple

# usage log "string to log" "color option"
function log_success() {
if [ $# -lt 1 ]; then
${SETCOLOR_FAILURE}
echo "Not enough arguments for log function! Expecting 1 argument got $#"
exit 1
fi

timestamp=$(date "+%Y-%m-%d %H:%M:%S %Z")

${SETCOLOR_SUCCESS}
printf "[${timestamp}] $1\n"
${SETCOLOR_NORMAL}
}

function log_failure() {
if [ $# -lt 1 ]; then
${SETCOLOR_FAILURE}
echo "Not enough arguments for log function! Expecting 1 argument got $#"
exit 1
fi

timestamp=$(date "+%Y-%m-%d %H:%M:%S %Z")

${SETCOLOR_FAILURE}
printf "[${timestamp}] $1\n"
${SETCOLOR_NORMAL}
}

function log_title() {
if [ $# -lt 1 ]; then
${SETCOLOR_FAILURE}
log_failure "Not enough arguments for log function! Expecting 1 argument got $#"
exit 1
fi

${SETCOLOR_TITLE_PURPLE}
printf "|-------------------------------------------------------------------------|\n"
printf "|$1|\n";
printf "|-------------------------------------------------------------------------|\n"
${SETCOLOR_NORMAL}
}

mycookie="$PWD/mycookie"
counter=0

function init() {
DATE_TIME=$(date '+%d%m%Y_%H%M%S')
DASH_DIR="$PWD/dashboards_${HOST}_${DATE_TIME}"
if [ ! -d "${DASH_DIR}" ]; then
mkdir "${DASH_DIR}"
else
log_title "----------------- A $DASH_DIR directory already exists! -----------------"
fi
}

init

# host url
if [[ ! "$HOST" == "https://"* ]]; then
HOST="https://$HOST"
fi

curl --noproxy '*' -k -c mycookie --data "uname=$USER&psw=$PASSWORD" "$HOST/sess/login?rp=/vb/"

folder_json=$(curl --noproxy '*' -k -b "$mycookie" "$HOST/graph-engine/api/folders")
# From folder specified:
if [ ${#FROM} -gt 0 ]; then
# Find matching folder from remote (with folder title)
FOLDER_UID=$(echo $folder_json | jq -r '.[] | select(.title == "'"$FROM"'") | .uid')
# Folder not found, prompt error and get out
if [ -z "$FOLDER_UID" ] ; then
log_failure "Folder $FROM is not found. Please check spelling and double quote with any spaces."
exit 1
fi
# Folder found: get the collection of dashboard uids in this folder
dashboard_uids=$(curl --noproxy '*' -k -b "$mycookie" "$HOST"/graph-engine/api/search\?query\=\& | \
jq -r '.[] | select(.type | contains("dash-db")) | select(.folderUid != null) | select(.folderUid == "'"$FOLDER_UID"'") | .uid')
# From all folders:
else
dashboard_uids=$(curl --noproxy '*' -k -b "$mycookie" "$HOST"/graph-engine/api/search\?query\=\& | \
jq -r '.[] | select(.type | contains("dash-db")) | .uid')
fi

# Export dashboards
for dashboard_uid in $dashboard_uids; do
url=$(echo "$HOST/graph-engine/api/dashboards/uid/$dashboard_uid" | tr -d '\r')
dashboard_json=$(curl --noproxy '*' -k -b "$mycookie" "$url")
dashboard_title=$(echo "$dashboard_json" | jq -r '.dashboard | .title' | sed -r 's/[ \/]+/_/g' )
dashboard_version=$(echo "$dashboard_json" | jq -r '.dashboard | .version')
dashboard_folder_raw=$(echo "$dashboard_json" | jq -r '.meta | .folderTitle')
dashboard_folder=$(echo "$dashboard_json" | jq -r '.meta | .folderTitle' | sed -r 's/[ \/]+/_/g' )
dashboard_folderId=$(echo "$dashboard_json" | jq -r '.meta | .folderId')

# Find folder uid to save (so that importing can find the right folder to import to)
folder_uid=$(echo "$folder_json" | jq -r '.[] | select(.id=='$dashboard_folderId') | .uid ')

# create the folder if not existing
if [ ! -d "${DASH_DIR}/${dashboard_folder}" ]; then
mkdir "${DASH_DIR}/${dashboard_folder}"
fi

counter=$((counter + 1))
# save dashboard with meta, dashboard and folder uid.
echo "$dashboard_json" | jq '.dashboard | . += {"folderUid":"'$folder_uid'", "folderTitle": "'"$dashboard_folder_raw"'"}' > \
"$DASH_DIR/${dashboard_folder}/${dashboard_title}_v${dashboard_version}.json"
log_success "Dashboard has been saved\t\t title=\"${dashboard_title}\", uid=\"${dashboard_uid}\",
path=\"${DASH_DIR}/${dashboard_folder}/${dashboard_title}_v${dashboard_version}.json\"."
done

# zip -r -m ${DASH_DIR}.zip ${DASH_DIR}
rm mycookie

log_title "${counter} dashboards were saved in ${DASH_DIR}";
log_title "------------------------------ FINISHED ---------------------------------";
188 changes: 188 additions & 0 deletions utilities/transfer/dashboard_import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
#!/bin/bash
# todo: Import from dashboard files without .folderTitle...from git repo or manual exported
OPTSPEC=":hu:p:z:t:"

show_help() {
cat << EOF
Usage: $0 [-u USER] [-p PASSWORD] [-z PATH] [-t TARGET_HOST]
Script to import dashboards into Grafana
-u Required. cClear user to login
-p Required. cClear user password to login
-z Required. Full path to the folder or zip file containing JSON exports of the dashboards
you want to be imported.
-t Required. The IP of the destination cClear host i.e 10.51.10.32
-h Display this help and exit.
EOF
}

###### Check script invocation options ######
while getopts "$OPTSPEC" optchar; do
case "$optchar" in
h)
show_help
exit
;;
u)
USER="$OPTARG";;
p)
PASSWORD="$OPTARG";;
z)
DASH_PATH="$OPTARG";;
t)
HOST="$OPTARG";;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done

if [ -z "$USER" ] || [ -z "$PASSWORD" ] || [ -z "$DASH_PATH" ] || [ -z "$HOST" ]; then
show_help
exit 1
fi

# set some colors for status OK, FAIL and titles
SETCOLOR_SUCCESS="echo -en \\033[0;32m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_NORMAL="echo -en \\033[0;39m"
SETCOLOR_TITLE_PURPLE="echo -en \\033[0;35m" # purple

# usage log "string to log" "color option"
function log_success() {
if [ $# -lt 1 ]; then
${SETCOLOR_FAILURE}
echo "Not enough arguments for log function! Expecting 1 argument got $#"
exit 1
fi

timestamp=$(date "+%Y-%m-%d %H:%M:%S %Z")

${SETCOLOR_SUCCESS}
printf "[%s] $1\n" "$timestamp"
${SETCOLOR_NORMAL}
}

function log_failure() {
if [ $# -lt 1 ]; then
${SETCOLOR_FAILURE}
echo "Not enough arguments for log function! Expecting 1 argument got $#"
exit 1
fi

timestamp=$(date "+%Y-%m-%d %H:%M:%S %Z")

${SETCOLOR_FAILURE}
printf "[%s] $1\n" "$timestamp"
${SETCOLOR_NORMAL}
}

function log_title() {
if [ $# -lt 1 ]; then
${SETCOLOR_FAILURE}
log_failure "Not enough arguments for log function! Expecting 1 argument got $#"
exit 1
fi

${SETCOLOR_TITLE_PURPLE}
printf "|---------------------------------------------------------------------------------------|\n"
printf "| %s |\n" "$1";
printf "|---------------------------------------------------------------------------------------|\n"
${SETCOLOR_NORMAL}
}


ZIP_FILE=$(basename $DASH_PATH)

if [[ $ZIP_FILE =~ \.zip$ ]]; then
DASH_DIR=$(unzip -qql $DASH_PATH | head -n1 | tr -s ' ' | cut -d' ' -f5-)
unzip $DASH_PATH
DIR_LENGTH=${#DASH_DIR}
DASH_DIR=${DASH_DIR:0:DIR_LENGTH-1}
else
DASH_DIR=$DASH_PATH
fi

if [ -d "$DASH_DIR" ]; then
DASH_LIST=$(find "$PWD/$DASH_DIR" -mindepth 1 -name \*.json)

if [ -z "$DASH_LIST" ]; then
log_title "----------------- $DASH_DIR contains no JSON files! -----------------"
log_failure "Directory $DASH_DIR does not appear to contain any JSON files for import. Check your path and try again."
exit 1
else
FILESTOTAL=$(echo "$DASH_LIST" | wc -l)
log_title "----------------- Starting import of $FILESTOTAL dashboards -----------------"
fi
else
log_title "-------------------- $DASH_DIR directory not found! -----------------"
log_failure "Directory $DASH_DIR does not exist. Check your path and try again."
exit 1
fi

NUMSUCCESS=0
NUMFAILURE=0
COUNTER=0


# host url
if [[ ! "$HOST" == "http"* ]]; then
HOST="https://$HOST"
fi

mycookie="$PWD/mycookie"
curl --noproxy '*' -k -c mycookie --data "uname=$USER&psw=$PASSWORD" $HOST/sess/login?rp=/vb/

for DASH_FILE in $DASH_LIST; do
COUNTER=$((COUNTER + 1))
echo "Import $COUNTER/$FILESTOTAL: $DASH_FILE..."

# Get folder uid and title from dashboard
dashboard=$(cat "$DASH_FILE")
folder_title=$(echo "$dashboard" | jq -r '.folderTitle')
folder_uid=$(echo "$dashboard" | jq -r '.folderUid')
dashboard=$(echo "$dashboard" | jq -r 'del(.folderTitle) | del(.folderUid)')
# shellcheck disable=SC2116
dashboard=$(echo '{"dashboard": ' "${dashboard}"'}')

# echo "$dashboard"
# If folder uid id not provided, import to the "General" folder
if [ ${#folder_uid} -eq 0 ]; then
RESULT=$(echo "$dashboard" | jq -r '. * {overwrite: true, dashboard: {id: null}}' | curl -k -b $mycookie -X POST
-H \
"Content-Type: application/json" $HOST/graph-engine/api/dashboards/db -d @-)
else
# Find the folder id from $HOST with this folder uid
folder_id=$(curl --noproxy '*' -k -b "$mycookie" "$HOST"/graph-engine/api/folders/$folder_uid | jq -r '.id')
# If not found, create a folder with this folder uid and folder title.
if [ "$folder_id" == "null" ]; then
folder_new=$(echo '{"uid": "'$folder_uid'", "title": "'"$folder_title"'"}' | curl --noproxy '*' -k -b \
$mycookie -X POST -H "Content-Type: application/json" $HOST/graph-engine/api/folders -d @-)
folder_id=$(echo $folder_new | jq -r '.id')
fi

# Import dashboard with folder id, uid, and title
RESULT=$(echo "$dashboard" | jq -r '. * {overwrite: true, dashboard: {id: null}} | . += {folderId:'$folder_id',
folderUid:
"'$folder_uid'", folderTitle: "'"$folder_title"'"}' \
| curl --noproxy '*' -k -b $mycookie -X POST -H "Content-Type: application/json" $HOST/graph-engine/api/dashboards/db -d @-)
fi

# log result
if [[ "$RESULT" == *"success"* ]]; then
log_success "$RESULT"
NUMSUCCESS=$((NUMSUCCESS + 1))
else
log_failure "$RESULT"
NUMFAILURE=$((NUMFAILURE + 1))
fi
done

rm mycookie

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