add send message aggregation

This commit is contained in:
ehlxr 2019-12-12 17:57:34 +08:00
parent 03859221d7
commit 745ef9ee9b

38
main.go
View File

@ -1,15 +1,18 @@
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"github.com/ehlxr/monitor/pkg"
"github.com/hpcloud/tail"
"strings" "strings"
"time" "time"
"github.com/ehlxr/monitor/pkg"
"github.com/hpcloud/tail"
log "unknwon.dev/clog/v2" log "unknwon.dev/clog/v2"
dt "github.com/JetBlink/dingtalk-notify-go-sdk"
"regexp" "regexp"
dt "github.com/JetBlink/dingtalk-notify-go-sdk"
) )
var ( var (
@ -66,6 +69,19 @@ func tailFile() {
pkg.Opts.KeyWord, pkg.Opts.KeyWord,
pkg.Opts.KeyWordIgnoreCase) pkg.Opts.KeyWordIgnoreCase)
var buffer bytes.Buffer
go func() {
ticker := time.NewTicker(10 * time.Second)
for {
<-ticker.C
log.Info("will send msg to dingtalk...")
if buffer.Len() > 0 {
sendMsg(buffer.String())
buffer.Reset()
}
}
}()
for line := range tf.Lines { for line := range tf.Lines {
text := line.Text text := line.Text
if pkg.Opts.KeyWordIgnoreCase { if pkg.Opts.KeyWordIgnoreCase {
@ -75,12 +91,16 @@ func tailFile() {
keys := strings.Split(pkg.Opts.KeyWord, ",") keys := strings.Split(pkg.Opts.KeyWord, ",")
for _, key := range keys { for _, key := range keys {
if ok, _ := regexp.Match(strings.TrimSpace(key), []byte(text)); ok { if ok, _ := regexp.Match(strings.TrimSpace(key), []byte(text)); ok {
if limiter.IsAvailable() { //if limiter.IsAvailable() {
sendMsg(line.Text) // sendMsg(line.Text)
} else { //} else {
log.Error("dingTalk 1 m allow send 20 msg. msg %v discarded.", // log.Error("dingTalk 1 m allow send 20 msg. msg %v discarded.",
line.Text) // line.Text)
} //}
buffer.WriteString(line.Text)
buffer.WriteByte('\n')
break break
} }
} }