curl 通过API导出Grafana Jmeter 板

ubby3x7f  于 2023-05-23  发布在  其他
关注(0)|答案(2)|浏览(194)

关于Grafana API的问题
我需要导出所有使用GUI制作的 Jmeter 板的JSON模型,以便将它们导入另一个Grafana示例。我尝试使用 Jmeter 板API - api/dashboards/ using curl和 Jmeter 板uuid或uri(db/),但由于某种原因,我总是得到消息 not found
我找到的uids和uri

$URL/api/search?query=&

然后我试着把模型或任何数据

curl -k -H “Authorization: Bearer $KEY” $URL/api/dashboards/db/$dash_name

curl -k -H “Authorization: Bearer $KEY” $URL/api/dashboards/uid/$uid

结果是一样的。
有人知道为什么吗?我在其他地方找不到任何信息。
先谢谢你了。

zdwk9cvp

zdwk9cvp1#

溶液取自:https://gist.github.com/crisidev/bd52bdcc7f029be2f295#gistcomment-3975489

#!/bin/bash

HOST='http://localhost:3000'
KEY="<add-valid-key>"
DIR="grafana_dashboards"

# Iterate through dashboards using the current API Key
for dashboard_uid in $(curl -sS -H "Authorization: Bearer $KEY" $HOST/api/search\?query\=\& | jq -r '.[] | select( .type | contains("dash-db")) | .uid'); do
    url=$(echo $HOST/api/dashboards/uid/$dashboard_uid | tr -d '\r')
    dashboard_json=$(curl -sS -H "Authorization: Bearer $KEY" $url)
    dashboard_title=$(echo $dashboard_json | jq -r '.dashboard | .title' | sed -r 's/[ \/]+/_/g')
    dashboard_version=$(echo $dashboard_json | jq -r '.dashboard | .version')
    folder_title="$(echo $dashboard_json | jq -r '.meta | .folderTitle')"

    echo "Creating: ${DIR}/${folder_title}/${dashboard_title}_v${dashboard_version}.json"
    mkdir -p "${DIR}/${folder_title}"
    echo ${dashboard_json} | jq -r {meta:.meta}+.dashboard > "${DIR}/${folder_title}/${dashboard_title}_v${dashboard_version}.json"
done
brjng4g3

brjng4g32#

Grafana v9.1.7中的工作解决方案

#!/bin/bash

HOST='http://<YOUR_GRAFANA_URL>:3000'
DASH_DIR=./

# Declare a list with the api keys using as a prefix the organization name plus "_" character
declare -a StringArray=("<YOUR_ORG_NAME>_<API_KEY>")

# Iterate through api keys:
for API_KEY in "${StringArray[@]}"; do
    ORG=$(echo $API_KEY | cut -d "_" -f1) # Name of the organization based on the prefix
    KEY=$(echo $API_KEY | cut -d "_" -f2) # API Key for that organization after removing the prefix

    # Iterate through dashboards using the current API Key
    for dashboard_uid in $(curl -sS -H "Authorization: Bearer $KEY" $HOST/api/search\?query\=\& | jq -r '.[] | select( .type | contains("dash-db")) | .uid'); do
        url=`echo $HOST/api/dashboards/uid/$dashboard_uid | tr -d '\r'`
        dashboard_json=$(curl -sS -H "Authorization: Bearer $KEY" $url)
        dashboard_title=$(echo $dashboard_json | jq -r '.dashboard | .title' | sed -r 's/[ \/]+/_/g' )
        dashboard_version=$(echo $dashboard_json | jq -r '.dashboard | .version')
        folder_title="$(echo $dashboard_json | jq -r '.meta | .folderTitle')"

        # You can export the files like this to keep them organized by organization:
        mkdir -p "$DASH_DIR/$ORG/$folder_title/dashboards_$ORG"
        echo $dashboard_json | jq -r {meta:.meta}+.dashboard  > $DASH_DIR/$ORG/$folder_title/dashboards_$ORG/${dashboard_title}_v${dashboard_version}.json
    done
done

相关问题