Skip to content

Commit

Permalink
add parse file,template说明
Browse files Browse the repository at this point in the history
Signed-off-by: sdvdxl杜龙少 <sdvdxl@163.com>
  • Loading branch information
sdvdxl committed Jul 19, 2019
1 parent b023970 commit 8226d03
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,17 @@ open-falcon alarm 发送消息组件,注意这个是基于2.0版本新增的im
1. 在要告警的地方把上面的这个群组加入即可。
1. 修改alarm组件中 `api` 下,增加 配置:`"im": "http://localhost:23329/api/v1/message"`,端口按照本项目配置文件中的进行修改。
1. 启动本项目即可。

## 模板

默认模板文件是 `message-template.md`,可以使用的参数如下:

- 告警等级: {{.Level}}
- 告警类型: {{.Type}}
- 告警指标: {{.Condition}}
- 告警主机: {{.Endpoint}}
- 告警时间: {{.Time}}
- 告警说明: {{.Desc}}
- 次数:{{.Count}}

**如果参数配置错误会导致模板渲染失败无法发送。**
12 changes: 5 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ var (
cfg config.Config
ding *sender.DingTalk
wx *sender.Weixin

tpl *template.Template
)

func main() {
cfg = config.Read()

cfg = config.Read()
tpl = template.Must(template.ParseFiles(cfg.DingTalk.TemplateFile))
if cfg.DingTalk.Enable {
ding = sender.NewDingTalk()
}
Expand Down Expand Up @@ -74,13 +77,8 @@ func main() {
return err
}

t, err := template.New("alarm").ParseFiles(cfg.DingTalk.TemplateFile)
if err != nil {
return err
}

var buffer bytes.Buffer
if err := t.Execute(&buffer, msg); err != nil {
if err := tpl.Execute(&buffer, msg); err != nil {
return err
}
content = buffer.String()
Expand Down
2 changes: 1 addition & 1 deletion message-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
- 告警等级: {{.Level}}
- 告警类型: {{.Type}}
- 告警指标: {{.Condition}}
- 告警主机: {{.Host}}
- 告警主机: {{.Endpoint}}
- 告警时间: {{.Time}}
- 告警说明: {{.Desc}},已持续{{.Count}}分钟

0 comments on commit 8226d03

Please sign in to comment.