Skip to content

Commit 1211eee

Browse files
committed
改用 go mod 构建、支持 bearychat
1 parent 031edd7 commit 1211eee

14 files changed

+221
-33
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Binaries for programs and plugins
2+
./supervisor-event-listener
23
*.exe
34
*.dll
45
*.so
@@ -13,3 +14,5 @@
1314
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
1415
.glide/
1516
.idear
17+
vendor/**/*.yml
18+
vendor/**/.gitignore

Makefile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
install:
3+
sudo supervisorctl stop alert
4+
go build
5+
sudo cp ./supervisor-event-listener /usr/local/bin/
6+
sudo supervisorctl start alert
7+
8+
test:
9+
sudo supervisorctl start test
10+

config/config.go

+33-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package config
33
import (
44
"flag"
55
"fmt"
6-
"github.com/ouqiang/supervisor-event-listener/utils"
7-
"gopkg.in/ini.v1"
86
"os"
97
"strings"
8+
9+
"github.com/ouqiang/supervisor-event-listener/utils"
10+
"github.com/ouqiang/supervisor-event-listener/utils/tmpfslog"
11+
"gopkg.in/ini.v1"
1012
)
1113

1214
type Config struct {
@@ -15,6 +17,7 @@ type Config struct {
1517
MailServer MailServer
1618
MailUser MailUser
1719
Slack Slack
20+
BearyChat BearyChat
1821
}
1922

2023
type WebHook struct {
@@ -26,6 +29,12 @@ type Slack struct {
2629
Channel string
2730
}
2831

32+
type BearyChat struct {
33+
WebHookUrl string
34+
Channel string
35+
Timeout int
36+
}
37+
2938
// 邮件服务器
3039
type MailServer struct {
3140
User string
@@ -54,12 +63,14 @@ func ParseConfig() *Config {
5463
section := file.Section("default")
5564
notifyType := section.Key("notify_type").String()
5665
notifyType = strings.TrimSpace(notifyType)
57-
if !utils.InStringSlice([]string{"mail", "slack", "webhook"}, notifyType) {
66+
if !utils.InStringSlice([]string{"mail", "slack", "webhook", "bearychat"}, notifyType) {
5867
Exit("不支持的通知类型-" + notifyType)
5968
}
6069

6170
config := &Config{}
6271
config.NotifyType = notifyType
72+
73+
tmpfslog.Info("notifyType: %+v\n", config.NotifyType)
6374
switch notifyType {
6475
case "mail":
6576
config.MailServer = parseMailServer(section)
@@ -68,8 +79,9 @@ func ParseConfig() *Config {
6879
config.Slack = parseSlack(section)
6980
case "webhook":
7081
config.WebHook = parseWebHook(section)
82+
case "bearychat":
83+
config.BearyChat = parseBearyChat(section)
7184
}
72-
7385
return config
7486
}
7587

@@ -134,6 +146,23 @@ func parseWebHook(section *ini.Section) WebHook {
134146
return webHook
135147
}
136148

149+
func parseBearyChat(section *ini.Section) BearyChat {
150+
url := section.Key("bearychat.webhook_url").String()
151+
if url == "" {
152+
Exit("WebHookUrl配置错误")
153+
}
154+
timeout, err := section.Key("bearychat.timeout").Int()
155+
channel := section.Key("bearychat.channel").String()
156+
if err != nil {
157+
Exit(err.Error())
158+
}
159+
return BearyChat{
160+
WebHookUrl: strings.TrimSpace(url),
161+
Channel: strings.TrimSpace(channel),
162+
Timeout: timeout,
163+
}
164+
}
165+
137166
func Exit(msg string) {
138167
fmt.Fprintln(os.Stderr, msg)
139168
os.Exit(1)

go.mod

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module github.com/ouqiang/supervisor-event-listener
2+
3+
require (
4+
github.com/go-gomail/gomail v0.0.0-20160411212932-81ebce5c23df
5+
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a // indirect
6+
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
7+
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df // indirect
8+
gopkg.in/ini.v1 v1.28.0
9+
)

go.sum

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
github.com/go-gomail/gomail v0.0.0-20160411212932-81ebce5c23df h1:Bao6dhmbTA1KFVxmJ6nBoMuOJit2yjEgLJpIMYpop0E=
2+
github.com/go-gomail/gomail v0.0.0-20160411212932-81ebce5c23df/go.mod h1:GJr+FCSXshIwgHBtLglIg9M2l2kQSi6QjVAngtzI08Y=
3+
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
4+
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
5+
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
6+
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
7+
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
8+
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
9+
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a h1:pa8hGb/2YqsZKovtsgrwcDH1RZhVbTKCjLp47XpqCDs=
10+
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
11+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
12+
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
13+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
14+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
15+
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
16+
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk=
17+
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk=
18+
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE=
19+
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw=
20+
gopkg.in/ini.v1 v1.28.0 h1:Bp9VlwLuKFwZwag5eRqc0KdIfk3I/iJfkxh4qb/rsS4=
21+
gopkg.in/ini.v1 v1.28.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=

listener/listener.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"github.com/ouqiang/supervisor-event-listener/event"
88
"github.com/ouqiang/supervisor-event-listener/listener/notify"
9+
"github.com/ouqiang/supervisor-event-listener/utils/tmpfslog"
910
"log"
1011
"os"
1112
)
@@ -29,19 +30,19 @@ func listen() {
2930
for {
3031
ready()
3132
header, err := readHeader(reader)
33+
tmpfslog.Info("header:%+v err:%+v", header, err)
3234
if err != nil {
3335
failure(err)
3436
continue
3537
}
3638
payload, err := readPayload(reader, header.Len)
39+
tmpfslog.Info("payloadL%+v err:%+v", payload, err)
3740
if err != nil {
3841
failure(err)
3942
continue
4043
}
4144
// 只处理进程异常退出事件
42-
if header.EventName == "PROCESS_STATE_EXITED" {
43-
notify.Push(header, payload)
44-
}
45+
notify.Push(header, payload)
4546
success()
4647
}
4748
}

listener/notify/bearychat.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package notify
2+
3+
import (
4+
"encoding/json"
5+
6+
"github.com/ouqiang/supervisor-event-listener/event"
7+
"github.com/ouqiang/supervisor-event-listener/utils/httpclient"
8+
"github.com/ouqiang/supervisor-event-listener/utils/tmpfslog"
9+
)
10+
11+
type BearyChat struct{}
12+
13+
func (this *BearyChat) Send(msg event.Message) error {
14+
url := Conf.BearyChat.WebHookUrl
15+
channel := Conf.BearyChat.Channel
16+
timeout := Conf.BearyChat.Timeout
17+
18+
params := map[string]interface{}{
19+
"text": msg.String(),
20+
}
21+
if channel != "" {
22+
params["channel"] = channel
23+
}
24+
25+
body, err := json.Marshal(params)
26+
if err != nil {
27+
return err
28+
}
29+
tmpfslog.Info("url: %s", url)
30+
tmpfslog.Info("timeout: %d", timeout)
31+
tmpfslog.Info("params: %v", params)
32+
33+
resp := httpclient.PostJson(url, string(body), timeout)
34+
if !resp.IsOK() {
35+
return resp.Error()
36+
}
37+
return nil
38+
}
39+
40+
func (this *BearyChat) format(msg event.Message) string {
41+
return ""
42+
}

listener/notify/notify.go

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package notify
33
import (
44
"github.com/ouqiang/supervisor-event-listener/config"
55
"github.com/ouqiang/supervisor-event-listener/event"
6+
"github.com/ouqiang/supervisor-event-listener/utils/tmpfslog"
67

78
"fmt"
89
"os"
@@ -16,6 +17,8 @@ var (
1617

1718
func init() {
1819
Conf = config.ParseConfig()
20+
21+
tmpfslog.Info("loading config: %+v\n", Conf)
1922
queue = make(chan event.Message, 10)
2023
go start()
2124
}
@@ -33,13 +36,16 @@ func start() {
3336
var notifyHandler Notifiable
3437
for {
3538
message = <-queue
39+
tmpfslog.Info("%+v\n", message)
3640
switch Conf.NotifyType {
3741
case "mail":
3842
notifyHandler = &Mail{}
3943
case "slack":
4044
notifyHandler = &Slack{}
4145
case "webhook":
4246
notifyHandler = &WebHook{}
47+
case "bearychat":
48+
notifyHandler = &BearyChat{}
4349
}
4450
if notifyHandler == nil {
4551
continue

supervisor-event-listener.ini

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,9 @@ slack.webhook_url = https://hooks.slack.com/services/xxxx/xxx/xxxx
1616
slack.channel = exception
1717

1818
# WebHook通知URL配置
19-
webhook_url = http://my.webhook.com
19+
webhook_url = http://my.webhook.com
20+
21+
# BearyChat
22+
bearychat.webhook_url = https://xx/xx
23+
bearychat.channel =
24+
bearychat.timeout = 8

supervisor-event-listener.toml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[default]
2+
# 通知类型 mail,slack,webhook 只能选择一种
3+
notify_type = "mail"
4+
5+
# 邮件服务器配置
6+
[mail]
7+
8+
9+
[mail.server]
10+
11+
password = "123456"
12+
host = "smtp.163.com"
13+
port = 25
14+
15+
16+
# Slack配置
17+
[slack]
18+
webhook_url = "https://hooks.slack.com/services/xxxx/xxx/xxxx"
19+
channel = "exception"
20+
21+
# Custom WebHook
22+
[webhook]
23+
url = "http://my.webhook.com"
24+
25+
26+
# BearyChat
27+
[bearychat]
28+
webhook_url = "https://hook.bearychat.com/=xxx/xxxxx/xxxx"
29+
channel = "alert"
30+
timeout = 8

utils/httpclient/http_client.go

+8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ type ResponseWrapper struct {
1616
Header http.Header
1717
}
1818

19+
func (this *ResponseWrapper) IsOK() bool {
20+
return this.StatusCode == 200
21+
}
22+
23+
func (this *ResponseWrapper) Error() error {
24+
return fmt.Errorf(this.Body)
25+
}
26+
1927
func Get(url string, timeout int) ResponseWrapper {
2028
req, err := http.NewRequest("GET", url, nil)
2129
if err != nil {

utils/tmpfslog/tmpfslog.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package tmpfslog
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"time"
7+
)
8+
9+
var f *os.File
10+
11+
func init() {
12+
fpath := "/tmp/supervisor-event-listener.log"
13+
// mode := os.O_CREATE | os.O_RDWR
14+
mode := os.O_APPEND | os.O_RDWR
15+
file, err := os.OpenFile(fpath, mode, 0644)
16+
if err != nil {
17+
panic(err)
18+
}
19+
f = file
20+
}
21+
22+
func log(level int, _fmt string, args ...interface{}) {
23+
now := time.Now()
24+
f.WriteString(now.String())
25+
f.WriteString(fmt.Sprintf(_fmt, args...))
26+
f.WriteString("\n")
27+
}
28+
29+
func Debug(fmt string, args ...interface{}) {
30+
log(3, fmt, args...)
31+
}
32+
33+
func Info(fmt string, args ...interface{}) {
34+
log(3, fmt, args...)
35+
}
36+
37+
func Error(fmt string, args ...interface{}) {
38+
log(3, fmt, args...)
39+
}
40+
41+
func Fatal(fmt string, args ...interface{}) {
42+
log(3, fmt, args...)
43+
}

vendor/modules.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# github.com/go-gomail/gomail v0.0.0-20160411212932-81ebce5c23df
2+
github.com/go-gomail/gomail
3+
# gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc
4+
gopkg.in/alexcesaro/quotedprintable.v3
5+
# gopkg.in/ini.v1 v1.28.0
6+
gopkg.in/ini.v1

vendor/vendor.json

-25
This file was deleted.

0 commit comments

Comments
 (0)