fix
This commit is contained in:
@@ -46,8 +46,20 @@ func parseSyslogPayload(payload []byte) ParsedSyslog {
|
||||
tokens := strings.SplitN(rest, " ", 3)
|
||||
if len(tokens) >= 2 {
|
||||
if len(tokens) >= 3 && isMonthAbbr(tokens[0]) {
|
||||
p.Hostname = tokens[2]
|
||||
if idx := strings.Index(rest, ": "); idx > 0 {
|
||||
parts := strings.Fields(rest)
|
||||
if len(parts) >= 4 && isDayOfMonth(parts[1]) && isHHMMSS(parts[2]) {
|
||||
p.Hostname = parts[3]
|
||||
if len(parts) > 4 {
|
||||
tagMsg := strings.Join(parts[4:], " ")
|
||||
if idx := strings.Index(tagMsg, ": "); idx > 0 {
|
||||
p.Tag = tagMsg[:idx]
|
||||
p.Message = strings.TrimSpace(tagMsg[idx+2:])
|
||||
} else {
|
||||
p.Message = tagMsg
|
||||
}
|
||||
}
|
||||
} else if idx := strings.Index(rest, ": "); idx > 0 {
|
||||
// 兼容无法严格按 RFC3164 切分的历史格式。
|
||||
p.Message = strings.TrimSpace(rest[idx+2:])
|
||||
}
|
||||
} else {
|
||||
@@ -66,6 +78,28 @@ func parseSyslogPayload(payload []byte) ParsedSyslog {
|
||||
return p
|
||||
}
|
||||
|
||||
func isDayOfMonth(s string) bool {
|
||||
n, err := strconv.Atoi(s)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return n >= 1 && n <= 31
|
||||
}
|
||||
|
||||
func isHHMMSS(s string) bool {
|
||||
parts := strings.Split(s, ":")
|
||||
if len(parts) != 3 {
|
||||
return false
|
||||
}
|
||||
h, err1 := strconv.Atoi(parts[0])
|
||||
m, err2 := strconv.Atoi(parts[1])
|
||||
sec, err3 := strconv.Atoi(parts[2])
|
||||
if err1 != nil || err2 != nil || err3 != nil {
|
||||
return false
|
||||
}
|
||||
return h >= 0 && h <= 23 && m >= 0 && m <= 59 && sec >= 0 && sec <= 59
|
||||
}
|
||||
|
||||
func isMonthAbbr(s string) bool {
|
||||
if len(s) < 3 {
|
||||
return false
|
||||
|
||||
Reference in New Issue
Block a user