Skip to content

重构了一下,改得有点多。 #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Binaries for programs and plugins
supervisor-eventlistener
*.exe
*.dll
*.so
Expand All @@ -11,5 +12,7 @@
*.out

# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/
.idear
.*/
vendor/**/*.yml
vendor/**/.gitignore
build/
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 qiang.ou
Copyright (c) 2017-2021 qiang.ou

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
58 changes: 58 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
project_name:=supervisor-eventlistener
project_version:=1.2.3
root_dir := $(abspath $(CURDIR))
build_dir := $(root_dir)/build
GOPATH := ${HOME}/go

all: clean release


clean:
rm -fr $(build_dir)


build-bydocker:
sudo docker run -it --rm \
-v $(GOPATH)/:/root/go/ \
-v $(root_dir)/:/$(project_name) \
-w /$(project_name)/ \
golang:1.17.0-buster \
make build


build:
GO111MODULE=on go build -o $(project_name) ./main.go


release: clean build-bydocker
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.


log:
tmux new-session -d -s dev
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-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


test-integration:
go build
sudo supervisorctl stop $(project_name)
sudo cp ./$(project_name) /usr/local/bin/
sudo cp ./tests/supervisor-app.ini /etc/supervisor.d/
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
136 changes: 57 additions & 79 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 = [email protected]
mail.server.password = 123456
mail.server.host = smtp.163.com
mail.server.port = 25

# 邮件收件人配置, 多个收件人, 逗号分隔
mail.user = [email protected]

# 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
Host: ip(hostname)
Process: process-name
PID: 6152
EXITED FROM state: RUNNING
```
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 = ["[email protected]", "[email protected]"]
server_user = "[email protected]"
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.
96 changes: 0 additions & 96 deletions build.sh

This file was deleted.

56 changes: 56 additions & 0 deletions conf/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package conf

import (
"fmt"
"sync/atomic"

validator "github.com/go-playground/validator/v10"
)

var (
// c = &Config{}
cfg atomic.Value
)

type Config struct {
DryRun *DryRun
WebHook *WebHook
Mail *Mail
Slack *Slack
BearyChat *BearyChat
Feishu *Feishu
}

func (c *Config) Validate() error {
if c == nil {
return fmt.Errorf("nil config")
}

if c.BearyChat != nil {
if c.BearyChat.URL == "" {
return fmt.Errorf("Invalid bearcychat config")
}
}
if c.Mail != nil {
if len(c.Mail.Receivers) <= 0 {
return fmt.Errorf("Invalid mail config")
}
}
if c.Slack != nil {
if c.Slack.URL == "" {
return fmt.Errorf("Invalid slack config")
}
}

if c.WebHook != nil {
if c.WebHook.URL == "" {
return fmt.Errorf("Invalid slack config")
}
}

validate := validator.New()
if err := validate.Struct(c); err != nil {
return err
}
return nil
}
Loading