From 80add26f3caa826dd6316a8bd6659e809bf0096b Mon Sep 17 00:00:00 2001 From: Kavashen Pather Date: Fri, 10 Jan 2020 14:50:56 +0800 Subject: [PATCH] feat: allow split output to work vertically (#54) --- internal/inputs/commands.go | 51 +++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/internal/inputs/commands.go b/internal/inputs/commands.go index 1a646fc1..e0c2fd4f 100644 --- a/internal/inputs/commands.go +++ b/internal/inputs/commands.go @@ -150,6 +150,9 @@ func splitOutput(dataStore *[]interface{}, output string, command load.Command, for i, line := range lines { if formatter.KvFinder("regex", line, command.SplitOutput) { if startSplit == -1 { + startSplit = 0 + endSplit = i + outputBlocks = append(outputBlocks, lines[startSplit:endSplit]) startSplit = i } else { endSplit = i @@ -171,25 +174,32 @@ func splitOutput(dataStore *[]interface{}, output string, command load.Command, func processBlocks(dataStore *[]interface{}, blocks [][]string, command load.Command, startTime int64) { for _, block := range blocks { sample := map[string]interface{}{} - regmatchCount := 0 - for _, regmatch := range command.RegexMatches { - for _, line := range block { - matches := formatter.RegMatch(line, regmatch.Expression) - if len(matches) > 0 { - for i, match := range matches { - if len(regmatch.Keys) > 0 { - key := regmatch.Keys[i] - if len(regmatch.KeysMulti) > 0 { - key = regmatch.KeysMulti[regmatchCount] + key + + if len(command.RegexMatches) > 0 { + regmatchCount := 0 + for _, regmatch := range command.RegexMatches { + for _, line := range block { + matches := formatter.RegMatch(line, regmatch.Expression) + if len(matches) > 0 { + for i, match := range matches { + if len(regmatch.Keys) > 0 { + key := regmatch.Keys[i] + if len(regmatch.KeysMulti) > 0 { + key = regmatch.KeysMulti[regmatchCount] + key + } + sample[key] = match } - sample[key] = match } + regmatchCount++ } - regmatchCount++ } + regmatchCount = 0 } - regmatchCount = 0 + + } else { + processRaw(&sample, "", block, command) } + sample["flex.commandTimeMs"] = makeTimestamp() - startTime *dataStore = append(*dataStore, sample) } @@ -210,7 +220,7 @@ func processOutput(dataStore *[]interface{}, output string, dataSample *map[stri if command.Split == "" { // default vertical split applyCustomAttributes(dataSample, &command.CustomAttributes) - processRaw(dataSample, dataOutput, command.SplitBy, command.LineStart, command.LineEnd) + processRaw(dataSample, dataOutput, []string{}, command) } else if command.Split == load.TypeColumns || command.Split == "horizontal" { if *processType == load.TypeColumns { load.Logrus.Debug(fmt.Sprintf("command: horizontal split only allowed once per command set %v %v", api.Name, command.Name)) @@ -230,9 +240,18 @@ func processOutput(dataStore *[]interface{}, output string, dataSample *map[stri } // processRaw processes a raw data output -func processRaw(dataSample *map[string]interface{}, dataOutput string, splitBy string, lineStart int, lineEnd int) { +func processRaw(dataSample *map[string]interface{}, dataOutput string, lines []string, command load.Command) { + splitBy := command.SplitBy + lineStart := command.LineStart + lineEnd := command.LineEnd + + // if no lines exist, check the dataOutput to split lines from + if len(lines) == 0 && dataOutput != "" { + lines = strings.Split(strings.TrimSuffix(dataOutput, "\n"), "\n") + } + // SplitBy key is required else we cannot easily distinguish between keys and values - for i, line := range strings.Split(strings.TrimSuffix(dataOutput, "\n"), "\n") { + for i, line := range lines { if i >= lineStart { if i >= lineEnd && lineEnd != 0 { load.Logrus.Debug(fmt.Sprintf("command: reached line limit %d", lineEnd))