Skip to content
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

fix the empty result when the value of an output field contains a dash/slash + trim prefix with the timestamp and priority for the output #136

Merged
merged 1 commit into from
Feb 23, 2024
Merged
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
2 changes: 2 additions & 0 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func AddEvent(c echo.Context) error {

models.GetOutputs().Update(payload.Outputs)

payload.Event.Output = utils.TrimPrefix(payload.Event.Output)

if err := events.Add(&payload.Event); err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions internal/database/redis/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func CreateIndex(client *redisearch.Client) {
AddField(redisearch.NewTextField("hostname")).
AddField(redisearch.NewTextField("source")).
AddField(redisearch.NewTextField("tags")).
AddField(redisearch.NewTextField("outputfields")).
AddField(redisearch.NewNumericField("timestamp")).
AddField(redisearch.NewTextField("json"))

Expand Down
7 changes: 7 additions & 0 deletions internal/database/redis/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ func SetKey(client *redisearch.Client, event *models.Event) error {

jsonString, _ := json.Marshal(event)

of := make([]string, 0, len(event.OutputFields))

for _, i := range event.OutputFields {
of = append(of, fmt.Sprintf("%v", i))
}

doc := redisearch.NewDocument(fmt.Sprintf("event:%v", event.UUID), 1.0).
Set("rule", event.Rule).
Set("priority", event.Priority).
Expand All @@ -38,6 +44,7 @@ func SetKey(client *redisearch.Client, event *models.Event) error {
Set("timestamp", event.Time.UnixNano()/1e3).
Set("tags", utils.Escape(strings.Join(event.Tags, ","))).
Set("json", string(jsonString)).
Set("outputfields", utils.Escape(strings.Join(of, ","))).
Set("uuid", event.UUID).
SetTTL(c.TTL)
if event.Hostname != "" {
Expand Down
16 changes: 13 additions & 3 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
const (
extractNumber = "^[0-9]+"
extractUnity = "[a-z-A-Z]+$"
trimPrefix = "(?i)^\\d{2}:\\d{2}:\\d{2}\\.\\d{9}\\:\\ (Debug|Info|Informational|Notice|Warning|Error|Critical|Alert|Emergency)"
)

const (
Expand All @@ -39,11 +40,12 @@ const (
fatalLog
)

var regExtractNumber, regExtractUnity *regexp.Regexp
var regExtractNumber, regExtractUnity, regTrimPrefix *regexp.Regexp

func init() {
regExtractNumber, _ = regexp.Compile(extractNumber)
regExtractUnity, _ = regexp.Compile(extractUnity)
regTrimPrefix, _ = regexp.Compile(trimPrefix)
}

func CheckErr(e error) {
Expand Down Expand Up @@ -164,9 +166,17 @@ func GetPriortiyInt(prio string) int {
}

func Escape(s string) string {
return strings.ReplaceAll(s, "-", `\-`)
s = strings.ReplaceAll(s, "-", `\-`)
s = strings.ReplaceAll(s, "/", "\\/")
return s
}

func UnEscape(s string) string {
return strings.ReplaceAll(s, `\-`, "-")
s = strings.ReplaceAll(s, `\-`, "-")
s = strings.ReplaceAll(s, `\\/`, "/")
return s
}

func TrimPrefix(s string) string {
return regTrimPrefix.ReplaceAllString(s, "")
}
Loading