微服務框架(二十六)Grafana dashboard 定時報表

此係列文章將會描述Java框架Spring Boot、服務治理框架Dubbo、應用容器引擎Docker,及使用Spring Boot集成Dubbo、Mybatis等開源框架,其中穿插着Spring Boot中日誌切面等技術的實現,然後通過gitlab-CI以持續集成爲Docker鏡像。

本文爲使用grafana-reporter生成grafana dashboard報表,並使用定時任務郵件發送

本系列文章中所使用的框架版本爲Spring Boot 2.0.3-RELEASE,Spring 5.0.7-RELEASE,Dubbo 2.6.2。

定時報表

Grafana是一套開源的監控圖表顯示框架,支持監控報警功能,而dashboard定時報表功能可使用開源軟件grafana-reporter或集成Grafana API來實現

grafana-reporter

grafana-reporter由Go語言編寫,是根據grafana dashboard生成PDF報表的HTTP服務

grafana-reporter生成報表的功能,需設置grafana允許匿名訪問

[auth.anonymous]
# enable anonymous access
enabled = true

# specify organization name that should be used for unauthenticated users
org_name = Linghit

docker安裝

需注意時區問題,默認生成的報表爲UTC時區,使用修改時區後的linghit/grafanalinghit/grafana-reporter鏡像即可

sudo docker run -d \
--name grafana-reporter \
--net monitor \
--net host \
-p 8686:8686 \
linghit/grafana-reporter

前臺生成

settings —> Links —> New —> Type -> link Url

link Url:http://ip:8686/api/v5/report/uid

後臺下載

wget命令下載pdf

wget -O test.pdf http://ip:8686/api/v5/report/uid?from=now-24h&to=now

mail命令

Linux系統發送郵件的命令插件有很多,如mailmuttsandmail

SMTP配置

阿里雲禁止使用25端口,需使用SMTPS的465端口,並配置協議認證文件,詳見參考資料

/etc/mail.rc文件末增加SMTP配置

set from=[email protected]
set smtp=smtps://smtp.exmail.qq.com:465
set smtp-auth-user=[email protected]
set smtp-auth-password=<auth_token>
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/root/.certs

示例

mail -v -s "title" [email protected] < content.txt

定時任務

contrab

59 23 * * * /media/raid10/grafana/report.sh

定時報告腳本

增加對應的dashboard報告可依照格式添加,並在mail命令中使用-a參數添加附件

#/bin/bash
#auuthor:ricardo
#shell for creating grafana dashboard report
filepath=/media/raid10/grafana/report/
date=$(date +%Y-%m-%d)

# dashboard report name
filename_es_general=Elasticsearch-Nginx-generalapi.linghit.com-${date}.pdf
filename_spring=SpringBoot-Statistics-${date}.pdf
filename_es_api=Elasticsearch-Nginx-api.linghit.com-${date}.pdf

# download grafana dashboard report
wget -O ${filepath}${filename_es_general} http://172.16.7.5:8686/api/v5/report/8oPnVDCmz?from=now-24h&to=now&var-host=generalapi.linghit.com
wget -O ${filepath}${filename_spring} http://172.16.7.5:8686/api/v5/report/wAu8Swerd?from=now-24h&to=now
wget -O ${filepath}${filename_es_api} http://172.16.7.5:8686/api/v5/report/8oPnVDCmz?from=now-24h&to=now&var-host=api.linghit.com

sleep 30s

# send email
mail -v \
-a ${filepath}${filename_es_general} -a ${filepath}${filename_spring} -a ${filepath}${filename_es_api} \
-s "Grafana監控日報"-`date +%Y-%m-%d` \
-c "[email protected]" [email protected] < /media/raid10/grafana/content.txt

參考資料:

  1. Dashboard API
  2. 六種使用Linux命令發送帶附件的郵件
  3. Centos7 配置 sendmail、postfix 端口號25、465
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章