grafana儀表盤導(dǎo)入導(dǎo)出腳本

grafana儀表盤的導(dǎo)出是前端應(yīng)用處理,我們想要導(dǎo)出grafana中的所有的儀表盤的時候可以通過調(diào)用grafna的API接口來完成導(dǎo)入導(dǎo)出的操作。這里提供一個導(dǎo)入導(dǎo)出功能集合的腳本用于完成這項任務(wù):

腳本執(zhí)行的環(huán)境要求:
1、有curl工具;
2、有jq工具,一般centos默認(rèn)不會安裝需要自行百度安裝(當(dāng)然也可以不用jq工具,直接用shell腳本來處理json內(nèi)容,但是比較麻煩);

dashboard_export_import.sh腳本使用:

使用的時候替換腳本中開頭的相關(guān)變量:

SOURCE_GRAFANA_ENDPOINT="源grafana的地址,如:http://127.0.0.1:3000"

DEST_GRAFANA_ENDPOINT="目標(biāo)grafana的地址,如:http://127.0.0.1:3100" 

SOURCE_GRAFANA_API_KEY='訪問源grafana的api密鑰'

DEST_GRAFANA_API_KEY='訪問目標(biāo)grafana的api密鑰'

若grafana都是通過匿名訪問無效密鑰,那么無需填寫,腳本中刪除引用密鑰的變量即可

導(dǎo)出儀表盤到 "DASH_DIR" 變量指定的目錄下,執(zhí)行如下方式導(dǎo)出:

$ sh dashboard_export_import.sh -e

導(dǎo)入儀表盤到目標(biāo)grafana中,執(zhí)行如下方式導(dǎo)入:

$ sh dashboard_export_import.sh -i

腳本內(nèi)容如下:

#!/bin/sh

# env var info
SCRIPT_PATH=$(cd $(dirname $0) && pwd -P)
DASH_DIR=${SCRIPT_PATH}/backup_dashboard
DASH_DB_FILE=${DASH_DIR}/das_db.json

SOURCE_GRAFANA_ENDPOINT="source_grafana_endpoint_here"
DEST_GRAFANA_ENDPOINT='dest_grafana_endpoint_here'

SOURCE_GRAFANA_API_KEY='source_grafana_api_key_here'
DEST_GRAFANA_API_KEY='dest_grafana_api_key_here'



log() {
    local type="$1"; shift
    # accept argument string or stdin
    local text="$*"; if [ "$#" -eq 0 ]; then text="$(cat)"; fi
    local dt; dt="$(date "+%Y-%m-%d %H:%M:%S")"
    printf '%s [%s] [upgrade.sh]: %s\n' "$dt" "$type" "$text"
}
info() {
    log INFO "$@"
}
warn() {
    log WARN "$@" >&2
}
error() {
    log ERROR "$@" >&2
    exit 1
}


if ! command -v curl &> /dev/null
then
    error "Please install curl before running this script"
fi

if ! command -v jq &> /dev/null
then
    error "Please install jq before running this script"
fi

function usage() {
    echo "Usage: ${0} -e -i" >&2
}

if [[ $# -eq 0 ]]; then
    usage
    exit 1
fi

while getopts "ei" opt; do
    case "${opt}" in
        e)
            export_dash="true"
            ;;
        i)
            import_dash="true"
            ;;
        *)
            usage
            exit 1
            ;;
    esac
done


if [[ "${export_dash}" == "true" ]]; then
    if [ ! -d "${DASH_DIR}" ]; then
        mkdir -p ${DASH_DIR}
    fi

    info "Downloading dash-db.........................."
    curl -sS -XGET \
         -H "Content-Type: application/json" \
         -H "Authorization: Bearer ${SOURCE_GRAFANA_API_KEY}" ${SOURCE_GRAFANA_ENDPOINT}/api/search?type="dash-db" > ${DASH_DB_FILE}
    [ $? -ne 0 ] && error "Failed: curl ${SOURCE_GRAFANA_ENDPOINT}/api/search?type="dash-db" > ${DASH_DB_FILE}"
    
    for uid in $(jq -r ".[].uid" ${DASH_DB_FILE}); do
      info "Downloading dashboard: ${uid}"
      curl -sS -XGET \
           -H "Authorization: Bearer ${SOURCE_GRAFANA_API_KEY}" ${SOURCE_GRAFANA_ENDPOINT}/api/dashboards/uid/${uid} > ${DASH_DIR}/uid.${uid}.json
      title=$(jq .dashboard.title ${DASH_DIR}/uid.${uid}.json | tr -d '"')
      cat ${DASH_DIR}/uid.${uid}.json|jq '.dashboard | del(.id)' > ${DASH_DIR}/dashboards.${title}.json
      info "Downloaded successed: ${title}-${uid}"
    done
fi

if [[ "${import_dash}" == "true" ]]; then
    if [ ! -d "${DASH_DIR}" ]; then
        error "${DASH_DIR} doesn't exist"
    fi

    if ! compgen -G "${DASH_DIR}/dashboards.*.json" > /dev/null; then
        error "No dashboards.*.json files in ${DASH_DIR}"
    fi

    for dashboard in $(ls ${DASH_DIR}/dashboards.*.json); do
        info "Uploading ${dashboard} to Grafana"
        dash_tmp=$(cat ${dashboard})
        dashJson='{"dashboard":'"${dash_tmp}}"

        curl -sS -XPOST -H "Content-Type: application/json" \
             -H "Authorization: Bearer ${DEST_GRAFANA_API_KEY}" \
             -d "${dashJson}" "${GRAFANA_ENDPOINT}/api/dashboards/db"
    
        echo -e "\n"
    done
fi

更多相關(guān)工具內(nèi)容請看github倉庫:

https://github.com/HugoWw/ScriptHub,ps:喜歡的麻煩給該倉庫start下,感謝,后續(xù)會持續(xù)更新好用的搬磚工具上去!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容