91 lines
3 KiB
Go
91 lines
3 KiB
Go
package parser
|
|
|
|
import (
|
|
"log/slog"
|
|
"regexp"
|
|
"strings"
|
|
"watch-tool/helpers"
|
|
"watch-tool/models"
|
|
)
|
|
|
|
var (
|
|
tjmServicePattern = regexp.MustCompile(`^(?<time>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3})\s+(?<level>\S+)\s+(?<pid>\d+).*?\[(?<collatation_id>[^\]]*)\]\s+\[(?<username>[^\]]*)\]\s+\[(?<thread>[^\]]*)\]\s+(?<class>.*?)\s+:\s+(?<message>.*)`)
|
|
tjmTransferNamePattern = regexp.MustCompile(`^(\d{8}T\d{6}-[A-Za-z0-9]+-.+?-(?:in|out)) ?: (.*)$`)
|
|
tjmTransferIDPattern1 = regexp.MustCompile(`(?P<transfer>\w{8}-\w{4}-\w{4}-\w{4}-\w{12}).*?(?P<message>.*)`)
|
|
tjmTransferIDPattern2 = regexp.MustCompile(`(?P<before>.*)(?P<transfer>\w{8}-\w{4}-\w{4}-\w{4}-\w{12}).*?(?P<message>.*)`)
|
|
)
|
|
|
|
type TJMParser struct{}
|
|
|
|
func (t *TJMParser) Parse(line string) (models.LogMessage, error) {
|
|
newEntry := models.LogMessage{
|
|
Service: "transfer-job-manager",
|
|
}
|
|
syslogFields, logContent := helpers.ExtractSyslogHeader(line)
|
|
newEntry.Host = syslogFields.Hostname
|
|
|
|
msg := strings.TrimSpace(logContent)
|
|
msg = strings.ReplaceAll(msg, " ", " ")
|
|
msg = strings.ReplaceAll(msg, "---", "")
|
|
msg = strings.ReplaceAll(msg, " ", " ")
|
|
parts := strings.Fields(msg)
|
|
if len(parts) < 4 {
|
|
newEntry.LogMessage = logContent
|
|
return newEntry, nil
|
|
}
|
|
matches := tjmServicePattern.FindStringSubmatch(logContent)
|
|
var baseInfo models.TJMTransferInfo
|
|
if len(matches) > 0 {
|
|
timestampStr := strings.Join(strings.Split(matches[1], " "), "T")
|
|
timestamp, err := helpers.ParseRFC3339WithOptionalZ(timestampStr)
|
|
if err != nil {
|
|
slog.Error("unable to parse time", "error", err)
|
|
}
|
|
newEntry.Timestamp = timestamp
|
|
newEntry.LogLevel = strings.TrimSpace(matches[2])
|
|
newEntry.LogMessage = strings.TrimSpace(matches[8])
|
|
baseInfo = models.TJMTransferInfo{
|
|
ProcessID: strings.TrimSpace(matches[3]),
|
|
CorrelationID: strings.TrimSpace(matches[4]),
|
|
Username: strings.TrimSpace(matches[5]),
|
|
ThreadID: strings.TrimSpace(matches[6]),
|
|
JavaClass: strings.TrimSpace(matches[7]),
|
|
}
|
|
} else {
|
|
newEntry.LogMessage = logContent
|
|
}
|
|
trNameMatch := tjmTransferNamePattern.FindStringSubmatch(newEntry.LogMessage)
|
|
var transferName string
|
|
var transferID string
|
|
if len(trNameMatch) > 0 {
|
|
transferName = trNameMatch[1]
|
|
newEntry.LogMessage = trNameMatch[2]
|
|
if strings.Contains(trNameMatch[1], "-in") {
|
|
baseInfo.Direction = "incoming"
|
|
}
|
|
if strings.Contains(trNameMatch[1], "-out") {
|
|
baseInfo.Direction = "outgoing"
|
|
}
|
|
}
|
|
trIDMatch := tjmTransferIDPattern1.FindStringSubmatch(newEntry.LogMessage)
|
|
if len(trIDMatch) > 0 {
|
|
transferID = trIDMatch[1]
|
|
}
|
|
trIDMatch = tjmTransferIDPattern2.FindStringSubmatch(newEntry.LogMessage)
|
|
if len(trIDMatch) > 0 {
|
|
transferID = trIDMatch[2]
|
|
}
|
|
if transferID != "" {
|
|
baseInfo.TransferID = transferID
|
|
} else if transferName != "" {
|
|
baseInfo.TransferID = transferName
|
|
} else {
|
|
baseInfo.TransferID = "no_transfer_id"
|
|
}
|
|
if baseInfo.StartTime.IsZero() {
|
|
baseInfo.StartTime = newEntry.Timestamp
|
|
}
|
|
newEntry.ServiceInformation = baseInfo
|
|
|
|
return newEntry, nil
|
|
}
|