美文网首页
grafana仪表盘导入导出脚本

grafana仪表盘导入导出脚本

作者: Feel_狗焕 | 来源:发表于2023-02-25 15:08 被阅读0次

grafana仪表盘的导出是前端应用处理,我们想要导出grafana中的所有的仪表盘的时候可以通过调用grafna的API接口来完成导入导出的操作。这里提供一个导入导出功能集合的脚本用于完成这项任务:

脚本执行的环境要求:
1、有curl工具;
2、有jq工具,一般centos默认不会安装需要自行百度安装(当然也可以不用jq工具,直接用shell脚本来处理json内容,但是比较麻烦);

dashboard_export_import.sh脚本使用:

使用的时候替换脚本中开头的相关变量:

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

DEST_GRAFANA_ENDPOINT="目标grafana的地址,如:http://127.0.0.1:3100" 

SOURCE_GRAFANA_API_KEY='访问源grafana的api密钥'

DEST_GRAFANA_API_KEY='访问目标grafana的api密钥'

若grafana都是通过匿名访问无效密钥,那么无需填写,脚本中删除引用密钥的变量即可

导出仪表盘到 "DASH_DIR" 变量指定的目录下,执行如下方式导出:

$ sh dashboard_export_import.sh -e

导入仪表盘到目标grafana中,执行如下方式导入:

$ sh dashboard_export_import.sh -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

更多相关工具内容请看github仓库:

https://github.com/HugoWw/ScriptHub,ps:喜欢的麻烦给该仓库start下,感谢,后续会持续更新好用的搬砖工具上去!

相关文章

网友评论

      本文标题:grafana仪表盘导入导出脚本

      本文链接:https://www.haomeiwen.com/subject/qfifldtx.html