【grafana】Grafana Image Renderer插件获取grafana仪表盘图片(docker方式、python爬虫)

1、编写docker-compose.yml文件 

#如果你已启动grafana容器,只需修改docker-compose.yml,在grafana添加环境变量,grafana-image-renderer插件的服务,官网地址:Grafana Image Renderer plugin for Grafana | Grafana Labs,根据官网该插件需要16G的内存,但我测试4c8g也可正常运行

version: '2'
  services:
    grafana:
      image: grafana/grafana:latest
      ports:
        - '3000:3000'
      environment:
        GF_RENDERING_SERVER_URL: http://renderer:8081/render
        GF_RENDERING_CALLBACK_URL: http://grafana:3000/
        GF_LOG_FILTERS: rendering:debug
    renderer:
      image: grafana/grafana-image-renderer:latest
      ports:
        - 8081:8081

2、启动docker-compose.yml

docker-compose up -d

3、测试图片

4、写python脚本批量获取图片


[root@dt1 bin]# cat download_dashboard.py
# -*- coding: utf-8 -*-

import requests
import os
import time
from datetime import datetime, timedelta

#获取当前目录的上一层目录
script_dir = os.path.dirname(os.getcwd())
#script_dir = os.path.dirname(os.path.abspath(__file__))
#图片保存目录
target_dir = os.path.join(script_dir+"/png")

#获取指定日期时间的时间戳,毫秒级别
def get_timestamp(date_time):
    return int(time.mktime(date_time.timetuple())) * 1000
#获取几天前仪表盘数据
days=7
#当前时间戳
time_now=get_timestamp(datetime.now())
#7天前时间戳
time_old=get_timestamp(datetime.now() - timedelta(days=days))
#em ip 地址
em_ip="172.16.8.193"


##获取仪表盘图片
def download_img_with_timeout(url, headers, img_name, timeout):
    try:
        response = requests.get(url, headers=headers, timeout=timeout)
        target_path = os.path.join(target_dir, img_name)
        if response.status_code == 200:
            with open(target_path, 'wb') as f:
                f.write(response.content)
            print(img_name+"仪表盘图片下载成功")
        else:
            print("获取仪表盘图片失败,HTTP 状态码:", response.status_code)
    except requests.exceptions.Timeout:
        print("请求超时")
    except requests.exceptions.RequestException as e:
        print("请求异常:", e)

#请求头,cookie可以不加,根据实际情况
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0",
    "Cookie": "dk=test,token=eyJhbGciOiJzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MDk3MDU5NjcsInNlc3Npb25faGFzaF9pZCI6ImNmY2QyMDg0OTVkNTY1ZWY2NmU3ZGZmOWY5ODc2NGRhIiwidXNlcl9pZCI6MSwidXNlcl9uYW1lIjoiYWRtaW5AZHRzdGFjay5jb20iLCJ1c2VyX3Blcm1pc3Npb24iOjF9.rGjRbK-61VT-t38OXeBtmzoB9CmIGhEUdD8Nf0Crs1Y; em_username=admin@dtk.com;"
}

# url列表
url_img_map = {
  "dashboard_disk":["Ne_roaViz/host_overview?theme=light&orgId=1&from=","44","DiskUsed.png"],
  "dashboard_mem":["Ne_roaViz/host_overview?theme=light&orgId=1&from=","50","MemUsed.png"],
  "dashboard_systemload":["Ne_roaViz/host_overview?theme=light&orgId=1&from=","38","SystemLoad.png"],
}
##调用
for key, value in url_img_map.items():
  url=value[0]
  id=value[1]
  img_name=value[2]
  url="http://"+em_ip+"/grafana/render/d-solo/"+url+str(time_old)+"&to="+str(time_now)+"&panelId="+id+"&width=1000&height=500&tz=Asia%2FShanghai"
  #print url,img_name
  download_img_with_timeout(url, headers, img_name, timeout=5)

把图片加载到doc文档

[root@dt1 bin]# cat doc.py
# -*- coding: utf-8 -*-
from docx import Document
from docx.shared import Inches
import os


# 创建一个新的Word文档
doc = Document()

#获取当前目录的上一层目录
script_dir = os.path.dirname(os.getcwd())
#仪表盘路径
image_path = os.path.join(script_dir,"png")

#
title1 = u'二、服务器层面'
title2 = u'2.1 磁盘'
title3 = u'2.1.1 磁盘使用率'
doc.add_heading(title1, level=1)
doc.add_heading(title2, level=2)
doc.add_heading(title3, level=3)
doc.add_picture(image_path+"/DiskUsed.png", width=Inches(3))
#
title2 = u'2.2 内存'
title3 = u'2.2.1 内存使用率'
doc.add_heading(title2, level=2)
doc.add_heading(title3, level=3)
doc.add_picture(image_path+"/MemUsed.png", width=Inches(3))
#
title2 = u'2.3 CPU'
title3 = u'2.3.1 cpu负载情况'
doc.add_heading(title2, level=2)
doc.add_heading(title3, level=3)
doc.add_picture(image_path+"/SystemLoad.png", width=Inches(3))




# 保存Word文档
doc.save(script_dir+"/doc/output.docx")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

维运

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值