如何透过邮件自动发送 Grafana 仪表板报告

由于 Grafana 多样的面板选择,我们经常使用 Grafana 将採集到的监控数据进行多种视觉化的呈现,并透过时间序列的方式进行分析。

但是 Grafana Community 版本没有办法将 Dashboard 以 PDF 格式直接产生报告。该功能只有 Grafana Cloud Pro、Grafana Cloud Advanced 以及 Grafana Enterprise 有支援。

若您只想要在本地运行 Grafana 的话,那就只剩 Grafana Enterprise 可以考虑。官方网站并没有提供 Grafana Enterprise 的价格,目前只能透过电话与业务代表联繫,据说每年至少要 40,000 美金。

今天就来教大家如何透过邮件自动发送 Grafana 仪表板报告吧。

Export Dashboard as PDF
还好网路上已经有大神帮我们实现了,直接拿来使用就可以。
https://github.com/IzakMarais/reporter

若还没安装 Docker 的朋友,可以参考这篇文章。
https://medium.com/@jieshiun/%E5%A6%82%E4%BD%95%E5%9C%A8-ubuntu-20-04-%E5%AE%89%E8%A3%9D-docker-%E8%88%87-docker-compose-27a88b3d746b

我们透过下列指令建置容器

sudo docker run -d -p 8686:8686 --net="host" --name grafana-reporter --restart=always izakmarais/grafana-reporter

打开浏览器确认一下 8686 埠号,正常画面如下。

回到 Grafana 仪表板的设定,新增一笔 Links 如下。

Type:LinkURL:http://{your_grafana_ip}:8686/api/v5/report/{your_dashboard_id}Icon:docOption:Include current time range、Open link in new tab

仪表板的右上方就会多一个 Report 的连结

点选该连结,就会以 PDF 格式产生报告。

可以在仪表板的一般资讯维护报表的名称与描述。

若想要自动产生报告,就必须下载程式码并使用指令模式。

必要条件
我们需要先安装 Grafana 图像渲染器外挂

sudo grafana-cli plugins install grafana-image-renderer
✔ Downloaded and extracted grafana-image-renderer v3.7.0 zip successfully to /var/lib/grafana/plugins/grafana-image-rendererPlease restart Grafana after installing plugins. Refer to Grafana documentation for instructions if necessary.

记得重启 Grafana 服务

sudo service grafana-server restart

随便找一个 Panel 右键点选 Share,点选 Direct link rendered image。

若渲染发生错误提示如下

logger=plugin.grafana-image-renderer t=2023-05-05T11:54:51.675605095+08:00 level=error msg="Render request failed" url="http://localhost:3000/d-solo/TaRQ7YyVk/windows-logon-events?orgId=1&from=1683255284739&to=1683258884739&panelId=14&width=1000&height=500&tz=Asia%2FTaipei&render=1" error="Error: Failed to launch the browser process!\n/var/lib/grafana/plugins/grafana-image-renderer/chrome-linux/chrome: error while loading shared libraries: libatk-1.0.so.0: cannot open shared object file: No such file or directory\n\n\nTROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md\n"

透过下面指令安装缺少的套件

sudo apt-get install libatk1.0-0 libatk-bridge2.0-0 libcups2 libxkbcommon-x11-0 libxcomposite1 libxdamage1 libxfixes3 libxrandr2 libgbm1 libpango-1.0-0 libcairo2

安装 Latex 套件,产生 PDF 的时候会使用到。

sudo apt install texlive-latex-extra

由于程式码是用 Go 开发的,所以还需要能够执行 Go。

curl -OL https://golang.org/dl/go1.16.7.linux-amd64.tar.gzsudo tar -C /usr/local -xvf go1.16.7.linux-amd64.tar.gz

设置 Go 路径

sudo nano ~/.profile

在文件底部添加如下

export PATH=$PATH:/usr/local/go/bin

检查是否可以运行 go version

source ~/.profilego version
go version go1.16.7 linux/amd64

运行程式码
下载 IzakMarais/reporter 程式码

go get github.com/IzakMarais/reporter/...

查询程式支援那些参数

./go/bin/grafana-reporter --help
Usage of ./go/bin/grafana-reporter:  -cmd_apiKey string     Grafana api key. Required (and only used) in command line mode.  -cmd_apiVersion string     Api version: [v4, v5]. Required (and only used) in command line mode, example: -apiVersion v5. (default "v5")  -cmd_dashboard string     Dashboard identifier. Required (and only used) in command line mode.  -cmd_enable     Enable command line mode. Generate report from command line without starting webserver (-cmd_enable=1).  -cmd_o string     Output file. Required (and only used) in command line mode. (default "out.pdf")  -cmd_template string     Specify a custom TeX template file. Only used in command line mode, but is optional even there.  -cmd_ts string     Time span. Required (and only used) in command line mode. (default "from=now-3h&to=now")  -grid-layout     Enable grid layout (-grid-layout=1). Panel width and height will be calculated based off Grafana gridPos width and height.  -ip string     Grafana IP and port. (default "localhost:3000")  -port string     Port to serve on. (default ":8686")  -proto string     Grafana Protocol. Change to 'https://' if Grafana is using https. Reporter will still serve http. (default "http://")  -ssl-check     Check the SSL issuer and validity. Set this to false if your Grafana serves https using an unverified, self-signed certificate. (default true)  -templates string     Directory for custom TeX templates. (default "templates/")

新增一组 API Key 才能透过 API 存取 Grafana 的仪錶板

使用下列指令产生 PDF 报告

./go/bin/grafana-reporter \    -cmd_enable=1 \    -cmd_apiKey your_api_key \    -ip localhost:3000 \    -cmd_dashboard your_dashboard_id \    -cmd_ts from=now-30m \    -grid-layout \    -cmd_o output.pdf
2023/05/09 11:57:31 grafana reporter, version: 2.3-1 hash: 6cfc3e220dc4f8fc0175ae0ec8698d35546d8c102023/05/09 11:57:31 serving at ':8686' and using grafana at 'http://localhost:3000'2023/05/09 11:57:31 SSL check enforced2023/05/09 11:57:31 Using grid layout.2023/05/09 11:57:31 Called with command line mode enabled, will save report to file and exit.2023/05/09 11:57:31 Called with command line mode 'dashboard' 'tBtMq6sVz'2023/05/09 11:57:31 Called with command line mode 'apiKey' 'eyJrIjoiOUV1dlFnTFVobVB3U2FkUXdsYjZ5djk3cTZ4MkNHdmoiLCJuIjoiUmVwb3J0IiwiaWQiOjF9'2023/05/09 11:57:31 Called with command line mode 'apiVersion' 'v5'2023/05/09 11:57:31 Called with command line mode 'outputFile' 'output.pdf'2023/05/09 11:57:31 Called with command line mode 'timeSpan' 'from=now-30m'2023/05/09 11:57:31 Reporter called2023/05/09 11:57:31 Called with api Token: eyJrIjoiOUV1dlFnTFVobVB3U2FkUXdsYjZ5djk3cTZ4MkNHdmoiLCJuIjoiUmVwb3J0IiwiaWQiOjF92023/05/09 11:57:31 Called without variable2023/05/09 11:57:31 Called with dashboard: tBtMq6sVz2023/05/09 11:57:31 Called with time range: {now-30m now}2023/05/09 11:57:31 Connecting to dashboard at http://localhost:3000/api/dashboards/uid/tBtMq6sVz2023/05/09 11:57:31 Populated dashboard datastructure: {Title:Windows Logon Events Description:Event ID 4624 – Successful logon \& Event ID 4625 – Failed logon VariableValues: Rows:[] Panels:[{Id:6 Type:piechart Title:稽核动作 GridPos:{H:5 W:8 X:0 Y:0}} {Id:10 Type:piechart Title:来源网路位址 GridPos:{H:5 W:8 X:8 Y:0}} {Id:11 Type:piechart Title:帐户名称 GridPos:{H:5 W:8 X:16 Y:0}} {Id:12 Type:timeseries Title:时间轴 GridPos:{H:5 W:24 X:0 Y:5}} {Id:16 Type:table Title:事件纪录 GridPos:{H:8 W:24 X:0 Y:10}}]}2023/05/09 11:57:31 Downloading image  10 http://localhost:3000/render/d-solo/tBtMq6sVz/_?from=now-30m&height=200&panelId=10&theme=light&to=now&width=3202023/05/09 11:57:31 Downloading image  6 http://localhost:3000/render/d-solo/tBtMq6sVz/_?from=now-30m&height=200&panelId=6&theme=light&to=now&width=3202023/05/09 11:57:31 Downloading image  11 http://localhost:3000/render/d-solo/tBtMq6sVz/_?from=now-30m&height=200&panelId=11&theme=light&to=now&width=3202023/05/09 11:57:31 Downloading image  12 http://localhost:3000/render/d-solo/tBtMq6sVz/_?from=now-30m&height=200&panelId=12&theme=light&to=now&width=9602023/05/09 11:57:31 Downloading image  16 http://localhost:3000/render/d-solo/tBtMq6sVz/_?from=now-30m&height=320&panelId=16&theme=light&to=now&width=9602023/05/09 11:57:42 Calling LaTeX - preprocessing2023/05/09 11:57:43 Calling LaTeX and building PDF2023/05/09 11:57:43 Extracted filename from dashboard title:  Windows Logon Events.pdf2023/05/09 11:57:43 Report generated correctly

产出的报告画面如下,LaTeX 的排版看起来就是舒服。

邮件发送
若您没有 SMTP 伺服器请参考下篇文章进行建置,接下来的配置我们会需要使用 SMTP 服务来发送邮件。
https://medium.com/@jieshiun/%E5%A6%82%E4%BD%95%E5%9C%A8-azure-%E8%A8%AD%E5%AE%9A-sendgrid-smtp-%E7%99%BC%E4%BF%A1-f02ab6074f2d

我们打算使用 PHPMailer 来发送电子邮件,先安装需要的套件。

sudo apt-get install php libphp-phpmailer sendmail

检查是否可以运行 php version

php -v
PHP 7.4.3-4ubuntu2.18 (cli) (built: Feb 23 2023 12:43:23) ( NTS )Copyright (c) The PHP GroupZend Engine v3.4.0, Copyright (c) Zend Technologies    with Zend OPcache v7.4.3-4ubuntu2.18, Copyright (c), by Zend Technologies

撰写发送邮件程式

vi sendmail.php

添加程式码如下

<?php    require '/usr/share/php/libphp-phpmailer/autoload.php';    $mail = new PHPMailer\PHPMailer\PHPMailer();    $mail->IsSMTP(); // enable SMTP    $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only    $mail->SMTPAuth = true; // authentication enabled    $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail    $mail->Host = "smtp.sendgrid.net";    $mail->Port = 465;    $mail->IsHTML(true);    $mail->Username = "apikey";    $mail->Password = "your_api_password";    $mail->From = "your_sender_email";    $mail->FromName = "your_sender_name";    $mail->addAddress("your_receiver_email", "your_receiver_name");    $mail->isHTML(true);    $mail->Subject = "Daily Login Report";    $mail->Body = "Attached is the report you requested.";    $mail->addAttachment("output.pdf");    if(!$mail->send())    {        echo "Mailer Error: ". $mail->ErrorInfo ."\n";    }    else    {        echo "Message has been sent successfully\n";    }?>

执行发送邮件程式

php sendmail.php
2023-05-09 06:22:27 CLIENT -> SERVER: DZ0abrJXj6/+VFERNwplbmRzdHJlYW0KZW5kb2JqCnN0YXJ0eHJlZgoxMDcwMDUKJSVFT0YK2023-05-09 06:22:27 CLIENT -> SERVER:2023-05-09 06:22:27 CLIENT -> SERVER: --b1_WCynrK2YnT6KWPU9VqQw612SZR4YK561U0xvh0tXg--2023-05-09 06:22:27 CLIENT -> SERVER:2023-05-09 06:22:27 CLIENT -> SERVER: .2023-05-09 06:22:28 CLIENT -> SERVER: QUITMessage has been sent successfully

邮件寄出成功

自动发送
我们希望每天上午都能收到发送前一天的报告,撰写执行脚本把产生报告跟发送邮件串起来。

vi grafana-reporter.sh
#!/bin/bashcd /home/administrator  filename=`date +%Y-%m-%d -d "yesterday"`.pdf./go/bin/grafana-reporter \    -cmd_enable=1 \    -cmd_apiKey your_api_key \    -ip localhost:3000 \    -cmd_dashboard your_dashboard_id \    -cmd_ts from=now-24h \    -grid-layout \    -cmd_o $filenameif test -f "$filename"; then    php sendmail.phpfi

编辑发送邮件程式,修改附件档名。

<?php    require '/usr/share/php/libphp-phpmailer/autoload.php';    $mail = new PHPMailer\PHPMailer\PHPMailer();    $mail->IsSMTP(); // enable SMTP    $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only    $mail->SMTPAuth = true; // authentication enabled    $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail    $mail->Host = "smtp.sendgrid.net";    $mail->Port = 465;    $mail->IsHTML(true);    $mail->Username = "apikey";    $mail->Password = "your_api_password";    $mail->From = "your_sender_email";    $mail->FromName = "your_sender_name";    $mail->addAddress("your_receiver_email", "your_receiver_name");    $mail->isHTML(true);    $mail->Subject = "Daily Login Report";    $mail->Body = "Attached is the report you requested.";    $filename = date('Y-m-d', strtotime('-1 day', time())).".pdf";    $mail->addAttachment($filename);    if(!$mail->send())    {        echo "Mailer Error: ". $mail->ErrorInfo ."\n";    }    else    {        echo "Message has been sent successfully\n";    }?>

赋予脚本执行权限并执行

chmod +x grafana-reporter.sh./grafana-reporter.sh

确认运行正常的话,就可以放入排程了。

sudo crontab -e

设定每天上午八点半进行寄送报告

# For example, you can run a backup of all your user accounts# at 5 a.m every week with:# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/# # For more information see the manual pages of crontab(5) and cron(8)# # m h  dom mon dow   command30 8  * * * /home/administrator/grafana-reporter.sh > /tmp/grafana-reporter.log

相信大家已经学会了如何透过邮件自动发送 Grafana 仪表板报告

我们 Dashboard 的资料来源是 Grafana Loki,若对 Loki 不熟的朋友可以参考下列文章。
https://medium.com/@jieshiun/%E6%89%8B%E6%8A%8A%E6%89%8B%E6%95%99%E4%BD%A0-grafana-loki-%E5%BE%9E%E5%85%A5%E9%96%80%E5%88%B0%E7%B2%BE%E9%80%9A-feab84c209f0

最后附上整体的架构图,感谢收看。

参考文件

https://grafana.com/docs/grafana/latest/introduction/grafana-enterprise/https://grafana.com/docs/grafana/latest/dashboards/create-reports/https://github.com/IzakMarais/reporterhttps://grafana.com/docs/grafana/latest/setup-grafana/image-rendering/troubleshooting/https://www.digitalocean.com/community/tutorials/how-to-install-go-on-ubuntu-20-04https://www.hostinger.com/tutorials/send-emails-using-php-mail

关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章