-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdingding_example.go
57 lines (51 loc) · 1.22 KB
/
dingding_example.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package dingding
import (
"fmt"
"log"
"time"
)
const (
// 一般通过配置文件配置
openUrl = "https://oapi.dingtalk.com/robot/send?access_token=xxx" //"钉钉生成的url"
mobiles = "18012345678,18212345678" // @ 通知对象手机号
MaxNum = 1<<32 - 1
)
func SendAlarmMessAge() {
ch := GetDingCh()
alarmCntTimer := time.NewTimer(time.Second * 60)
var alarmCnt, lastAlarmCnt int
for {
select {
case ctx := <-ch:
alarmCnt++
if (alarmCnt - lastAlarmCnt) <= 10 { // 每分钟最大允许发送告警次数
alarmStr := fmt.Sprintf("时间: %s 服务xxx 告警信息: %s", time.Now().Format("2006-01-02 15:04:05"), ctx)
SendDingCh(openUrl, mobiles, alarmStr)
}
case <-alarmCntTimer.C:
if alarmCnt >= MaxNum {
alarmCnt = 0
}
lastAlarmCnt = alarmCnt
log.Printf("%d alarms have been sent.", alarmCnt)
alarmCntTimer.Reset(time.Second * 60)
}
}
}
var PanicHandler func(interface{})
func WithRecovery(f func()) {
defer func() {
if err := recover(); err != nil {
if PanicHandler != nil {
PanicHandler(err)
}
}
}()
f()
}
func main() {
// usage
go WithRecovery(SendAlarmMessAge)
PushMessage("alarm xxx") // 发送告警信息
select {}
}