因为监控的图标比较多,手动页面上一点点操作太浪费时间了,通过对其分析,用Python写了个脚本可快速生成。
代码如下
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 5 15:51:02 2022
@author: liuyunshengsir
@describe 批量生成dashboard 的panel程序
"""
import pandas as pd
dataFrame=pd.read_excel('E:\\person_management\\gitspace\\data_process\\grafana\\auto_panel.xlsx')
rawDashbord="";
with open("dashbord.json", "r") as f:
data1 = f.read()
rawDashbord=data1
rawPanel="";
with open("panel.json", "r") as f:
data2 = f.read()
rawPanel=data2
newPanels=""
for index, row in dataFrame.iterrows():
expr=str(row["expr"]).replace("\"","\\\"")
newPanel=rawPanel.replace("grafana_replace_id",str(row["id"])).replace("grafana_replace_title",str(row["title"])).replace("grafana_replace_expr",expr).replace("grafana_replace_legendFormat",str(row["legendFormat"])).replace("grafana_replace_unit",str(row["unit"])).replace("gridPos_h",str(row["gridPos_h"])).replace("gridPos_w",str(row["gridPos_w"])).replace("gridPos_x",str(row["gridPos_x"])).replace("gridPos_y",str(row["gridPos_y"]))
newPanels+=" %s ," % (newPanel)
print(row["title"])
newDashbord=rawDashbord.replace("grafana_replace_panels",newPanels)
with open("new_dashbord.txt","w",encoding='utf-8') as f:
f.write(newDashbord)
生成后直接导入即可,如下