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

返回结果

相关推荐
_李小白6 小时前
【Android 美颜相机】第十二天:GPUImageFilterGroup 源码解析
android·数码相机
_李小白6 小时前
【Android GLSurfaceView源码学习】第三天:GLSurfaceView的Surface、GLES与EGLSurface的关联
android·学习
技术摆渡人6 小时前
专题三:【Android 架构】全栈性能优化与架构演进全书
android·性能优化·架构
花卷HJ6 小时前
Android 10+ 使用 WifiNetworkSpecifier 连接指定 WiFi(完整封装 + 实战)
android
前端世界6 小时前
鸿蒙系统中时间与日期的国际化实践:一次把不同文化显示问题讲清楚
android·华为·harmonyos
牛奶咖啡136 小时前
Prometheus+Grafana构建云原生分布式监控系统(五)
云原生·grafana·prometheus·监控nginx·两种监控nginx方法·nginx源码编译参数解析·编译nginx源码实操
木卫四科技6 小时前
【Claude Agent - 入门篇】:从原生 SDK 到自主智能体
android
2501_915918416 小时前
Mac 抓包软件有哪些?Charles、mitmproxy、Wireshark和Sniffmaster哪个更合适
android·ios·小程序·https·uni-app·iphone·webview
2501_915106326 小时前
iOS 抓包绕过 SSL 证书认证, HTTPS 暴力抓包、数据流分析
android·ios·小程序·https·uni-app·iphone·ssl
2501_9159214314 小时前
iOS App 电耗管理 通过系统电池记录、Xcode Instruments 与克魔(KeyMob)组合使用
android·ios·小程序·https·uni-app·iphone·webview