This commit is contained in:
ehlxr 2019-11-18 11:07:01 +08:00
parent b4365edd1b
commit e3587d6025
2 changed files with 23 additions and 18 deletions

View File

@ -13,11 +13,11 @@ LD_GO_VERSION := -X '$(VERSION_PATH).GoVersion=`go version`'
LD_VERSION := -X '$(VERSION_PATH).Version=$(BUILD_VERSION)' LD_VERSION := -X '$(VERSION_PATH).Version=$(BUILD_VERSION)'
LD_FLAGS := "$(LD_APP_NAMW) $(LD_GIT_COMMIT) $(LD_BUILD_TIME) $(LD_GO_VERSION) $(LD_VERSION) -w -s" LD_FLAGS := "$(LD_APP_NAMW) $(LD_GIT_COMMIT) $(LD_BUILD_TIME) $(LD_GO_VERSION) $(LD_VERSION) -w -s"
RELEASE_VERSION = $(version) DOCKER_TAG := $(tag)
REGISTRY_URL = $(url) REGISTRY_URL := $(url)
ifeq ("$(RELEASE_VERSION)","") ifeq ("$(DOCKER_TAG)","")
RELEASE_VERSION := $(shell echo `date "+%Y%m%d_%H%M%S"`) DOCKER_TAG := $(shell echo `date "+%Y%m%d_%H%M%S"`)
endif endif
.PHONY : build release clean install upx docker-push docker .PHONY : build release clean install upx docker-push docker
@ -33,15 +33,15 @@ endif
docker: build upx docker: build upx
ifneq ("$(REGISTRY_URL)","") ifneq ("$(REGISTRY_URL)","")
@echo ========== current docker tag is: $(RELEASE_VERSION) ========== @echo ========== current docker tag is: $(DOCKER_TAG) ==========
docker build -t $(REGISTRY_URL)/monitor_server:$(RELEASE_VERSION) -f Dockerfile . docker build -t $(REGISTRY_URL)/monitor_server:$(DOCKER_TAG) -f Dockerfile .
else else
@echo "url arg should not be empty" @echo "url arg should not be empty"
endif endif
docker-push: docker docker-push: docker
docker push $(REGISTRY_URL)/monitor_server:$(RELEASE_VERSION) docker push $(REGISTRY_URL)/monitor_server:$(DOCKER_TAG)
clean: clean:
rm -rf $(DIST_DIR)* rm -rf $(DIST_DIR)*

27
main.go
View File

@ -33,13 +33,17 @@ GoVersion: %s
bannerBase64 = "DQogX19fXyAgX19fXyAgICBfX18gIF9fX19fIA0KKCAgXyBcKCAgXyBcICAvIF9fKSggIF8gICkNCiApKF8pICkpKF8pICkoIChfLS4gKShfKSggDQooX19fXy8oX19fXy8gIFxfX18vKF9fX19fKQ0K" bannerBase64 = "DQogX19fXyAgX19fXyAgICBfX18gIF9fX19fIA0KKCAgXyBcKCAgXyBcICAvIF9fKSggIF8gICkNCiApKF8pICkpKF8pICkoIChfLS4gKShfKSggDQooX19fXy8oX19fXy8gIFxfX18vKF9fX19fKQ0K"
opts struct { opts struct {
MonitorFile string `short:"f" long:"monitor-file" env:"MONITOR_FILE" description:"The file to be monitored" required:"true"` Version bool `short:"v" long:"version" description:"Show version info"`
KeyWord string `short:"k" long:"key-word" env:"KEY_WORD" description:"Key word to be filter" required:"true"` Monitor monitor `group:"MONITOR" env-namespace:"MONITOR"`
WebHookUrl string `short:"u" long:"webhook-url" env:"URL" description:"Webhook url of dingtalk" required:"true"`
Version bool `short:"v" long:"version" description:"Show version info"`
} }
) )
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"`
}
func init() { func init() {
initLog() initLog()
} }
@ -47,18 +51,19 @@ func init() {
func main() { func main() {
parseArg() parseArg()
tf, err := tail.TailFile(opts.MonitorFile, tf, err := tail.TailFile(opts.Monitor.File,
tail.Config{ tail.Config{
ReOpen: true,
Follow: true, Follow: true,
Location: &tail.SeekInfo{Offset: 0, Whence: 2}, Location: &tail.SeekInfo{Offset: 0, Whence: 2},
}) })
if err != nil { if err != nil {
log.Fatal("Tail file %+v", err) log.Fatal("Tail file %+v", err)
} }
log.Info("monitor file %s...", opts.MonitorFile) log.Info("monitor file %s...", opts.Monitor.File)
for line := range tf.Lines { for line := range tf.Lines {
if ok, _ := regexp.Match(opts.KeyWord, []byte(line.Text)); ok { if ok, _ := regexp.Match(opts.Monitor.KeyWord, []byte(line.Text)); ok {
log.Info("%s", dingToInfo(line.Text)) log.Info("%s", dingToInfo(line.Text))
} }
} }
@ -105,15 +110,15 @@ func dingToInfo(msg string) []byte {
b, _ := json.Marshal(data) b, _ := json.Marshal(data)
log.Info("send to %s data <%s>", log.Info("send to %s data <%s>",
opts.WebHookUrl, opts.Monitor.WebHookUrl,
b) b)
resp, err := http.Post(opts.WebHookUrl, resp, err := http.Post(opts.Monitor.WebHookUrl,
"application/json", "application/json",
bytes.NewBuffer(b)) bytes.NewBuffer(b))
if err != nil { if err != nil {
log.Error("send request to %s %+v", log.Error("send request to %s %+v",
opts.WebHookUrl, opts.Monitor.WebHookUrl,
err) err)
} }
@ -123,7 +128,7 @@ func dingToInfo(msg string) []byte {
body, _ := ioutil.ReadAll(resp.Body) body, _ := ioutil.ReadAll(resp.Body)
log.Info("send to %s data <%s> result is %s", log.Info("send to %s data <%s> result is %s",
opts.WebHookUrl, opts.Monitor.WebHookUrl,
b, b,
body) body)
return body return body