monitor/main.go

142 lines
3.0 KiB
Go
Raw Normal View History

2019-11-15 10:05:28 +00:00
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/hpcloud/tail"
"github.com/jessevdk/go-flags"
"io/ioutil"
log "unknwon.dev/clog/v2"
"net/http"
"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-18 03:07:01 +00:00
Version bool `short:"v" long:"version" description:"Show version info"`
Monitor monitor `group:"MONITOR" env-namespace:"MONITOR"`
2019-11-15 10:05:28 +00:00
}
)
2019-11-18 03:07:01 +00:00
type monitor struct {
File string `short:"f" long:"file" env:"FILE" description:"The file to be monitored" required:"true"`
KeyWord string `short:"k" long:"key-word" env:"KEY_WORD" description:"Key word to be filter" required:"true"`
WebHookUrl string `short:"u" long:"dt-wh-url" env:"DT_WH_URL" description:"Webhook url of dingtalk" required:"true"`
}
2019-11-15 10:05:28 +00:00
func init() {
initLog()
}
func main() {
parseArg()
2019-11-18 03:07:01 +00:00
tf, err := tail.TailFile(opts.Monitor.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-18 03:07:01 +00:00
log.Info("monitor file %s...", opts.Monitor.File)
2019-11-15 10:05:28 +00:00
for line := range tf.Lines {
2019-11-18 03:07:01 +00:00
if ok, _ := regexp.Match(opts.Monitor.KeyWord, []byte(line.Text)); ok {
2019-11-15 10:05:28 +00:00
log.Info("%s", dingToInfo(line.Text))
}
}
}
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)
if AppName != "" {
parser.Name = AppName
}
if _, err := parser.Parse(); err != nil {
if opts.Version {
printVersion()
os.Exit(0)
}
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
_, _ = fmt.Fprintln(os.Stdout, err)
os.Exit(0)
}
_, _ = fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr)
os.Exit(1)
}
}
func dingToInfo(msg string) []byte {
content, data := make(map[string]string), make(map[string]interface{})
content["content"] = msg
data["msgtype"] = "text"
data["text"] = content
b, _ := json.Marshal(data)
log.Info("send to %s data <%s>",
2019-11-18 03:07:01 +00:00
opts.Monitor.WebHookUrl,
2019-11-15 10:05:28 +00:00
b)
2019-11-18 03:07:01 +00:00
resp, err := http.Post(opts.Monitor.WebHookUrl,
2019-11-15 10:05:28 +00:00
"application/json",
bytes.NewBuffer(b))
if err != nil {
log.Error("send request to %s %+v",
2019-11-18 03:07:01 +00:00
opts.Monitor.WebHookUrl,
2019-11-15 10:05:28 +00:00
err)
}
2019-11-15 10:22:14 +00:00
defer func() {
_ = resp.Body.Close()
}()
2019-11-15 10:05:28 +00:00
body, _ := ioutil.ReadAll(resp.Body)
log.Info("send to %s data <%s> result is %s",
2019-11-18 03:07:01 +00:00
opts.Monitor.WebHookUrl,
2019-11-15 10:05:28 +00:00
b,
body)
return body
}
// printVersion Print out version information
func printVersion() {
banner, _ := base64.StdEncoding.DecodeString(bannerBase64)
fmt.Printf(versionTpl, banner, AppName, Version, BuildTime, GitCommit, GoVersion)
}