Compare commits
18 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
2cc44aa12b | ||
f1a22cce86 | |||
8961313ee1 | |||
72e5c0f6ad | |||
2574de9a2f | |||
113d3a14e7 | |||
745ef9ee9b | |||
03859221d7 | |||
8b10e1466e | |||
17a4513c01 | |||
e6869549e5 | |||
4718b4ebd5 | |||
0b4c02be64 | |||
c441f95a2c | |||
c8a97cc9ae | |||
7dda8a09c3 | |||
1aa3e391b0 | |||
b37194ae12 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
logs
|
logs
|
||||||
go.sum
|
go.sum
|
||||||
dist
|
dist
|
||||||
|
.idea
|
4
Makefile
4
Makefile
@@ -4,8 +4,8 @@ COMMIT_SHA1 := $(shell git rev-parse HEAD)
|
|||||||
ROOT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
|
ROOT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
|
||||||
DIST_DIR := $(ROOT_DIR)/dist/
|
DIST_DIR := $(ROOT_DIR)/dist/
|
||||||
|
|
||||||
#VERSION_PATH := $(shell cat `go env GOMOD` | awk '/^module/{print $$2}')/
|
VERSION_PATH := $(shell cat `go env GOMOD` | awk '/^module/{print $$2}')/pkg
|
||||||
VERSION_PATH := main
|
#VERSION_PATH := main
|
||||||
LD_APP_NAMW := -X '$(VERSION_PATH).AppName=$(shell basename `pwd`)'
|
LD_APP_NAMW := -X '$(VERSION_PATH).AppName=$(shell basename `pwd`)'
|
||||||
LD_GIT_COMMIT := -X '$(VERSION_PATH).GitCommit=$(COMMIT_SHA1)'
|
LD_GIT_COMMIT := -X '$(VERSION_PATH).GitCommit=$(COMMIT_SHA1)'
|
||||||
LD_BUILD_TIME := -X '$(VERSION_PATH).BuildTime=$(BUILD_TIME)'
|
LD_BUILD_TIME := -X '$(VERSION_PATH).BuildTime=$(BUILD_TIME)'
|
||||||
|
30
README.md
30
README.md
@@ -1,19 +1,28 @@
|
|||||||
# Monitor
|
# Monitor
|
||||||
|
|
||||||
|
[](./LICENSE)
|
||||||
|
[](https://github.com/ehlxr/monitor/commits/)
|
||||||
|
[]((https://github.com/ehlxr/monitor/commits/))
|
||||||
|
[](https://github.com/ehlxr/monitor/releases)
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
Usage:
|
Usage:
|
||||||
monitor [OPTIONS]
|
monitor [OPTIONS]
|
||||||
|
|
||||||
Application Options:
|
Application Options:
|
||||||
-v, --version Show version info
|
-f, --monitor-file= The file to be monitored [$MONITOR_FILE]
|
||||||
|
-k, --search-keyword= Key word to be search for [$SEARCH_KEYWORD]
|
||||||
|
-v, --version Show version info
|
||||||
|
|
||||||
MONITOR:
|
DingTalk Robot Options:
|
||||||
-f, --file= The file to be monitored [$MONITOR_FILE]
|
-t, --robot-token= DingTalk robot access token [$ROBOT_TOKEN]
|
||||||
-k, --key-word= Key word to be filter [$MONITOR_KEY_WORD]
|
-s, --robot-secret= DingTalk robot secret [$ROBOT_SECRET]
|
||||||
-u, --dt-wh-url= Webhook url of dingtalk [$MONITOR_DT_WH_URL]
|
-m, --robot-at-mobiles= The mobile of the person will be at [$ROBOT_AT_MOBILES]
|
||||||
|
-a, --robot-at-all Whether at everyone [$ROBOT_AT_ALL]
|
||||||
|
|
||||||
Help Options:
|
Help Options:
|
||||||
-h, --help Show this help message
|
-h, --help Show this help message
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -24,8 +33,9 @@ Help Options:
|
|||||||
(_/\/\_)(_____)(_)\_)(____) (__) (_____)(_)\_)
|
(_/\/\_)(_____)(_)\_)(____) (__) (_____)(_)\_)
|
||||||
|
|
||||||
Name: monitor
|
Name: monitor
|
||||||
Version: v0.0.1
|
Version: v0.0.2
|
||||||
BuildTime: 2019-11-18 11:07:06
|
BuildTime: 2019-11-20 16:48:12
|
||||||
GitCommit: e3587d6025ce5a777ba4e7a600c06b1e7e6f7004
|
GitCommit: d91a475301b48eea4401e7454ab83d25474399ae
|
||||||
GoVersion: go version go1.13.1 darwin/amd64
|
GoVersion: go version go1.13.4 darwin/amd64
|
||||||
|
|
||||||
```
|
```
|
1
go.mod
1
go.mod
@@ -8,6 +8,7 @@ require (
|
|||||||
github.com/hpcloud/tail v1.0.1-0.20180514194441-a1dbeea552b7
|
github.com/hpcloud/tail v1.0.1-0.20180514194441-a1dbeea552b7
|
||||||
github.com/jessevdk/go-flags v1.4.1-0.20181221193153-c0795c8afcf4
|
github.com/jessevdk/go-flags v1.4.1-0.20181221193153-c0795c8afcf4
|
||||||
golang.org/x/sys v0.0.0-20191113165036-4c7a9d0fe056 // indirect
|
golang.org/x/sys v0.0.0-20191113165036-4c7a9d0fe056 // indirect
|
||||||
|
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect
|
||||||
gopkg.in/fsnotify/fsnotify.v1 v1.4.7 // indirect
|
gopkg.in/fsnotify/fsnotify.v1 v1.4.7 // indirect
|
||||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
|
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
|
||||||
unknwon.dev/clog/v2 v2.0.0-beta.5
|
unknwon.dev/clog/v2 v2.0.0-beta.5
|
||||||
|
168
main.go
168
main.go
@@ -1,57 +1,57 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/ehlxr/monitor/pkg"
|
||||||
"github.com/hpcloud/tail"
|
"github.com/hpcloud/tail"
|
||||||
"github.com/jessevdk/go-flags"
|
|
||||||
log "unknwon.dev/clog/v2"
|
log "unknwon.dev/clog/v2"
|
||||||
|
|
||||||
dt "github.com/JetBlink/dingtalk-notify-go-sdk"
|
|
||||||
"os"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
|
dt "github.com/JetBlink/dingtalk-notify-go-sdk"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
AppName string
|
dingTalk *dt.Robot
|
||||||
Version string
|
//limiter *pkg.LimiterServer
|
||||||
BuildTime string
|
|
||||||
GitCommit string
|
|
||||||
GoVersion string
|
|
||||||
|
|
||||||
versionTpl = `%s
|
|
||||||
Name: %s
|
|
||||||
Version: %s
|
|
||||||
BuildTime: %s
|
|
||||||
GitCommit: %s
|
|
||||||
GoVersion: %s
|
|
||||||
|
|
||||||
`
|
|
||||||
bannerBase64 = "DQogX18gIF9fICBfX19fXyAgXyAgXyAgX19fXyAgX19fXyAgX19fX18gIF9fX18gDQooICBcLyAgKSggIF8gICkoIFwoICkoXyAgXykoXyAgXykoICBfICApKCAgXyBcDQogKSAgICAoICApKF8pKCAgKSAgKCAgXykoXyAgICkoICAgKShfKSggICkgICAvDQooXy9cL1xfKShfX19fXykoXylcXykoX19fXykgKF9fKSAoX19fX18pKF8pXF8pDQo="
|
|
||||||
|
|
||||||
opts struct {
|
|
||||||
File string `short:"f" long:"monitor-file" env:"MONITOR_FILE" description:"The file to be monitored" required:"true"`
|
|
||||||
KeyWord string `short:"k" long:"search-keyword" env:"SEARCH_KEYWORD" description:"Key word to be search for" required:"true"`
|
|
||||||
Version bool `short:"v" long:"version" description:"Show version info"`
|
|
||||||
Robot robot `group:"DingTalk Robot Options" namespace:"robot" env-namespace:"ROBOT" `
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type robot struct {
|
|
||||||
Token string `short:"t" long:"token" env:"TOKEN" description:"DingTalk robot access token" required:"true"`
|
|
||||||
Secret string `short:"s" long:"secret" env:"SECRET" description:"DingTalk robot secret"`
|
|
||||||
AtMobiles []string `short:"m" long:"at-mobiles" env:"AT_MOBILES" env-delim:"," description:"The mobile of the person will be at"`
|
|
||||||
IsAtAll bool `short:"a" long:"at-all" env:"AT_ALL" description:"Whether at everyone"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
initLog()
|
err := log.NewConsole()
|
||||||
|
if err != nil {
|
||||||
|
panic("unable to create new logger: " + err.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
parseArg()
|
pkg.ParseArg()
|
||||||
|
|
||||||
tf, err := tail.TailFile(opts.File,
|
dingTalk = dt.NewRobot(pkg.Opts.Robot.Token, pkg.Opts.Robot.Secret)
|
||||||
|
//limiter = pkg.NewLimiterServer(1*time.Minute, 20)
|
||||||
|
|
||||||
|
tailFile()
|
||||||
|
}
|
||||||
|
|
||||||
|
func sendMsg(content string) {
|
||||||
|
if err := dingTalk.SendMarkdownMessage(
|
||||||
|
"new message",
|
||||||
|
fmt.Sprintf("%s\n%s", pkg.Opts.AppName, content),
|
||||||
|
pkg.Opts.Robot.AtMobiles,
|
||||||
|
pkg.Opts.Robot.IsAtAll,
|
||||||
|
); err != nil {
|
||||||
|
log.Error("%+v", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Info("send message <%s> success", content)
|
||||||
|
}
|
||||||
|
|
||||||
|
func tailFile() {
|
||||||
|
tf, err := tail.TailFile(pkg.Opts.File,
|
||||||
tail.Config{
|
tail.Config{
|
||||||
ReOpen: true,
|
ReOpen: true,
|
||||||
Follow: true,
|
Follow: true,
|
||||||
@@ -60,62 +60,54 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Tail file %+v", err)
|
log.Fatal("Tail file %+v", err)
|
||||||
}
|
}
|
||||||
log.Info("monitor file %s...", opts.File)
|
|
||||||
|
|
||||||
dingTalk := dt.NewRobot(opts.Robot.Token, opts.Robot.Secret)
|
if pkg.Opts.KeyWordIgnoreCase {
|
||||||
|
pkg.Opts.KeyWord = strings.ToLower(pkg.Opts.KeyWord)
|
||||||
|
}
|
||||||
|
|
||||||
for line := range tf.Lines {
|
log.Info("monitor app <%s> file <%s>, filter by <%s>, ignore case <%v>...",
|
||||||
if ok, _ := regexp.Match(opts.KeyWord, []byte(line.Text)); ok {
|
pkg.Opts.AppName,
|
||||||
err = dingTalk.SendTextMessage(line.Text, opts.Robot.AtMobiles, opts.Robot.IsAtAll)
|
pkg.Opts.File,
|
||||||
if err != nil {
|
pkg.Opts.KeyWord,
|
||||||
log.Error("%+v", err)
|
pkg.Opts.KeyWordIgnoreCase)
|
||||||
continue
|
|
||||||
|
var buffer strings.Builder
|
||||||
|
var times int
|
||||||
|
go func() {
|
||||||
|
ticker := time.NewTicker(1 * time.Minute)
|
||||||
|
for {
|
||||||
|
<-ticker.C
|
||||||
|
//if buffer.Len() > 0 && times > 2 {
|
||||||
|
if buffer.Len() > 0 {
|
||||||
|
sendMsg(buffer.String())
|
||||||
|
buffer.Reset()
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info("send message <%s> success", line.Text)
|
//buffer.Reset()
|
||||||
|
//times = 0
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
for line := range tf.Lines {
|
||||||
|
text := line.Text
|
||||||
|
if pkg.Opts.KeyWordIgnoreCase {
|
||||||
|
text = strings.ToLower(text)
|
||||||
|
}
|
||||||
|
|
||||||
|
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 + "\n")
|
||||||
|
//} else {
|
||||||
|
// log.Error("dingTalk 1 m allow send 20 msg. msg %v discarded.",
|
||||||
|
// line.Text)
|
||||||
|
//}
|
||||||
|
buffer.WriteString("- " + line.Text + "\n")
|
||||||
|
times++
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func initLog() {
|
|
||||||
err := log.NewConsole()
|
|
||||||
if err != nil {
|
|
||||||
panic("unable to create new logger: " + err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseArg() {
|
|
||||||
parser := flags.NewParser(&opts, flags.HelpFlag|flags.PassDoubleDash)
|
|
||||||
parser.NamespaceDelimiter = "-"
|
|
||||||
|
|
||||||
if AppName != "" {
|
|
||||||
parser.Name = AppName
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := parser.Parse(); err != nil {
|
|
||||||
if opts.Version {
|
|
||||||
// -v
|
|
||||||
printVersion()
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
|
|
||||||
// -h
|
|
||||||
_, _ = fmt.Fprintln(os.Stdout, err)
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
// err
|
|
||||||
_, _ = fmt.Fprintln(os.Stderr, err)
|
|
||||||
|
|
||||||
parser.WriteHelp(os.Stderr)
|
|
||||||
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// printVersion Print out version information
|
|
||||||
func printVersion() {
|
|
||||||
banner, _ := base64.StdEncoding.DecodeString(bannerBase64)
|
|
||||||
fmt.Printf(versionTpl, banner, AppName, Version, BuildTime, GitCommit, GoVersion)
|
|
||||||
}
|
|
||||||
|
40
pkg/limiter.go
Normal file
40
pkg/limiter.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package pkg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type LimiterServer struct {
|
||||||
|
interval time.Duration
|
||||||
|
maxCount int
|
||||||
|
sync.Mutex
|
||||||
|
reqCount int
|
||||||
|
time time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewLimiterServer(interval time.Duration, maxCount int) *LimiterServer {
|
||||||
|
return &LimiterServer{
|
||||||
|
interval: interval,
|
||||||
|
maxCount: maxCount,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (limiter *LimiterServer) IsAvailable() bool {
|
||||||
|
limiter.Lock()
|
||||||
|
defer limiter.Unlock()
|
||||||
|
now := time.Now()
|
||||||
|
|
||||||
|
if limiter.time.IsZero() ||
|
||||||
|
limiter.time.Add(limiter.interval).Before(now) {
|
||||||
|
limiter.reqCount = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
if limiter.reqCount < limiter.maxCount {
|
||||||
|
limiter.reqCount += 1
|
||||||
|
limiter.time = now
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
32
pkg/limiter_test.go
Normal file
32
pkg/limiter_test.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package pkg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLimiter(t *testing.T) {
|
||||||
|
limiter := NewLimiterServer(1*time.Second, 5)
|
||||||
|
|
||||||
|
for {
|
||||||
|
if limiter.IsAvailable() {
|
||||||
|
t.Log("hello...", limiter.reqCount)
|
||||||
|
} else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLimiter2(t *testing.T) {
|
||||||
|
limiter := NewLimiterServer(10*time.Second, 10)
|
||||||
|
|
||||||
|
for i := 0; i < 20; i++ {
|
||||||
|
if limiter.IsAvailable() {
|
||||||
|
fmt.Println("hello...", limiter.reqCount)
|
||||||
|
} else {
|
||||||
|
fmt.Println("limited")
|
||||||
|
}
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
|
}
|
56
pkg/parser.go
Normal file
56
pkg/parser.go
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
package pkg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/jessevdk/go-flags"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
Opts struct {
|
||||||
|
AppName string `short:"n" long:"monitor-app-name" env:"MONITOR_APP_NAME" description:"The name of the application being monitored, which will be added to the content before"`
|
||||||
|
File string `short:"f" long:"monitor-file" env:"MONITOR_FILE" description:"The file to be monitored" required:"true"`
|
||||||
|
KeyWord string `short:"k" long:"search-keyword" env:"SEARCH_KEYWORD" description:"Keyword to be search for, Multiple values separated by ','" default:"ERRO"`
|
||||||
|
KeyWordIgnoreCase bool `short:"c" long:"keyword-case-sensitive" env:"KEYWORD_IGNORE_CASE" description:"Whether Keyword ignore case"`
|
||||||
|
Version bool `short:"v" long:"version" description:"Show version info"`
|
||||||
|
Robot Robot `group:"DingTalk Robot Options" namespace:"robot" env-namespace:"ROBOT" `
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
type Robot struct {
|
||||||
|
Token string `short:"t" long:"token" env:"TOKEN" description:"DingTalk robot access token" required:"true"`
|
||||||
|
Secret string `short:"s" long:"secret" env:"SECRET" description:"DingTalk robot secret"`
|
||||||
|
AtMobiles []string `short:"m" long:"at-mobiles" env:"AT_MOBILES" env-delim:"," description:"The mobile of the person will be at"`
|
||||||
|
IsAtAll bool `short:"a" long:"at-all" env:"AT_ALL" description:"Whether at everyone"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseArg() {
|
||||||
|
parser := flags.NewParser(&Opts, flags.HelpFlag|flags.PassDoubleDash)
|
||||||
|
parser.NamespaceDelimiter = "-"
|
||||||
|
|
||||||
|
if AppName != "" {
|
||||||
|
parser.Name = AppName
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := parser.Parse(); err != nil {
|
||||||
|
if Opts.Version {
|
||||||
|
// -v
|
||||||
|
PrintVersion()
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
|
||||||
|
// -h
|
||||||
|
_, _ = fmt.Fprintln(os.Stdout, err)
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// err
|
||||||
|
_, _ = fmt.Fprintln(os.Stderr, err)
|
||||||
|
|
||||||
|
parser.WriteHelp(os.Stderr)
|
||||||
|
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
30
pkg/version.go
Normal file
30
pkg/version.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package pkg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
AppName string
|
||||||
|
Version string
|
||||||
|
BuildTime string
|
||||||
|
GitCommit string
|
||||||
|
GoVersion string
|
||||||
|
|
||||||
|
versionTpl = `%s
|
||||||
|
Name: %s
|
||||||
|
Version: %s
|
||||||
|
BuildTime: %s
|
||||||
|
GitCommit: %s
|
||||||
|
GoVersion: %s
|
||||||
|
|
||||||
|
`
|
||||||
|
bannerBase64 = "DQogX18gIF9fICBfX19fXyAgXyAgXyAgX19fXyAgX19fXyAgX19fX18gIF9fX18gDQooICBcLyAgKSggIF8gICkoIFwoICkoXyAgXykoXyAgXykoICBfICApKCAgXyBcDQogKSAgICAoICApKF8pKCAgKSAgKCAgXykoXyAgICkoICAgKShfKSggICkgICAvDQooXy9cL1xfKShfX19fXykoXylcXykoX19fXykgKF9fKSAoX19fX18pKF8pXF8pDQo="
|
||||||
|
)
|
||||||
|
|
||||||
|
// PrintVersion Print out version information
|
||||||
|
func PrintVersion() {
|
||||||
|
banner, _ := base64.StdEncoding.DecodeString(bannerBase64)
|
||||||
|
fmt.Printf(versionTpl, banner, AppName, Version, BuildTime, GitCommit, GoVersion)
|
||||||
|
}
|
Reference in New Issue
Block a user