From fb0be1cc63dca5c9ea1106a659df7506dff01bf8 Mon Sep 17 00:00:00 2001 From: cupen Date: Thu, 9 Sep 2021 16:29:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=E9=A1=B9=E7=9B=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 7 +- .vscode/launch.json | 17 --- Makefile | 36 ++--- README.md | 136 ++++++++---------- .../config.toml | 4 - supervisor-event-listener.go => main.go | 0 6 files changed, 78 insertions(+), 122 deletions(-) delete mode 100644 .vscode/launch.json rename supervisor-event-listener.toml => conf/config.toml (99%) rename supervisor-event-listener.go => main.go (100%) diff --git a/.gitignore b/.gitignore index d2f8b58..f38a348 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ # Binaries for programs and plugins -supervisor-event-listener +supervisor-eventlistener *.exe *.dll *.so @@ -12,8 +12,7 @@ supervisor-event-listener *.out # Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 -.glide/ -.idear +.*/ vendor/**/*.yml vendor/**/.gitignore -build/ \ No newline at end of file +build/ diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index c23774c..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "name": "Launch", - "type": "go", - "request": "launch", - "mode": "auto", - "program": "${fileDirname}", - "env": {}, - "args": [] - } - ] -} \ No newline at end of file diff --git a/Makefile b/Makefile index 9234664..6d27091 100644 --- a/Makefile +++ b/Makefile @@ -1,35 +1,33 @@ -project_name:=supervisor-event-listener -project_version:=1.2.2 +project_name:=supervisor-eventlistener +project_version:=1.2.3 root_dir := $(abspath $(CURDIR)) build_dir := $(root_dir)/build GOPATH := ${HOME}/go -.PHONY: clean clean: rm -fr $(build_dir) -.PHONY: build-bydocker + build-bydocker: sudo docker run -it --rm \ -v $(GOPATH)/:/root/go/ \ -v $(root_dir)/:/$(project_name) \ -w /$(project_name)/ \ - golang:1.16.2-buster \ + golang:1.17.0-buster \ make build -.PHONY: build build: - GO111MODULE=on go build -o $(project_name) ./$(project_name).go + GO111MODULE=on go build -o $(project_name) ./main.go -.PHONY: release release: clean build-bydocker - mkdir -p $(build_dir)/$(project_name)/ - mv ./supervisor-event-listener $(build_dir)/$(project_name)/ - cp ./supervisor-event-listener.toml $(build_dir)/$(project_name)/ + mkdir -p $(build_dir)/$(project_name)/ + mv ./supervisor-eventlistener $(build_dir)/$(project_name)/ + cp ./conf/config.toml $(build_dir)/$(project_name)/ cd $(build_dir) && tar -zcvf $(project_name)-$(project_version).tar.gz $(project_name) + @echo ...created $(build_dir)/$(project_name)-$(project_version).tar.gz @echo ...done. @@ -38,8 +36,8 @@ log: tmux split-window -t "dev:0" tmux split-window -t "dev:0.0" -h tmux split-window -t "dev:0.2" -h - tmux send-keys -t "dev:0.0" "bash -c 'tail -f /tmp/supervisor-event-listener.log'" Enter - tmux send-keys -t "dev:0.1" "bash -c 'sudo supervisorctl tail -f supervisor-event-listener'" Enter + tmux send-keys -t "dev:0.0" "bash -c 'tail -f /tmp/supervisor-eventlistener.log'" Enter + tmux send-keys -t "dev:0.1" "bash -c 'sudo supervisorctl tail -f $(project_name)'" Enter tmux set-option -g mouse on tmux attach -t dev tmux kill-session -t dev @@ -47,10 +45,12 @@ log: test-integration: go build - sudo supervisorctl stop supervisor-event-listener - sudo cp ./supervisor-event-listener /usr/local/bin/ + sudo supervisorctl stop $(project_name) + sudo cp ./$(project_name) /usr/local/bin/ sudo cp ./tests/supervisor-app.ini /etc/supervisor.d/ - sudo supervisorctl remove supervisor-event-listener - sudo supervisorctl update supervisor-event-listener - sudo supervisorctl tail -f supervisor-event-listener stderr + sudo supervisorctl remove $(project_name) + sudo supervisorctl update $(project_name) + sudo supervisorctl tail -f $(project_name) stderr + +.PHONY: clean build build-bydocker release log test-integration diff --git a/README.md b/README.md index 317d478..2fa6492 100644 --- a/README.md +++ b/README.md @@ -1,79 +1,57 @@ -# supervisor-event-listener -Supervisor事件通知, 支持邮件, Slack, WebHook - -## 简介 -Supervisor是*nix环境下的进程管理工具, 可以把前台进程转换为守护进程, 当进程异常退出时自动重启. -supervisor-event-listener监听进程异常退出事件, 并发送通知. - -## 下载 -[v1.0](https://github.com/ouqiang/supervisor-event-listener/releases) - -### 源码安装 -* `go get -u github.com/ouqiang/supervisor-event-listener` - -## Supervisor配置 -```ini -[eventlistener:supervisor-event-listener] -; 默认读取配置文件/etc/supervisor-event-listener.ini -command=/path/to/supervisor-event-listener -; 指定配置文件路径 -;command=/path/to/supervisor-event-listener -c /path/to/supervisor-event-listener.ini -events=PROCESS_STATE_EXITED -...... -``` - -## 配置文件, 默认读取`/etc/supervisor-event-listener.ini` - -```ini -[default] -# 通知类型 mail,slack,webhook 只能选择一种 -notify_type = mail - -# 邮件服务器配置 -mail.server.user = test@163.com -mail.server.password = 123456 -mail.server.host = smtp.163.com -mail.server.port = 25 - -# 邮件收件人配置, 多个收件人, 逗号分隔 -mail.user = hello@163.com - -# Slack配置 -slack.webhook_url = https://hooks.slack.com/services/xxxx/xxx/xxxx -slack.channel = exception - -# WebHook通知URL配置 -webhook_url = http://my.webhook.com - -``` - -## 通知内容 -邮件、Slack -```shell -Process: process-name -Host: ip(hostname) -EXITED FROM state: RUNNING -PID: 6152 -``` -WebHook, Post请求, 字段含义查看Supervisor文档 -```json -{ - "Header": { - "Ver": "3.0", - "Server": "supervisor", - "Serial": 11, - "Pool": "supervisor-listener", - "PoolSerial": 11, - "EventName": "PROCESS_STATE_EXITED", - "Len": 84 - }, - "Payload": { - "Ip": "ip(hostname)", - "ProcessName": "process-name", - "GroupName": "group-name", - "FromState": "RUNNING", - "Expected": 0, - "Pid": 6371 - } -} -``` +# Introduction +A `eventlistener` for supervisor, it may listen and redirect events to e-mail, webhook, slack and so on. +More details http://supervisord.org/events.html + +# Features +* support e-mail, webhook, slack +* support bearychat, lack(feishu) + +# Usage + +1. setup supervisor-eventlistener + ```toml + [mail] + receivers = ["hello@163.com", "world@163.com"] + server_user = "test@163.com" + server_password = "123456" + server_host = "smtp.163.com" + server_port = 25 + + [slack] + url = "https://hooks.slack.com/services/xxxx/xxx/xxxx" + channel = "exception" + timeout = 6 + + [webhook] + url = "http://my.webhook.com" + timeout = 6 + + [bearychat] + url = "https://hook.bearychat.com/xxx/xxxx" + channel = "alert" + timeout = 6 + + [feishu] + url = "https://hook.feishu.com/xxx/xxxx?signKey=it_is_optional" + timeout = 6 + ``` + +2. setup supervisor + ```ini + [eventlistener:supervisor-event-listener] + command=/usr/local/bin/supervisor-event-listener -c /etc/supervisor-event-listener.toml + user=nobody + group=nobody + events= + PROCESS_STATE_EXITED, + PROCESS_STATE_FATAL, + PROCESS_STATE_STOPPED, + PROCESS_STATE_RUNNING + ``` + +3. start supervisor-eventlistener + ```bash + supervisorctl start supervisor-event-listener + ``` + +That's all. \ No newline at end of file diff --git a/supervisor-event-listener.toml b/conf/config.toml similarity index 99% rename from supervisor-event-listener.toml rename to conf/config.toml index cf4984e..9e7ec34 100644 --- a/supervisor-event-listener.toml +++ b/conf/config.toml @@ -5,24 +5,20 @@ server_password = "123456" server_host = "smtp.163.com" server_port = 25 - [slack] url = "https://hooks.slack.com/services/xxxx/xxx/xxxx" channel = "exception" timeout = 6 - [webhook] url = "http://my.webhook.com" timeout = 6 - [bearychat] url = "https://hook.bearychat.com/xxx/xxxx" channel = "alert" timeout = 6 - [feishu] url = "https://hook.feishu.com/xxx/xxxx?signKey=it_is_optional" timeout = 6 \ No newline at end of file diff --git a/supervisor-event-listener.go b/main.go similarity index 100% rename from supervisor-event-listener.go rename to main.go