grafana自动截图
作者:互联网
Grafana支持将面板自动呈现为PNG图像。这允许Grafana自动生成面板的图像,以便将其包含在警报通知中。需要安装grafana-image-renderer插件,安装可以参考官网https://grafana.com/grafana/plugins/grafana-image-renderer/
docker模式运行yml
grafana: container_name: grafana depends_on: - prometheus image: grafana/grafana:7.5.7 volumes: - type: bind source: /etc/localtime target: /etc/localtime - type: volume source: grafana target: /var/lib/grafana environment: GF_SERVER_DOMAIN: 127.0.0.1 GF_RENDERING_SERVER_URL: http://127.0.0.1:8081/render GF_RENDERING_CALLBACK_URL: http://127.0.0.1:7000/ GF_LOG_FILTERS: rendering:debug ports: - "7000:3000" networks: - prom renderer: container_name: renderer image: grafana/grafana-image-renderer:latest ports: - "8081:8081" networks: - promView Code
然后打开grafan,选择share,点击 Direct link rendered image,可以看到图片和对应的url
在呈现图像时,PNG图像被临时写入文件系统。当图像呈现时,PNG图像被临时写入Grafana数据文件夹中的PNG文件夹。
url格式如下
http://127.0.0.1:8300/render/d-solo/ZypwSrZnz/xxxx?orgId=1&from=1645077536501&to=1645099136501&panelId=30&width=1000&height=500&tz=Asia%2FShanghai
然后就可以通过python请求下载和保存图片,下载时需要认证,可以通过grafana生成api key,用来做认证授权
# 获取时间戳 def get_time(): t = datetime.datetime.now() # 当前日期 t1 = t.strftime('%Y-%m-%d %H:%M:%S') # 转为秒级时间戳 ts1 = time.mktime(time.strptime(t1, '%Y-%m-%d %H:%M:%S')) # 转为毫秒级 end_time = int(str(ts1 * 1000).split(".")[0]) # 1小时前 t2 = (t - datetime.timedelta(hours=1)).strftime("%Y-%m-%d %H:%M:%S") # 转为秒级时间戳 ts2 = time.mktime(time.strptime(t2, '%Y-%m-%d %H:%M:%S')) # 转为毫秒级 start_time = int(str(ts2 * 1000).split(".")[0]) return str(start_time), str(end_time) # 下载pic def download_pic(): grafana_server = "xxxxx" url = (grafana_server + '/render/d-solo/xxx' + '?orgId=1&from=' + get_time()[0] + '&to=' + get_time()[1] + "&panelId=30&width=1000&height=500&tz=Asia%2FShanghai" ) print(url) header = {"Content-Type": "application/json","Authorization": "Bearer xxxx"} # 用管理员去Grafana生成API Key res = requests.get(url,headers=header) time_now = int(time.time()) time_local = time.localtime(time_now) dt = time.strftime("%Y-%m-%d",time_local) img_name = "img"+dt+".jpg" filename = img_name with open(filename, "wb") as f: f.write(res.content) return filename download_pic()View Code
标签:-%,截图,url,image,grafana,自动,renderer,time 来源: https://www.cnblogs.com/quanloveshui/p/15906241.html