From b00b7817f2ea61d1aadeee4c0dea09fac68f60b8 Mon Sep 17 00:00:00 2001 From: qinyening <710leo@gmail.com> Date: Fri, 15 Jan 2021 15:58:21 +0800 Subject: [PATCH] Support screen and alert template (#517) --- src/modules/judge/judge.go | 2 +- src/modules/monapi/config/yaml.go | 11 ++++++ src/modules/monapi/http/router.go | 6 ++++ src/modules/monapi/http/router_tpl.go | 49 +++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 src/modules/monapi/http/router_tpl.go diff --git a/src/modules/judge/judge.go b/src/modules/judge/judge.go index 35c80ec1..e7e01100 100644 --- a/src/modules/judge/judge.go +++ b/src/modules/judge/judge.go @@ -117,7 +117,7 @@ func pconf() { func start() { runner.Init() - fmt.Println("transfer start, use configuration file:", *conf) + fmt.Println("judge start, use configuration file:", *conf) fmt.Println("runner.Cwd:", runner.Cwd) fmt.Println("runner.Hostname:", runner.Hostname) } diff --git a/src/modules/monapi/config/yaml.go b/src/modules/monapi/config/yaml.go index 3422ae63..a9d18010 100644 --- a/src/modules/monapi/config/yaml.go +++ b/src/modules/monapi/config/yaml.go @@ -29,6 +29,12 @@ type ConfYaml struct { Link linkSection `yaml:"link"` IndexMod string `yaml:"indexMod"` I18n i18n.I18nSection `yaml:"i18n"` + Tpl tplSection `yaml:"tpl"` +} + +type tplSection struct { + AlertPath string `yaml:"alertPath"` + ScreenPath string `yaml:"screenPath"` } type mergeSection struct { @@ -175,6 +181,11 @@ func Parse(ymlfile string) error { "converge": true, // 历史告警的数据库表,对于已收敛的告警,默认删掉,不保留,省得告警太多 }) + viper.SetDefault("tpl", map[string]string{ + "alertPath": "./etc/alert", + "screenPath": "./etc/screen", + }) + err = viper.Unmarshal(&yaml) if err != nil { return fmt.Errorf("Unmarshal %v", err) diff --git a/src/modules/monapi/http/router.go b/src/modules/monapi/http/router.go index f40ae558..217e0425 100644 --- a/src/modules/monapi/http/router.go +++ b/src/modules/monapi/http/router.go @@ -144,6 +144,12 @@ func Config(r *gin.Engine) { aggr.GET("/:id", aggrCalcGet) } + tpl := r.Group("/api/mon/tpl") + { + tpl.GET("", tplNameGets) + tpl.GET("/content", tplGet) + } + aggrs := r.Group("/api/mon/aggrs").Use() { aggrs.GET("", aggrCalcsWithEndpointGet) diff --git a/src/modules/monapi/http/router_tpl.go b/src/modules/monapi/http/router_tpl.go new file mode 100644 index 00000000..5bcfa06a --- /dev/null +++ b/src/modules/monapi/http/router_tpl.go @@ -0,0 +1,49 @@ +package http + +import ( + "github.com/didi/nightingale/src/modules/monapi/config" + + "github.com/gin-gonic/gin" + "github.com/toolkits/pkg/file" +) + +func tplNameGets(c *gin.Context) { + tplType := mustQueryStr(c, "tplType") + + var files []string + var err error + switch tplType { + case "alert": + files, err = file.FilesUnder(config.Get().Tpl.AlertPath) + dangerous(err) + case "screen": + files, err = file.FilesUnder(config.Get().Tpl.ScreenPath) + dangerous(err) + default: + bomb("tpl type not found") + } + + renderData(c, files, err) +} + +func tplGet(c *gin.Context) { + tplName := mustQueryStr(c, "tplName") + tplType := mustQueryStr(c, "tplType") + + var filePath string + switch tplType { + case "alert": + filePath = config.Get().Tpl.AlertPath + "/" + tplName + case "screen": + filePath = config.Get().Tpl.ScreenPath + "/" + tplName + default: + bomb("tpl type not found") + } + + if !file.IsExist(filePath) { + bomb("tpl not found") + } + + content, err := file.ToString(filePath) + renderData(c, content, err) +}