前言
Grafana是一个开源的,拥有丰富dashboard和图表编辑的指标分析平台,支持多种数据源。
- DashBoad:仪表盘,以各种图形的方式来展示从datasource拿到的数据
- Row:行,DashBoad的基本组成单元,一个DashBoad可以包含多个row。一个row可以展示一种信息或者多种信息的组合
- Panel:面板,实际上就是row展示信息的方式,支持表格,列表,热图等多种形式。
- Query Editor:查询编辑器,用来指定获取那一部分的数据,类似于sql 查询语句。
版本信息
系统版本: 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
学习到了,感谢博主
学习到了,感谢博主
看看
111
2
1