• 【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也可正常运行

    1. version: '2'
    2. services:
    3. grafana:
    4. image: grafana/grafana:latest
    5. ports:
    6. - '3000:3000'
    7. environment:
    8. GF_RENDERING_SERVER_URL: http://renderer:8081/render
    9. GF_RENDERING_CALLBACK_URL: http://grafana:3000/
    10. GF_LOG_FILTERS: rendering:debug
    11. renderer:
    12. image: grafana/grafana-image-renderer:latest
    13. ports:
    14. - 8081:8081

    2、启动docker-compose.yml

    docker-compose up -d

    3、测试图片

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

    1. [root@dt1 bin]# cat download_dashboard.py
    2. # -*- coding: utf-8 -*-
    3. import requests
    4. import os
    5. import time
    6. from datetime import datetime, timedelta
    7. #获取当前目录的上一层目录
    8. script_dir = os.path.dirname(os.getcwd())
    9. #script_dir = os.path.dirname(os.path.abspath(__file__))
    10. #图片保存目录
    11. target_dir = os.path.join(script_dir+"/png")
    12. #获取指定日期时间的时间戳,毫秒级别
    13. def get_timestamp(date_time):
    14. return int(time.mktime(date_time.timetuple())) * 1000
    15. #获取几天前仪表盘数据
    16. days=7
    17. #当前时间戳
    18. time_now=get_timestamp(datetime.now())
    19. #7天前时间戳
    20. time_old=get_timestamp(datetime.now() - timedelta(days=days))
    21. #em ip 地址
    22. em_ip="172.16.8.193"
    23. ##获取仪表盘图片
    24. def download_img_with_timeout(url, headers, img_name, timeout):
    25. try:
    26. response = requests.get(url, headers=headers, timeout=timeout)
    27. target_path = os.path.join(target_dir, img_name)
    28. if response.status_code == 200:
    29. with open(target_path, 'wb') as f:
    30. f.write(response.content)
    31. print(img_name+"仪表盘图片下载成功")
    32. else:
    33. print("获取仪表盘图片失败,HTTP 状态码:", response.status_code)
    34. except requests.exceptions.Timeout:
    35. print("请求超时")
    36. except requests.exceptions.RequestException as e:
    37. print("请求异常:", e)
    38. #请求头,cookie可以不加,根据实际情况
    39. headers = {
    40. "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",
    41. "Cookie": "dtstack=test,em_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MDk3MDU5NjcsInNlc3Npb25faGFzaF9pZCI6ImNmY2QyMDg0OTVkNTY1ZWY2NmU3ZGZmOWY5ODc2NGRhIiwidXNlcl9pZCI6MSwidXNlcl9uYW1lIjoiYWRtaW5AZHRzdGFjay5jb20iLCJ1c2VyX3Blcm1pc3Npb24iOjF9.rGjRbK-61VT-t38OXeBtmzoB9CmIGhEUdD8Nf0Crs1Y; em_username=admin@dtstack.com;"
    42. }
    43. # url列表
    44. url_img_map = {
    45. "dashboard_disk":["Ne_roaViz/host_overview?theme=light&orgId=1&from=","44","DiskUsed.png"],
    46. "dashboard_mem":["Ne_roaViz/host_overview?theme=light&orgId=1&from=","50","MemUsed.png"],
    47. "dashboard_systemload":["Ne_roaViz/host_overview?theme=light&orgId=1&from=","38","SystemLoad.png"],
    48. "dashboard_uic":["qVfmgmTqf/dtuic_overview?theme=light&orgId=1&from=","210","uic_gc_count.png"],
    49. "dashboard_publicserver":["qVfmgmTqv/publicservice_overview?theme=light&orgId=1&from=","210","publcserver_gc_count.png"],
    50. "dashboard_schedulex":["qVfmgmTqg/engine_overview?theme=light&orgId=1&from=","210","schedulex_gc_count.png"],
    51. "dashboard_metadata":["qVfmgmTqc/metadata_overview?theme=light&orgId=1&from=","210","metadata_gc_count.png"],
    52. "dashboard_gateway":["qVfmgmTqh/gateway_overview?theme=light&orgId=1&from=","210","gateway_gc_count.png"],
    53. "dashboard_batch":["qVfmgmTqd/batch_overview?theme=light&orgId=1&from=","210","batch_gc_count.png"],
    54. "dashboard_stream":["qVfmgmTqn/stream_overview?theme=light&orgId=1&from=","210","stream_gc_count.png"],
    55. "dashboard_assets":["qVfmgmT4z-assets/assets_overview?theme=light&orgId=1&from=","210","assets_gc_count.png"],
    56. "dashboard_api":["qVfmgmT4z/api_overview?theme=light&orgId=1&from=","210","api_gc_count.png"],
    57. #"dashboard_easyindex":["
    58. "dashboard_tag":["qVfmgmTqm/tag_overview?theme=light&orgId=1&from=","210","tag_gc_count.png"]
    59. }
    60. ##调用
    61. for key, value in url_img_map.items():
    62. url=value[0]
    63. id=value[1]
    64. img_name=value[2]
    65. 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"
    66. #print url,img_name
    67. download_img_with_timeout(url, headers, img_name, timeout=5)

    把图片加载到doc文档

    1. [root@dt1 bin]# cat doc.py
    2. # -*- coding: utf-8 -*-
    3. from docx import Document
    4. from docx.shared import Inches
    5. import os
    6. # 创建一个新的Word文档
    7. doc = Document()
    8. #获取当前目录的上一层目录
    9. script_dir = os.path.dirname(os.getcwd())
    10. #仪表盘路径
    11. image_path = os.path.join(script_dir,"png")
    12. #
    13. title1 = u'二、服务器层面'
    14. title2 = u'2.1 磁盘'
    15. title3 = u'2.1.1 磁盘使用率'
    16. doc.add_heading(title1, level=1)
    17. doc.add_heading(title2, level=2)
    18. doc.add_heading(title3, level=3)
    19. doc.add_picture(image_path+"/DiskUsed.png", width=Inches(3))
    20. #
    21. title2 = u'2.2 内存'
    22. title3 = u'2.2.1 内存使用率'
    23. doc.add_heading(title2, level=2)
    24. doc.add_heading(title3, level=3)
    25. doc.add_picture(image_path+"/MemUsed.png", width=Inches(3))
    26. #
    27. title2 = u'2.3 CPU'
    28. title3 = u'2.3.1 cpu负载情况'
    29. doc.add_heading(title2, level=2)
    30. doc.add_heading(title3, level=3)
    31. doc.add_picture(image_path+"/SystemLoad.png", width=Inches(3))
    32. #
    33. title1 = u'四、数栈服务组件'
    34. title2 = u'4.1 DTUic'
    35. doc.add_heading(title1, level=1)
    36. doc.add_heading(title2, level=2)
    37. doc.add_picture(image_path+"/uic_gc_count.png", width=Inches(3))
    38. title2 = u'4.2 DTPublicservice'
    39. doc.add_heading(title2, level=2)
    40. doc.add_picture(image_path+"/publcserver_gc_count.png", width=Inches(3))
    41. title2 = u'4.3 DTSchedulex'
    42. doc.add_heading(title2, level=2)
    43. doc.add_picture(image_path+"/schedulex_gc_count.png", width=Inches(3))
    44. title2 = u'4.4 DTMetadata'
    45. doc.add_heading(title2, level=2)
    46. doc.add_picture(image_path+"/metadata_gc_count.png", width=Inches(3))
    47. title2 = u'4.5 DTGateway'
    48. doc.add_heading(title2, level=2)
    49. doc.add_picture(image_path+"/gateway_gc_count.png", width=Inches(3))
    50. title2 = u'4.6 DTBatch'
    51. doc.add_heading(title2, level=2)
    52. doc.add_picture(image_path+"/batch_gc_count.png", width=Inches(3))
    53. title2 = u'4.7 DTStream'
    54. doc.add_heading(title2, level=2)
    55. doc.add_picture(image_path+"/stream_gc_count.png", width=Inches(3))
    56. title2 = u'4.8 DTAssets'
    57. doc.add_heading(title2, level=2)
    58. doc.add_picture(image_path+"/assets_gc_count.png", width=Inches(3))
    59. title2 = u'4.9 DTApi'
    60. doc.add_heading(title2, level=2)
    61. doc.add_picture(image_path+"/api_gc_count.png", width=Inches(3))
    62. title2 = u'4.10 DTEasyIndex'
    63. doc.add_heading(title2, level=2)
    64. #doc.add_picture(image_path+"/easyindex_gc_count.png", width=Inches(3))
    65. title2 = u'4.11 DTTag'
    66. doc.add_heading(title2, level=2)
    67. doc.add_picture(image_path+"/tag_gc_count.png", width=Inches(3))
    68. # 保存Word文档
    69. doc.save(script_dir+"/doc/output.docx")
  • 相关阅读:
    OSPF协议:优点、初始化流程和管理
    我们在SqlSugar开发框架中,用到的一些设计模式
    Django笔记三十之log日志记录详解
    4个顶级的区块链游戏开发包
    公司文件防泄密软件——「天锐绿盾」@德人合科技
    JavaScript中undefined和not defined的区别
    6年测试,面试大放厥词,最后竟.....
    网络I/O_04 IO模型
    剑指 Offer 10- I. 斐波那契数列
    泰山OFFICE技术讲座:JDK字体支持编码的研究1
  • 原文地址:https://blog.csdn.net/weixin_59128094/article/details/136564912