2019-11-15 10:05:28 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-11-21 07:23:25 +00:00
|
|
|
"strings"
|
2019-11-22 10:11:26 +00:00
|
|
|
"time"
|
2019-12-12 09:57:34 +00:00
|
|
|
|
|
|
|
"github.com/ehlxr/monitor/pkg"
|
|
|
|
"github.com/hpcloud/tail"
|
2019-11-15 10:05:28 +00:00
|
|
|
log "unknwon.dev/clog/v2"
|
|
|
|
|
|
|
|
"regexp"
|
2019-12-12 09:57:34 +00:00
|
|
|
|
|
|
|
dt "github.com/JetBlink/dingtalk-notify-go-sdk"
|
2019-11-15 10:05:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-11-22 10:11:26 +00:00
|
|
|
dingTalk *dt.Robot
|
|
|
|
limiter *pkg.LimiterServer
|
|
|
|
)
|
2019-11-15 10:05:28 +00:00
|
|
|
|
2019-11-22 10:11:26 +00:00
|
|
|
func init() {
|
|
|
|
err := log.NewConsole()
|
|
|
|
if err != nil {
|
|
|
|
panic("unable to create new logger: " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
2019-11-15 10:05:28 +00:00
|
|
|
|
2019-11-22 10:11:26 +00:00
|
|
|
func main() {
|
|
|
|
pkg.ParseArg()
|
2019-11-15 10:05:28 +00:00
|
|
|
|
2019-11-22 10:11:26 +00:00
|
|
|
dingTalk = dt.NewRobot(pkg.Opts.Robot.Token, pkg.Opts.Robot.Secret)
|
|
|
|
limiter = pkg.NewLimiterServer(1*time.Minute, 20)
|
2019-11-15 10:05:28 +00:00
|
|
|
|
2019-11-22 10:11:26 +00:00
|
|
|
tailFile()
|
2019-11-18 03:07:01 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 10:11:26 +00:00
|
|
|
func sendMsg(content string) {
|
2019-12-16 10:28:18 +00:00
|
|
|
if err := dingTalk.SendMarkdownMessage(
|
|
|
|
"new message",
|
2019-11-22 10:11:26 +00:00
|
|
|
fmt.Sprintf("%s\n%s", pkg.Opts.AppName, content),
|
|
|
|
pkg.Opts.Robot.AtMobiles,
|
|
|
|
pkg.Opts.Robot.IsAtAll,
|
|
|
|
); err != nil {
|
|
|
|
log.Error("%+v", err)
|
2019-12-16 10:28:18 +00:00
|
|
|
|
|
|
|
return
|
2019-11-22 10:11:26 +00:00
|
|
|
}
|
2019-11-15 10:05:28 +00:00
|
|
|
|
2019-11-22 10:11:26 +00:00
|
|
|
log.Info("send message <%s> success", content)
|
|
|
|
}
|
2019-11-15 10:05:28 +00:00
|
|
|
|
2019-11-22 10:11:26 +00:00
|
|
|
func tailFile() {
|
|
|
|
tf, err := tail.TailFile(pkg.Opts.File,
|
2019-11-15 10:05:28 +00:00
|
|
|
tail.Config{
|
2019-11-18 03:07:01 +00:00
|
|
|
ReOpen: true,
|
2019-11-15 10:05:28 +00:00
|
|
|
Follow: true,
|
|
|
|
Location: &tail.SeekInfo{Offset: 0, Whence: 2},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Tail file %+v", err)
|
|
|
|
}
|
2019-11-20 08:32:57 +00:00
|
|
|
|
2019-11-22 10:11:26 +00:00
|
|
|
if pkg.Opts.KeyWordIgnoreCase {
|
|
|
|
pkg.Opts.KeyWord = strings.ToLower(pkg.Opts.KeyWord)
|
2019-11-21 08:52:42 +00:00
|
|
|
}
|
2019-11-22 10:11:26 +00:00
|
|
|
|
|
|
|
log.Info("monitor app <%s> file <%s>, filter by <%s>, ignore case <%v>...",
|
|
|
|
pkg.Opts.AppName,
|
|
|
|
pkg.Opts.File,
|
|
|
|
pkg.Opts.KeyWord,
|
|
|
|
pkg.Opts.KeyWordIgnoreCase)
|
|
|
|
|
2019-12-16 10:28:18 +00:00
|
|
|
var buffer strings.Builder
|
2019-12-13 10:35:05 +00:00
|
|
|
var times int
|
2019-12-12 09:57:34 +00:00
|
|
|
go func() {
|
2019-12-13 09:36:56 +00:00
|
|
|
ticker := time.NewTicker(1 * time.Minute)
|
2019-12-12 09:57:34 +00:00
|
|
|
for {
|
|
|
|
<-ticker.C
|
2019-12-17 09:27:15 +00:00
|
|
|
//if buffer.Len() > 0 && times > 2 {
|
|
|
|
if buffer.Len() > 0 {
|
2019-12-12 09:57:34 +00:00
|
|
|
sendMsg(buffer.String())
|
|
|
|
buffer.Reset()
|
|
|
|
}
|
2019-12-13 10:35:05 +00:00
|
|
|
|
2019-12-17 09:27:15 +00:00
|
|
|
//buffer.Reset()
|
|
|
|
//times = 0
|
2019-12-12 09:57:34 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-11-15 10:05:28 +00:00
|
|
|
for line := range tf.Lines {
|
2019-11-21 08:52:42 +00:00
|
|
|
text := line.Text
|
2019-11-22 10:11:26 +00:00
|
|
|
if pkg.Opts.KeyWordIgnoreCase {
|
2019-11-21 08:52:42 +00:00
|
|
|
text = strings.ToLower(text)
|
|
|
|
}
|
|
|
|
|
2019-11-28 09:01:42 +00:00
|
|
|
keys := strings.Split(pkg.Opts.KeyWord, ",")
|
|
|
|
for _, key := range keys {
|
|
|
|
if ok, _ := regexp.Match(strings.TrimSpace(key), []byte(text)); ok {
|
2019-12-12 09:57:34 +00:00
|
|
|
//if limiter.IsAvailable() {
|
2019-12-17 09:27:15 +00:00
|
|
|
// sendMsg("- " + line.Text + "\n")
|
2019-12-12 09:57:34 +00:00
|
|
|
//} else {
|
|
|
|
// log.Error("dingTalk 1 m allow send 20 msg. msg %v discarded.",
|
|
|
|
// line.Text)
|
|
|
|
//}
|
2019-12-16 10:28:18 +00:00
|
|
|
buffer.WriteString("- " + line.Text + "\n")
|
2019-12-13 10:35:05 +00:00
|
|
|
times++
|
2019-12-12 09:57:34 +00:00
|
|
|
|
2019-11-29 02:55:13 +00:00
|
|
|
break
|
2019-11-20 08:32:57 +00:00
|
|
|
}
|
2019-11-15 10:05:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|