文章目录
序言
首先,明确目的:我想通过调用API来修改dashboard的报警阈值。因为grafana的报警(alerts)模块无法使用变量,而实际报警阈值应该随着数据的更新改变,这就有点落后了。
Create API tokens
- 通过基本任何(若打开),返回组织
curl http://admin:admin@localhost:3000/api/org #将admin:admin是grafana登录的用户名,密码
#结果如下:
{
"id":1,"name":"Main Org.","address":{
"address1":"","address2":"","city":"","zipCode":"","state":"","country":""}}
- 创建一个名字为apitoken的token
curl -X POST -H "Content-Type: application/json" -d '{"name":"apitoken", "role": "Admin"}' http://admin:admin@localhost:3000/api/auth/keys
#结果如下
{
"id":5,"name":"apitoken","key":"xyzabcoieDlVuiMxblY3Y2VoTEh2RTBkMXpiMDFvdGlOdXJmZUEiLctuIjoiabBpdabcdW4iLCJpZCI6MX0="}
#记得保存此token,用于认证
生成token后,登录grafana界面|Configuration|API keys,就可以看到(图二)。当然可以从grafana界面直接增加,但要记得设置合适有效期限(Expires)。用curl命令请求生成的token默认无过期日期。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-swt6NqZq-1664681092337)(C:\Users\paulwang\AppData\Roaming\Typora\typora-user-images\image-20220518140126020.png)]
Folder HTTP API
按照一定的标准把dashboard分类,放到不同的folder中。默认处于General中。
因此,可从folder开始找寻目标dashboard。
- 查看自定义的folder
url -X GET --insecure -H "Authorization: Bearer xyzbxxxieDlVuiMxblY3Y2VoTEh2RTBkMXpiMDFvdGlOdXJmZUEiLctuIjoiabBpdabcdW4iLCJpZCI6MX0=" -H "Content-Type: application/json" http://localhost:3000/api/folders
响应结果:
[
{
"id":1, #文件夹id
"uid": "nErXDvCkzz", #文件夹uid
"title": "Department ABC" #文件夹名字
},
{
"id":2,
"uid": "k3S1cklGk",
"title": "Department RND"
}
]
-
通过
folderIds
获取某个文件夹下的所有dashboardfolderIds
的id从上面的请求可以获取。
curl -X GET --insecure -H "Authorization: Bearer xyzbcjoieDlVuiMxblYxxxTEh2RTBkMXpiMDFvdGlOdXJmZUEiLctuIjoiabBpdabcdW4iLCJpZCI6MX0=" -H "Content-Type: application/json" http://localhost:3000/api/search?folderIds=59
响应格式:
[
{
"id": 58, # dashboard的id
"uid": "tah4yssnk", #dashboard的uid
"title": "xxxxx(exx1min)", #dashboard的名字
"uri": "db/xxx-clxxront-every-1min",
"url": "/d/tah4yssnk/krew-cxxront-every-1min",
"slug": "",
"type": "dash-db",
"tags": [],
"isStarred": false,
"folderId": 59, #文件夹id
"folderUid": "VG6nqgU7z",
"folderTitle": "xxxx",
"folderUrl": "/dashboards/f/VG6nqgU7z/xxx",
"sortMeta": 0
},
{
"id": 55,
"uid": "usOvuoynk",
"title": "xxx(evxxx 1min)",
"uri": "db/xxxxx-every-1min",
"url": "/d/usOvuoynk/krxxxxb-evxxxry-1min",
"slug": "",
"type": "dash-db",
"tags": [],
"isStarred": false,
"folderId": 59,
"folderUid": "VG6nqgU7z",
"folderTitle": "Krew",
"folderUrl": "/dashboards/f/VG6nqgU7z/xxx",
"sortMeta": 0