-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
281 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package event | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
var ( | ||
ErrParseHeader = errors.New("parse header failed") | ||
ErrParsePayload = errors.New("parse payload failed") | ||
ErrPayloadLength = errors.New("invalid payload length") | ||
) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package event | ||
|
||
import "strconv" | ||
|
||
type Header struct { | ||
Ver string | ||
Server string | ||
Serial int | ||
Pool string | ||
PoolSerial int | ||
EventName string // 事件名称 | ||
Len int // Payload长度 | ||
} | ||
|
||
func ParseHeader(header string) (*Header, error) { | ||
h := &Header{} | ||
fields := parseFields(header) | ||
if len(fields) == 0 { | ||
return h, ErrParseHeader | ||
} | ||
|
||
h.Ver = fields["ver"] | ||
h.Server = fields["server"] | ||
h.Serial, _ = strconv.Atoi(fields["serial"]) | ||
h.Pool = fields["pool"] | ||
h.PoolSerial, _ = strconv.Atoi(fields["poolserial"]) | ||
h.EventName = fields["eventname"] | ||
h.Len, _ = strconv.Atoi(fields["len"]) | ||
|
||
return h, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package event | ||
|
||
import ( | ||
"bufio" | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/ouqiang/supervisor-event-listener/utils/errlog" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type Message struct { | ||
TS time.Time | ||
Header *Header | ||
Payload *Payload | ||
} | ||
|
||
func NewMessage(h *Header, p *Payload) Message { | ||
return Message{ | ||
TS: time.Now(), | ||
Header: h, | ||
Payload: p, | ||
} | ||
} | ||
|
||
func ReadMessage(reader *bufio.Reader) (Message, error) { | ||
header, err := readHeader(reader) | ||
if err != nil { | ||
errlog.Error("header:%+v err:%+v", header, err) | ||
return Message{}, err | ||
} | ||
payload, err := readPayload(reader, header.Len) | ||
if err != nil { | ||
errlog.Error("payload:%+v err:%+v", payload, err) | ||
return Message{}, err | ||
} | ||
return NewMessage(header, payload), nil | ||
} | ||
|
||
// 读取header | ||
func readHeader(reader *bufio.Reader) (*Header, error) { | ||
// 读取Header | ||
data, err := reader.ReadString('\n') | ||
if err != nil { | ||
return nil, err | ||
} | ||
// 解析Header | ||
header, err := ParseHeader(data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return header, nil | ||
} | ||
|
||
// 读取payload | ||
func readPayload(reader *bufio.Reader, payloadLen int) (*Payload, error) { | ||
// 读取payload | ||
buf := make([]byte, payloadLen) | ||
length, err := reader.Read(buf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if payloadLen != length { | ||
err := ErrPayloadLength | ||
err = errors.Wrapf(err, " payloadLen:%d != length:%d", payloadLen, length) | ||
return nil, err | ||
} | ||
// 解析payload | ||
payload, err := ParsePayload(string(buf)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return payload, nil | ||
} | ||
|
||
func (msg *Message) String() string { | ||
tmpl := `Proc: %s | ||
Host: %s | ||
State: %s | ||
PID: %d | ||
Date: %s` | ||
return fmt.Sprintf(tmpl, | ||
msg.Payload.ProcessName, | ||
msg.Payload.IP, | ||
msg.Payload.FromState, | ||
msg.Payload.PID, | ||
msg.TS.Format(time.RFC3339), | ||
) | ||
} | ||
|
||
func (msg *Message) ToJson(indent ...int) string { | ||
realIndent := 0 | ||
if len(indent) > 0 { | ||
realIndent = indent[0] | ||
} | ||
t := "" | ||
switch realIndent { | ||
case 0: | ||
case 1: | ||
t = " " | ||
case 2: | ||
t = " " | ||
case 3: | ||
t = " " | ||
case 4: | ||
t = " " | ||
default: | ||
t = " " | ||
} | ||
_bytes, _ := json.MarshalIndent(msg, "", t) | ||
return string(_bytes) | ||
} | ||
|
||
// Header Supervisord触发事件时会先发送Header,根据Header中len字段去读取Payload |
Oops, something went wrong.