Skip to content

Commit

Permalink
计算
Browse files Browse the repository at this point in the history
Signed-off-by: sdvdxl杜龙少 <[email protected]>
  • Loading branch information
sdvdxl committed Jul 26, 2019
1 parent fbb920a commit e51df1a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,18 @@ open-falcon alarm 发送消息组件,注意这个是基于2.0版本新增的im
- 告警时间: {{.Time}}
- 告警说明: {{.Desc}}
- 次数:{{.Count}}
- 表达式中的连续次数 {{.TriggerCount}}

**如果参数配置错误会导致模板渲染失败无法发送。**
**如果参数配置错误会导致模板渲染失败无法发送。**

默认模板中有如下表达式:

```text
{{with elapse .Count 60 .TriggerCount 300}}{{divide . 60}}{{end}}
```

其中 60 是数据上报周期,默认是60s,如果修改了默认值,需要修改这里的值。

300 是告警之间推迟时间,默认5分钟。

{{divide . 60}} 是除以60s,得到分钟
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"io/ioutil"
"log"
"time"
)

// Config 配置
Expand Down Expand Up @@ -32,7 +33,7 @@ type AlarmMessage struct {
Tags string // tags
TriggerCount int // 间隔
Count int // 当前告警次数
Time string
Time time.Time
Expression string
// 告警时间
}
Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ func main() {
// 第一次,直接返回上报间隔
return reportInterval * triggerCount
}, "divide": func(a, b int) int { return a / b },
"timeFormat": func(t time.Time, format string) string { return t.Format(format) }}
"timeFormat": func(t time.Time, format string) string {
return t.Format(format)
},
"timeDiffFormat": func(t time.Time, format string, seconds int) string {
return t.Add(-(time.Second * time.Duration(seconds))).Format(format)
}}

tpl = template.Must(template.New(path.Base(cfg.DingTalk.TemplateFile)).Funcs(funcMap).ParseFiles(cfg.DingTalk.TemplateFile))
if cfg.DingTalk.Enable {
Expand Down
3 changes: 2 additions & 1 deletion message-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- 告警指标: {{.Counter}} {{.Tags}}
- 表达式: {{.Expression}}
- 告警主机: {{.Endpoint}}
- 告警时间: {{.Time}}
- 通知时间: {{timeFormat .Time "2006-01-02 15:04:05"}}
- 告警时间: {{elapse .Count 60 .TriggerCount 300 | timeDiffFormat .Time "2006-01-02 15:04:05"}}
- 当前次数: {{.Count}}
- 告警说明: {{.Desc}},已持续 {{with elapse .Count 60 .TriggerCount 300}}{{divide . 60}}{{end}}分钟
9 changes: 7 additions & 2 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"regexp"
"strconv"
"strings"
"time"
)

// EncodeJSON json序列化(禁止 html 符号转义)
Expand Down Expand Up @@ -72,7 +73,11 @@ func HandleContent(content string) (*config.AlarmMessage, error) {
if err != nil {
return nil, err
}
time := strings.Join(subArgs[1:], " ")
timeStr := strings.Join(subArgs[1:], " ")
t, err := time.Parse("2006-01-02 15:04:05", timeStr)
if err != nil {
return nil, err
}
return &config.AlarmMessage{Level: args[0], Type: args[1], Endpoint: args[2], Expression: expression,
Desc: desc, Counter: counter, Tags: tags, TriggerCount: triggerCount, Count: count, Time: time}, nil
Desc: desc, Counter: counter, Tags: tags, TriggerCount: triggerCount, Count: count, Time: t}, nil
}

0 comments on commit e51df1a

Please sign in to comment.