From a573f2b7adb4efdb25d486fcdf97435eb995013d Mon Sep 17 00:00:00 2001 From: Marina Zheng Date: Fri, 30 Sep 2022 12:08:49 -0700 Subject: [PATCH] Add dashboard transfer scripts - Add utility scripts to explort dashboards, datasources and preferences from a cclear into local files. - Add utility scripts to import dashboards, datasources and preferences to a cclear from local files. --- utilities/transfer/dashboard_export.sh | 169 +++++++++++++++++++++ utilities/transfer/dashboard_import.sh | 188 ++++++++++++++++++++++++ utilities/transfer/datasource_export.sh | 126 ++++++++++++++++ utilities/transfer/datasource_import.sh | 153 +++++++++++++++++++ utilities/transfer/preference_export.sh | 143 ++++++++++++++++++ utilities/transfer/preference_import.sh | 137 +++++++++++++++++ 6 files changed, 916 insertions(+) create mode 100755 utilities/transfer/dashboard_export.sh create mode 100755 utilities/transfer/dashboard_import.sh create mode 100755 utilities/transfer/datasource_export.sh create mode 100755 utilities/transfer/datasource_import.sh create mode 100755 utilities/transfer/preference_export.sh create mode 100755 utilities/transfer/preference_import.sh diff --git a/utilities/transfer/dashboard_export.sh b/utilities/transfer/dashboard_export.sh new file mode 100755 index 0000000..d48ae5a --- /dev/null +++ b/utilities/transfer/dashboard_export.sh @@ -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 ---------------------------------"; diff --git a/utilities/transfer/dashboard_import.sh b/utilities/transfer/dashboard_import.sh new file mode 100755 index 0000000..c18b03d --- /dev/null +++ b/utilities/transfer/dashboard_import.sh @@ -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----------------------------------------"; diff --git a/utilities/transfer/datasource_export.sh b/utilities/transfer/datasource_export.sh new file mode 100755 index 0000000..36cdfdf --- /dev/null +++ b/utilities/transfer/datasource_export.sh @@ -0,0 +1,126 @@ +#!/bin/bash + +OPTSPEC=":hu:p:t:" + +show_help() { +cat << EOF +Usage: $0 [-u USER] [-p PASSWORD] [-t TARGET_HOST] +Script to export grafana datasources + -u Required. cClear user to login + -p Required. cClear user password to login + -t Required. The IP of the target 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";; + 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 "$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() { + DS_DIR="$PWD/datasources" + + if [ ! -d "${DS_DIR}" ]; then + mkdir "${DS_DIR}" + else + log_title "----------------- A $DS_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/" +datasource_json=$(curl --noproxy '*' -k -b $mycookie "$HOST/graph-engine/api/datasources") +for id in $(echo $datasource_json | jq -r '.[] | .id'); do + counter=$((counter + 1)) + curl --noproxy '*' -f -k -b $mycookie "$HOST/graph-engine/api/datasources/${id}" | jq '' > "$DS_DIR/${id}.json" + log_success "Datasource has been saved\t id=\"${id}\", path=\"${DS_DIR}/${id}.json\"." +done + +zip -r -m datasources.zip datasources +rm mycookie + +log_title "${counter} datasource(s) were saved and zipped in $PWD/datasources.zip"; +log_title "------------------------------ FINISHED ---------------------------------"; diff --git a/utilities/transfer/datasource_import.sh b/utilities/transfer/datasource_import.sh new file mode 100755 index 0000000..bb77951 --- /dev/null +++ b/utilities/transfer/datasource_import.sh @@ -0,0 +1,153 @@ +#!/bin/bash -x + +OPTSPEC=":hu:p:z:t:" + +show_help() { +cat << EOF +Usage: $0 [-u USER] [-p PASSWORD] [-z PATH] [-t TARGET_HOST] +Script to import datasource into Grafana + -u Required. cClear user to login + -p Required. cClear user password to login + -z Required. Full path to the zip file containing JSON exports of the datasource you want to be imported. + -t Required. The IP of the target 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) + DS_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 "$DS_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 $DS_PATH) +echo $ZIP_FILE + +if [[ $ZIP_FILE =~ \.zip$ ]]; then + DS_DIR=$(unzip -qql $DS_PATH | head -n1 | tr -s ' ' | cut -d' ' -f5-) + unzip $DS_PATH + DS_DIR=${DS_DIR: : -1} + if [ -d "$DS_DIR" ]; then + DS_LIST=$(find "$PWD/$DS_DIR" -mindepth 1 -name \*.json) + echo $DS_LIST + if [ -z "$DS_LIST" ]; then + log_title "----------------- $DS_DIR contains no JSON files! -----------------" + log_failure "Directory $DS_DIR does not appear to contain any JSON files for import. Check your path and try again." + exit 1 + else + FILESTOTAL=$(echo "$DS_LIST" | wc -l) + log_title "----------------- Starting import of $FILESTOTAL datasource(s) -----------------" + fi + else + log_title "-------------------- $DS_DIR directory not found! -----------------" + log_failure "Directory $DS_DIR does not exist. Check your path and try again." + exit 1 + fi +else + log_title "-------------------- $ZIP_FILE Wrong format! -----------------" + log_failure "$ZIP_FILE is not a zip file. Please enter a correct file" +fi + +NUMSUCCESS=0 +NUMFAILURE=0 +COUNTER=0 + + +# host url +if [[ ! "$HOST" == "https://"* ]]; then + HOST="https://$HOST" +fi + +mycookie="$PWD/mycookie" +curl --noproxy '*' -k -c mycookie --data "uname=$USER&psw=$PASSWORD" "$HOST/sess/login?rp=/vb/" + +for i in datasources/*; do + RESULT=$(curl --noproxy '*' -k -b $mycookie -X "POST" "$HOST/graph-engine/api/datasources" \ + -H "Content-Type: application/json" --data-binary @$i) + if [[ "$RESULT" == *"Datasource added"* ]]; then + log_success "$RESULT" + NUMSUCCESS=$((NUMSUCCESS + 1)) + else + log_failure "$RESULT" + NUMFAILURE=$((NUMFAILURE + 1)) + fi +done + +rm mycookie + +log_title "Import complete. $NUMSUCCESS datasource(s) successfully imported. $NUMFAILURE datasource(s) imports failed."; +log_title "---------------------------------------FINISHED----------------------------------------"; diff --git a/utilities/transfer/preference_export.sh b/utilities/transfer/preference_export.sh new file mode 100755 index 0000000..5f8fbc2 --- /dev/null +++ b/utilities/transfer/preference_export.sh @@ -0,0 +1,143 @@ +#!/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 + -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";; + \?) + 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} +} + +function init() { + DATE_TIME=$(date '+%d%m%Y_%H%M%S') + DASH_DIR="$PWD/preferences_${HOST}_${DATE_TIME}" + if [ ! -d "${DASH_DIR}" ]; then + mkdir "${DASH_DIR}" + else + log_title "----------------- A $DASH_DIR directory already exists! -----------------" + fi +} + +init + +PREF_ORG="$DASH_DIR/preferences_org.json" +PREF_USER="$DASH_DIR/preferences_user.json" + +# host url +if [[ ! "$HOST" == "https://"* ]]; then + HOST="https://$HOST" +fi + +mycookie="$PWD/mycookie" +curl --noproxy '*' -k -c mycookie --data "uname=$USER&psw=$PASSWORD" "$HOST/sess/login?rp=/vb/" + +# Get preferences +pref_org_json=$(curl --noproxy '*' -k -b "$mycookie" "$HOST/graph-engine/api/org/preferences") +pref_user_json=$(curl --noproxy '*' -k -b "$mycookie" "$HOST/graph-engine/api/user/preferences") + +rm mycookie + +# log result +if [ -z "$pref_org_json" ] || [ -z "$pref_user_json" ]; then + log_failure "Failed to download preferences. Please check parameters passed in. " + show_help + exit 1 +elif [[ "$pref_org_json" == *"Unauthorized"* ]] || [[ "$pref_user_json" == *"Unauthorized"* ]]; then + log_failure "Failed to login: $pref_org_json; $pref_user_json" + exit 1 +fi + +# Save preferences +echo $pref_org_json | jq '.' > "$PREF_ORG" +log_success "Org. preferences saved: $pref_org_json" +echo $pref_user_json | jq '.' > "$PREF_USER" +log_success "User preferences saved: $pref_user_json" + +log_title "Preferences were saved in $DASH_DIR"; +log_title "------------------------------ FINISHED ---------------------------------"; diff --git a/utilities/transfer/preference_import.sh b/utilities/transfer/preference_import.sh new file mode 100755 index 0000000..61b79bd --- /dev/null +++ b/utilities/transfer/preference_import.sh @@ -0,0 +1,137 @@ +#!/bin/bash +# +# todo: ip to both ip and https; from folder to be both zipped or unzipped +# +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. Grafana preferences json file to import from. e.g. preferences.json + -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) + PREF_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 "$PREF_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} +} + +PREF_FILE=$(basename $PREF_PATH) + +if [[ ! $PREF_FILE =~ \.json$ ]]; then + log_title "-------------------- $PREF_FILE Wrong format! -----------------" + log_failure "$PREF_PATH is not a json file. Please enter a correct file" + exit 1 +fi + +if [[ ! -f "$PREF_FILE" ]]; then + log_failure "No such file: $PREF_FILE." + exit 1 +fi + +NUMSUCCESS=0 +NUMFAILURE=0 +COUNTER=0 + + +# host url +if [[ ! "$HOST" == "https://"* ]]; then + HOST="https://$HOST" +fi + +mycookie="$PWD/mycookie" +curl --noproxy '*' -k -c mycookie --data "uname=$USER&psw=$PASSWORD" $HOST/sess/login?rp=/vb/ + +RESULT=$(cat "$PREF_FILE" | jq '.' | curl --noproxy '*' -k -b $mycookie -X PUT -H \ +"Content-Type: application/json" "$HOST/graph-engine/api/user/preferences" -d @-) + +rm mycookie + +# log result +if [[ "$RESULT" == *"updated"* ]]; then + log_success "$RESULT" +else + log_failure "$RESULT" +fi + +log_title "-------------------- Preferences were successfully imported.-------------------------"