当前位置: 首页> 科技> 数码 > 【Grafana】通过api请求Grafana Datasource获取数据

【Grafana】通过api请求Grafana Datasource获取数据

时间:2025/7/12 6:09:25来源:https://blog.csdn.net/shen12138/article/details/141604908 浏览次数:0次

可以通过api请求Grafana Datasource获取数据,以此解决一部分想从Grafana获取数据的需求(有毒的需求),按道理说应该通过脚本或程序直接从数据源中获取

实现方式

    Grafana开启了匿名访问后,F12刷新页面,查看面板的实际访问路径、请求头部和请求参数。
    使用python请求,就可以返回Grafana面板看到的值。

Mysql数据源


在这里插入图片描述

在这里插入图片描述

python示例代码

import requests
import json########### MYSQL数据源查询 ###########
# 构造MYSQL请求头
headers = {'Accept': 'application/json','Content-Type': 'application/json',
}# 构造请求体
data = {"from": "1724742305825", "to": "1724742605825", "queries": [{"refId": "A", "intervalMs": 60000, "maxDataPoints": 689, "datasourceId": 6, "rawSql": "select version()","format": "table"}]}# 发送 POST 请求
url = 'http://192.168.113.118:3000/api/ds/query'response = requests.post(url, headers=headers, data=json.dumps(data))# 处理响应
if response.status_code == 200:# 成功获取响应result = response.json()print(json.dumps(result))
else:print(f"请求失败,状态码: {response.status_code}")

返回结果

在这里插入图片描述

Prometheus数据源

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

python示例代码

import requests
import json########### Prometheus数据源查询 ###########
headers = {'Accept': 'application/json, text/plain, */*','Content-Type': 'application/x-www-form-urlencoded'
}data = 'query=up%7Binstance%3D%22192.168.113.127%3A9308%22%2Cjob%3D%22kafka%22%7D%09&start=1724722380&end=1724743980&step=30'
url = 'http://192.168.113.118:3000/api/datasources/proxy/3/api/v1/query_range'response = requests.post(url, headers=headers, data=data)# 处理响应
if response.status_code == 200:# 成功获取响应result = response.json()print(json.dumps(result))
else:print(f"请求失败,状态码: {response.status_code}")

返回结果

在这里插入图片描述

关键字:【Grafana】通过api请求Grafana Datasource获取数据

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: