monitor/main.go

142 lines
3.7 KiB
Go
Raw Normal View History

2019-11-15 10:05:28 +00:00
package main
import (
"encoding/base64"
"fmt"
"github.com/hpcloud/tail"
"github.com/jessevdk/go-flags"
2019-11-21 07:23:25 +00:00
"strings"
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
"os"
"regexp"
)
var (
AppName string
Version string
BuildTime string
GitCommit string
GoVersion string
versionTpl = `%s
Name: %s
Version: %s
BuildTime: %s
GitCommit: %s
GoVersion: %s
`
2019-11-19 08:35:59 +00:00
bannerBase64 = "DQogX18gIF9fICBfX19fXyAgXyAgXyAgX19fXyAgX19fXyAgX19fX18gIF9fX18gDQooICBcLyAgKSggIF8gICkoIFwoICkoXyAgXykoXyAgXykoICBfICApKCAgXyBcDQogKSAgICAoICApKF8pKCAgKSAgKCAgXykoXyAgICkoICAgKShfKSggICkgICAvDQooXy9cL1xfKShfX19fXykoXylcXykoX19fXykgKF9fKSAoX19fX18pKF8pXF8pDQo="
2019-11-15 10:05:28 +00:00
opts struct {
2019-11-22 07:17:41 +00:00
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"`
2019-11-21 08:52:42 +00:00
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" 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" `
2019-11-15 10:05:28 +00:00
}
)
2019-11-20 08:32:57 +00:00
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"`
2019-11-18 03:07:01 +00:00
}
2019-11-15 10:05:28 +00:00
func init() {
initLog()
}
func main() {
parseArg()
2019-11-20 08:32:57 +00:00
tf, err := tail.TailFile(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-22 07:17:41 +00:00
log.Info("monitor app <%s> file <%s>, filter by <%s>, ignore case <%v>...",
opts.AppName,
2019-11-21 08:52:42 +00:00
opts.File,
opts.KeyWord,
opts.KeyWordIgnoreCase)
2019-11-20 08:32:57 +00:00
dingTalk := dt.NewRobot(opts.Robot.Token, opts.Robot.Secret)
2019-11-15 10:05:28 +00:00
2019-11-21 08:52:42 +00:00
if opts.KeyWordIgnoreCase {
opts.KeyWord = strings.ToLower(opts.KeyWord)
}
2019-11-15 10:05:28 +00:00
for line := range tf.Lines {
2019-11-21 08:52:42 +00:00
text := line.Text
if opts.KeyWordIgnoreCase {
text = strings.ToLower(text)
}
2019-11-21 07:23:25 +00:00
if ok, _ := regexp.Match(opts.KeyWord, []byte(text)); ok {
2019-11-22 07:17:41 +00:00
if err = dingTalk.SendTextMessage(
// opts.AppName,
fmt.Sprintf("%s\n%s", opts.AppName, line.Text),
opts.Robot.AtMobiles,
opts.Robot.IsAtAll,
); err != nil {
2019-11-20 08:32:57 +00:00
log.Error("%+v", err)
continue
}
log.Info("send message <%s> success", line.Text)
2019-11-15 10:05:28 +00:00
}
}
}
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)
2019-11-20 08:32:57 +00:00
parser.NamespaceDelimiter = "-"
2019-11-15 10:05:28 +00:00
if AppName != "" {
parser.Name = AppName
}
if _, err := parser.Parse(); err != nil {
if opts.Version {
2019-11-20 08:32:57 +00:00
// -v
2019-11-15 10:05:28 +00:00
printVersion()
os.Exit(0)
}
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
2019-11-20 08:32:57 +00:00
// -h
2019-11-15 10:05:28 +00:00
_, _ = fmt.Fprintln(os.Stdout, err)
os.Exit(0)
}
2019-11-20 08:32:57 +00:00
// err
2019-11-15 10:05:28 +00:00
_, _ = 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)
}