docker下运行grafana和grafana Image Renderer

行云流水
2022-03-14 / 7 评论 / 2,655 阅读 / 正在检测是否收录...

前言

Grafana是一个开源的,拥有丰富dashboard和图表编辑的指标分析平台,支持多种数据源。

版本信息

系统版本: centos7.9,软件版本: grafana-8.4.1

资源下载

代码下载地址:



安装grafana

安装docker和docker-compose

docker和docker-compose一键安装脚本

编辑docker-compose文件

version: '3.5'
services:
 grafana:
  image: grafana/grafana:latest
  restart: always
  ports:
   - "3000:3000"
  volumes:
   - /etc/localtime:/etc/localtime:ro
   - /etc/timezone:/etc/timezone:ro
   - ./db/:/db:rw
   - ./env_gra/grafana.ini:/etc/grafana/grafana.ini
   - /var/log/grafana:/var/log/grafana
  environment:
   - GF_SECURITY_ADMIN_PASSWORD=xxx@123
   - GF_INSTALL_IMAGE_RENDERER_PLUGIN=true
   - GRAFANA_VERSION=latest
  networks:
   gra_net:

 renderer:
  image: grafana/grafana-image-renderer:latest
  restart: always
  ports:
   - "3001:8081"
  volumes:
   - /etc/localtime:/etc/localtime:ro
   - /etc/timezone:/etc/timezone:ro
  environment:
   - GF_RENDERER_PLUGIN_TZ=Asia/Shanghai
   - GF_RENDERER_PLUGIN_IGNORE_HTTPS_ERRORS=true
  networks:
   gra_net:

networks:
  gra_net:
    driver: bridge
    driver_opts:
      com.docker.network.enable_ipv6: "false"
    ipam:
      driver: default
      config:
      - subnet: 172.16.240.0/24

编辑grafana.ini配置

在配置文件最后位置添加
[rendering]
server_url = http://xx.xx.xx.xx:3001/render
callback_url = http://xx.xx.xx.xx:3000

[plugin.grafana-image-renderer]
rendering_timezone = Asia/Shanghai
rendering_language = zh

启动grafana服务

cd grafana  && docker-compose up -d

安装插件

docker exec -it grafana-server bash
grafana-cli plugins install grafana-piechart-panel

邮件日报

利用python脚本调用grafana-image-renderer每天生成统计日报,邮件发给相关人员

编辑docker-compose配置文件

version: '3.5'
services:
  pyreport:
    build:
      context: .
      dockerfile: ./docker/Dockerfile
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - ./myapp:/myapp
      - /var/log/pyreport:/log/
    environment:
      - TZ=Asia/Shanghai
    command: sh /myapp/start_task.sh
    restart: unless-stopped
    network_mode: "host"

python核心脚本

下载指定的dashboard
def download_dashboard():
    # 组装url,跑代码之前现在浏览器试试
    dbuid = GRAFANA['dbuid-tuluban']
    grafana_server = "http://{}:{}".format(GRAFANA['server'], GRAFANA['port'])
    url = grafana_server + '/render/d/' + dbuid + '?from=' + str(sTime*1000) + '&to=' + str(eTime*1000) + '&var-datasource=MySQL&width=' + GRAFANA['width'] + '&height=' + GRAFANA['height'] + '&tz=Asia/Shanghai'
    #用管理员去Grafana生成API Key
    header = {"Authorization": GRAFANA['apikey']}
    request = urllib.request.Request(url,headers=header)
    try:
        # 访问并下载面板图
        response = urllib.request.urlopen(request)
        time_now = int(Time.time())
        time_local = Time.localtime(time_now)
        dt = Time.strftime("%Y-%m-%d",time_local)
        img_name = "bimg"+dt+".png"
        filename = '/tmp/' + img_name
        # print(response.getcode())
        if (int(response.getcode()) == 200):
            with open(filename, "wb") as f:
                f.write(response.read())
            return filename
        else:
            return "failed"
    except Exception as ex:
        print(ex)
        return "failed"
发送邮件python脚本
def send_mail(img_name):
    msgRoot = MIMEMultipart('related')
    msgRoot['Subject'] = '订单统计日报-{}'.format(Yesterday)
    msgRoot['From'] = formataddr(["订单统计", MAIL['sender']])
    msgRoot['To'] = ",".join(TOEMAIL) # 发给多人

    #添加正文
    content = MIMEText('<html><head><style>#string{text-align:center;font-size:25px;}</style><div id="string">统计结果:<div></head><body><img src="cid:image1" alt="image1"></body></html>','html','utf-8')
    msgRoot.attach(content)

    # 获取图片
    fp = open(img_name, 'rb')
    msgImage = MIMEImage(fp.read())
    fp.close()
    msgImage.add_header('Content-ID', 'image1') # 该id和html中的img src对应
    msgRoot.attach(msgImage)

    smtp = smtplib.SMTP_SSL(MAIL['smtp'])
    smtp.login(MAIL['username'], MAIL['password'])
    smtp.sendmail(MAIL['sender'], TOEMAIL, msgRoot.as_string())
    smtp.quit()
计划任务

部署计划任务,每天9:00自动发送报告到邮箱(宿主机配置)

0  9  *  *  *   docker exec -i pyreport_pyreport_1  /bin/bash -c 'cd /myapp/ &&  sh start_report.sh'

面板变量

FAQ

时区问题

可以在 Configuration -> Preferences -> TimeZone 修改为UTC

参考文章

评论 (7)

取消
只有登录/注册用户才可评论
  1. 头像
    wenguojiao
    · Windows 10 · Google Chrome
    沙发

    学习到了,感谢博主

    回复
  2. 头像
    11
    · Windows 10 · Google Chrome
    板凳

    学习到了,感谢博主

    回复
  3. 头像
    卡卡
    · MacOS · Google Chrome
    地毯

    表情表情

    回复
  4. 头像
    卡卡
    · MacOS · Google Chrome
    第4楼

    看看

    回复
  5. 头像
    123
    · Windows 10 · Google Chrome
    第5楼

    画图

    回复
  6. 头像
    111
    · Windows 10 · Google Chrome
    第6楼

    111

    回复
  7. 头像
    zhou
    · Windows 10 · Google Chrome
    第7楼

    2

    回复
  8. 头像
    lanlan
    · Windows 10 · Google Chrome
    第8楼

    1

    回复