Support screen and alert template (#517)

This commit is contained in:
qinyening 2021-01-15 15:58:21 +08:00 committed by GitHub
parent 6b1e432f6d
commit b00b7817f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 67 additions and 1 deletions

View File

@ -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)
}

View File

@ -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)

View File

@ -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)

View File

@ -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)
}