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

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

实现方式

    Grafana开启了匿名访问后,F12刷新页面,查看面板的实际访问路径、请求头部和请求参数。

    使用python请求,就可以返回Grafana面板看到的值。

Mysql数据源


python示例代码

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示例代码

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}")

返回结果

相关推荐
游戏开发爱好者85 小时前
iOS 推送怎么配置,APNs 推送证书、设备库与群发
android·ios·小程序·https·uni-app·iphone·webview
wxson728219 小时前
【Android视频监控系统技术总结】
android·音视频
hunterandroid21 小时前
[Android 从零到一] LiveData 粘性事件与单次消费:从重复触发到可靠事件总线
android
hunterandroid21 小时前
[Android 从零到一] Android 事件分发机制:从 ACTION_DOWN 到 View 事件消费链路
android
阿巴斯甜21 小时前
adb‑shell 全套命令实操总结
android
奔跑的架构师1 天前
[A-49]ARMv9/v8-PSCI接口规范与工作流程简介
android·linux·arm开发·arm
AFinalStone1 天前
Android 7系统国际化(七)Resources 与 AssetManager 源码剖析
android·国际化·local
Android-Flutter1 天前
Android 内存泄漏详解
android·kotlin
catchadmin1 天前
Fiber 与 PHP 8.6 Polling API 异步初探
android·开发语言·php