2019-11-15 10:05:28 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-11-22 10:11:26 +00:00
|
|
|
"github.com/ehlxr/monitor/pkg"
|
2019-11-15 10:05:28 +00:00
|
|
|
"github.com/hpcloud/tail"
|
2019-11-21 07:23:25 +00:00
|
|
|
"strings"
|
2019-11-22 10:11:26 +00:00
|
|
|
"time"
|
2019-11-15 10:05:28 +00:00
|
|
|
log "unknwon.dev/clog/v2"
|
|
|
|
|
2019-11-20 08:32:57 +00:00
|
|
|
dt "github.com/JetBlink/dingtalk-notify-go-sdk"
|
2019-11-15 10:05:28 +00:00
|
|
|
"regexp"
|
|
|
|
)
|
|
|
|
|
|
|
|
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) {
|
|
|
|
if err := dingTalk.SendTextMessage(
|
|
|
|
fmt.Sprintf("%s\n%s", pkg.Opts.AppName, content),
|
|
|
|
pkg.Opts.Robot.AtMobiles,
|
|
|
|
pkg.Opts.Robot.IsAtAll,
|
|
|
|
); err != nil {
|
|
|
|
log.Error("%+v", err)
|
|
|
|
}
|
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-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 {
|
|
|
|
if limiter.IsAvailable() {
|
|
|
|
sendMsg(line.Text)
|
|
|
|
} else {
|
|
|
|
log.Error("dingTalk 1 m allow send 20 msg. msg %v discarded.",
|
|
|
|
line.Text)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|